diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/consensus-timestamp.go (renamed from core/timestamp.go) | 26 | ||||
-rw-r--r-- | core/consensus-timestamp_test.go (renamed from core/timestamp_test.go) | 52 | ||||
-rw-r--r-- | core/reliable-broadcast.go (renamed from core/acking.go) | 108 | ||||
-rw-r--r-- | core/reliable-broadcast_test.go (renamed from core/acking_test.go) | 210 | ||||
-rw-r--r-- | core/total-ordering.go (renamed from core/sequencer.go) | 120 | ||||
-rw-r--r-- | core/total-ordering_test.go (renamed from core/sequencer_test.go) | 268 |
6 files changed, 392 insertions, 392 deletions
diff --git a/core/timestamp.go b/core/consensus-timestamp.go index 896d137..e9b5cce 100644 --- a/core/timestamp.go +++ b/core/consensus-timestamp.go @@ -27,7 +27,7 @@ import ( ) // Timestamp is for Concensus Timestamp Algorithm. -type Timestamp struct { +type consensusTimestamp struct { lastMainChainBlock *types.Block blocksNotInMainChain []*types.Block } @@ -41,27 +41,27 @@ var ( ) // NewTimestamp create Timestamp object. -func NewTimestamp() *Timestamp { - return &Timestamp{} +func newConsensusTimestamp() *consensusTimestamp { + return &consensusTimestamp{} } // ProcessBlocks is the entry function. -func (ts *Timestamp) ProcessBlocks(blocks []*types.Block) ( +func (ct *consensusTimestamp) processBlocks(blocks []*types.Block) ( blocksWithTimestamp []*types.Block, mainChain []*types.Block, err error) { if len(blocks) == 0 { // TODO (jimmy-dexon): Remove this panic before release. panic("Unexpected empty block list.") } outputFirstBlock := true - blocks = append(ts.blocksNotInMainChain, blocks...) - if ts.lastMainChainBlock != nil { + blocks = append(ct.blocksNotInMainChain, blocks...) + if ct.lastMainChainBlock != nil { // TODO (jimmy-dexon): The performance here can be optimized. - blocks = append([]*types.Block{ts.lastMainChainBlock}, blocks...) + blocks = append([]*types.Block{ct.lastMainChainBlock}, blocks...) outputFirstBlock = false } - mainChain, nonMainChain := ts.selectMainChain(blocks) - ts.blocksNotInMainChain = nonMainChain - ts.lastMainChainBlock = mainChain[len(mainChain)-1] + mainChain, nonMainChain := ct.selectMainChain(blocks) + ct.blocksNotInMainChain = nonMainChain + ct.lastMainChainBlock = mainChain[len(mainChain)-1] blocksWithTimestamp = blocks[:len(blocks)-len(nonMainChain)] leftMainChainIdx := 0 rightMainChainIdx := 0 @@ -72,7 +72,7 @@ func (ts *Timestamp) ProcessBlocks(blocks []*types.Block) ( return } else if block.Hash == mainChain[idxMainChain].Hash { rightMainChainIdx = idx - blocksWithTimestamp[idx].ConsensusTime, err = ts.getMedianTime(block) + blocksWithTimestamp[idx].ConsensusTime, err = ct.getMedianTime(block) if err != nil { return } @@ -95,7 +95,7 @@ func (ts *Timestamp) ProcessBlocks(blocks []*types.Block) ( return } -func (ts *Timestamp) selectMainChain(blocks []*types.Block) ( +func (ct *consensusTimestamp) selectMainChain(blocks []*types.Block) ( mainChain []*types.Block, nonMainChain []*types.Block) { for _, block := range blocks { if len(mainChain) != 0 { @@ -110,7 +110,7 @@ func (ts *Timestamp) selectMainChain(blocks []*types.Block) ( return } -func (ts *Timestamp) getMedianTime(block *types.Block) ( +func (ct *consensusTimestamp) getMedianTime(block *types.Block) ( timestamp time.Time, err error) { timestamps := []time.Time{} for _, timestamp := range block.Timestamps { diff --git a/core/timestamp_test.go b/core/consensus-timestamp_test.go index d47c776..8a82080 100644 --- a/core/timestamp_test.go +++ b/core/consensus-timestamp_test.go @@ -29,7 +29,7 @@ import ( "github.com/dexon-foundation/dexon-consensus-core/core/types" ) -type TimestampTest struct { +type ConsensusTimestampTest struct { suite.Suite } @@ -84,26 +84,26 @@ func extractTimestamps(blocks []*types.Block) []time.Time { return timestamps } -func (s *TimestampTest) TestMainChainSelection() { - ts := NewTimestamp() - ts2 := NewTimestamp() +func (s *ConsensusTimestampTest) TestMainChainSelection() { + ct := newConsensusTimestamp() + ct2 := newConsensusTimestamp() blockNums := []int{50, 100, 30} maxAcks := 5 for _, blockNum := range blockNums { chain := generateBlocksWithAcks(blockNum, maxAcks) - mainChain, _ := ts.selectMainChain(chain) + mainChain, _ := ct.selectMainChain(chain) // Verify the selected main chain. for i := 1; i < len(mainChain); i++ { _, exists := mainChain[i].Acks[mainChain[i-1].Hash] s.True(exists) } // Verify if selectMainChain is stable. - mainChain2, _ := ts2.selectMainChain(chain) + mainChain2, _ := ct2.selectMainChain(chain) s.Equal(mainChain, mainChain2) } } -func (s *TimestampTest) TestTimestampPartition() { +func (s *ConsensusTimestampTest) TestTimestampPartition() { blockNums := []int{50, 100, 30} validatorNum := 19 sigma := 100 * time.Millisecond @@ -111,26 +111,26 @@ func (s *TimestampTest) TestTimestampPartition() { totalMainChain := make([]*types.Block, 1) totalChain := make([]*types.Block, 0) totalTimestamps := make([]time.Time, 0) - ts := NewTimestamp() + ct := newConsensusTimestamp() var lastMainChainBlock *types.Block for _, blockNum := range blockNums { chain := generateBlocksWithAcks(blockNum, maxAcks) fillBlocksTimestamps(chain, validatorNum, time.Second, sigma) - blocksWithTimestamps, mainChain, err := ts.ProcessBlocks(chain) + blocksWithTimestamps, mainChain, err := ct.processBlocks(chain) s.Require().Nil(err) timestamps := extractTimestamps(blocksWithTimestamps) if lastMainChainBlock != nil { s.Require().Equal(mainChain[0], lastMainChainBlock) } - s.Require().Equal(mainChain[len(mainChain)-1], ts.lastMainChainBlock) - lastMainChainBlock = ts.lastMainChainBlock + s.Require().Equal(mainChain[len(mainChain)-1], ct.lastMainChainBlock) + lastMainChainBlock = ct.lastMainChainBlock totalMainChain = append(totalMainChain[:len(totalMainChain)-1], mainChain...) totalChain = append(totalChain, chain...) totalTimestamps = append(totalTimestamps, timestamps...) } - ts2 := NewTimestamp() - blocksWithTimestamps2, mainChain2, err := ts2.ProcessBlocks(totalChain) + ct2 := newConsensusTimestamp() + blocksWithTimestamps2, mainChain2, err := ct2.processBlocks(totalChain) s.Require().Nil(err) timestamps2 := extractTimestamps(blocksWithTimestamps2) s.Equal(totalMainChain, mainChain2) @@ -144,35 +144,35 @@ func timeDiffWithinTolerance(t1, t2 time.Time, tolerance time.Duration) bool { return t1.Add(tolerance).After(t2) } -func (s *TimestampTest) TestTimestampIncrease() { +func (s *ConsensusTimestampTest) TestTimestampIncrease() { validatorNum := 19 sigma := 100 * time.Millisecond - ts := NewTimestamp() + ct := newConsensusTimestamp() chain := generateBlocksWithAcks(1000, 5) fillBlocksTimestamps(chain, validatorNum, time.Second, sigma) - blocksWithTimestamps, _, err := ts.ProcessBlocks(chain) + blocksWithTimestamps, _, err := ct.processBlocks(chain) s.Require().Nil(err) timestamps := extractTimestamps(blocksWithTimestamps) for i := 1; i < len(timestamps); i++ { s.True(timestamps[i].After(timestamps[i-1])) } - // Test if the ProcessBlocks is stable. - ts2 := NewTimestamp() - blocksWithTimestamps2, _, err := ts2.ProcessBlocks(chain) + // Test if the processBlocks is stable. + ct2 := newConsensusTimestamp() + blocksWithTimestamps2, _, err := ct2.processBlocks(chain) s.Require().Nil(err) timestamps2 := extractTimestamps(blocksWithTimestamps2) s.Equal(timestamps, timestamps2) } -func (s *TimestampTest) TestByzantineBiasTime() { +func (s *ConsensusTimestampTest) TestByzantineBiasTime() { // Test that Byzantine node cannot bias the timestamps. validatorNum := 19 sigma := 100 * time.Millisecond tolerance := 4 * sigma - ts := NewTimestamp() + ct := newConsensusTimestamp() chain := generateBlocksWithAcks(1000, 5) fillBlocksTimestamps(chain, validatorNum, time.Second, sigma) - blocksWithTimestamps, _, err := ts.ProcessBlocks(chain) + blocksWithTimestamps, _, err := ct.processBlocks(chain) s.Require().Nil(err) timestamps := extractTimestamps(blocksWithTimestamps) byzantine := validatorNum / 3 @@ -190,8 +190,8 @@ func (s *TimestampTest) TestByzantineBiasTime() { block.Timestamps[vID] = time.Time{} } } - tsByzantine := NewTimestamp() - blocksWithTimestampsB, _, err := tsByzantine.ProcessBlocks(chain) + ctByzantine := newConsensusTimestamp() + blocksWithTimestampsB, _, err := ctByzantine.processBlocks(chain) s.Require().Nil(err) timestampsWithByzantine := extractTimestamps(blocksWithTimestampsB) for idx, timestamp := range timestamps { @@ -201,6 +201,6 @@ func (s *TimestampTest) TestByzantineBiasTime() { } } -func TestTimestamp(t *testing.T) { - suite.Run(t, new(TimestampTest)) +func TestConsensusTimestamp(t *testing.T) { + suite.Run(t, new(ConsensusTimestampTest)) } diff --git a/core/acking.go b/core/reliable-broadcast.go index 47f13fb..dd57241 100644 --- a/core/acking.go +++ b/core/reliable-broadcast.go @@ -24,8 +24,8 @@ import ( "github.com/dexon-foundation/dexon-consensus-core/core/types" ) -// acking is for acking module. -type acking struct { +// reliableBroadcast is a module for reliable broadcast. +type reliableBroadcast struct { // lattice stores blocks by its validator ID and height. lattice map[types.ValidatorID]*ackingValidatorStatus @@ -48,7 +48,7 @@ type ackingValidatorStatus struct { // nextAck stores the height of next height that should be acked, i.e. last // acked height + 1. Initialized to 0, when genesis blocks are still not - // being acked. For example, a.lattice[vid1].NextAck[vid2] - 1 is the last + // being acked. For example, rb.lattice[vid1].NextAck[vid2] - 1 is the last // acked height by vid1 acking vid2. nextAck map[types.ValidatorID]uint64 @@ -68,9 +68,9 @@ var ( ErrInvalidBlockHeight = fmt.Errorf("invalid block height") ) -// newAcking creates a new acking struct. -func newAcking() *acking { - return &acking{ +// newReliableBroadcast creates a new reliableBroadcast struct. +func newReliableBroadcast() *reliableBroadcast { + return &reliableBroadcast{ lattice: make(map[types.ValidatorID]*ackingValidatorStatus), blocks: make(map[common.Hash]*types.Block), receivedBlocks: make(map[common.Hash]*types.Block), @@ -78,14 +78,14 @@ func newAcking() *acking { } } -func (a *acking) sanityCheck(b *types.Block) error { +func (rb *reliableBroadcast) sanityCheck(b *types.Block) error { // Check if its proposer is in validator set. - if _, exist := a.lattice[b.ProposerID]; !exist { + if _, exist := rb.lattice[b.ProposerID]; !exist { return ErrInvalidProposerID } // Check if it forks. - if bInLattice, exist := a.lattice[b.ProposerID].blocks[b.Height]; exist { + if bInLattice, exist := rb.lattice[b.ProposerID].blocks[b.Height]; exist { if b.Hash != bInLattice.Hash { return ErrForkBlock } @@ -96,7 +96,7 @@ func (a *acking) sanityCheck(b *types.Block) error { if _, exist := b.Acks[b.ParentHash]; !exist { return ErrNotAckParent } - bParent, exists := a.blocks[b.ParentHash] + bParent, exists := rb.blocks[b.ParentHash] if exists && bParent.Height != b.Height-1 { return ErrInvalidBlockHeight } @@ -104,8 +104,8 @@ func (a *acking) sanityCheck(b *types.Block) error { // Check if it acks older blocks. for hash := range b.Acks { - if bAck, exist := a.blocks[hash]; exist { - if bAck.Height < a.lattice[b.ProposerID].nextAck[bAck.ProposerID] { + if bAck, exist := rb.blocks[hash]; exist { + if bAck.Height < rb.lattice[b.ProposerID].nextAck[bAck.ProposerID] { return ErrDoubleAck } } @@ -117,18 +117,18 @@ func (a *acking) sanityCheck(b *types.Block) error { } // areAllAcksReceived checks if all ack blocks of a block are all in lattice. -func (a *acking) areAllAcksInLattice(b *types.Block) bool { +func (rb *reliableBroadcast) areAllAcksInLattice(b *types.Block) bool { for h := range b.Acks { - bAck, exist := a.blocks[h] + bAck, exist := rb.blocks[h] if !exist { return false } - bAckInLattice, exist := a.lattice[bAck.ProposerID].blocks[bAck.Height] + bAckInLattice, exist := rb.lattice[bAck.ProposerID].blocks[bAck.Height] if !exist { return false } if bAckInLattice.Hash != bAck.Hash { - panic("areAllAcksInLattice: acking.lattice has corrupted") + panic("areAllAcksInLattice: reliableBroadcast.lattice has corrupted") } } return true @@ -136,14 +136,14 @@ func (a *acking) areAllAcksInLattice(b *types.Block) bool { // processBlock processes block, it does sanity check, inserts block into // lattice, handles strong acking and deletes blocks which will not be used. -func (a *acking) processBlock(block *types.Block) { +func (rb *reliableBroadcast) processBlock(block *types.Block) { // If a block does not pass sanity check, discard this block. - if err := a.sanityCheck(block); err != nil { + if err := rb.sanityCheck(block); err != nil { return } - a.blocks[block.Hash] = block + rb.blocks[block.Hash] = block block.AckedValidators = make(map[types.ValidatorID]struct{}) - a.receivedBlocks[block.Hash] = block + rb.receivedBlocks[block.Hash] = block // Check blocks in receivedBlocks if its acks are all in lattice. If a block's // acking blocks are all in lattice, execute sanity check and add the block @@ -151,8 +151,8 @@ func (a *acking) processBlock(block *types.Block) { blocksToAcked := map[common.Hash]*types.Block{} for { blocksToLattice := map[common.Hash]*types.Block{} - for _, b := range a.receivedBlocks { - if a.areAllAcksInLattice(b) { + for _, b := range rb.receivedBlocks { + if rb.areAllAcksInLattice(b) { blocksToLattice[b.Hash] = b } } @@ -166,19 +166,19 @@ func (a *acking) processBlock(block *types.Block) { // B C Block B and C both ack A and are valid. B, C received first // \ / (added in receivedBlocks), and A comes, if sanity check is // A not being executed here, B and C will both be added in lattice - if err := a.sanityCheck(b); err != nil { - delete(a.blocks, b.Hash) - delete(a.receivedBlocks, b.Hash) + if err := rb.sanityCheck(b); err != nil { + delete(rb.blocks, b.Hash) + delete(rb.receivedBlocks, b.Hash) continue } - a.lattice[b.ProposerID].blocks[b.Height] = b - delete(a.receivedBlocks, b.Hash) + rb.lattice[b.ProposerID].blocks[b.Height] = b + delete(rb.receivedBlocks, b.Hash) for h := range b.Acks { - bAck := a.blocks[h] + bAck := rb.blocks[h] // Update nextAck only when bAck.Height + 1 is greater. A block might // ack blocks proposed by same validator with different height. - if a.lattice[b.ProposerID].nextAck[bAck.ProposerID] < bAck.Height+1 { - a.lattice[b.ProposerID].nextAck[bAck.ProposerID] = bAck.Height + 1 + if rb.lattice[b.ProposerID].nextAck[bAck.ProposerID] < bAck.Height+1 { + rb.lattice[b.ProposerID].nextAck[bAck.ProposerID] = bAck.Height + 1 } // Update AckedValidators for each ack blocks and its parents. for { @@ -191,20 +191,20 @@ func (a *acking) processBlock(block *types.Block) { bAck.AckedValidators[b.ProposerID] = struct{}{} // A block is strongly acked if it is acked by more than // 2 * (maximum number of byzatine validators) unique validators. - if len(bAck.AckedValidators) > 2*((len(a.lattice)-1)/3) { + if len(bAck.AckedValidators) > 2*((len(rb.lattice)-1)/3) { blocksToAcked[bAck.Hash] = bAck } if bAck.Height == 0 { break } - bAck = a.blocks[bAck.ParentHash] + bAck = rb.blocks[bAck.ParentHash] } } } } for _, b := range blocksToAcked { - a.ackedBlocks[b.Hash] = b + rb.ackedBlocks[b.Hash] = b b.Status = types.BlockStatusAcked } @@ -214,15 +214,15 @@ func (a *acking) processBlock(block *types.Block) { // Delete old blocks in "lattice" and "blocks" for release memory space. // First, find the height that blocks below it can be deleted. This height // is defined by finding minimum of validator's nextOutput and last acking - // heights from other validators, i.e. a.lattice[v_other].nextAck[this_vid]. + // heights from other validators, i.e. rb.lattice[v_other].nextAck[this_vid]. // This works because blocks of height below this minimum are not going to be // acked anymore, the ackings of these blocks are illegal. - for vid := range a.lattice { + for vid := range rb.lattice { // Find the minimum height that heights lesser can be deleted. - min := a.lattice[vid].nextOutput - for vid2 := range a.lattice { - if a.lattice[vid2].nextAck[vid] < min { - min = a.lattice[vid2].nextAck[vid] + min := rb.lattice[vid].nextOutput + for vid2 := range rb.lattice { + if rb.lattice[vid2].nextAck[vid] < min { + min = rb.lattice[vid2].nextAck[vid] } } // "min" is the height of "next" last acked, min - 1 is the last height. @@ -232,13 +232,13 @@ func (a *acking) processBlock(block *types.Block) { } min -= 2 for { - b, exist := a.lattice[vid].blocks[min] + b, exist := rb.lattice[vid].blocks[min] if !exist { break } if b.Status >= types.BlockStatusOrdering { - delete(a.lattice[vid].blocks, b.Height) - delete(a.blocks, b.Hash) + delete(rb.lattice[vid].blocks, b.Height) + delete(rb.blocks, b.Hash) } if min == 0 { break @@ -251,12 +251,12 @@ func (a *acking) processBlock(block *types.Block) { // extractBlocks returns all blocks that can be inserted into total ordering's // DAG. This function changes the status of blocks from types.BlockStatusAcked // to blockStatusOrdering. -func (a *acking) extractBlocks() []*types.Block { +func (rb *reliableBroadcast) extractBlocks() []*types.Block { ret := []*types.Block{} for { updated := false - for vid := range a.lattice { - b, exist := a.lattice[vid].blocks[a.lattice[vid].nextOutput] + for vid := range rb.lattice { + b, exist := rb.lattice[vid].blocks[rb.lattice[vid].nextOutput] if !exist || b.Status < types.BlockStatusAcked { continue } @@ -265,7 +265,7 @@ func (a *acking) extractBlocks() []*types.Block { // does not exist means that it deleted but its status is definitely Acked // or ordering. for ackHash := range b.Acks { - bAck, exist := a.blocks[ackHash] + bAck, exist := rb.blocks[ackHash] if !exist { continue } @@ -279,9 +279,9 @@ func (a *acking) extractBlocks() []*types.Block { } updated = true b.Status = types.BlockStatusOrdering - delete(a.ackedBlocks, b.Hash) + delete(rb.ackedBlocks, b.Hash) ret = append(ret, b) - a.lattice[vid].nextOutput++ + rb.lattice[vid].nextOutput++ } if !updated { break @@ -291,8 +291,8 @@ func (a *acking) extractBlocks() []*types.Block { } // addValidator adds validator in the validator set. -func (a *acking) addValidator(h types.ValidatorID) { - a.lattice[h] = &ackingValidatorStatus{ +func (rb *reliableBroadcast) addValidator(h types.ValidatorID) { + rb.lattice[h] = &ackingValidatorStatus{ blocks: make(map[uint64]*types.Block), nextAck: make(map[types.ValidatorID]uint64), nextOutput: 0, @@ -301,9 +301,9 @@ func (a *acking) addValidator(h types.ValidatorID) { } // deleteValidator deletes validator in validator set. -func (a *acking) deleteValidator(h types.ValidatorID) { - for h := range a.lattice { - delete(a.lattice[h].nextAck, h) +func (rb *reliableBroadcast) deleteValidator(h types.ValidatorID) { + for h := range rb.lattice { + delete(rb.lattice[h].nextAck, h) } - delete(a.lattice, h) + delete(rb.lattice, h) } diff --git a/core/acking_test.go b/core/reliable-broadcast_test.go index b1f26b2..7c15fe5 100644 --- a/core/acking_test.go +++ b/core/reliable-broadcast_test.go @@ -30,15 +30,15 @@ import ( "github.com/dexon-foundation/dexon-consensus-core/core/types" ) -type AckingTest struct { +type ReliableBroadcastTest struct { suite.Suite } -func (s *AckingTest) SetupSuite() { +func (s *ReliableBroadcastTest) SetupSuite() { } -func (s *AckingTest) SetupTest() { +func (s *ReliableBroadcastTest) SetupTest() { } @@ -51,14 +51,14 @@ func (s *AckingTest) SetupTest() { // | | | // 0 0 0 0 (block height) // 0 1 2 3 (validator) -func genTestCase1(s *AckingTest, a *acking) []types.ValidatorID { - // Create new acking instance with 4 validators +func genTestCase1(s *ReliableBroadcastTest, r *reliableBroadcast) []types.ValidatorID { + // Create new reliableBroadcast instance with 4 validators var b *types.Block var h common.Hash vids := []types.ValidatorID{} for i := 0; i < 4; i++ { vid := types.ValidatorID{Hash: common.NewRandomHash()} - a.addValidator(vid) + r.addValidator(vid) vids = append(vids, vid) } // Add genesis blocks. @@ -71,11 +71,11 @@ func genTestCase1(s *AckingTest, a *acking) []types.ValidatorID { Height: 0, Acks: map[common.Hash]struct{}{}, } - a.processBlock(b) + r.processBlock(b) } // Add block 0-1 which acks 0-0. - h = a.lattice[vids[0]].blocks[0].Hash + h = r.lattice[vids[0]].blocks[0].Hash b = &types.Block{ ProposerID: vids[0], ParentHash: h, @@ -85,11 +85,11 @@ func genTestCase1(s *AckingTest, a *acking) []types.ValidatorID { h: struct{}{}, }, } - a.processBlock(b) - s.NotNil(a.lattice[vids[0]].blocks[1]) + r.processBlock(b) + s.NotNil(r.lattice[vids[0]].blocks[1]) // Add block 0-2 which acks 0-1 and 1-0. - h = a.lattice[vids[0]].blocks[1].Hash + h = r.lattice[vids[0]].blocks[1].Hash b = &types.Block{ ProposerID: vids[0], ParentHash: h, @@ -97,14 +97,14 @@ func genTestCase1(s *AckingTest, a *acking) []types.ValidatorID { Height: 2, Acks: map[common.Hash]struct{}{ h: struct{}{}, - a.lattice[vids[1]].blocks[0].Hash: struct{}{}, + r.lattice[vids[1]].blocks[0].Hash: struct{}{}, }, } - a.processBlock(b) - s.NotNil(a.lattice[vids[0]].blocks[2]) + r.processBlock(b) + s.NotNil(r.lattice[vids[0]].blocks[2]) // Add block 0-3 which acks 0-2. - h = a.lattice[vids[0]].blocks[2].Hash + h = r.lattice[vids[0]].blocks[2].Hash b = &types.Block{ ProposerID: vids[0], ParentHash: h, @@ -114,11 +114,11 @@ func genTestCase1(s *AckingTest, a *acking) []types.ValidatorID { h: struct{}{}, }, } - a.processBlock(b) - s.NotNil(a.lattice[vids[0]].blocks[3]) + r.processBlock(b) + s.NotNil(r.lattice[vids[0]].blocks[3]) // Add block 3-1 which acks 3-0. - h = a.lattice[vids[3]].blocks[0].Hash + h = r.lattice[vids[3]].blocks[0].Hash b = &types.Block{ ProposerID: vids[3], ParentHash: h, @@ -128,26 +128,26 @@ func genTestCase1(s *AckingTest, a *acking) []types.ValidatorID { h: struct{}{}, }, } - a.processBlock(b) - s.NotNil(a.lattice[vids[3]].blocks[0]) + r.processBlock(b) + s.NotNil(r.lattice[vids[3]].blocks[0]) return vids } -func (s *AckingTest) TestAddValidator() { - a := newAcking() - s.Equal(len(a.lattice), 0) - genTestCase1(s, a) - s.Equal(len(a.lattice), 4) +func (s *ReliableBroadcastTest) TestAddValidator() { + r := newReliableBroadcast() + s.Equal(len(r.lattice), 0) + genTestCase1(s, r) + s.Equal(len(r.lattice), 4) } -func (s *AckingTest) TestSanityCheck() { +func (s *ReliableBroadcastTest) TestSanityCheck() { var b *types.Block var h common.Hash var vids []types.ValidatorID var err error - a := newAcking() - vids = genTestCase1(s, a) + r := newReliableBroadcast() + vids = genTestCase1(s, r) // Non-genesis block with no ack, should get error. b = &types.Block{ @@ -156,7 +156,7 @@ func (s *AckingTest) TestSanityCheck() { Height: 10, Acks: make(map[common.Hash]struct{}), } - err = a.sanityCheck(b) + err = r.sanityCheck(b) s.NotNil(err) s.Equal(ErrNotAckParent.Error(), err.Error()) @@ -166,15 +166,15 @@ func (s *AckingTest) TestSanityCheck() { ParentHash: common.NewRandomHash(), Height: 1, Acks: map[common.Hash]struct{}{ - a.lattice[vids[2]].blocks[0].Hash: struct{}{}, + r.lattice[vids[2]].blocks[0].Hash: struct{}{}, }, } - err = a.sanityCheck(b) + err = r.sanityCheck(b) s.NotNil(err) s.Equal(ErrNotAckParent.Error(), err.Error()) // Non-genesis block which acks its parent but the height is invalid. - h = a.lattice[vids[1]].blocks[0].Hash + h = r.lattice[vids[1]].blocks[0].Hash b = &types.Block{ ProposerID: vids[1], ParentHash: h, @@ -183,12 +183,12 @@ func (s *AckingTest) TestSanityCheck() { h: struct{}{}, }, } - err = a.sanityCheck(b) + err = r.sanityCheck(b) s.NotNil(err) s.Equal(ErrInvalidBlockHeight.Error(), err.Error()) // Invalid proposer ID. - h = a.lattice[vids[1]].blocks[0].Hash + h = r.lattice[vids[1]].blocks[0].Hash b = &types.Block{ ProposerID: types.ValidatorID{Hash: common.NewRandomHash()}, ParentHash: h, @@ -197,12 +197,12 @@ func (s *AckingTest) TestSanityCheck() { h: struct{}{}, }, } - err = a.sanityCheck(b) + err = r.sanityCheck(b) s.NotNil(err) s.Equal(ErrInvalidProposerID.Error(), err.Error()) // Fork block. - h = a.lattice[vids[0]].blocks[0].Hash + h = r.lattice[vids[0]].blocks[0].Hash b = &types.Block{ ProposerID: vids[0], ParentHash: h, @@ -211,27 +211,27 @@ func (s *AckingTest) TestSanityCheck() { h: struct{}{}, }, } - err = a.sanityCheck(b) + err = r.sanityCheck(b) s.NotNil(err) s.Equal(ErrForkBlock.Error(), err.Error()) // Replicated ack. - h = a.lattice[vids[0]].blocks[3].Hash + h = r.lattice[vids[0]].blocks[3].Hash b = &types.Block{ ProposerID: vids[0], ParentHash: h, Height: 4, Acks: map[common.Hash]struct{}{ h: struct{}{}, - a.lattice[vids[1]].blocks[0].Hash: struct{}{}, + r.lattice[vids[1]].blocks[0].Hash: struct{}{}, }, } - err = a.sanityCheck(b) + err = r.sanityCheck(b) s.NotNil(err) s.Equal(ErrDoubleAck.Error(), err.Error()) // Normal block. - h = a.lattice[vids[1]].blocks[0].Hash + h = r.lattice[vids[1]].blocks[0].Hash b = &types.Block{ ProposerID: vids[1], ParentHash: h, @@ -241,30 +241,30 @@ func (s *AckingTest) TestSanityCheck() { common.NewRandomHash(): struct{}{}, }, } - err = a.sanityCheck(b) + err = r.sanityCheck(b) s.Nil(err) } -func (s *AckingTest) TestAreAllAcksInLattice() { +func (s *ReliableBroadcastTest) TestAreAllAcksInLattice() { var b *types.Block var vids []types.ValidatorID - a := newAcking() - vids = genTestCase1(s, a) + r := newReliableBroadcast() + vids = genTestCase1(s, r) // Empty ack should get true, although won't pass sanity check. b = &types.Block{ Acks: map[common.Hash]struct{}{}, } - s.True(a.areAllAcksInLattice(b)) + s.True(r.areAllAcksInLattice(b)) // Acks blocks in lattice b = &types.Block{ Acks: map[common.Hash]struct{}{ - a.lattice[vids[0]].blocks[0].Hash: struct{}{}, - a.lattice[vids[0]].blocks[1].Hash: struct{}{}, + r.lattice[vids[0]].blocks[0].Hash: struct{}{}, + r.lattice[vids[0]].blocks[1].Hash: struct{}{}, }, } - s.True(a.areAllAcksInLattice(b)) + s.True(r.areAllAcksInLattice(b)) // Acks random block hash. b = &types.Block{ @@ -272,97 +272,97 @@ func (s *AckingTest) TestAreAllAcksInLattice() { common.NewRandomHash(): struct{}{}, }, } - s.False(a.areAllAcksInLattice(b)) + s.False(r.areAllAcksInLattice(b)) } -func (s *AckingTest) TestStrongAck() { +func (s *ReliableBroadcastTest) TestStrongAck() { var b *types.Block var vids []types.ValidatorID - a := newAcking() - vids = genTestCase1(s, a) + r := newReliableBroadcast() + vids = genTestCase1(s, r) // Check block 0-0 to 0-3 before adding 1-1 and 2-1. for i := uint64(0); i < 4; i++ { - s.Equal(types.BlockStatusInit, a.lattice[vids[0]].blocks[i].Status) + s.Equal(types.BlockStatusInit, r.lattice[vids[0]].blocks[i].Status) } // Add block 1-1 which acks 1-0 and 0-2, and block 0-0 to 0-3 are still // in BlockStatusInit, because they are not strongly acked. b = &types.Block{ ProposerID: vids[1], - ParentHash: a.lattice[vids[1]].blocks[0].Hash, + ParentHash: r.lattice[vids[1]].blocks[0].Hash, Hash: common.NewRandomHash(), Height: 1, Acks: map[common.Hash]struct{}{ - a.lattice[vids[0]].blocks[2].Hash: struct{}{}, - a.lattice[vids[1]].blocks[0].Hash: struct{}{}, + r.lattice[vids[0]].blocks[2].Hash: struct{}{}, + r.lattice[vids[1]].blocks[0].Hash: struct{}{}, }, } - a.processBlock(b) - s.NotNil(a.lattice[vids[1]].blocks[1]) + r.processBlock(b) + s.NotNil(r.lattice[vids[1]].blocks[1]) for i := uint64(0); i < 4; i++ { - s.Equal(types.BlockStatusInit, a.lattice[vids[0]].blocks[i].Status) + s.Equal(types.BlockStatusInit, r.lattice[vids[0]].blocks[i].Status) } // Add block 2-1 which acks 0-2 and 2-0, block 0-0 to 0-2 are strongly acked but // 0-3 is still not. b = &types.Block{ ProposerID: vids[2], - ParentHash: a.lattice[vids[2]].blocks[0].Hash, + ParentHash: r.lattice[vids[2]].blocks[0].Hash, Hash: common.NewRandomHash(), Height: 1, Acks: map[common.Hash]struct{}{ - a.lattice[vids[0]].blocks[2].Hash: struct{}{}, - a.lattice[vids[2]].blocks[0].Hash: struct{}{}, + r.lattice[vids[0]].blocks[2].Hash: struct{}{}, + r.lattice[vids[2]].blocks[0].Hash: struct{}{}, }, } - a.processBlock(b) - s.Equal(types.BlockStatusAcked, a.lattice[vids[0]].blocks[0].Status) - s.Equal(types.BlockStatusAcked, a.lattice[vids[0]].blocks[1].Status) - s.Equal(types.BlockStatusAcked, a.lattice[vids[0]].blocks[2].Status) - s.Equal(types.BlockStatusInit, a.lattice[vids[0]].blocks[3].Status) + r.processBlock(b) + s.Equal(types.BlockStatusAcked, r.lattice[vids[0]].blocks[0].Status) + s.Equal(types.BlockStatusAcked, r.lattice[vids[0]].blocks[1].Status) + s.Equal(types.BlockStatusAcked, r.lattice[vids[0]].blocks[2].Status) + s.Equal(types.BlockStatusInit, r.lattice[vids[0]].blocks[3].Status) } -func (s *AckingTest) TestExtractBlocks() { +func (s *ReliableBroadcastTest) TestExtractBlocks() { var b *types.Block - a := newAcking() - vids := genTestCase1(s, a) + r := newReliableBroadcast() + vids := genTestCase1(s, r) // Add block 1-1 which acks 1-0, 0-2, 3-0. b = &types.Block{ ProposerID: vids[1], - ParentHash: a.lattice[vids[1]].blocks[0].Hash, + ParentHash: r.lattice[vids[1]].blocks[0].Hash, Hash: common.NewRandomHash(), Height: 1, Acks: map[common.Hash]struct{}{ - a.lattice[vids[0]].blocks[2].Hash: struct{}{}, - a.lattice[vids[1]].blocks[0].Hash: struct{}{}, - a.lattice[vids[3]].blocks[0].Hash: struct{}{}, + r.lattice[vids[0]].blocks[2].Hash: struct{}{}, + r.lattice[vids[1]].blocks[0].Hash: struct{}{}, + r.lattice[vids[3]].blocks[0].Hash: struct{}{}, }, } - a.processBlock(b) + r.processBlock(b) // Add block 2-1 which acks 0-2, 2-0, 3-0. b = &types.Block{ ProposerID: vids[2], - ParentHash: a.lattice[vids[2]].blocks[0].Hash, + ParentHash: r.lattice[vids[2]].blocks[0].Hash, Hash: common.NewRandomHash(), Height: 1, Acks: map[common.Hash]struct{}{ - a.lattice[vids[0]].blocks[2].Hash: struct{}{}, - a.lattice[vids[2]].blocks[0].Hash: struct{}{}, - a.lattice[vids[3]].blocks[0].Hash: struct{}{}, + r.lattice[vids[0]].blocks[2].Hash: struct{}{}, + r.lattice[vids[2]].blocks[0].Hash: struct{}{}, + r.lattice[vids[3]].blocks[0].Hash: struct{}{}, }, } - a.processBlock(b) + r.processBlock(b) hashs := []common.Hash{ - a.lattice[vids[0]].blocks[0].Hash, - a.lattice[vids[0]].blocks[1].Hash, - a.lattice[vids[3]].blocks[0].Hash, + r.lattice[vids[0]].blocks[0].Hash, + r.lattice[vids[0]].blocks[1].Hash, + r.lattice[vids[3]].blocks[0].Hash, } hashExtracted := map[common.Hash]*types.Block{} - for _, b := range a.extractBlocks() { + for _, b := range r.extractBlocks() { hashExtracted[b.Hash] = b s.Equal(types.BlockStatusOrdering, b.Status) } @@ -372,8 +372,8 @@ func (s *AckingTest) TestExtractBlocks() { } } -func (s *AckingTest) TestRandomIntensiveAcking() { - a := newAcking() +func (s *ReliableBroadcastTest) TestRandomIntensiveAcking() { + r := newReliableBroadcast() vids := []types.ValidatorID{} heights := map[types.ValidatorID]uint64{} extractedBlocks := []*types.Block{} @@ -381,7 +381,7 @@ func (s *AckingTest) TestRandomIntensiveAcking() { // Generate validators and genesis blocks. for i := 0; i < 4; i++ { vid := types.ValidatorID{Hash: common.NewRandomHash()} - a.addValidator(vid) + r.addValidator(vid) vids = append(vids, vid) h := common.NewRandomHash() b := &types.Block{ @@ -391,7 +391,7 @@ func (s *AckingTest) TestRandomIntensiveAcking() { Height: 0, ProposerID: vid, } - a.processBlock(b) + r.processBlock(b) heights[vid] = 1 } @@ -399,10 +399,10 @@ func (s *AckingTest) TestRandomIntensiveAcking() { vid := vids[rand.Int()%len(vids)] height := heights[vid] heights[vid]++ - parentHash := a.lattice[vid].blocks[height-1].Hash + parentHash := r.lattice[vid].blocks[height-1].Hash acks := map[common.Hash]struct{}{} for _, vid2 := range vids { - if b, exist := a.lattice[vid2].blocks[a.lattice[vid].nextAck[vid2]]; exist { + if b, exist := r.lattice[vid2].blocks[r.lattice[vid].nextAck[vid2]]; exist { acks[b.Hash] = struct{}{} } } @@ -413,18 +413,18 @@ func (s *AckingTest) TestRandomIntensiveAcking() { Height: height, Acks: acks, } - a.processBlock(b) - extractedBlocks = append(extractedBlocks, a.extractBlocks()...) + r.processBlock(b) + extractedBlocks = append(extractedBlocks, r.extractBlocks()...) } - extractedBlocks = append(extractedBlocks, a.extractBlocks()...) + extractedBlocks = append(extractedBlocks, r.extractBlocks()...) // The len of array extractedBlocks should be about 5000. s.True(len(extractedBlocks) > 4500) - // The len of a.blocks should be small if deleting mechanism works. - s.True(len(a.blocks) < 500) + // The len of r.blocks should be small if deleting mechanism works. + s.True(len(r.blocks) < 500) } -func (s *AckingTest) TestRandomlyGeneratedBlocks() { +func (s *ReliableBroadcastTest) TestRandomlyGeneratedBlocks() { var ( validatorCount = 19 blockCount = 50 @@ -432,7 +432,7 @@ func (s *AckingTest) TestRandomlyGeneratedBlocks() { ) // Prepare a randomly generated blocks. - db, err := blockdb.NewMemBackedBlockDB("test-acking-random.blockdb") + db, err := blockdb.NewMemBackedBlockDB("test-reliable-broadcast-random.blockdb") s.Require().Nil(err) defer func() { // If the test fails, keep the block database for troubleshooting. @@ -451,7 +451,7 @@ func (s *AckingTest) TestRandomlyGeneratedBlocks() { stronglyAckedHashesAsString := map[string]struct{}{} for i := 0; i < repeat; i++ { validators := map[types.ValidatorID]struct{}{} - acking := newAcking() + rb := newReliableBroadcast() stronglyAckedHashes := common.Hashes{} revealer.Reset() @@ -466,14 +466,14 @@ func (s *AckingTest) TestRandomlyGeneratedBlocks() { } s.Require().Nil(err) - // It's a hack to add validator to Acking module. + // It's a hack to add validator to reliableBroadcast module. if _, added := validators[b.ProposerID]; !added { - acking.addValidator(b.ProposerID) + rb.addValidator(b.ProposerID) validators[b.ProposerID] = struct{}{} } // Perform reliable broadcast process. - acking.processBlock(&b) - for _, b := range acking.extractBlocks() { + rb.processBlock(&b) + for _, b := range rb.extractBlocks() { stronglyAckedHashes = append(stronglyAckedHashes, b.Hash) } } @@ -495,6 +495,6 @@ func (s *AckingTest) TestRandomlyGeneratedBlocks() { } } -func TestAcking(t *testing.T) { - suite.Run(t, new(AckingTest)) +func TestReliableBroadcast(t *testing.T) { + suite.Run(t, new(ReliableBroadcastTest)) } diff --git a/core/sequencer.go b/core/total-ordering.go index 36c1383..63d3416 100644 --- a/core/sequencer.go +++ b/core/total-ordering.go @@ -25,7 +25,7 @@ import ( "github.com/dexon-foundation/dexon-consensus-core/core/types" ) -// ErrNotValidDAG would be reported when block subbmitted to sequencer +// ErrNotValidDAG would be reported when block subbmitted to totalOrdering // didn't form a DAG. var ErrNotValidDAG = fmt.Errorf("not a valid dag") @@ -157,9 +157,9 @@ func (v blockVector) getHeightVector() ackingStatusVector { return ret } -// Sequencer represent a process unit to handle total ordering +// totalOrdering represent a process unit to handle total ordering // for blocks. -type sequencer struct { +type totalOrdering struct { // pendings stores blocks awaiting to be ordered. pendings map[common.Hash]*types.Block @@ -190,8 +190,8 @@ type sequencer struct { acked map[common.Hash]map[common.Hash]struct{} } -func newSequencer(k, phi, validatorCount uint64) *sequencer { - return &sequencer{ +func newTotalOrdering(k, phi, validatorCount uint64) *totalOrdering { + return &totalOrdering{ candidateAckingStatusVectors: make(map[common.Hash]ackingStatusVector), pendings: make(map[common.Hash]*types.Block), k: k, @@ -203,16 +203,16 @@ func newSequencer(k, phi, validatorCount uint64) *sequencer { } // buildBlockRelation populates the acked according their acking relationships. -func (s *sequencer) buildBlockRelation(b *types.Block) { +func (to *totalOrdering) buildBlockRelation(b *types.Block) { // populateAcked would update all blocks implcitly acked // by input block recursively. var populateAcked func(bx, target *types.Block) populateAcked = func(bx, target *types.Block) { for ack := range bx.Acks { - acked, exists := s.acked[ack] + acked, exists := to.acked[ack] if !exists { acked = make(map[common.Hash]struct{}) - s.acked[ack] = acked + to.acked[ack] = acked } // This means we've walked this block already. @@ -222,7 +222,7 @@ func (s *sequencer) buildBlockRelation(b *types.Block) { acked[target.Hash] = struct{}{} // See if we need to go forward. - if nextBlock, exists := s.pendings[ack]; !exists { + if nextBlock, exists := to.pendings[ack]; !exists { continue } else { populateAcked(nextBlock, target) @@ -234,23 +234,23 @@ func (s *sequencer) buildBlockRelation(b *types.Block) { // clean would remove a block from working set. This behaviour // would prevent our memory usage growing infinity. -func (s *sequencer) clean(h common.Hash) { - delete(s.acked, h) - delete(s.pendings, h) - delete(s.candidateAckingStatusVectors, h) +func (to *totalOrdering) clean(h common.Hash) { + delete(to.acked, h) + delete(to.pendings, h) + delete(to.candidateAckingStatusVectors, h) } // updateVectors is a helper function to update all cached vectors. -func (s *sequencer) updateVectors(b *types.Block) (err error) { +func (to *totalOrdering) updateVectors(b *types.Block) (err error) { // Update global height vector - err = s.globalVector.addBlock(b) + err = to.globalVector.addBlock(b) if err != nil { return } // Update acking status of candidates. - for candidate, vector := range s.candidateAckingStatusVectors { - if _, acked := s.acked[candidate][b.Hash]; !acked { + for candidate, vector := range to.candidateAckingStatusVectors { + if _, acked := to.acked[candidate][b.Hash]; !acked { continue } if err = vector.addBlock(b); err != nil { @@ -261,7 +261,7 @@ func (s *sequencer) updateVectors(b *types.Block) (err error) { } // grade implements the 'grade' potential function described in white paper. -func (s *sequencer) grade( +func (to *totalOrdering) grade( hvFrom, hvTo map[types.ValidatorID]uint64, globalAns map[types.ValidatorID]struct{}) int { @@ -277,9 +277,9 @@ func (s *sequencer) grade( } } - if count >= s.phi { + if count >= to.phi { return 1 - } else if count < s.phi-s.validatorCount+uint64(len(globalAns)) { + } else if count < to.phi-to.validatorCount+uint64(len(globalAns)) { return 0 } else { return -1 @@ -288,10 +288,10 @@ func (s *sequencer) grade( // buildAckingStatusVectorForNewCandidate is a helper function to // build ackingStatusVector for new candidate. -func (s *sequencer) buildAckingStatusVectorForNewCandidate( +func (to *totalOrdering) buildAckingStatusVectorForNewCandidate( candidate *types.Block) (hVec ackingStatusVector) { - blocks := s.globalVector[candidate.ProposerID] + blocks := to.globalVector[candidate.ProposerID] hVec = ackingStatusVector{ candidate.ProposerID: &struct { minHeight, count uint64 @@ -301,13 +301,13 @@ func (s *sequencer) buildAckingStatusVectorForNewCandidate( }, } - ackedsForCandidate, exists := s.acked[candidate.Hash] + ackedsForCandidate, exists := to.acked[candidate.Hash] if !exists { // This candidate is acked by nobody. return } - for vID, blocks := range s.globalVector { + for vID, blocks := range to.globalVector { if vID == candidate.ProposerID { continue } @@ -333,9 +333,9 @@ func (s *sequencer) buildAckingStatusVectorForNewCandidate( // isAckOnlyPrecedings is a helper function to check if a block // only contain acks to delivered blocks. -func (s *sequencer) isAckOnlyPrecedings(b *types.Block) bool { +func (to *totalOrdering) isAckOnlyPrecedings(b *types.Block) bool { for ack := range b.Acks { - if _, pending := s.pendings[ack]; pending { + if _, pending := to.pendings[ack]; pending { return false } } @@ -344,40 +344,40 @@ func (s *sequencer) isAckOnlyPrecedings(b *types.Block) bool { // output is a helper function to finish the delivery of // deliverable preceding set. -func (s *sequencer) output(precedings map[common.Hash]struct{}) common.Hashes { +func (to *totalOrdering) output(precedings map[common.Hash]struct{}) common.Hashes { ret := common.Hashes{} for p := range precedings { ret = append(ret, p) // Remove the first element from corresponding blockVector. - b := s.pendings[p] - s.globalVector[b.ProposerID] = s.globalVector[b.ProposerID][1:] + b := to.pendings[p] + to.globalVector[b.ProposerID] = to.globalVector[b.ProposerID][1:] // Remove block relations. - s.clean(p) + to.clean(p) } sort.Sort(ret) // Find new candidates from tip of globalVector of each validator. // The complexity here is O(N^2logN). - for _, blocks := range s.globalVector { + for _, blocks := range to.globalVector { if len(blocks) == 0 { continue } tip := blocks[0] if _, alreadyCandidate := - s.candidateAckingStatusVectors[tip.Hash]; alreadyCandidate { + to.candidateAckingStatusVectors[tip.Hash]; alreadyCandidate { continue } - if !s.isAckOnlyPrecedings(tip) { + if !to.isAckOnlyPrecedings(tip) { continue } // Build ackingStatusVector for new candidate. - s.candidateAckingStatusVectors[tip.Hash] = - s.buildAckingStatusVectorForNewCandidate(tip) + to.candidateAckingStatusVectors[tip.Hash] = + to.buildAckingStatusVectorForNewCandidate(tip) } return ret } @@ -385,25 +385,25 @@ func (s *sequencer) output(precedings map[common.Hash]struct{}) common.Hashes { // generateDeliverSet would: // - generate preceding set // - check if the preceding set deliverable by checking potential function -func (s *sequencer) generateDeliverSet() ( +func (to *totalOrdering) generateDeliverSet() ( delivered map[common.Hash]struct{}, early bool) { - globalHeightVector := s.globalVector.getHeightVector() + globalHeightVector := to.globalVector.getHeightVector() ahvs := map[common.Hash]map[types.ValidatorID]uint64{} - for candidate, v := range s.candidateAckingStatusVectors { - ahvs[candidate] = v.getAckingHeightVector(globalHeightVector, s.k) + for candidate, v := range to.candidateAckingStatusVectors { + ahvs[candidate] = v.getAckingHeightVector(globalHeightVector, to.k) } - globalAns := globalHeightVector.getAckingNodeSet(globalHeightVector, s.k) + globalAns := globalHeightVector.getAckingNodeSet(globalHeightVector, to.k) precedings := make(map[common.Hash]struct{}) CheckNextCandidateLoop: - for candidate := range s.candidateAckingStatusVectors { - for otherCandidate := range s.candidateAckingStatusVectors { + for candidate := range to.candidateAckingStatusVectors { + for otherCandidate := range to.candidateAckingStatusVectors { if candidate == otherCandidate { continue } - if s.grade(ahvs[otherCandidate], ahvs[candidate], globalAns) != 0 { + if to.grade(ahvs[otherCandidate], ahvs[candidate], globalAns) != 0 { continue CheckNextCandidateLoop } } @@ -416,7 +416,7 @@ CheckNextCandidateLoop: // internal is a helper function to verify internal stability. internal := func() bool { - for candidate := range s.candidateAckingStatusVectors { + for candidate := range to.candidateAckingStatusVectors { if _, isPreceding := precedings[candidate]; isPreceding { continue } @@ -424,7 +424,7 @@ CheckNextCandidateLoop: beaten := false for p := range precedings { if beaten = - s.grade(ahvs[p], ahvs[candidate], globalAns) == 1; beaten { + to.grade(ahvs[p], ahvs[candidate], globalAns) == 1; beaten { break } } @@ -447,7 +447,7 @@ CheckNextCandidateLoop: } } - if count > s.phi { + if count > to.phi { return true } } @@ -459,9 +459,9 @@ CheckNextCandidateLoop: // to be delivered. checkANS := func() bool { for p := range precedings { - validatorAns := s.candidateAckingStatusVectors[p].getAckingNodeSet( - globalHeightVector, s.k) - if uint64(len(validatorAns)) < s.validatorCount-s.phi { + validatorAns := to.candidateAckingStatusVectors[p].getAckingNodeSet( + globalHeightVector, to.k) + if uint64(len(validatorAns)) < to.validatorCount-to.phi { return false } } @@ -476,7 +476,7 @@ CheckNextCandidateLoop: // If all validators propose enough blocks, we should force // to deliver since the whole picture of the DAG is revealed. - if uint64(len(globalAns)) != s.validatorCount { + if uint64(len(globalAns)) != to.validatorCount { // The whole picture is not ready, we need to check if // exteranl stability is met, and we can deliver earlier. if checkAHV() && checkANS() { @@ -490,8 +490,8 @@ CheckNextCandidateLoop: return } -// processBlock is the entry point of sequencer. -func (s *sequencer) processBlock(b *types.Block) ( +// processBlock is the entry point of totalOrdering. +func (to *totalOrdering) processBlock(b *types.Block) ( delivered common.Hashes, early bool, err error) { // NOTE: I assume the block 'b' is already safe for total ordering. @@ -499,14 +499,14 @@ func (s *sequencer) processBlock(b *types.Block) ( // total ordering stage. // Incremental part. - s.pendings[b.Hash] = b - s.buildBlockRelation(b) - if err = s.updateVectors(b); err != nil { + to.pendings[b.Hash] = b + to.buildBlockRelation(b) + if err = to.updateVectors(b); err != nil { return } - if s.isAckOnlyPrecedings(b) { - s.candidateAckingStatusVectors[b.Hash] = - s.buildAckingStatusVectorForNewCandidate(b) + if to.isAckOnlyPrecedings(b) { + to.candidateAckingStatusVectors[b.Hash] = + to.buildAckingStatusVectorForNewCandidate(b) } // Not-Incremental part (yet). @@ -514,9 +514,9 @@ func (s *sequencer) processBlock(b *types.Block) ( // - generate ans for each candidate // - generate global ans // - find preceding set - hashes, early := s.generateDeliverSet() + hashes, early := to.generateDeliverSet() // output precedings - delivered = s.output(hashes) + delivered = to.output(hashes) return } diff --git a/core/sequencer_test.go b/core/total-ordering_test.go index ae9cca7..11e253a 100644 --- a/core/sequencer_test.go +++ b/core/total-ordering_test.go @@ -29,11 +29,11 @@ import ( "github.com/stretchr/testify/suite" ) -type SequencerTestSuite struct { +type TotalOrderingTestSuite struct { suite.Suite } -func (s *SequencerTestSuite) generateValidatorIDs( +func (s *TotalOrderingTestSuite) generateValidatorIDs( count int) []types.ValidatorID { validatorIDs := []types.ValidatorID{} @@ -45,7 +45,7 @@ func (s *SequencerTestSuite) generateValidatorIDs( return validatorIDs } -func (s *SequencerTestSuite) genRootBlock( +func (s *TotalOrderingTestSuite) genRootBlock( vID types.ValidatorID, acks map[common.Hash]struct{}) *types.Block { hash := common.NewRandomHash() @@ -58,21 +58,21 @@ func (s *SequencerTestSuite) genRootBlock( } } -func (s *SequencerTestSuite) checkNotDeliver(seq *sequencer, b *types.Block) { - hashes, eqrly, err := seq.processBlock(b) +func (s *TotalOrderingTestSuite) checkNotDeliver(to *totalOrdering, b *types.Block) { + hashes, eqrly, err := to.processBlock(b) s.Empty(hashes) s.False(eqrly) s.Nil(err) } -func (s *SequencerTestSuite) checkNotInWorkingSet( - seq *sequencer, b *types.Block) { +func (s *TotalOrderingTestSuite) checkNotInWorkingSet( + to *totalOrdering, b *types.Block) { - s.NotContains(seq.pendings, b.Hash) - s.NotContains(seq.acked, b.Hash) + s.NotContains(to.pendings, b.Hash) + s.NotContains(to.acked, b.Hash) } -func (s *SequencerTestSuite) TestBlockRelation() { +func (s *TotalOrderingTestSuite) TestBlockRelation() { // This test case would verify if 'acking' and 'acked' // accumulated correctly. // @@ -108,27 +108,27 @@ func (s *SequencerTestSuite) TestBlockRelation() { }, } - seq := newSequencer(1, 3, 5) - s.checkNotDeliver(seq, blockA) - s.checkNotDeliver(seq, blockB) - s.checkNotDeliver(seq, blockC) + to := newTotalOrdering(1, 3, 5) + s.checkNotDeliver(to, blockA) + s.checkNotDeliver(to, blockB) + s.checkNotDeliver(to, blockC) // Check 'acked'. - ackedA := seq.acked[blockA.Hash] + ackedA := to.acked[blockA.Hash] s.Require().NotNil(ackedA) s.Len(ackedA, 2) s.Contains(ackedA, blockB.Hash) s.Contains(ackedA, blockC.Hash) - ackedB := seq.acked[blockB.Hash] + ackedB := to.acked[blockB.Hash] s.Require().NotNil(ackedB) s.Len(ackedB, 1) s.Contains(ackedB, blockC.Hash) - s.Nil(seq.acked[blockC.Hash]) + s.Nil(to.acked[blockC.Hash]) } -func (s *SequencerTestSuite) TestCreateAckingHeightVectorFromHeightVector() { +func (s *TotalOrderingTestSuite) TestCreateAckingHeightVectorFromHeightVector() { validators := s.generateValidatorIDs(5) global := ackingStatusVector{ validators[0]: &struct{ minHeight, count uint64 }{ @@ -172,7 +172,7 @@ func (s *SequencerTestSuite) TestCreateAckingHeightVectorFromHeightVector() { s.Len(ahv, 0) } -func (s *SequencerTestSuite) TestCreateAckingNodeSetFromHeightVector() { +func (s *TotalOrderingTestSuite) TestCreateAckingNodeSetFromHeightVector() { validators := s.generateValidatorIDs(5) global := ackingStatusVector{ validators[0]: &struct{ minHeight, count uint64 }{ @@ -194,9 +194,9 @@ func (s *SequencerTestSuite) TestCreateAckingNodeSetFromHeightVector() { s.Len(local.getAckingNodeSet(global, 3), 0) } -func (s *SequencerTestSuite) TestGrade() { +func (s *TotalOrderingTestSuite) TestGrade() { validators := s.generateValidatorIDs(5) - seq := newSequencer(1, 3, 5) // K doesn't matter when calculating preceding. + to := newTotalOrdering(1, 3, 5) // K doesn't matter when calculating preceding. ans := map[types.ValidatorID]struct{}{ validators[0]: struct{}{}, @@ -223,13 +223,13 @@ func (s *SequencerTestSuite) TestGrade() { validators[2]: infinity, validators[3]: infinity, } - s.Equal(seq.grade(ahv2, ahv1, ans), 1) - s.Equal(seq.grade(ahv1, ahv2, ans), 0) - s.Equal(seq.grade(ahv2, ahv3, ans), -1) - s.Equal(seq.grade(ahv3, ahv2, ans), 0) + s.Equal(to.grade(ahv2, ahv1, ans), 1) + s.Equal(to.grade(ahv1, ahv2, ans), 0) + s.Equal(to.grade(ahv2, ahv3, ans), -1) + s.Equal(to.grade(ahv3, ahv2, ans), 0) } -func (s *SequencerTestSuite) TestCycleDetection() { +func (s *TotalOrderingTestSuite) TestCycleDetection() { // Make sure we don't get hang by cycle from // block's acks. validators := s.generateValidatorIDs(5) @@ -287,20 +287,20 @@ func (s *SequencerTestSuite) TestCycleDetection() { } // Make sure we won't hang when cycle exists. - seq := newSequencer(1, 3, 5) - s.checkNotDeliver(seq, b00) - s.checkNotDeliver(seq, b01) - s.checkNotDeliver(seq, b02) + to := newTotalOrdering(1, 3, 5) + s.checkNotDeliver(to, b00) + s.checkNotDeliver(to, b01) + s.checkNotDeliver(to, b02) // Should not hang in this line. - s.checkNotDeliver(seq, b03) + s.checkNotDeliver(to, b03) // Should not hang in this line - s.checkNotDeliver(seq, b10) + s.checkNotDeliver(to, b10) } -func (s *SequencerTestSuite) TestNotValidDAGDetection() { +func (s *TotalOrderingTestSuite) TestNotValidDAGDetection() { validators := s.generateValidatorIDs(4) - seq := newSequencer(1, 3, 5) + to := newTotalOrdering(1, 3, 5) hash := common.NewRandomHash() b00 := &types.Block{ @@ -316,14 +316,14 @@ func (s *SequencerTestSuite) TestNotValidDAGDetection() { Hash: common.NewRandomHash(), } - // When submit to block with lower height to sequencer, + // When submit to block with lower height to totalOrdering, // caller should receive an error. - s.checkNotDeliver(seq, b01) - _, _, err := seq.processBlock(b00) + s.checkNotDeliver(to, b01) + _, _, err := to.processBlock(b00) s.Equal(err, ErrNotValidDAG) } -func (s *SequencerTestSuite) TestEarlyDeliver() { +func (s *TotalOrderingTestSuite) TestEarlyDeliver() { // The test scenario: // // o o o o o @@ -334,7 +334,7 @@ func (s *SequencerTestSuite) TestEarlyDeliver() { // A B // Even when B is not received, A should // be able to be delivered. - seq := newSequencer(2, 3, 5) + to := newTotalOrdering(2, 3, 5) validators := s.generateValidatorIDs(5) genNextBlock := func(b *types.Block) *types.Block { @@ -373,29 +373,29 @@ func (s *SequencerTestSuite) TestEarlyDeliver() { // It's a valid block sequence to deliver // to total ordering algorithm: DAG. - s.checkNotDeliver(seq, b00) - s.checkNotDeliver(seq, b01) - s.checkNotDeliver(seq, b02) + s.checkNotDeliver(to, b00) + s.checkNotDeliver(to, b01) + s.checkNotDeliver(to, b02) - vec := seq.candidateAckingStatusVectors[b00.Hash] + vec := to.candidateAckingStatusVectors[b00.Hash] s.Require().NotNil(vec) s.Len(vec, 1) s.Equal(vec[validators[0]].minHeight, b00.Height) s.Equal(vec[validators[0]].count, uint64(3)) - s.checkNotDeliver(seq, b10) - s.checkNotDeliver(seq, b11) - s.checkNotDeliver(seq, b12) - s.checkNotDeliver(seq, b20) - s.checkNotDeliver(seq, b21) - s.checkNotDeliver(seq, b22) - s.checkNotDeliver(seq, b30) - s.checkNotDeliver(seq, b31) + s.checkNotDeliver(to, b10) + s.checkNotDeliver(to, b11) + s.checkNotDeliver(to, b12) + s.checkNotDeliver(to, b20) + s.checkNotDeliver(to, b21) + s.checkNotDeliver(to, b22) + s.checkNotDeliver(to, b30) + s.checkNotDeliver(to, b31) // Check the internal state before delivering. - s.Len(seq.candidateAckingStatusVectors, 1) // b00 is the only candidate. + s.Len(to.candidateAckingStatusVectors, 1) // b00 is the only candidate. - vec = seq.candidateAckingStatusVectors[b00.Hash] + vec = to.candidateAckingStatusVectors[b00.Hash] s.Require().NotNil(vec) s.Len(vec, 4) s.Equal(vec[validators[0]].minHeight, b00.Height) @@ -407,50 +407,50 @@ func (s *SequencerTestSuite) TestEarlyDeliver() { s.Equal(vec[validators[3]].minHeight, b30.Height) s.Equal(vec[validators[3]].count, uint64(2)) - hashes, early, err := seq.processBlock(b32) + hashes, early, err := to.processBlock(b32) s.Require().Len(hashes, 1) s.True(early) s.Nil(err) s.Equal(hashes[0], b00.Hash) // Check the internal state after delivered. - s.Len(seq.candidateAckingStatusVectors, 4) // b01, b10, b20, b30 are candidates. + s.Len(to.candidateAckingStatusVectors, 4) // b01, b10, b20, b30 are candidates. // Check b01. - vec = seq.candidateAckingStatusVectors[b01.Hash] + vec = to.candidateAckingStatusVectors[b01.Hash] s.Require().NotNil(vec) s.Len(vec, 1) s.Equal(vec[validators[0]].minHeight, b01.Height) s.Equal(vec[validators[0]].count, uint64(2)) // Check b10. - vec = seq.candidateAckingStatusVectors[b10.Hash] + vec = to.candidateAckingStatusVectors[b10.Hash] s.Require().NotNil(vec) s.Len(vec, 1) s.Equal(vec[validators[1]].minHeight, b10.Height) s.Equal(vec[validators[1]].count, uint64(3)) // Check b20. - vec = seq.candidateAckingStatusVectors[b20.Hash] + vec = to.candidateAckingStatusVectors[b20.Hash] s.Require().NotNil(vec) s.Len(vec, 1) s.Equal(vec[validators[2]].minHeight, b20.Height) s.Equal(vec[validators[2]].count, uint64(3)) // Check b30. - vec = seq.candidateAckingStatusVectors[b30.Hash] + vec = to.candidateAckingStatusVectors[b30.Hash] s.Require().NotNil(vec) s.Len(vec, 1) s.Equal(vec[validators[3]].minHeight, b30.Height) s.Equal(vec[validators[3]].count, uint64(3)) // Make sure b00 doesn't exist in current working set: - s.checkNotInWorkingSet(seq, b00) + s.checkNotInWorkingSet(to, b00) } -func (s *SequencerTestSuite) TestBasicCaseForK2() { +func (s *TotalOrderingTestSuite) TestBasicCaseForK2() { // It's a handcrafted test case. - seq := newSequencer(2, 3, 5) + to := newTotalOrdering(2, 3, 5) validators := s.generateValidatorIDs(5) b00 := s.genRootBlock(validators[0], map[common.Hash]struct{}{}) @@ -596,20 +596,20 @@ func (s *SequencerTestSuite) TestBasicCaseForK2() { }, } - s.checkNotDeliver(seq, b00) - s.checkNotDeliver(seq, b10) - s.checkNotDeliver(seq, b11) - s.checkNotDeliver(seq, b01) - s.checkNotDeliver(seq, b20) - s.checkNotDeliver(seq, b30) - s.checkNotDeliver(seq, b21) - s.checkNotDeliver(seq, b31) - s.checkNotDeliver(seq, b32) - s.checkNotDeliver(seq, b22) - s.checkNotDeliver(seq, b12) + s.checkNotDeliver(to, b00) + s.checkNotDeliver(to, b10) + s.checkNotDeliver(to, b11) + s.checkNotDeliver(to, b01) + s.checkNotDeliver(to, b20) + s.checkNotDeliver(to, b30) + s.checkNotDeliver(to, b21) + s.checkNotDeliver(to, b31) + s.checkNotDeliver(to, b32) + s.checkNotDeliver(to, b22) + s.checkNotDeliver(to, b12) // Make sure 'acked' for current precedings is correct. - acked := seq.acked[b00.Hash] + acked := to.acked[b00.Hash] s.Require().NotNil(acked) s.Len(acked, 7) s.Contains(acked, b01.Hash) @@ -620,7 +620,7 @@ func (s *SequencerTestSuite) TestBasicCaseForK2() { s.Contains(acked, b31.Hash) s.Contains(acked, b32.Hash) - acked = seq.acked[b10.Hash] + acked = to.acked[b10.Hash] s.Require().NotNil(acked) s.Len(acked, 9) s.Contains(acked, b01.Hash) @@ -634,10 +634,10 @@ func (s *SequencerTestSuite) TestBasicCaseForK2() { s.Contains(acked, b32.Hash) // Make sure there are 2 candidates. - s.Require().Len(seq.candidateAckingStatusVectors, 2) + s.Require().Len(to.candidateAckingStatusVectors, 2) // Check b00's height vector. - vec := seq.candidateAckingStatusVectors[b00.Hash] + vec := to.candidateAckingStatusVectors[b00.Hash] s.Require().NotNil(vec) s.NotContains(vec, validators[4]) s.Equal(vec[validators[0]].minHeight, b00.Height) @@ -650,7 +650,7 @@ func (s *SequencerTestSuite) TestBasicCaseForK2() { s.Equal(vec[validators[3]].count, uint64(2)) // Check b10's height vector. - vec = seq.candidateAckingStatusVectors[b10.Hash] + vec = to.candidateAckingStatusVectors[b10.Hash] s.Require().NotNil(vec) s.NotContains(vec, validators[4]) s.Equal(vec[validators[0]].minHeight, b01.Height) @@ -663,7 +663,7 @@ func (s *SequencerTestSuite) TestBasicCaseForK2() { s.Equal(vec[validators[3]].count, uint64(3)) // Check the first deliver. - hashes, early, err := seq.processBlock(b02) + hashes, early, err := to.processBlock(b02) s.True(early) s.Nil(err) expected := common.Hashes{b00.Hash, b10.Hash} @@ -671,14 +671,14 @@ func (s *SequencerTestSuite) TestBasicCaseForK2() { s.Equal(hashes, expected) // Make sure b00, b10 are removed from current working set. - s.checkNotInWorkingSet(seq, b00) - s.checkNotInWorkingSet(seq, b10) + s.checkNotInWorkingSet(to, b00) + s.checkNotInWorkingSet(to, b10) // Check if candidates of next round are picked correctly. - s.Len(seq.candidateAckingStatusVectors, 2) + s.Len(to.candidateAckingStatusVectors, 2) // Check b01's height vector. - vec = seq.candidateAckingStatusVectors[b11.Hash] + vec = to.candidateAckingStatusVectors[b11.Hash] s.Require().NotNil(vec) s.NotContains(vec, validators[4]) s.Equal(vec[validators[0]].minHeight, b01.Height) @@ -691,7 +691,7 @@ func (s *SequencerTestSuite) TestBasicCaseForK2() { s.Equal(vec[validators[3]].count, uint64(2)) // Check b20's height vector. - vec = seq.candidateAckingStatusVectors[b20.Hash] + vec = to.candidateAckingStatusVectors[b20.Hash] s.Require().NotNil(vec) s.NotContains(vec, validators[4]) s.Equal(vec[validators[0]].minHeight, b02.Height) @@ -703,10 +703,10 @@ func (s *SequencerTestSuite) TestBasicCaseForK2() { s.Equal(vec[validators[3]].minHeight, b30.Height) s.Equal(vec[validators[3]].count, uint64(3)) - s.checkNotDeliver(seq, b13) + s.checkNotDeliver(to, b13) // Check the second deliver. - hashes, early, err = seq.processBlock(b03) + hashes, early, err = to.processBlock(b03) s.True(early) s.Nil(err) expected = common.Hashes{b11.Hash, b20.Hash} @@ -714,18 +714,18 @@ func (s *SequencerTestSuite) TestBasicCaseForK2() { s.Equal(hashes, expected) // Make sure b11, b20 are removed from current working set. - s.checkNotInWorkingSet(seq, b11) - s.checkNotInWorkingSet(seq, b20) + s.checkNotInWorkingSet(to, b11) + s.checkNotInWorkingSet(to, b20) // Add b40, b41, b42 to pending set. - s.checkNotDeliver(seq, b40) - s.checkNotDeliver(seq, b41) - s.checkNotDeliver(seq, b42) - s.checkNotDeliver(seq, b14) + s.checkNotDeliver(to, b40) + s.checkNotDeliver(to, b41) + s.checkNotDeliver(to, b42) + s.checkNotDeliver(to, b14) // Make sure b01, b30, b40 are candidate in next round. - s.Len(seq.candidateAckingStatusVectors, 3) - vec = seq.candidateAckingStatusVectors[b01.Hash] + s.Len(to.candidateAckingStatusVectors, 3) + vec = to.candidateAckingStatusVectors[b01.Hash] s.Require().NotNil(vec) s.NotContains(vec, validators[4]) s.Equal(vec[validators[0]].minHeight, b01.Height) @@ -737,7 +737,7 @@ func (s *SequencerTestSuite) TestBasicCaseForK2() { s.Equal(vec[validators[3]].minHeight, b31.Height) s.Equal(vec[validators[3]].count, uint64(2)) - vec = seq.candidateAckingStatusVectors[b30.Hash] + vec = to.candidateAckingStatusVectors[b30.Hash] s.Require().NotNil(vec) s.NotContains(vec, validators[4]) s.Equal(vec[validators[0]].minHeight, b03.Height) @@ -749,7 +749,7 @@ func (s *SequencerTestSuite) TestBasicCaseForK2() { s.Equal(vec[validators[3]].minHeight, b30.Height) s.Equal(vec[validators[3]].count, uint64(3)) - vec = seq.candidateAckingStatusVectors[b40.Hash] + vec = to.candidateAckingStatusVectors[b40.Hash] s.Require().NotNil(vec) s.NotContains(vec, validators[0]) s.NotContains(vec, validators[1]) @@ -760,7 +760,7 @@ func (s *SequencerTestSuite) TestBasicCaseForK2() { // Make 'Acking Node Set' contains blocks from all validators, // this should trigger not-early deliver. - hashes, early, err = seq.processBlock(b23) + hashes, early, err = to.processBlock(b23) s.False(early) s.Nil(err) expected = common.Hashes{b01.Hash, b30.Hash} @@ -768,15 +768,15 @@ func (s *SequencerTestSuite) TestBasicCaseForK2() { s.Equal(expected, hashes) // Make sure b01, b30 not in working set - s.checkNotInWorkingSet(seq, b01) - s.checkNotInWorkingSet(seq, b30) + s.checkNotInWorkingSet(to, b01) + s.checkNotInWorkingSet(to, b30) // Make sure b21, b40 are candidates of next round. - s.Contains(seq.candidateAckingStatusVectors, b21.Hash) - s.Contains(seq.candidateAckingStatusVectors, b40.Hash) + s.Contains(to.candidateAckingStatusVectors, b21.Hash) + s.Contains(to.candidateAckingStatusVectors, b40.Hash) } -func (s *SequencerTestSuite) TestBasicCaseForK0() { +func (s *TotalOrderingTestSuite) TestBasicCaseForK0() { // This is a relatively simple test for K=0. // // 0 1 2 3 4 @@ -787,7 +787,7 @@ func (s *SequencerTestSuite) TestBasicCaseForK0() { // | \ | \ | | // v v v v // o o o <- o Height: 0 - seq := newSequencer(0, 3, 5) + to := newTotalOrdering(0, 3, 5) validators := s.generateValidatorIDs(5) b00 := s.genRootBlock(validators[0], map[common.Hash]struct{}{}) @@ -839,23 +839,23 @@ func (s *SequencerTestSuite) TestBasicCaseForK0() { b31.Hash: struct{}{}, }) - s.checkNotDeliver(seq, b00) - s.checkNotDeliver(seq, b10) - s.checkNotDeliver(seq, b20) - s.checkNotDeliver(seq, b30) - s.checkNotDeliver(seq, b01) - s.checkNotDeliver(seq, b11) - s.checkNotDeliver(seq, b21) - s.checkNotDeliver(seq, b31) + s.checkNotDeliver(to, b00) + s.checkNotDeliver(to, b10) + s.checkNotDeliver(to, b20) + s.checkNotDeliver(to, b30) + s.checkNotDeliver(to, b01) + s.checkNotDeliver(to, b11) + s.checkNotDeliver(to, b21) + s.checkNotDeliver(to, b31) // Check status before delivering. - vec := seq.candidateAckingStatusVectors[b00.Hash] + vec := to.candidateAckingStatusVectors[b00.Hash] s.Require().NotNil(vec) s.Len(vec, 1) s.Equal(vec[validators[0]].minHeight, b00.Height) s.Equal(vec[validators[0]].count, uint64(2)) - vec = seq.candidateAckingStatusVectors[b10.Hash] + vec = to.candidateAckingStatusVectors[b10.Hash] s.Require().NotNil(vec) s.Len(vec, 2) s.Equal(vec[validators[0]].minHeight, b01.Height) @@ -863,7 +863,7 @@ func (s *SequencerTestSuite) TestBasicCaseForK0() { s.Equal(vec[validators[1]].minHeight, b10.Height) s.Equal(vec[validators[1]].count, uint64(2)) - vec = seq.candidateAckingStatusVectors[b20.Hash] + vec = to.candidateAckingStatusVectors[b20.Hash] s.Require().NotNil(vec) s.Len(vec, 3) s.Equal(vec[validators[1]].minHeight, b11.Height) @@ -874,7 +874,7 @@ func (s *SequencerTestSuite) TestBasicCaseForK0() { s.Equal(vec[validators[3]].count, uint64(2)) // This new block should trigger non-early deliver. - hashes, early, err := seq.processBlock(b40) + hashes, early, err := to.processBlock(b40) s.False(early) s.Nil(err) expected := common.Hashes{b20.Hash} @@ -882,15 +882,15 @@ func (s *SequencerTestSuite) TestBasicCaseForK0() { s.Equal(expected, hashes) // Make sure b20 is no long existing in working set. - s.checkNotInWorkingSet(seq, b20) + s.checkNotInWorkingSet(to, b20) // Make sure b10, b30 are candidates for next round. - s.Contains(seq.candidateAckingStatusVectors, b10.Hash) - s.Contains(seq.candidateAckingStatusVectors, b30.Hash) + s.Contains(to.candidateAckingStatusVectors, b10.Hash) + s.Contains(to.candidateAckingStatusVectors, b30.Hash) } -func (s *SequencerTestSuite) baseTestRandomlyGeneratedBlocks( - seqConstructor func() *sequencer, +func (s *TotalOrderingTestSuite) baseTestRandomlyGeneratedBlocks( + totalOrderingConstructor func() *totalOrdering, revealer test.Revealer, repeat int) { @@ -901,7 +901,7 @@ func (s *SequencerTestSuite) baseTestRandomlyGeneratedBlocks( revealed := "" ordered := "" revealer.Reset() - seq := seqConstructor() + to := totalOrderingConstructor() for { // Reveal next block. b, err := revealer.Next() @@ -915,7 +915,7 @@ func (s *SequencerTestSuite) baseTestRandomlyGeneratedBlocks( revealed += b.Hash.String() + "," // Perform total ordering. - hashes, _, err := seq.processBlock(&b) + hashes, _, err := to.processBlock(&b) s.Require().Nil(err) for _, h := range hashes { ordered += h.String() + "," @@ -942,7 +942,7 @@ func (s *SequencerTestSuite) baseTestRandomlyGeneratedBlocks( } } -func (s *SequencerTestSuite) TestRandomlyGeneratedBlocks() { +func (s *TotalOrderingTestSuite) TestRandomlyGeneratedBlocks() { var ( validatorCount = 19 blockCount = 50 @@ -951,7 +951,7 @@ func (s *SequencerTestSuite) TestRandomlyGeneratedBlocks() { ) // Prepare a randomly genearated blocks. - db, err := blockdb.NewMemBackedBlockDB("test-sequencer-random.blockdb") + db, err := blockdb.NewMemBackedBlockDB("test-total-ordering-random.blockdb") s.Require().Nil(err) defer func() { // If the test fails, keep the block database for troubleshooting. @@ -970,27 +970,27 @@ func (s *SequencerTestSuite) TestRandomlyGeneratedBlocks() { s.Require().Nil(err) // Test for K=0. - constructor := func() *sequencer { - return newSequencer(0, phi, uint64(validatorCount)) + constructor := func() *totalOrdering { + return newTotalOrdering(0, phi, uint64(validatorCount)) } s.baseTestRandomlyGeneratedBlocks(constructor, revealer, repeat) // Test for K=1, - constructor = func() *sequencer { - return newSequencer(1, phi, uint64(validatorCount)) + constructor = func() *totalOrdering { + return newTotalOrdering(1, phi, uint64(validatorCount)) } s.baseTestRandomlyGeneratedBlocks(constructor, revealer, repeat) // Test for K=2, - constructor = func() *sequencer { - return newSequencer(2, phi, uint64(validatorCount)) + constructor = func() *totalOrdering { + return newTotalOrdering(2, phi, uint64(validatorCount)) } s.baseTestRandomlyGeneratedBlocks(constructor, revealer, repeat) // Test for K=3, - constructor = func() *sequencer { - return newSequencer(2, phi, uint64(validatorCount)) + constructor = func() *totalOrdering { + return newTotalOrdering(2, phi, uint64(validatorCount)) } s.baseTestRandomlyGeneratedBlocks(constructor, revealer, repeat) } -func TestSequencer(t *testing.T) { - suite.Run(t, new(SequencerTestSuite)) +func TestTotalOrdering(t *testing.T) { + suite.Run(t, new(TotalOrderingTestSuite)) } |