diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-08-31 13:34:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-31 13:34:00 +0800 |
commit | 18c6a28ff021c9dc643091b5dc420b51183253cf (patch) | |
tree | 3017ffcfc03532289b1856f5f9a1a83012eb5d8e /simulation | |
parent | 123a7ee3bcf96c5bbef2ea16737d1a8e25f5ef30 (diff) | |
download | tangerine-consensus-18c6a28ff021c9dc643091b5dc420b51183253cf.tar tangerine-consensus-18c6a28ff021c9dc643091b5dc420b51183253cf.tar.gz tangerine-consensus-18c6a28ff021c9dc643091b5dc420b51183253cf.tar.bz2 tangerine-consensus-18c6a28ff021c9dc643091b5dc420b51183253cf.tar.lz tangerine-consensus-18c6a28ff021c9dc643091b5dc420b51183253cf.tar.xz tangerine-consensus-18c6a28ff021c9dc643091b5dc420b51183253cf.tar.zst tangerine-consensus-18c6a28ff021c9dc643091b5dc420b51183253cf.zip |
Add methods to Application interface. (#86)
Diffstat (limited to 'simulation')
-rw-r--r-- | simulation/app.go | 9 | ||||
-rw-r--r-- | simulation/validator.go | 94 |
2 files changed, 11 insertions, 92 deletions
diff --git a/simulation/app.go b/simulation/app.go index 78e1d9a..f12290c 100644 --- a/simulation/app.go +++ b/simulation/app.go @@ -54,7 +54,8 @@ func newSimApp(id types.ValidatorID, Network PeerServerNetwork) *simApp { } } -func (a *simApp) addBlock(block *types.Block) { +// BlockConfirmed implements core.Application. +func (a *simApp) BlockConfirmed(block *types.Block) { a.blockByHashMutex.Lock() defer a.blockByHashMutex.Unlock() @@ -62,6 +63,11 @@ func (a *simApp) addBlock(block *types.Block) { a.blockByHash[block.Hash] = block } +// VerifyPayloads implements core.Application. +func (a *simApp) VerifyPayloads(payloads [][]byte) bool { + return true +} + // getAckedBlocks will return all unconfirmed blocks' hash with lower Height // than the block with ackHash. func (a *simApp) getAckedBlocks(ackHash common.Hash) (output common.Hashes) { @@ -101,7 +107,6 @@ func (a *simApp) StronglyAcked(blockHash common.Hash) { // TotalOrderingDeliver is called when blocks are delivered by the total // ordering algorithm. func (a *simApp) TotalOrderingDeliver(blockHashes common.Hashes, early bool) { - now := time.Now() blocks := make([]*types.Block, len(blockHashes)) a.blockByHashMutex.RLock() diff --git a/simulation/validator.go b/simulation/validator.go index a54c848..6d73c50 100644 --- a/simulation/validator.go +++ b/simulation/validator.go @@ -105,26 +105,15 @@ func (v *Validator) Run() { time.Duration(v.config.ProposeIntervalMean)*time.Millisecond), v.prvKey, v.sigToPub) - genesisBlock := &types.Block{ - ProposerID: v.ID, - ChainID: v.chainID, - } - err := v.consensus.PrepareGenesisBlock(genesisBlock, time.Now().UTC()) - if err != nil { - panic(err) - } - isStopped := make(chan struct{}, 2) + go v.consensus.Run() + isShutdown := make(chan struct{}) - v.app.addBlock(genesisBlock) - v.consensus.ProcessBlock(genesisBlock) - v.BroadcastGenesisBlock(genesisBlock) - go v.MsgServer(isStopped) go v.CheckServerInfo(isShutdown) - go v.BlockProposer(isStopped, isShutdown) // Blocks forever. - <-isStopped + <-isShutdown + v.consensus.Stop() if err := v.db.Close(); err != nil { fmt.Println(err) } @@ -151,78 +140,3 @@ func (v *Validator) CheckServerInfo(isShutdown chan struct{}) { time.Sleep(250 * time.Millisecond) } } - -// MsgServer listen to the network channel for message and handle it. -func (v *Validator) MsgServer( - isStopped chan struct{}) { - - for { - var msg interface{} - select { - case msg = <-v.msgChannel: - case <-isStopped: - return - } - - switch val := msg.(type) { - case *types.Block: - v.app.addBlock(val) - if err := v.consensus.ProcessBlock(val); err != nil { - fmt.Println(err) - } - types.RecycleBlock(val) - case *types.NotaryAck: - if err := v.consensus.ProcessNotaryAck(val); err != nil { - fmt.Println(err) - } - case *types.Vote: - if err := v.consensus.ProcessVote(val); err != nil { - fmt.Println(err) - } - } - } -} - -// BroadcastGenesisBlock broadcasts genesis block to all peers. -func (v *Validator) BroadcastGenesisBlock(genesisBlock *types.Block) { - // Wait until all peer joined the network. - for v.network.NumPeers() != v.config.Num { - time.Sleep(time.Second) - } - v.network.BroadcastBlock(genesisBlock) -} - -// BlockProposer propose blocks to be send to the DEXON network. -func (v *Validator) BlockProposer(isStopped, isShutdown chan struct{}) { - model := &NormalNetwork{ - Sigma: v.config.ProposeIntervalSigma, - Mean: v.config.ProposeIntervalMean, - } -ProposingBlockLoop: - for { - time.Sleep(model.Delay()) - - block := &types.Block{ - ProposerID: v.ID, - ChainID: v.chainID, - Hash: common.NewRandomHash(), - } - if err := v.consensus.PrepareBlock(block, time.Now().UTC()); err != nil { - panic(err) - } - v.app.addBlock(block) - if err := v.consensus.ProcessBlock(block); err != nil { - fmt.Println(err) - //panic(err) - } - v.network.BroadcastBlock(block) - select { - case <-isShutdown: - isStopped <- struct{}{} - isStopped <- struct{}{} - break ProposingBlockLoop - default: - break - } - } -} |