aboutsummaryrefslogtreecommitdiffstats
path: root/core/test
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-08-12 00:40:29 +0800
committerWei-Ning Huang <aitjcize@gmail.com>2018-08-12 00:40:29 +0800
commita57a1c2392f6f150d2127cc28236ca74f338dd7e (patch)
tree1d295e5d43688b3346504068b2f0038faf574dd1 /core/test
parent08c208c21f93d55bf3275610cbaf3ee6a545956a (diff)
downloaddexon-consensus-a57a1c2392f6f150d2127cc28236ca74f338dd7e.tar
dexon-consensus-a57a1c2392f6f150d2127cc28236ca74f338dd7e.tar.gz
dexon-consensus-a57a1c2392f6f150d2127cc28236ca74f338dd7e.tar.bz2
dexon-consensus-a57a1c2392f6f150d2127cc28236ca74f338dd7e.tar.lz
dexon-consensus-a57a1c2392f6f150d2127cc28236ca74f338dd7e.tar.xz
dexon-consensus-a57a1c2392f6f150d2127cc28236ca74f338dd7e.tar.zst
dexon-consensus-a57a1c2392f6f150d2127cc28236ca74f338dd7e.zip
core: Hash block in Consensus.PrepareBlock. (#46)
* Add hash to block * Check block hash in Consensus.sanityCheck * Add hashBlockFn in block generator.go
Diffstat (limited to 'core/test')
-rw-r--r--core/test/blocks-generator.go30
-rw-r--r--core/test/blocks-generator_test.go19
-rw-r--r--core/test/revealer_test.go14
-rw-r--r--core/test/utils.go31
4 files changed, 61 insertions, 33 deletions
diff --git a/core/test/blocks-generator.go b/core/test/blocks-generator.go
index 976b661..926ef5b 100644
--- a/core/test/blocks-generator.go
+++ b/core/test/blocks-generator.go
@@ -39,6 +39,8 @@ type validatorStatus struct {
lastAckingHeight map[types.ValidatorID]uint64
}
+type hashBlockFn func(types.BlockConverter) (common.Hash, error)
+
// getAckedBlockHash would randomly pick one block between
// last acked one to current head.
func (vs *validatorStatus) getAckedBlockHash(
@@ -71,9 +73,10 @@ type validatorSetStatus struct {
status map[types.ValidatorID]*validatorStatus
validatorIDs []types.ValidatorID
randGen *rand.Rand
+ hashBlock hashBlockFn
}
-func newValidatorSetStatus(vIDs []types.ValidatorID) *validatorSetStatus {
+func newValidatorSetStatus(vIDs []types.ValidatorID, hashBlock hashBlockFn) *validatorSetStatus {
status := make(map[types.ValidatorID]*validatorStatus)
for _, vID := range vIDs {
status[vID] = &validatorStatus{
@@ -85,6 +88,7 @@ func newValidatorSetStatus(vIDs []types.ValidatorID) *validatorSetStatus {
status: status,
validatorIDs: vIDs,
randGen: rand.New(rand.NewSource(time.Now().UnixNano())),
+ hashBlock: hashBlock,
}
}
@@ -139,10 +143,9 @@ func (vs *validatorSetStatus) prepareAcksForNewBlock(
// proposeBlock propose new block and update validator status.
func (vs *validatorSetStatus) proposeBlock(
proposerID types.ValidatorID,
- acks map[common.Hash]struct{}) *types.Block {
+ acks map[common.Hash]struct{}) (*types.Block, error) {
status := vs.status[proposerID]
- hash := common.NewRandomHash()
parentHash := common.Hash{}
if len(status.blocks) > 0 {
parentHash = status.blocks[len(status.blocks)-1].Hash
@@ -151,13 +154,17 @@ func (vs *validatorSetStatus) proposeBlock(
newBlock := &types.Block{
ProposerID: proposerID,
ParentHash: parentHash,
- Hash: hash,
Height: uint64(len(status.blocks)),
Acks: acks,
// TODO(mission.liao): Generate timestamp randomly.
}
+ var err error
+ newBlock.Hash, err = vs.hashBlock(newBlock)
+ if err != nil {
+ return nil, err
+ }
status.blocks = append(status.blocks, newBlock)
- return newBlock
+ return newBlock, nil
}
// normalAckingCountGenerator would randomly pick acking count
@@ -189,17 +196,20 @@ func generateValidatorPicker() func([]types.ValidatorID) types.ValidatorID {
// BlocksGenerator could generate blocks forming valid DAGs.
type BlocksGenerator struct {
validatorPicker func([]types.ValidatorID) types.ValidatorID
+ hashBlock hashBlockFn
}
// NewBlocksGenerator constructs BlockGenerator.
func NewBlocksGenerator(validatorPicker func(
- []types.ValidatorID) types.ValidatorID) *BlocksGenerator {
+ []types.ValidatorID) types.ValidatorID,
+ hashBlock hashBlockFn) *BlocksGenerator {
if validatorPicker == nil {
validatorPicker = generateValidatorPicker()
}
return &BlocksGenerator{
validatorPicker: validatorPicker,
+ hashBlock: hashBlock,
}
}
@@ -228,7 +238,7 @@ func (gen *BlocksGenerator) Generate(
validators = append(
validators, types.ValidatorID{Hash: common.NewRandomHash()})
}
- status := newValidatorSetStatus(validators)
+ status := newValidatorSetStatus(validators, gen.hashBlock)
// We would record the smallest height of block that could be acked
// from each validator's point-of-view.
@@ -255,7 +265,11 @@ func (gen *BlocksGenerator) Generate(
if err != nil {
return
}
- newBlock := status.proposeBlock(proposerID, acks)
+ var newBlock *types.Block
+ newBlock, err = status.proposeBlock(proposerID, acks)
+ if err != nil {
+ return
+ }
// Persist block to db.
err = writer.Put(*newBlock)
diff --git a/core/test/blocks-generator_test.go b/core/test/blocks-generator_test.go
index 84bee4c..0624da2 100644
--- a/core/test/blocks-generator_test.go
+++ b/core/test/blocks-generator_test.go
@@ -15,23 +15,6 @@
// along with the dexon-consensus-core library. If not, see
// <http://www.gnu.org/licenses/>.
-// 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
-// <http://www.gnu.org/licenses/>.
-
package test
import (
@@ -52,7 +35,7 @@ func (s *BlocksGeneratorTestCase) TestGenerate() {
// This test case is to make sure the generated blocks are legimate.
validatorCount := 19
blockCount := 50
- gen := NewBlocksGenerator(nil)
+ gen := NewBlocksGenerator(nil, stableRandomHash)
db, err := blockdb.NewMemBackedBlockDB()
s.Require().Nil(err)
diff --git a/core/test/revealer_test.go b/core/test/revealer_test.go
index 0ef19a1..8087136 100644
--- a/core/test/revealer_test.go
+++ b/core/test/revealer_test.go
@@ -1,15 +1,15 @@
// 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
+// 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.
+// 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
@@ -44,7 +44,7 @@ func (s *RevealerTestSuite) SetupSuite() {
s.Require().Nil(err)
// Randomly generate blocks.
- gen := NewBlocksGenerator(nil)
+ gen := NewBlocksGenerator(nil, stableRandomHash)
err = gen.Generate(
validatorCount, blockCount, nil, s.db)
s.Require().Nil(err)
diff --git a/core/test/utils.go b/core/test/utils.go
new file mode 100644
index 0000000..eae12a0
--- /dev/null
+++ b/core/test/utils.go
@@ -0,0 +1,31 @@
+// 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
+// <http://www.gnu.org/licenses/>.
+
+package test
+
+import (
+ "github.com/dexon-foundation/dexon-consensus-core/common"
+ "github.com/dexon-foundation/dexon-consensus-core/core/types"
+)
+
+func stableRandomHash(blockConv types.BlockConverter) (common.Hash, error) {
+ block := blockConv.Block()
+ if (block.Hash != common.Hash{}) {
+ return block.Hash, nil
+ }
+ return common.NewRandomHash(), nil
+}