diff options
-rw-r--r-- | cmd/dexcon-simulation/main.go | 3 | ||||
-rw-r--r-- | core/agreement-state.go | 4 | ||||
-rw-r--r-- | core/agreement-state_test.go | 18 | ||||
-rw-r--r-- | core/agreement.go | 33 | ||||
-rw-r--r-- | core/agreement_test.go | 18 | ||||
-rw-r--r-- | core/consensus.go | 34 | ||||
-rw-r--r-- | simulation/node.go | 11 | ||||
-rw-r--r-- | simulation/simulation.go | 4 |
8 files changed, 48 insertions, 77 deletions
diff --git a/cmd/dexcon-simulation/main.go b/cmd/dexcon-simulation/main.go index 3334d23..901d004 100644 --- a/cmd/dexcon-simulation/main.go +++ b/cmd/dexcon-simulation/main.go @@ -36,7 +36,6 @@ var initialize = flag.Bool("init", false, "initialize config file") var configFile = flag.String("config", "", "path to simulation config file") var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`") var memprofile = flag.String("memprofile", "", "write memory profile to `file`") -var legacy = flag.Bool("legacy", false, "legacy consensus protocal") var logfile = flag.String("log", "", "write log to `file`") func main() { @@ -81,7 +80,7 @@ func main() { if err != nil { panic(err) } - simulation.Run(cfg, *legacy) + simulation.Run(cfg) if *memprofile != "" { f, err := os.Create(*memprofile) diff --git a/core/agreement-state.go b/core/agreement-state.go index fbee21a..fc74998 100644 --- a/core/agreement-state.go +++ b/core/agreement-state.go @@ -66,10 +66,8 @@ func (s *prepareState) state() agreementStateType { return statePrepare } func (s *prepareState) clocks() int { return 0 } func (s *prepareState) terminate() {} func (s *prepareState) nextState() (agreementState, error) { - hash := common.Hash{} if s.a.period == 1 { - hash = s.a.blockProposer().Hash - s.a.recv.ProposeBlock(hash) + s.a.recv.ProposeBlock() } else { var proposed bool _, proposed = s.a.countVote(s.a.period-1, types.VotePass) diff --git a/core/agreement-state_test.go b/core/agreement-state_test.go index c09dcfd..602b5c7 100644 --- a/core/agreement-state_test.go +++ b/core/agreement-state_test.go @@ -40,15 +40,17 @@ type AgreementStateTestSuite struct { } type agreementStateTestReceiver struct { - s *AgreementStateTestSuite + s *AgreementStateTestSuite + leader *leaderSelector } func (r *agreementStateTestReceiver) ProposeVote(vote *types.Vote) { r.s.voteChan <- vote } -func (r *agreementStateTestReceiver) ProposeBlock(block common.Hash) { - r.s.blockChan <- block +func (r *agreementStateTestReceiver) ProposeBlock() { + block := r.s.proposeBlock(r.leader) + r.s.blockChan <- block.Hash } func (r *agreementStateTestReceiver) ConfirmBlock(block common.Hash) { @@ -99,10 +101,6 @@ func (s *AgreementStateTestSuite) SetupTest() { func (s *AgreementStateTestSuite) newAgreement(numNode int) *agreement { leader := newGenesisLeaderSelector([]byte("I ❤️ DEXON")) - blockProposer := func() *types.Block { - return s.proposeBlock(leader) - } - notarySet := make(map[types.NodeID]struct{}) for i := 0; i < numNode-1; i++ { prvKey, err := ecdsa.NewPrivateKey() @@ -114,10 +112,12 @@ func (s *AgreementStateTestSuite) newAgreement(numNode int) *agreement { notarySet[s.ID] = struct{}{} agreement := newAgreement( s.ID, - &agreementStateTestReceiver{s}, + &agreementStateTestReceiver{ + s: s, + leader: leader, + }, notarySet, leader, - blockProposer, ) return agreement } diff --git a/core/agreement.go b/core/agreement.go index d9e7432..d88ddba 100644 --- a/core/agreement.go +++ b/core/agreement.go @@ -56,8 +56,6 @@ func (e *ErrForkVote) Error() string { e.nID.String(), e.old, e.new) } -type blockProposerFn func() *types.Block - func newVoteListMap() []map[types.NodeID]*types.Vote { listMap := make([]map[types.NodeID]*types.Vote, types.MaxVoteType) for idx := range listMap { @@ -69,7 +67,7 @@ func newVoteListMap() []map[types.NodeID]*types.Vote { // agreementReceiver is the interface receiving agreement event. type agreementReceiver interface { ProposeVote(vote *types.Vote) - ProposeBlock(common.Hash) + ProposeBlock() ConfirmBlock(common.Hash) } @@ -87,16 +85,15 @@ type pendingVote struct { type agreementData struct { recv agreementReceiver - ID types.NodeID - leader *leaderSelector - defaultBlock common.Hash - period uint64 - requiredVote int - votes map[uint64][]map[types.NodeID]*types.Vote - votesLock sync.RWMutex - blocks map[types.NodeID]*types.Block - blocksLock sync.Mutex - blockProposer blockProposerFn + ID types.NodeID + leader *leaderSelector + defaultBlock common.Hash + period uint64 + requiredVote int + votes map[uint64][]map[types.NodeID]*types.Vote + votesLock sync.RWMutex + blocks map[types.NodeID]*types.Block + blocksLock sync.Mutex } // agreement is the agreement protocal describe in the Crypto Shuffle Algorithm. @@ -117,14 +114,12 @@ func newAgreement( ID types.NodeID, recv agreementReceiver, notarySet map[types.NodeID]struct{}, - leader *leaderSelector, - blockProposer blockProposerFn) *agreement { + leader *leaderSelector) *agreement { agreement := &agreement{ data: &agreementData{ - recv: recv, - ID: ID, - leader: leader, - blockProposer: blockProposer, + recv: recv, + ID: ID, + leader: leader, }, aID: &atomic.Value{}, candidateBlock: make(map[common.Hash]*types.Block), diff --git a/core/agreement_test.go b/core/agreement_test.go index d384dc6..db50bd5 100644 --- a/core/agreement_test.go +++ b/core/agreement_test.go @@ -29,15 +29,17 @@ import ( // agreementTestReceiver implements core.agreementReceiveer type agreementTestReceiver struct { - s *AgreementTestSuite + s *AgreementTestSuite + agreementIndex int } func (r *agreementTestReceiver) ProposeVote(vote *types.Vote) { r.s.voteChan <- vote } -func (r *agreementTestReceiver) ProposeBlock(block common.Hash) { - r.s.blockChan <- block +func (r *agreementTestReceiver) ProposeBlock() { + block := r.s.proposeBlock(r.agreementIndex) + r.s.blockChan <- block.Hash } func (r *agreementTestReceiver) ConfirmBlock(block common.Hash) { @@ -82,10 +84,6 @@ func (s *AgreementTestSuite) SetupTest() { func (s *AgreementTestSuite) newAgreement(numNotarySet int) *agreement { leader := newGenesisLeaderSelector([]byte("🖖👽")) agreementIdx := len(s.agreement) - blockProposer := func() *types.Block { - return s.proposeBlock(agreementIdx) - } - notarySet := make(map[types.NodeID]struct{}) for i := 0; i < numNotarySet-1; i++ { prvKey, err := ecdsa.NewPrivateKey() @@ -97,10 +95,12 @@ func (s *AgreementTestSuite) newAgreement(numNotarySet int) *agreement { notarySet[s.ID] = struct{}{} agreement := newAgreement( s.ID, - &agreementTestReceiver{s}, + &agreementTestReceiver{ + s: s, + agreementIndex: agreementIdx, + }, notarySet, leader, - blockProposer, ) s.agreement = append(s.agreement, agreement) return agreement diff --git a/core/consensus.go b/core/consensus.go index f1f4a2e..2f62191 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -79,14 +79,10 @@ func (recv *consensusReceiver) ProposeVote(vote *types.Vote) { }() } -func (recv *consensusReceiver) ProposeBlock(hash common.Hash) { - block, exist := recv.consensus.baModules[recv.chainID].findCandidateBlock(hash) - if !exist { - log.Println(ErrUnknownBlockProposed) - log.Println(hash) - return - } - if err := recv.consensus.PreProcessBlock(block); err != nil { +func (recv *consensusReceiver) ProposeBlock() { + block := recv.consensus.proposeBlock(recv.chainID) + recv.consensus.baModules[recv.chainID].addCandidateBlock(block) + if err := recv.consensus.preProcessBlock(block); err != nil { log.Println(err) return } @@ -289,17 +285,11 @@ func NewConsensus( chainID: chainID, restart: make(chan struct{}, 1), } - blockProposer := func() *types.Block { - block := con.proposeBlock(chainID) - con.baModules[chainID].addCandidateBlock(block) - return block - } con.baModules[chainID] = newAgreement( con.ID, con.receivers[chainID], nodes, newGenesisLeaderSelector(crs), - blockProposer, ) } return con @@ -307,7 +297,7 @@ func NewConsensus( // Run starts running DEXON Consensus. func (con *Consensus) Run() { - go con.processMsg(con.network.ReceiveChan(), con.PreProcessBlock) + go con.processMsg(con.network.ReceiveChan()) con.runDKGTSIG() con.dkgReady.L.Lock() defer con.dkgReady.L.Unlock() @@ -420,18 +410,12 @@ func (con *Consensus) runDKGTSIG() { }() } -// RunLegacy starts running Legacy DEXON Consensus. -func (con *Consensus) RunLegacy() { -} - // Stop the Consensus core. func (con *Consensus) Stop() { con.ctxCancel() } -func (con *Consensus) processMsg( - msgChan <-chan interface{}, - blockProcesser func(*types.Block) error) { +func (con *Consensus) processMsg(msgChan <-chan interface{}) { for { var msg interface{} select { @@ -442,7 +426,7 @@ func (con *Consensus) processMsg( switch val := msg.(type) { case *types.Block: - if err := blockProcesser(val); err != nil { + if err := con.preProcessBlock(val); err != nil { log.Println(err) } case *types.WitnessAck: @@ -555,8 +539,8 @@ func (con *Consensus) sanityCheck(b *types.Block) (err error) { return nil } -// PreProcessBlock performs Byzantine Agreement on the block. -func (con *Consensus) PreProcessBlock(b *types.Block) (err error) { +// preProcessBlock performs Byzantine Agreement on the block. +func (con *Consensus) preProcessBlock(b *types.Block) (err error) { if err := con.sanityCheck(b); err != nil { return err } diff --git a/simulation/node.go b/simulation/node.go index c9163b3..f2ddd5a 100644 --- a/simulation/node.go +++ b/simulation/node.go @@ -75,7 +75,7 @@ func (n *node) GetID() types.NodeID { } // run starts the node. -func (n *node) run(serverEndpoint interface{}, legacy bool) { +func (n *node) run(serverEndpoint interface{}) { // Run network. if err := n.netModule.setup(serverEndpoint); err != nil { panic(err) @@ -98,13 +98,8 @@ func (n *node) run(serverEndpoint interface{}, legacy bool) { break } } - n.consensus = core.NewConsensus( - n.app, n.gov, n.db, n.netModule, n.prvKey) - if legacy { - go n.consensus.RunLegacy() - } else { - go n.consensus.Run() - } + n.consensus = core.NewConsensus(n.app, n.gov, n.db, n.netModule, n.prvKey) + go n.consensus.Run() // Blocks forever. MainLoop: diff --git a/simulation/simulation.go b/simulation/simulation.go index 0a47e68..ce9f68b 100644 --- a/simulation/simulation.go +++ b/simulation/simulation.go @@ -25,7 +25,7 @@ import ( ) // Run starts the simulation. -func Run(cfg *config.Config, legacy bool) { +func Run(cfg *config.Config) { var ( networkType = cfg.Networking.Type server *PeerServer @@ -43,7 +43,7 @@ func Run(cfg *config.Config, legacy bool) { wg.Add(1) go func() { defer wg.Done() - v.run(serverEndpoint, legacy) + v.run(serverEndpoint) }() } |