diff options
author | Haoping Ku <haoping.ku@dexon.org> | 2018-08-01 15:33:09 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-01 15:33:09 +0800 |
commit | 819030245cf4114429f8d8cdd32488a15a8666ee (patch) | |
tree | 3e9ab4e9b007c13ea8ff22e02bf39ad837c55493 | |
parent | 855b763b7bd5a2525e14a0c1192f265554fbcdb3 (diff) | |
download | dexon-consensus-819030245cf4114429f8d8cdd32488a15a8666ee.tar dexon-consensus-819030245cf4114429f8d8cdd32488a15a8666ee.tar.gz dexon-consensus-819030245cf4114429f8d8cdd32488a15a8666ee.tar.bz2 dexon-consensus-819030245cf4114429f8d8cdd32488a15a8666ee.tar.lz dexon-consensus-819030245cf4114429f8d8cdd32488a15a8666ee.tar.xz dexon-consensus-819030245cf4114429f8d8cdd32488a15a8666ee.tar.zst dexon-consensus-819030245cf4114429f8d8cdd32488a15a8666ee.zip |
core: make acking module implicit (#28)
This commit made acking module in core implicit for avoiding other
package to use. And fix underscore error messages.
-rw-r--r-- | core/acking.go | 44 | ||||
-rw-r--r-- | core/acking_test.go | 48 |
2 files changed, 46 insertions, 46 deletions
diff --git a/core/acking.go b/core/acking.go index b244831..b74712d 100644 --- a/core/acking.go +++ b/core/acking.go @@ -24,8 +24,8 @@ import ( "github.com/dexon-foundation/dexon-consensus-core/core/types" ) -// Acking is for acking module. -type Acking struct { +// acking is for acking module. +type acking struct { // lattice stores blocks by its validator ID and height. lattice map[types.ValidatorID]*ackingValidatorStatus @@ -61,16 +61,16 @@ type ackingValidatorStatus struct { // Errors for sanity check error. var ( - ErrInvalidProposerID = fmt.Errorf("invalid_proposer_id") - ErrForkBlock = fmt.Errorf("fork_block") - ErrNotAckParent = fmt.Errorf("not_ack_parent") - ErrDoubleAck = fmt.Errorf("double_ack") - ErrInvalidBlockHeight = fmt.Errorf("invalid_block_height") + ErrInvalidProposerID = fmt.Errorf("invalid proposer id") + ErrForkBlock = fmt.Errorf("fork block") + ErrNotAckParent = fmt.Errorf("not ack parent") + ErrDoubleAck = fmt.Errorf("double ack") + ErrInvalidBlockHeight = fmt.Errorf("invalid block height") ) -// NewAcking creates a new Acking struct. -func NewAcking() *Acking { - return &Acking{ +// newAcking creates a new acking struct. +func newAcking() *acking { + return &acking{ lattice: make(map[types.ValidatorID]*ackingValidatorStatus), blocks: make(map[common.Hash]*types.Block), receivedBlocks: make(map[common.Hash]*types.Block), @@ -78,7 +78,7 @@ func NewAcking() *Acking { } } -func (a *Acking) sanityCheck(b *types.Block) error { +func (a *acking) sanityCheck(b *types.Block) error { // Check if its proposer is in validator set. if _, exist := a.lattice[b.ProposerID]; !exist { return ErrInvalidProposerID @@ -117,7 +117,7 @@ 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 (a *acking) areAllAcksInLattice(b *types.Block) bool { for h := range b.Acks { bAck, exist := a.blocks[h] if !exist { @@ -125,7 +125,7 @@ func (a *Acking) areAllAcksInLattice(b *types.Block) bool { } if bAckInLattice, exist := a.lattice[bAck.ProposerID].blocks[bAck.Height]; !exist { if bAckInLattice.Hash != bAck.Hash { - panic("areAllAcksInLattice: Acking.lattice has corrupted") + panic("areAllAcksInLattice: acking.lattice has corrupted") } return false } @@ -133,9 +133,9 @@ func (a *Acking) areAllAcksInLattice(b *types.Block) bool { return true } -// ProcessBlock processes block, it does sanity check, inserts block into +// 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 (a *acking) processBlock(block *types.Block) { // If a block does not pass sanity check, discard this block. if err := a.sanityCheck(block); err != nil { return @@ -247,10 +247,10 @@ func (a *Acking) ProcessBlock(block *types.Block) { } } -// ExtractBlocks returns all blocks that can be inserted into total ordering's +// extractBlocks returns all blocks that can be inserted into total ordering's // DAG. This function changes the status of blocks from types.BlockStatusAcked -// to types.BlockStatusOrdering. -func (a *Acking) ExtractBlocks() []*types.Block { +// to blockStatusOrdering. +func (a *acking) extractBlocks() []*types.Block { ret := []*types.Block{} for { updated := false @@ -289,8 +289,8 @@ func (a *Acking) ExtractBlocks() []*types.Block { return ret } -// AddValidator adds validator in the validator set. -func (a *Acking) AddValidator(h types.ValidatorID) { +// addValidator adds validator in the validator set. +func (a *acking) addValidator(h types.ValidatorID) { a.lattice[h] = &ackingValidatorStatus{ blocks: make(map[uint64]*types.Block), nextAck: make(map[types.ValidatorID]uint64), @@ -299,8 +299,8 @@ func (a *Acking) AddValidator(h types.ValidatorID) { } } -// DeleteValidator deletes validator in validator set. -func (a *Acking) DeleteValidator(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) } diff --git a/core/acking_test.go b/core/acking_test.go index 5c89d24..c0b6402 100644 --- a/core/acking_test.go +++ b/core/acking_test.go @@ -48,14 +48,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 *AckingTest, a *acking) []types.ValidatorID { + // Create new acking 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) + a.addValidator(vid) vids = append(vids, vid) } // Add genesis blocks. @@ -68,7 +68,7 @@ func genTestCase1(s *AckingTest, a *Acking) []types.ValidatorID { Height: 0, Acks: map[common.Hash]struct{}{}, } - a.ProcessBlock(b) + a.processBlock(b) } // Add block 0-1 which acks 0-0. @@ -82,7 +82,7 @@ func genTestCase1(s *AckingTest, a *Acking) []types.ValidatorID { h: struct{}{}, }, } - a.ProcessBlock(b) + a.processBlock(b) s.NotNil(a.lattice[vids[0]].blocks[1]) // Add block 0-2 which acks 0-1 and 1-0. @@ -97,7 +97,7 @@ func genTestCase1(s *AckingTest, a *Acking) []types.ValidatorID { a.lattice[vids[1]].blocks[0].Hash: struct{}{}, }, } - a.ProcessBlock(b) + a.processBlock(b) s.NotNil(a.lattice[vids[0]].blocks[2]) // Add block 0-3 which acks 0-2. @@ -111,7 +111,7 @@ func genTestCase1(s *AckingTest, a *Acking) []types.ValidatorID { h: struct{}{}, }, } - a.ProcessBlock(b) + a.processBlock(b) s.NotNil(a.lattice[vids[0]].blocks[3]) // Add block 3-1 which acks 3-0. @@ -125,14 +125,14 @@ func genTestCase1(s *AckingTest, a *Acking) []types.ValidatorID { h: struct{}{}, }, } - a.ProcessBlock(b) + a.processBlock(b) s.NotNil(a.lattice[vids[3]].blocks[0]) return vids } func (s *AckingTest) TestAddValidator() { - a := NewAcking() + a := newAcking() s.Equal(len(a.lattice), 0) genTestCase1(s, a) s.Equal(len(a.lattice), 4) @@ -143,7 +143,7 @@ func (s *AckingTest) TestSanityCheck() { var h common.Hash var vids []types.ValidatorID var err error - a := NewAcking() + a := newAcking() vids = genTestCase1(s, a) // Non-genesis block with no ack, should get error. @@ -245,7 +245,7 @@ func (s *AckingTest) TestSanityCheck() { func (s *AckingTest) TestAreAllAcksInLattice() { var b *types.Block var vids []types.ValidatorID - a := NewAcking() + a := newAcking() vids = genTestCase1(s, a) // Empty ack should get true, although won't pass sanity check. @@ -275,7 +275,7 @@ func (s *AckingTest) TestAreAllAcksInLattice() { func (s *AckingTest) TestStrongAck() { var b *types.Block var vids []types.ValidatorID - a := NewAcking() + a := newAcking() vids = genTestCase1(s, a) // Check block 0-0 to 0-3 before adding 1-1 and 2-1. @@ -295,7 +295,7 @@ func (s *AckingTest) TestStrongAck() { a.lattice[vids[1]].blocks[0].Hash: struct{}{}, }, } - a.ProcessBlock(b) + a.processBlock(b) s.NotNil(a.lattice[vids[1]].blocks[1]) for i := uint64(0); i < 4; i++ { s.Equal(types.BlockStatusInit, a.lattice[vids[0]].blocks[i].Status) @@ -313,7 +313,7 @@ func (s *AckingTest) TestStrongAck() { a.lattice[vids[2]].blocks[0].Hash: struct{}{}, }, } - a.ProcessBlock(b) + 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) @@ -322,7 +322,7 @@ func (s *AckingTest) TestStrongAck() { func (s *AckingTest) TestExtractBlocks() { var b *types.Block - a := NewAcking() + a := newAcking() vids := genTestCase1(s, a) // Add block 1-1 which acks 1-0, 0-2, 3-0. @@ -337,7 +337,7 @@ func (s *AckingTest) TestExtractBlocks() { a.lattice[vids[3]].blocks[0].Hash: struct{}{}, }, } - a.ProcessBlock(b) + a.processBlock(b) // Add block 2-1 which acks 0-2, 2-0, 3-0. b = &types.Block{ @@ -351,7 +351,7 @@ func (s *AckingTest) TestExtractBlocks() { a.lattice[vids[3]].blocks[0].Hash: struct{}{}, }, } - a.ProcessBlock(b) + a.processBlock(b) hashs := []common.Hash{ a.lattice[vids[0]].blocks[0].Hash, @@ -359,7 +359,7 @@ func (s *AckingTest) TestExtractBlocks() { a.lattice[vids[3]].blocks[0].Hash, } hashExtracted := map[common.Hash]*types.Block{} - for _, b := range a.ExtractBlocks() { + for _, b := range a.extractBlocks() { hashExtracted[b.Hash] = b s.Equal(types.BlockStatusOrdering, b.Status) } @@ -370,7 +370,7 @@ func (s *AckingTest) TestExtractBlocks() { } func (s *AckingTest) TestRandomIntensiveAcking() { - a := NewAcking() + a := newAcking() vids := []types.ValidatorID{} heights := map[types.ValidatorID]uint64{} extractedBlocks := []*types.Block{} @@ -378,7 +378,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) + a.addValidator(vid) vids = append(vids, vid) h := common.NewRandomHash() b := &types.Block{ @@ -388,7 +388,7 @@ func (s *AckingTest) TestRandomIntensiveAcking() { Height: 0, ProposerID: vid, } - a.ProcessBlock(b) + a.processBlock(b) heights[vid] = 1 } @@ -410,11 +410,11 @@ func (s *AckingTest) TestRandomIntensiveAcking() { Height: height, Acks: acks, } - a.ProcessBlock(b) - extractedBlocks = append(extractedBlocks, a.ExtractBlocks()...) + a.processBlock(b) + extractedBlocks = append(extractedBlocks, a.extractBlocks()...) } - extractedBlocks = append(extractedBlocks, a.ExtractBlocks()...) + extractedBlocks = append(extractedBlocks, a.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. |