diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-11-14 15:25:54 +0800 |
---|---|---|
committer | Jimmy Hu <jimmy.hu@dexon.org> | 2018-11-14 15:25:54 +0800 |
commit | 3a04c0d3405960e3a6eabb6adc700fa2fd5aac21 (patch) | |
tree | e13d65047af0c5646867a7424683b96bfc2e843f /core | |
parent | e33261e6008dfba5b3401d0adc22a7da8649c9dc (diff) | |
download | dexon-consensus-3a04c0d3405960e3a6eabb6adc700fa2fd5aac21.tar dexon-consensus-3a04c0d3405960e3a6eabb6adc700fa2fd5aac21.tar.gz dexon-consensus-3a04c0d3405960e3a6eabb6adc700fa2fd5aac21.tar.bz2 dexon-consensus-3a04c0d3405960e3a6eabb6adc700fa2fd5aac21.tar.lz dexon-consensus-3a04c0d3405960e3a6eabb6adc700fa2fd5aac21.tar.xz dexon-consensus-3a04c0d3405960e3a6eabb6adc700fa2fd5aac21.tar.zst dexon-consensus-3a04c0d3405960e3a6eabb6adc700fa2fd5aac21.zip |
utils: add utils package (#327)
Diffstat (limited to 'core')
-rw-r--r-- | core/configuration-chain.go | 5 | ||||
-rw-r--r-- | core/configuration-chain_test.go | 3 | ||||
-rw-r--r-- | core/consensus.go | 7 | ||||
-rw-r--r-- | core/lattice-data.go | 6 | ||||
-rw-r--r-- | core/lattice-data_test.go | 3 | ||||
-rw-r--r-- | core/utils/nodeset-cache.go (renamed from core/nodeset-cache.go) | 4 | ||||
-rw-r--r-- | core/utils/nodeset-cache_test.go (renamed from core/nodeset-cache_test.go) | 15 |
7 files changed, 18 insertions, 25 deletions
diff --git a/core/configuration-chain.go b/core/configuration-chain.go index bda2fdf..03a4602 100644 --- a/core/configuration-chain.go +++ b/core/configuration-chain.go @@ -26,6 +26,7 @@ import ( "github.com/dexon-foundation/dexon-consensus/core/crypto" "github.com/dexon-foundation/dexon-consensus/core/types" typesDKG "github.com/dexon-foundation/dexon-consensus/core/types/dkg" + "github.com/dexon-foundation/dexon-consensus/core/utils" ) // Errors for configuration chain.. @@ -51,7 +52,7 @@ type configurationChain struct { tsig map[common.Hash]*tsigProtocol tsigTouched map[common.Hash]struct{} tsigReady *sync.Cond - cache *NodeSetCache + cache *utils.NodeSetCache dkgSet map[types.NodeID]struct{} mpkReady bool pendingPrvShare map[types.NodeID]*typesDKG.PrivateShare @@ -64,7 +65,7 @@ func newConfigurationChain( ID types.NodeID, recv dkgReceiver, gov Governance, - cache *NodeSetCache, + cache *utils.NodeSetCache, logger common.Logger) *configurationChain { return &configurationChain{ ID: ID, diff --git a/core/configuration-chain_test.go b/core/configuration-chain_test.go index b3d8ff4..129d07c 100644 --- a/core/configuration-chain_test.go +++ b/core/configuration-chain_test.go @@ -32,6 +32,7 @@ import ( "github.com/dexon-foundation/dexon-consensus/core/test" "github.com/dexon-foundation/dexon-consensus/core/types" typesDKG "github.com/dexon-foundation/dexon-consensus/core/types/dkg" + "github.com/dexon-foundation/dexon-consensus/core/utils" ) type ConfigurationChainTestSuite struct { @@ -174,7 +175,7 @@ func (s *ConfigurationChainTestSuite) runDKG( for _, nID := range s.nIDs { gov, err := test.NewGovernance(pks, 50*time.Millisecond, ConfigRoundShift) s.Require().NoError(err) - cache := NewNodeSetCache(gov) + cache := utils.NewNodeSetCache(gov) cfgChains[nID] = newConfigurationChain( nID, recv, gov, cache, &common.NullLogger{}) recv.nodes[nID] = cfgChains[nID] diff --git a/core/consensus.go b/core/consensus.go index 69131e6..99cb7c1 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -29,6 +29,7 @@ import ( "github.com/dexon-foundation/dexon-consensus/core/crypto" "github.com/dexon-foundation/dexon-consensus/core/types" typesDKG "github.com/dexon-foundation/dexon-consensus/core/types/dkg" + "github.com/dexon-foundation/dexon-consensus/core/utils" ) // Errors for consensus core. @@ -187,7 +188,7 @@ type consensusDKGReceiver struct { ID types.NodeID gov Governance authModule *Authenticator - nodeSetCache *NodeSetCache + nodeSetCache *utils.NodeSetCache cfgModule *configurationChain network Network logger common.Logger @@ -295,7 +296,7 @@ type Consensus struct { // Misc. dMoment time.Time - nodeSetCache *NodeSetCache + nodeSetCache *utils.NodeSetCache round uint64 roundToNotify uint64 lock sync.RWMutex @@ -319,7 +320,7 @@ func NewConsensus( var round uint64 logger.Debug("Calling Governance.Configuration", "round", round) config := gov.Configuration(round) - nodeSetCache := NewNodeSetCache(gov) + nodeSetCache := utils.NewNodeSetCache(gov) logger.Debug("Calling Governance.CRS", "round", round) // Setup auth module. authModule := NewAuthenticator(prv) diff --git a/core/lattice-data.go b/core/lattice-data.go index ce6d32b..6fe810a 100644 --- a/core/lattice-data.go +++ b/core/lattice-data.go @@ -26,12 +26,12 @@ import ( "github.com/dexon-foundation/dexon-consensus/common" "github.com/dexon-foundation/dexon-consensus/core/blockdb" "github.com/dexon-foundation/dexon-consensus/core/types" + "github.com/dexon-foundation/dexon-consensus/core/utils" ) // Errors for sanity check error. var ( ErrDuplicatedAckOnOneChain = fmt.Errorf("duplicated ack on one chain") - ErrInvalidChainID = fmt.Errorf("invalid chain id") ErrInvalidProposerID = fmt.Errorf("invalid proposer id") ErrInvalidWitness = fmt.Errorf("invalid witness data") ErrInvalidBlock = fmt.Errorf("invalid block") @@ -181,7 +181,7 @@ func (data *latticeData) sanityCheck(b *types.Block) error { } // Check if the chain id is valid. if b.Position.ChainID >= config.numChains { - return ErrInvalidChainID + return utils.ErrInvalidChainID } // Make sure parent block is arrived. chain := data.chains[b.Position.ChainID] @@ -382,7 +382,7 @@ func (data *latticeData) prepareBlock(b *types.Block) error { } // If chainID is illegal in this round, reject it. if b.Position.ChainID >= config.numChains { - return ErrInvalidChainID + return utils.ErrInvalidChainID } // Reset fields to make sure we got these information from parent block. b.Position.Height = 0 diff --git a/core/lattice-data_test.go b/core/lattice-data_test.go index e4a894f..24f45e6 100644 --- a/core/lattice-data_test.go +++ b/core/lattice-data_test.go @@ -27,6 +27,7 @@ import ( "github.com/dexon-foundation/dexon-consensus/core/blockdb" "github.com/dexon-foundation/dexon-consensus/core/test" "github.com/dexon-foundation/dexon-consensus/core/types" + "github.com/dexon-foundation/dexon-consensus/core/utils" "github.com/stretchr/testify/suite" ) @@ -217,7 +218,7 @@ func (s *LatticeDataTestSuite) TestSanityCheck() { Timestamp: time.Now().UTC(), }) // Invalid chain ID. - check(ErrInvalidChainID, &types.Block{ + check(utils.ErrInvalidChainID, &types.Block{ ParentHash: blocks[1][0].Hash, Position: types.Position{ ChainID: 100, diff --git a/core/nodeset-cache.go b/core/utils/nodeset-cache.go index 26e3d55..a8f8fe5 100644 --- a/core/nodeset-cache.go +++ b/core/utils/nodeset-cache.go @@ -15,7 +15,7 @@ // along with the dexon-consensus library. If not, see // <http://www.gnu.org/licenses/>. -package core +package utils import ( "errors" @@ -29,6 +29,8 @@ import ( var ( // ErrRoundNotReady means we got nil config. ErrRoundNotReady = errors.New("round is not ready") + // ErrInvalidChainID means the chain ID is unexpected. + ErrInvalidChainID = errors.New("invalid chain id") ) type sets struct { diff --git a/core/nodeset-cache_test.go b/core/utils/nodeset-cache_test.go index 828ef49..27c8c83 100644 --- a/core/nodeset-cache_test.go +++ b/core/utils/nodeset-cache_test.go @@ -15,16 +15,14 @@ // along with the dexon-consensus library. If not, see // <http://www.gnu.org/licenses/>. -package core +package utils import ( "testing" - "time" "github.com/dexon-foundation/dexon-consensus/common" "github.com/dexon-foundation/dexon-consensus/core/crypto" "github.com/dexon-foundation/dexon-consensus/core/crypto/ecdsa" - "github.com/dexon-foundation/dexon-consensus/core/test" "github.com/dexon-foundation/dexon-consensus/core/types" "github.com/stretchr/testify/suite" ) @@ -58,17 +56,6 @@ type NodeSetCacheTestSuite struct { suite.Suite } -func (s *NodeSetCacheTestSuite) TestGovernanceIntf() { - // NodeSetCacheInterface should let Governance implement it. - var gov Governance - _, pubKeys, err := test.NewKeys(7) - s.Require().NoError(err) - gov, err = test.NewGovernance(pubKeys, 250*time.Millisecond, ConfigRoundShift) - s.Require().NoError(err) - _, ok := gov.(NodeSetCacheInterface) - s.True(ok) -} - func (s *NodeSetCacheTestSuite) TestBasicUsage() { var ( nsIntf = &nsIntf{ |