diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-10-27 17:00:18 +0800 |
---|---|---|
committer | Jimmy Hu <jimmy.hu@dexon.org> | 2018-10-27 17:00:18 +0800 |
commit | f4a9edfab58063f42526c08b27311a871f8b7207 (patch) | |
tree | be7e194dd5aeb136f877111457cc1f60f94d44a7 | |
parent | 47c29f1bc537f2108755f943e190a9f9a58c1ac0 (diff) | |
download | dexon-consensus-f4a9edfab58063f42526c08b27311a871f8b7207.tar dexon-consensus-f4a9edfab58063f42526c08b27311a871f8b7207.tar.gz dexon-consensus-f4a9edfab58063f42526c08b27311a871f8b7207.tar.bz2 dexon-consensus-f4a9edfab58063f42526c08b27311a871f8b7207.tar.lz dexon-consensus-f4a9edfab58063f42526c08b27311a871f8b7207.tar.xz dexon-consensus-f4a9edfab58063f42526c08b27311a871f8b7207.tar.zst dexon-consensus-f4a9edfab58063f42526c08b27311a871f8b7207.zip |
core: Fix missing fields when cloning blocks. (#268)
-rw-r--r-- | core/types/block.go | 1 | ||||
-rw-r--r-- | core/types/block_test.go | 36 |
2 files changed, 33 insertions, 4 deletions
diff --git a/core/types/block.go b/core/types/block.go index e12e0d5..6722692 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -215,6 +215,7 @@ func (b *Block) Clone() (bcopy *Block) { bcopy.ProposerID = b.ProposerID bcopy.ParentHash = b.ParentHash bcopy.Hash = b.Hash + bcopy.Position.Round = b.Position.Round bcopy.Position.ChainID = b.Position.ChainID bcopy.Position.Height = b.Position.Height bcopy.Signature = b.Signature.Clone() diff --git a/core/types/block_test.go b/core/types/block_test.go index 7bf6a75..4b899e1 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -40,12 +40,33 @@ func (s *BlockTestSuite) randomBytes() []byte { return h[:] } +func (s *BlockTestSuite) noZeroInStruct(v reflect.Value) { + t := v.Type() + for i := 0; i < t.NumField(); i++ { + tf := t.Field(i) + vf := v.FieldByName(tf.Name) + if vf.Type().Kind() == reflect.Struct { + s.noZeroInStruct(vf) + continue + } + if !vf.CanInterface() { + s.T().Log("unable to check private field", tf.Name) + continue + } + if reflect.DeepEqual( + vf.Interface(), reflect.Zero(vf.Type()).Interface()) { + s.Failf("", "should not be zero value %s", tf.Name) + } + } +} + func (s *BlockTestSuite) createRandomBlock() *Block { b := &Block{ ProposerID: NodeID{common.NewRandomHash()}, ParentHash: common.NewRandomHash(), Hash: common.NewRandomHash(), Position: Position{ + Round: rand.Uint64(), ChainID: rand.Uint32(), Height: rand.Uint64(), }, @@ -59,15 +80,22 @@ func (s *BlockTestSuite) createRandomBlock() *Block { Data: s.randomBytes(), }, Finalization: FinalizationResult{ + ParentHash: common.NewRandomHash(), Timestamp: time.Now().UTC(), Height: rand.Uint64(), Randomness: s.randomBytes(), }, - Payload: s.randomBytes(), - Signature: crypto.Signature{Signature: s.randomBytes()}, - CRSSignature: crypto.Signature{Signature: s.randomBytes()}, + Payload: s.randomBytes(), + Signature: crypto.Signature{ + Type: "some type", + Signature: s.randomBytes()}, + CRSSignature: crypto.Signature{ + Type: "some type", + Signature: s.randomBytes(), + }, } - + // Check if all fields are initialized with non zero values. + s.noZeroInStruct(reflect.ValueOf(*b)) return b } |