diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-08-08 19:32:20 +0800 |
---|---|---|
committer | Wei-Ning Huang <aitjcize@gmail.com> | 2018-08-08 19:32:20 +0800 |
commit | 295c7b5efbc36f59e3ae8d10bc3abc3a5d17e785 (patch) | |
tree | bdb93aaa638ec890596a18490505a45204dc2cc7 /simulation/validator.go | |
parent | a418ea95c0f5afb50cbb78aedecc68373353d06e (diff) | |
download | tangerine-consensus-295c7b5efbc36f59e3ae8d10bc3abc3a5d17e785.tar tangerine-consensus-295c7b5efbc36f59e3ae8d10bc3abc3a5d17e785.tar.gz tangerine-consensus-295c7b5efbc36f59e3ae8d10bc3abc3a5d17e785.tar.bz2 tangerine-consensus-295c7b5efbc36f59e3ae8d10bc3abc3a5d17e785.tar.lz tangerine-consensus-295c7b5efbc36f59e3ae8d10bc3abc3a5d17e785.tar.xz tangerine-consensus-295c7b5efbc36f59e3ae8d10bc3abc3a5d17e785.tar.zst tangerine-consensus-295c7b5efbc36f59e3ae8d10bc3abc3a5d17e785.zip |
core: Add Consensus to replace core.Blocklattice (#35)
* Make Sequencer return slice of blocks.
* Fix naming issue
The function 'getHeightVecto' would return ackingStatusVector.
* Fix comment error.
* Add methods to collect info when proposing blocks.
* Add test.App
* Add test.Gov
* Move this type to core.types to avoid cyclic import.
* Add core.Consensus
* Move getMedianTime, interpoTime to util
These functions are not depending on members of core.consensusTimestamp and
is required when testing core.Consensus.
* Make sure types.Block.Clone would copy critical fields.
* Remove core.blocklattice
* Define 'infinity' in core/total-ordering
This definition is defined in core/blocklattice originally.
* Fix a bug when processing the same block twice.
* Integrate simulation with core.Consensus
core.Consensus is a replacement of core.Blocklattice
* Fix the comment to use sigular form.
* Move lock mechanism to sub modules.
* phi should be 2*fmax+1
* Fixup: should aborting when the validator is added
* Fix for new block fields
* Fix the bug that the total ordering sequence is wrong.
Diffstat (limited to 'simulation/validator.go')
-rw-r--r-- | simulation/validator.go | 94 |
1 files changed, 58 insertions, 36 deletions
diff --git a/simulation/validator.go b/simulation/validator.go index 7397284..69ee317 100644 --- a/simulation/validator.go +++ b/simulation/validator.go @@ -18,6 +18,7 @@ package simulation import ( + "fmt" "time" "github.com/dexon-foundation/dexon-consensus-core/blockdb" @@ -31,13 +32,15 @@ import ( type Validator struct { network Network app *SimApp + gov *simGov + db blockdb.BlockDatabase config config.Validator msgChannel chan interface{} isFinished chan struct{} ID types.ValidatorID - lattice *core.BlockLattice + consensus *core.Consensus compactionChain *core.BlockChain genesis *types.Block @@ -49,18 +52,19 @@ func NewValidator( id types.ValidatorID, config config.Validator, network Network) *Validator { - app := NewSimApp(id, network) - db, err := blockdb.NewMemBackedBlockDB() + + db, err := blockdb.NewMemBackedBlockDB( + id.String() + ".blockdb") if err != nil { panic(err) } - lattice := core.NewBlockLattice(db, app) return &Validator{ ID: id, config: config, network: network, - app: app, - lattice: lattice, + app: NewSimApp(id, network), + gov: newSimGov(config.Num), + db: db, isFinished: make(chan struct{}), } } @@ -84,6 +88,9 @@ func (v *Validator) Run() { // Blocks forever. <-isStopped + if err := v.db.Close(); err != nil { + fmt.Println(err) + } v.network.NotifyServer(Message{ Type: shutdownAck, }) @@ -110,15 +117,36 @@ func (v *Validator) CheckServerInfo(isShutdown chan struct{}) { // MsgServer listen to the network channel for message and handle it. func (v *Validator) MsgServer() { + var pendingBlocks []*types.Block for { msg := <-v.msgChannel switch val := msg.(type) { case *types.Block: - //if val.ProposerID.Equal(v.ID) { - // continue - //} - v.lattice.ProcessBlock(val, true) + if v.consensus != nil { + if err := v.consensus.ProcessBlock(val); err != nil { + fmt.Println(err) + //panic(err) + } + } else { + pendingBlocks = append(pendingBlocks, val) + if val.ParentHash == val.Hash { + v.gov.addValidator(val.ProposerID) + } + validatorSet := v.gov.GetValidatorSet() + if len(validatorSet) != v.config.Num { + // We don't collect all validators yet. + break + } + v.consensus = core.NewConsensus(v.app, v.gov, v.db) + for _, b := range pendingBlocks { + if err := v.consensus.ProcessBlock(b); err != nil { + fmt.Println(err) + //panic(err) + } + } + pendingBlocks = pendingBlocks[:0] + } } } } @@ -129,34 +157,26 @@ func (v *Validator) BroadcastGenesisBlock() { for v.network.NumPeers() != v.config.Num { time.Sleep(time.Second) } - - if v.genesis == nil { - hash := common.NewRandomHash() - b := &types.Block{ - ProposerID: v.ID, - ParentHash: hash, - Hash: hash, - Height: 0, - Acks: map[common.Hash]struct{}{}, - } - v.genesis = b - v.current = b - - v.lattice.AddValidator(v.ID, b) - v.lattice.SetOwner(v.ID) - - v.lattice.PrepareBlock(b) - v.network.BroadcastBlock(b) + hash := common.NewRandomHash() + b := &types.Block{ + ProposerID: v.ID, + ParentHash: hash, + Hash: hash, + Height: 0, + Acks: map[common.Hash]struct{}{}, + Timestamps: map[types.ValidatorID]time.Time{ + v.ID: time.Now().UTC(), + }, } + v.network.BroadcastBlock(b) } // BlockProposer propose blocks to be send to the DEXON network. func (v *Validator) BlockProposer(isStopped, isShutdown chan struct{}) { - // Wait until all peer knows each other. - for len(v.lattice.ValidatorSet) != v.config.Num { + // Wait until all genesis blocks are received. + for v.consensus == nil { time.Sleep(time.Second) } - model := &NormalNetwork{ Sigma: v.config.ProposeIntervalSigma, Mean: v.config.ProposeIntervalMean, @@ -167,13 +187,15 @@ ProposingBlockLoop: block := &types.Block{ ProposerID: v.ID, - ParentHash: v.current.Hash, Hash: common.NewRandomHash(), - Height: v.current.Height + 1., - Acks: map[common.Hash]struct{}{}, } - v.current = block - v.lattice.PrepareBlock(block) + if err := v.consensus.PrepareBlock(block); err != nil { + panic(err) + } + if err := v.consensus.ProcessBlock(block); err != nil { + fmt.Println(err) + //panic(err) + } v.network.BroadcastBlock(block) select { case <-isShutdown: |