aboutsummaryrefslogtreecommitdiffstats
path: root/core/test
diff options
context:
space:
mode:
Diffstat (limited to 'core/test')
-rw-r--r--core/test/block-revealer_test.go2
-rw-r--r--core/test/blocks-generator.go29
-rw-r--r--core/test/blocks-generator_test.go16
-rw-r--r--core/test/state-change-request.go8
-rw-r--r--core/test/state.go12
-rw-r--r--core/test/state_test.go32
-rw-r--r--core/test/utils.go33
7 files changed, 59 insertions, 73 deletions
diff --git a/core/test/block-revealer_test.go b/core/test/block-revealer_test.go
index ba4cf71..0e56eea 100644
--- a/core/test/block-revealer_test.go
+++ b/core/test/block-revealer_test.go
@@ -48,7 +48,7 @@ func (s *BlockRevealerTestSuite) SetupSuite() {
NumChains: 19,
MinBlockTimeInterval: 250 * time.Millisecond,
}
- gen := NewBlocksGenerator(config, nil, stableRandomHash)
+ gen := NewBlocksGenerator(config, nil)
s.Require().NoError(gen.Generate(
0,
genesisTime,
diff --git a/core/test/blocks-generator.go b/core/test/blocks-generator.go
index aab7f96..5f1dbea 100644
--- a/core/test/blocks-generator.go
+++ b/core/test/blocks-generator.go
@@ -24,10 +24,10 @@ import (
"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/db"
"github.com/dexon-foundation/dexon-consensus/core/types"
+ "github.com/dexon-foundation/dexon-consensus/core/utils"
)
// ErrParentNotAcked would be raised when some block doesn't
@@ -39,13 +39,11 @@ var ErrParentNotAcked = errors.New("parent is not acked")
type nodeStatus struct {
blocks []*types.Block
genesisTime time.Time
- prvKey crypto.PrivateKey
+ signer *utils.Signer
tip *types.Block
nextAckingIndex map[types.NodeID]uint64
}
-type hashBlockFn func(*types.Block) (common.Hash, error)
-
// getAckedBlockHash would randomly pick one block between
// last acked one to current head.
func (ns *nodeStatus) getAckedBlockHash(
@@ -85,7 +83,6 @@ type nodeSetStatus struct {
nIDs []types.NodeID
randGen *rand.Rand
timePicker func(time.Time) time.Time
- hashBlock hashBlockFn
}
func newNodeSetStatus(
@@ -93,8 +90,7 @@ func newNodeSetStatus(
tips map[uint32]*types.Block,
round uint64,
genesisTime, endTime time.Time,
- timePicker func(time.Time) time.Time,
- hashBlock hashBlockFn) *nodeSetStatus {
+ timePicker func(time.Time) time.Time) *nodeSetStatus {
var (
status = make(map[types.NodeID]*nodeStatus)
proposerChain = make(map[types.NodeID]uint32)
@@ -110,7 +106,7 @@ func newNodeSetStatus(
status[nID] = &nodeStatus{
blocks: []*types.Block{},
genesisTime: genesisTime,
- prvKey: prvKey,
+ signer: utils.NewSigner(prvKey),
tip: tips[i],
nextAckingIndex: make(map[types.NodeID]uint64),
}
@@ -124,7 +120,6 @@ func newNodeSetStatus(
nIDs: nIDs,
randGen: rand.New(rand.NewSource(time.Now().UnixNano())),
timePicker: timePicker,
- hashBlock: hashBlock,
}
}
@@ -200,15 +195,8 @@ func (ns *nodeSetStatus) proposeBlock(
},
Timestamp: status.getNextBlockTime(ns.timePicker),
}
- newBlock.ProposerID = proposerID
newBlock.Acks = common.NewSortedHashes(acks)
- var err error
- newBlock.Hash, err = ns.hashBlock(newBlock)
- if err != nil {
- return nil, err
- }
- newBlock.Signature, err = status.prvKey.Sign(newBlock.Hash)
- if err != nil {
+ if err := status.signer.SignBlock(newBlock); err != nil {
return nil, err
}
status.blocks = append(status.blocks, newBlock)
@@ -276,7 +264,6 @@ type BlocksGenerator struct {
nodePicker func([]types.NodeID) types.NodeID
timePicker func(time.Time) time.Time
ackingCountGenerator func() int
- hashBlock hashBlockFn
}
// NewBlocksGenerator constructs BlockGenerator.
@@ -290,8 +277,7 @@ type BlocksGenerator struct {
// the nodeCount you provided with a normal distribution.
func NewBlocksGenerator(
config *BlocksGeneratorConfig,
- ackingCountGenerator func() int,
- hashBlock hashBlockFn) *BlocksGenerator {
+ ackingCountGenerator func() int) *BlocksGenerator {
if config.MinBlockTimeInterval == time.Duration(0) {
panic(errors.New("min block interval cannot be 0"))
}
@@ -307,7 +293,6 @@ func NewBlocksGenerator(
nodePicker: generateNodePicker(),
timePicker: timePicker,
ackingCountGenerator: ackingCountGenerator,
- hashBlock: hashBlock,
}
}
@@ -325,7 +310,7 @@ func (gen *BlocksGenerator) Generate(
}
}
status := newNodeSetStatus(gen.config.NumChains, tips, roundID,
- roundBegin, roundEnd, gen.timePicker, gen.hashBlock)
+ roundBegin, roundEnd, gen.timePicker)
// We would record the smallest height of block that could be acked
// from each node's point-of-view.
toAck := make(map[types.NodeID]map[types.NodeID]uint64)
diff --git a/core/test/blocks-generator_test.go b/core/test/blocks-generator_test.go
index d71cd62..bcbd749 100644
--- a/core/test/blocks-generator_test.go
+++ b/core/test/blocks-generator_test.go
@@ -39,7 +39,7 @@ func (s *BlocksGeneratorTestSuite) TestGenerate() {
NumChains: 19,
MinBlockTimeInterval: 200 * time.Millisecond,
}
- gen = NewBlocksGenerator(config, nil, stableRandomHash)
+ gen = NewBlocksGenerator(config, nil)
req = s.Require()
beginTime = time.Now().UTC()
endTime = beginTime.Add(time.Minute)
@@ -129,8 +129,7 @@ func (s *BlocksGeneratorTestSuite) TestGenerateWithMaxAckCount() {
// Generate with 0 acks.
dbInst, err := db.NewMemBackedDB()
req.NoError(err)
- gen := NewBlocksGenerator(
- config, MaxAckingCountGenerator(0), stableRandomHash)
+ gen := NewBlocksGenerator(config, MaxAckingCountGenerator(0))
req.NoError(gen.Generate(
0,
genesisTime,
@@ -153,8 +152,7 @@ func (s *BlocksGeneratorTestSuite) TestGenerateWithMaxAckCount() {
// Generate with acks as many as possible.
dbInst, err = db.NewMemBackedDB()
req.NoError(err)
- gen = NewBlocksGenerator(
- config, MaxAckingCountGenerator(config.NumChains), stableRandomHash)
+ gen = NewBlocksGenerator(config, MaxAckingCountGenerator(config.NumChains))
req.NoError(gen.Generate(
0,
genesisTime,
@@ -190,7 +188,7 @@ func (s *BlocksGeneratorTestSuite) TestFindTips() {
genesisTime = time.Now().UTC()
endTime = genesisTime.Add(100 * time.Second)
)
- gen := NewBlocksGenerator(config, nil, stableRandomHash)
+ gen := NewBlocksGenerator(config, nil)
dbInst, err := db.NewMemBackedDB()
req.NoError(err)
req.NoError(gen.Generate(
@@ -220,7 +218,7 @@ func (s *BlocksGeneratorTestSuite) TestConcateBlocksFromRounds() {
gen := NewBlocksGenerator(&BlocksGeneratorConfig{
NumChains: 4,
MinBlockTimeInterval: 250 * time.Millisecond,
- }, MaxAckingCountGenerator(4), stableRandomHash)
+ }, MaxAckingCountGenerator(4))
req.NoError(gen.Generate(
0,
genesisTime,
@@ -233,7 +231,7 @@ func (s *BlocksGeneratorTestSuite) TestConcateBlocksFromRounds() {
gen = NewBlocksGenerator(&BlocksGeneratorConfig{
NumChains: 10,
MinBlockTimeInterval: 250 * time.Millisecond,
- }, MaxAckingCountGenerator(10), stableRandomHash)
+ }, MaxAckingCountGenerator(10))
req.NoError(gen.Generate(
1,
genesisTime.Add(10*time.Second),
@@ -246,7 +244,7 @@ func (s *BlocksGeneratorTestSuite) TestConcateBlocksFromRounds() {
gen = NewBlocksGenerator(&BlocksGeneratorConfig{
NumChains: 7,
MinBlockTimeInterval: 250 * time.Millisecond,
- }, MaxAckingCountGenerator(7), stableRandomHash)
+ }, MaxAckingCountGenerator(7))
req.NoError(gen.Generate(
2,
genesisTime.Add(20*time.Second),
diff --git a/core/test/state-change-request.go b/core/test/state-change-request.go
index 83119b5..687eef7 100644
--- a/core/test/state-change-request.go
+++ b/core/test/state-change-request.go
@@ -117,14 +117,14 @@ func (req *StateChangeRequest) Clone() (copied *StateChangeRequest) {
CRS: crsReq.CRS,
}
case StateAddDKGMPKReady:
- copied.Payload = cloneDKGMPKReady(req.Payload.(*typesDKG.MPKReady))
+ copied.Payload = CloneDKGMPKReady(req.Payload.(*typesDKG.MPKReady))
case StateAddDKGFinal:
- copied.Payload = cloneDKGFinalize(req.Payload.(*typesDKG.Finalize))
+ copied.Payload = CloneDKGFinalize(req.Payload.(*typesDKG.Finalize))
case StateAddDKGMasterPublicKey:
- copied.Payload = cloneDKGMasterPublicKey(
+ copied.Payload = CloneDKGMasterPublicKey(
req.Payload.(*typesDKG.MasterPublicKey))
case StateAddDKGComplaint:
- copied.Payload = cloneDKGComplaint(req.Payload.(*typesDKG.Complaint))
+ copied.Payload = CloneDKGComplaint(req.Payload.(*typesDKG.Complaint))
default:
copied.Payload = req.Payload
}
diff --git a/core/test/state.go b/core/test/state.go
index 3c24fb6..b834b11 100644
--- a/core/test/state.go
+++ b/core/test/state.go
@@ -476,7 +476,7 @@ func (s *State) Clone() (copied *State) {
for nID, comps := range complaintsForRound {
tmpComps := []*typesDKG.Complaint{}
for _, comp := range comps {
- tmpComps = append(tmpComps, cloneDKGComplaint(comp))
+ tmpComps = append(tmpComps, CloneDKGComplaint(comp))
}
copied.dkgComplaints[round][nID] = tmpComps
}
@@ -486,19 +486,19 @@ func (s *State) Clone() (copied *State) {
make(map[types.NodeID]*typesDKG.MasterPublicKey)
for nID, mKey := range mKeysForRound {
copied.dkgMasterPublicKeys[round][nID] =
- cloneDKGMasterPublicKey(mKey)
+ CloneDKGMasterPublicKey(mKey)
}
}
for round, readysForRound := range s.dkgReadys {
copied.dkgReadys[round] = make(map[types.NodeID]*typesDKG.MPKReady)
for nID, ready := range readysForRound {
- copied.dkgReadys[round][nID] = cloneDKGMPKReady(ready)
+ copied.dkgReadys[round][nID] = CloneDKGMPKReady(ready)
}
}
for round, finalsForRound := range s.dkgFinals {
copied.dkgFinals[round] = make(map[types.NodeID]*typesDKG.Finalize)
for nID, final := range finalsForRound {
- copied.dkgFinals[round][nID] = cloneDKGFinalize(final)
+ copied.dkgFinals[round][nID] = CloneDKGFinalize(final)
}
}
for _, crs := range s.crs {
@@ -825,7 +825,7 @@ func (s *State) DKGComplaints(round uint64) []*typesDKG.Complaint {
tmpComps := make([]*typesDKG.Complaint, 0, len(comps))
for _, compProp := range comps {
for _, comp := range compProp {
- tmpComps = append(tmpComps, cloneDKGComplaint(comp))
+ tmpComps = append(tmpComps, CloneDKGComplaint(comp))
}
}
return tmpComps
@@ -843,7 +843,7 @@ func (s *State) DKGMasterPublicKeys(round uint64) []*typesDKG.MasterPublicKey {
}
mpks := make([]*typesDKG.MasterPublicKey, 0, len(masterPublicKeys))
for _, mpk := range masterPublicKeys {
- mpks = append(mpks, cloneDKGMasterPublicKey(mpk))
+ mpks = append(mpks, CloneDKGMasterPublicKey(mpk))
}
return mpks
}
diff --git a/core/test/state_test.go b/core/test/state_test.go
index c05d41b..ad3d1c4 100644
--- a/core/test/state_test.go
+++ b/core/test/state_test.go
@@ -28,6 +28,7 @@ import (
"github.com/dexon-foundation/dexon-consensus/core/crypto/ecdsa"
"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"
"github.com/stretchr/testify/suite"
)
@@ -55,12 +56,9 @@ func (s *StateTestSuite) newDKGMasterPublicKey(
func (s *StateTestSuite) newDKGComplaint(round uint64) *typesDKG.Complaint {
prvKey, err := ecdsa.NewPrivateKey()
s.Require().NoError(err)
- pubKey := prvKey.PublicKey()
- nodeID := types.NewNodeID(pubKey)
- // TODO(mission): sign it, and it doesn't make sense to complaint self.
- return &typesDKG.Complaint{
- ProposerID: nodeID,
- Round: round,
+ nodeID := types.NewNodeID(prvKey.PublicKey())
+ comp := &typesDKG.Complaint{
+ Round: round,
PrivateShare: typesDKG.PrivateShare{
ProposerID: nodeID,
ReceiverID: nodeID,
@@ -68,29 +66,23 @@ func (s *StateTestSuite) newDKGComplaint(round uint64) *typesDKG.Complaint {
PrivateShare: *dkg.NewPrivateKey(),
},
}
+ s.Require().NoError(utils.NewSigner(prvKey).SignDKGComplaint(comp))
+ return comp
}
func (s *StateTestSuite) newDKGMPKReady(round uint64) *typesDKG.MPKReady {
prvKey, err := ecdsa.NewPrivateKey()
s.Require().NoError(err)
- pubKey := prvKey.PublicKey()
- nodeID := types.NewNodeID(pubKey)
- // TODO(mission): sign it.
- return &typesDKG.MPKReady{
- ProposerID: nodeID,
- Round: round,
- }
+ ready := &typesDKG.MPKReady{Round: round}
+ s.Require().NoError(utils.NewSigner(prvKey).SignDKGMPKReady(ready))
+ return ready
}
func (s *StateTestSuite) newDKGFinal(round uint64) *typesDKG.Finalize {
prvKey, err := ecdsa.NewPrivateKey()
s.Require().NoError(err)
- pubKey := prvKey.PublicKey()
- nodeID := types.NewNodeID(pubKey)
- // TODO(mission): sign it.
- return &typesDKG.Finalize{
- ProposerID: nodeID,
- Round: round,
- }
+ final := &typesDKG.Finalize{Round: round}
+ s.Require().NoError(utils.NewSigner(prvKey).SignDKGFinalize(final))
+ return final
}
func (s *StateTestSuite) compareNodes(node1, node2 []crypto.PublicKey) bool {
diff --git a/core/test/utils.go b/core/test/utils.go
index c6c08fc..84c90a9 100644
--- a/core/test/utils.go
+++ b/core/test/utils.go
@@ -33,13 +33,6 @@ import (
"github.com/dexon-foundation/dexon/rlp"
)
-func stableRandomHash(block *types.Block) (common.Hash, error) {
- if (block.Hash != common.Hash{}) {
- return block.Hash, nil
- }
- return common.NewRandomHash(), nil
-}
-
// GenerateRandomNodeIDs generates randomly a slices of types.NodeID.
func GenerateRandomNodeIDs(nodeCount int) (nIDs types.NodeIDs) {
nIDs = types.NodeIDs{}
@@ -121,7 +114,8 @@ func NewKeys(count int) (
return
}
-func cloneDKGComplaint(
+// CloneDKGComplaint clones a tpyesDKG.Complaint instance.
+func CloneDKGComplaint(
comp *typesDKG.Complaint) (copied *typesDKG.Complaint) {
b, err := rlp.EncodeToBytes(comp)
if err != nil {
@@ -134,7 +128,8 @@ func cloneDKGComplaint(
return
}
-func cloneDKGMasterPublicKey(mpk *typesDKG.MasterPublicKey) (
+// CloneDKGMasterPublicKey clones a typesDKG.MasterPublicKey instance.
+func CloneDKGMasterPublicKey(mpk *typesDKG.MasterPublicKey) (
copied *typesDKG.MasterPublicKey) {
b, err := rlp.EncodeToBytes(mpk)
if err != nil {
@@ -147,7 +142,8 @@ func cloneDKGMasterPublicKey(mpk *typesDKG.MasterPublicKey) (
return
}
-func cloneDKGMPKReady(ready *typesDKG.MPKReady) (
+// CloneDKGMPKReady clones a typesDKG.MPKReady instance.
+func CloneDKGMPKReady(ready *typesDKG.MPKReady) (
copied *typesDKG.MPKReady) {
b, err := rlp.EncodeToBytes(ready)
if err != nil {
@@ -160,7 +156,8 @@ func cloneDKGMPKReady(ready *typesDKG.MPKReady) (
return
}
-func cloneDKGFinalize(final *typesDKG.Finalize) (
+// CloneDKGFinalize clones a typesDKG.Finalize instance.
+func CloneDKGFinalize(final *typesDKG.Finalize) (
copied *typesDKG.Finalize) {
b, err := rlp.EncodeToBytes(final)
if err != nil {
@@ -173,6 +170,20 @@ func cloneDKGFinalize(final *typesDKG.Finalize) (
return
}
+// CloneDKGPrivateShare clones a typesDKG.PrivateShare instance.
+func CloneDKGPrivateShare(prvShare *typesDKG.PrivateShare) (
+ copied *typesDKG.PrivateShare) {
+ b, err := rlp.EncodeToBytes(prvShare)
+ if err != nil {
+ panic(err)
+ }
+ copied = &typesDKG.PrivateShare{}
+ if err = rlp.DecodeBytes(b, copied); err != nil {
+ panic(err)
+ }
+ return
+}
+
func cloneBlockRandomnessResult(rand *types.BlockRandomnessResult) (
copied *types.BlockRandomnessResult) {
b, err := rlp.EncodeToBytes(rand)