From 421d72b2d796195178104a0eb1dedf319ba8664c Mon Sep 17 00:00:00 2001 From: Mission Liao Date: Thu, 20 Sep 2018 09:09:37 +0800 Subject: Rename validator* to node* (#120) --- integration_test/network.go | 2 +- integration_test/node.go | 161 +++++++++++++++++++++++++++++++++ integration_test/non-byzantine_test.go | 8 +- integration_test/stats.go | 42 ++++----- integration_test/stats_test.go | 8 +- integration_test/utils.go | 40 ++++---- integration_test/validator.go | 161 --------------------------------- 7 files changed, 211 insertions(+), 211 deletions(-) create mode 100644 integration_test/node.go delete mode 100644 integration_test/validator.go (limited to 'integration_test') diff --git a/integration_test/network.go b/integration_test/network.go index b58cbcd..1c7265d 100644 --- a/integration_test/network.go +++ b/integration_test/network.go @@ -38,7 +38,7 @@ func (n *Network) BroadcastWitnessAck(witnessAck *types.WitnessAck) { // SendDKGPrivateShare sends PrivateShare to a DKG participant. func (n *Network) SendDKGPrivateShare( - recv types.ValidatorID, prvShare *types.DKGPrivateShare) { + recv types.NodeID, prvShare *types.DKGPrivateShare) { } // ReceiveChan returns a channel to receive messages from DEXON network. diff --git a/integration_test/node.go b/integration_test/node.go new file mode 100644 index 0000000..c0e226b --- /dev/null +++ b/integration_test/node.go @@ -0,0 +1,161 @@ +// Copyright 2018 The dexon-consensus-core Authors +// This file is part of the dexon-consensus-core library. +// +// The dexon-consensus-core library is free software: you can redistribute it +// and/or modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, either version 3 of the License, +// or (at your option) any later version. +// +// The dexon-consensus-core library is distributed in the hope that it will be +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser +// General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the dexon-consensus-core library. If not, see +// . + +package integration + +import ( + "fmt" + "sort" + "time" + + "github.com/dexon-foundation/dexon-consensus-core/common" + "github.com/dexon-foundation/dexon-consensus-core/core" + "github.com/dexon-foundation/dexon-consensus-core/core/blockdb" + "github.com/dexon-foundation/dexon-consensus-core/core/test" + "github.com/dexon-foundation/dexon-consensus-core/core/types" + "github.com/dexon-foundation/dexon-consensus-core/crypto" + "github.com/dexon-foundation/dexon-consensus-core/crypto/eth" +) + +type consensusEventType int + +const ( + evtProposeBlock consensusEventType = iota + evtReceiveBlock +) + +type consensusEventPayload struct { + Type consensusEventType + PiggyBack interface{} +} + +// NewProposeBlockEvent constructs an test.Event that would trigger +// block proposing. +func NewProposeBlockEvent(nID types.NodeID, when time.Time) *test.Event { + return test.NewEvent(nID, when, &consensusEventPayload{ + Type: evtProposeBlock, + }) +} + +// NewReceiveBlockEvent constructs an test.Event that would trigger +// block received. +func NewReceiveBlockEvent( + nID types.NodeID, when time.Time, block *types.Block) *test.Event { + + return test.NewEvent(nID, when, &consensusEventPayload{ + Type: evtReceiveBlock, + PiggyBack: block, + }) +} + +// Node is designed to work with test.Scheduler. +type Node struct { + ID types.NodeID + chainID uint32 + cons *core.Consensus + gov core.Governance + networkLatency test.LatencyModel + proposingLatency test.LatencyModel +} + +// NewNode constructs an instance of Node. +func NewNode( + app core.Application, + gov core.Governance, + db blockdb.BlockDatabase, + privateKey crypto.PrivateKey, + nID types.NodeID, + networkLatency test.LatencyModel, + proposingLatency test.LatencyModel) *Node { + + hashes := make(common.Hashes, 0) + for nID := range gov.GetNotarySet() { + hashes = append(hashes, nID.Hash) + } + sort.Sort(hashes) + chainID := uint32(0) + for i, hash := range hashes { + if hash == nID.Hash { + chainID = uint32(i) + break + } + } + + return &Node{ + ID: nID, + chainID: chainID, + gov: gov, + networkLatency: networkLatency, + proposingLatency: proposingLatency, + cons: core.NewConsensus( + app, gov, db, &Network{}, privateKey, eth.SigToPub), + } +} + +// Handle implements test.EventHandler interface. +func (n *Node) Handle(e *test.Event) (events []*test.Event) { + payload := e.Payload.(*consensusEventPayload) + switch payload.Type { + case evtProposeBlock: + events, e.ExecError = n.handleProposeBlock(e.Time, payload.PiggyBack) + case evtReceiveBlock: + events, e.ExecError = n.handleReceiveBlock(payload.PiggyBack) + default: + panic(fmt.Errorf("unknown consensus event type: %v", payload.Type)) + } + return +} + +func (n *Node) handleProposeBlock(when time.Time, piggyback interface{}) ( + events []*test.Event, err error) { + + b := &types.Block{ + ProposerID: n.ID, + Position: types.Position{ + ChainID: n.chainID, + }, + } + defer types.RecycleBlock(b) + if err = n.cons.PrepareBlock(b, when); err != nil { + return + } + if err = n.cons.ProcessBlock(b); err != nil { + return + } + // Create 'block received' event for each other nodes. + for nID := range n.gov.GetNotarySet() { + if nID == n.ID { + continue + } + events = append(events, NewReceiveBlockEvent( + nID, when.Add(n.networkLatency.Delay()), b.Clone())) + } + // Create next 'block proposing' event for this nodes. + events = append(events, NewProposeBlockEvent( + n.ID, when.Add(n.proposingLatency.Delay()))) + return +} + +func (n *Node) handleReceiveBlock(piggyback interface{}) ( + events []*test.Event, err error) { + + err = n.cons.ProcessBlock(piggyback.(*types.Block)) + if err != nil { + panic(err) + } + return +} diff --git a/integration_test/non-byzantine_test.go b/integration_test/non-byzantine_test.go index a5ecff6..34c6be1 100644 --- a/integration_test/non-byzantine_test.go +++ b/integration_test/non-byzantine_test.go @@ -41,17 +41,17 @@ func (s *NonByzantineTestSuite) TestNonByzantine() { Sigma: 30, Mean: 500, } - apps = make(map[types.ValidatorID]*test.App) - dbs = make(map[types.ValidatorID]blockdb.BlockDatabase) + apps = make(map[types.NodeID]*test.App) + dbs = make(map[types.NodeID]blockdb.BlockDatabase) req = s.Require() ) - apps, dbs, validators, err := PrepareValidators( + apps, dbs, nodes, err := PrepareNodes( 25, networkLatency, proposingLatency) req.Nil(err) now := time.Now().UTC() sch := test.NewScheduler(test.NewStopByConfirmedBlocks(50, apps, dbs)) - for vID, v := range validators { + for vID, v := range nodes { sch.RegisterEventHandler(vID, v) req.Nil(sch.Seed(NewProposeBlockEvent(vID, now))) } diff --git a/integration_test/stats.go b/integration_test/stats.go index ae8ded4..11b1db0 100644 --- a/integration_test/stats.go +++ b/integration_test/stats.go @@ -15,7 +15,7 @@ var ( ) // StatsSet represents accumulatee result of a group of related events -// (ex. All events from one validator). +// (ex. All events from one node). type StatsSet struct { ProposedBlockCount int ReceivedBlockCount int @@ -81,16 +81,16 @@ func (s *StatsSet) newBlockReceiveEvent( // done would divide the latencies we cached with related event count. This way // to calculate average latency is more accurate. -func (s *StatsSet) done(validatorCount int) { - s.ProposingLatency /= time.Duration(s.ProposedBlockCount - validatorCount) +func (s *StatsSet) done(nodeCount int) { + s.ProposingLatency /= time.Duration(s.ProposedBlockCount - nodeCount) s.ReceivingLatency /= time.Duration(s.ReceivedBlockCount) s.PrepareExecLatency /= time.Duration(s.ProposedBlockCount) s.ProcessExecLatency /= time.Duration(s.ReceivedBlockCount) } -// Stats is statistics of a slice of test.Event generated by validators. +// Stats is statistics of a slice of test.Event generated by nodes. type Stats struct { - ByValidator map[types.ValidatorID]*StatsSet + ByNode map[types.NodeID]*StatsSet All *StatsSet BPS float64 ExecutionTime time.Duration @@ -99,12 +99,12 @@ type Stats struct { // NewStats constructs an Stats instance by providing a slice of // test.Event. func NewStats( - history []*test.Event, apps map[types.ValidatorID]*test.App) ( + history []*test.Event, apps map[types.NodeID]*test.App) ( stats *Stats, err error) { stats = &Stats{ - ByValidator: make(map[types.ValidatorID]*StatsSet), - All: &StatsSet{}, + ByNode: make(map[types.NodeID]*StatsSet), + All: &StatsSet{}, } if err = stats.calculate(history, apps); err != nil { stats = nil @@ -114,11 +114,11 @@ func NewStats( } func (stats *Stats) calculate( - history []*test.Event, apps map[types.ValidatorID]*test.App) error { + history []*test.Event, apps map[types.NodeID]*test.App) error { defer func() { - stats.All.done(len(stats.ByValidator)) - for _, set := range stats.ByValidator { + stats.All.done(len(stats.ByNode)) + for _, set := range stats.ByNode { set.done(1) } }() @@ -132,13 +132,13 @@ func (stats *Stats) calculate( case evtProposeBlock: stats.All.newBlockProposeEvent( e, payload, history) - stats.getStatsSetByValidator(e.ValidatorID).newBlockProposeEvent( + stats.getStatsSetByNode(e.NodeID).newBlockProposeEvent( e, payload, history) case evtReceiveBlock: stats.All.newBlockReceiveEvent( - e, payload, history, apps[e.ValidatorID]) - stats.getStatsSetByValidator(e.ValidatorID).newBlockReceiveEvent( - e, payload, history, apps[e.ValidatorID]) + e, payload, history, apps[e.NodeID]) + stats.getStatsSetByNode(e.NodeID).newBlockReceiveEvent( + e, payload, history, apps[e.NodeID]) default: return ErrUnknownConsensusEventType } @@ -146,13 +146,13 @@ func (stats *Stats) calculate( return nil } -func (stats *Stats) getStatsSetByValidator( - vID types.ValidatorID) (s *StatsSet) { +func (stats *Stats) getStatsSetByNode( + vID types.NodeID) (s *StatsSet) { - s = stats.ByValidator[vID] + s = stats.ByNode[vID] if s == nil { s = &StatsSet{} - stats.ByValidator[vID] = s + stats.ByNode[vID] = s } return } @@ -160,10 +160,10 @@ func (stats *Stats) getStatsSetByValidator( func (stats *Stats) summary(history []*test.Event) { // Find average delivered block count among all blocks. totalConfirmedBlocks := 0 - for _, s := range stats.ByValidator { + for _, s := range stats.ByNode { totalConfirmedBlocks += s.DeliveredBlockCount } - averageConfirmedBlocks := totalConfirmedBlocks / len(stats.ByValidator) + averageConfirmedBlocks := totalConfirmedBlocks / len(stats.ByNode) // Find execution time. // Note: it's a simplified way to calculate the execution time: diff --git a/integration_test/stats_test.go b/integration_test/stats_test.go index 8816e8c..54c827d 100644 --- a/integration_test/stats_test.go +++ b/integration_test/stats_test.go @@ -21,13 +21,13 @@ func (s *EventStatsTestSuite) TestCalculate() { req = s.Require() ) - apps, dbs, validators, err := PrepareValidators( + apps, dbs, nodes, err := PrepareNodes( 7, networkLatency, proposingLatency) req.Nil(err) sch := test.NewScheduler(test.NewStopByConfirmedBlocks(50, apps, dbs)) now := time.Now().UTC() - for vID, v := range validators { + for vID, v := range nodes { sch.RegisterEventHandler(vID, v) req.Nil(sch.Seed(NewProposeBlockEvent(vID, now))) } @@ -43,8 +43,8 @@ func (s *EventStatsTestSuite) TestCalculate() { req.True(stats.All.DeliveredBlockCount >= 350) req.Equal(stats.All.ProposingLatency, 300*time.Millisecond) req.Equal(stats.All.ReceivingLatency, 100*time.Millisecond) - // Check statistics for each validator. - for _, vStats := range stats.ByValidator { + // Check statistics for each node. + for _, vStats := range stats.ByNode { req.True(vStats.ProposedBlockCount > 50) req.True(vStats.ReceivedBlockCount > 50) req.True(vStats.StronglyAckedBlockCount > 50) diff --git a/integration_test/utils.go b/integration_test/utils.go index c526bc2..f95d771 100644 --- a/integration_test/utils.go +++ b/integration_test/utils.go @@ -9,13 +9,13 @@ import ( "github.com/dexon-foundation/dexon-consensus-core/crypto" ) -// PrepareValidators setups validators for testing. -func PrepareValidators( - validatorCount int, +// PrepareNodes setups nodes for testing. +func PrepareNodes( + nodeCount int, networkLatency, proposingLatency test.LatencyModel) ( - apps map[types.ValidatorID]*test.App, - dbs map[types.ValidatorID]blockdb.BlockDatabase, - validators map[types.ValidatorID]*Validator, + apps map[types.NodeID]*test.App, + dbs map[types.NodeID]blockdb.BlockDatabase, + nodes map[types.NodeID]*Node, err error) { var ( @@ -23,32 +23,32 @@ func PrepareValidators( key crypto.PrivateKey ) - apps = make(map[types.ValidatorID]*test.App) - dbs = make(map[types.ValidatorID]blockdb.BlockDatabase) - validators = make(map[types.ValidatorID]*Validator) + apps = make(map[types.NodeID]*test.App) + dbs = make(map[types.NodeID]blockdb.BlockDatabase) + nodes = make(map[types.NodeID]*Node) - gov, err := test.NewGovernance(validatorCount, 700*time.Millisecond) + gov, err := test.NewGovernance(nodeCount, 700*time.Millisecond) if err != nil { return } - for vID := range gov.GetNotarySet() { - apps[vID] = test.NewApp() + for nID := range gov.GetNotarySet() { + apps[nID] = test.NewApp() if db, err = blockdb.NewMemBackedBlockDB(); err != nil { return } - dbs[vID] = db + dbs[nID] = db } - for vID := range gov.GetNotarySet() { - if key, err = gov.GetPrivateKey(vID); err != nil { + for nID := range gov.GetNotarySet() { + if key, err = gov.GetPrivateKey(nID); err != nil { return } - validators[vID] = NewValidator( - apps[vID], + nodes[nID] = NewNode( + apps[nID], gov, - dbs[vID], + dbs[nID], key, - vID, + nID, networkLatency, proposingLatency) } @@ -56,7 +56,7 @@ func PrepareValidators( } // VerifyApps is a helper to check delivery between test.Apps -func VerifyApps(apps map[types.ValidatorID]*test.App) (err error) { +func VerifyApps(apps map[types.NodeID]*test.App) (err error) { for vFrom, fromApp := range apps { if err = fromApp.Verify(); err != nil { return diff --git a/integration_test/validator.go b/integration_test/validator.go deleted file mode 100644 index 112f986..0000000 --- a/integration_test/validator.go +++ /dev/null @@ -1,161 +0,0 @@ -// Copyright 2018 The dexon-consensus-core Authors -// This file is part of the dexon-consensus-core library. -// -// The dexon-consensus-core library is free software: you can redistribute it -// and/or modify it under the terms of the GNU Lesser General Public License as -// published by the Free Software Foundation, either version 3 of the License, -// or (at your option) any later version. -// -// The dexon-consensus-core library is distributed in the hope that it will be -// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser -// General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the dexon-consensus-core library. If not, see -// . - -package integration - -import ( - "fmt" - "sort" - "time" - - "github.com/dexon-foundation/dexon-consensus-core/common" - "github.com/dexon-foundation/dexon-consensus-core/core" - "github.com/dexon-foundation/dexon-consensus-core/core/blockdb" - "github.com/dexon-foundation/dexon-consensus-core/core/test" - "github.com/dexon-foundation/dexon-consensus-core/core/types" - "github.com/dexon-foundation/dexon-consensus-core/crypto" - "github.com/dexon-foundation/dexon-consensus-core/crypto/eth" -) - -type consensusEventType int - -const ( - evtProposeBlock consensusEventType = iota - evtReceiveBlock -) - -type consensusEventPayload struct { - Type consensusEventType - PiggyBack interface{} -} - -// NewProposeBlockEvent constructs an test.Event that would trigger -// block proposing. -func NewProposeBlockEvent(vID types.ValidatorID, when time.Time) *test.Event { - return test.NewEvent(vID, when, &consensusEventPayload{ - Type: evtProposeBlock, - }) -} - -// NewReceiveBlockEvent constructs an test.Event that would trigger -// block received. -func NewReceiveBlockEvent( - vID types.ValidatorID, when time.Time, block *types.Block) *test.Event { - - return test.NewEvent(vID, when, &consensusEventPayload{ - Type: evtReceiveBlock, - PiggyBack: block, - }) -} - -// Validator is designed to work with test.Scheduler. -type Validator struct { - ID types.ValidatorID - chainID uint32 - cons *core.Consensus - gov core.Governance - networkLatency test.LatencyModel - proposingLatency test.LatencyModel -} - -// NewValidator constructs an instance of Validator. -func NewValidator( - app core.Application, - gov core.Governance, - db blockdb.BlockDatabase, - privateKey crypto.PrivateKey, - vID types.ValidatorID, - networkLatency test.LatencyModel, - proposingLatency test.LatencyModel) *Validator { - - hashes := make(common.Hashes, 0) - for vID := range gov.GetNotarySet() { - hashes = append(hashes, vID.Hash) - } - sort.Sort(hashes) - chainID := uint32(0) - for i, hash := range hashes { - if hash == vID.Hash { - chainID = uint32(i) - break - } - } - - return &Validator{ - ID: vID, - chainID: chainID, - gov: gov, - networkLatency: networkLatency, - proposingLatency: proposingLatency, - cons: core.NewConsensus( - app, gov, db, &Network{}, privateKey, eth.SigToPub), - } -} - -// Handle implements test.EventHandler interface. -func (v *Validator) Handle(e *test.Event) (events []*test.Event) { - payload := e.Payload.(*consensusEventPayload) - switch payload.Type { - case evtProposeBlock: - events, e.ExecError = v.handleProposeBlock(e.Time, payload.PiggyBack) - case evtReceiveBlock: - events, e.ExecError = v.handleReceiveBlock(payload.PiggyBack) - default: - panic(fmt.Errorf("unknown consensus event type: %v", payload.Type)) - } - return -} - -func (v *Validator) handleProposeBlock(when time.Time, piggyback interface{}) ( - events []*test.Event, err error) { - - b := &types.Block{ - ProposerID: v.ID, - Position: types.Position{ - ChainID: v.chainID, - }, - } - defer types.RecycleBlock(b) - if err = v.cons.PrepareBlock(b, when); err != nil { - return - } - if err = v.cons.ProcessBlock(b); err != nil { - return - } - // Create 'block received' event for each other validators. - for vID := range v.gov.GetNotarySet() { - if vID == v.ID { - continue - } - events = append(events, NewReceiveBlockEvent( - vID, when.Add(v.networkLatency.Delay()), b.Clone())) - } - // Create next 'block proposing' event for this validators. - events = append(events, NewProposeBlockEvent( - v.ID, when.Add(v.proposingLatency.Delay()))) - return -} - -func (v *Validator) handleReceiveBlock(piggyback interface{}) ( - events []*test.Event, err error) { - - err = v.cons.ProcessBlock(piggyback.(*types.Block)) - if err != nil { - panic(err) - } - return -} -- cgit v1.2.3