diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/types/block.go | 2 | ||||
-rw-r--r-- | core/types/block_test.go | 71 |
2 files changed, 73 insertions, 0 deletions
diff --git a/core/types/block.go b/core/types/block.go index c785969..297f479 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -95,6 +95,8 @@ func (b *Block) Clone() (bcopy *Block) { bcopy.ConsensusHeight = b.ConsensusHeight bcopy.Witness.Timestamp = b.Witness.Timestamp bcopy.Witness.Height = b.Witness.Height + bcopy.Witness.Data = make([]byte, len(b.Witness.Data)) + copy(bcopy.Witness.Data, b.Witness.Data) bcopy.Timestamp = b.Timestamp bcopy.Acks = make(common.SortedHashes, len(b.Acks)) copy(bcopy.Acks, b.Acks) diff --git a/core/types/block_test.go b/core/types/block_test.go index 2e6807d..3e601b6 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -18,10 +18,14 @@ package types import ( + "math/rand" + "reflect" "sort" "testing" + "time" "github.com/dexon-foundation/dexon-consensus-core/common" + "github.com/dexon-foundation/dexon-consensus-core/core/crypto" "github.com/stretchr/testify/suite" ) @@ -29,6 +33,55 @@ type BlockTestSuite struct { suite.Suite } +func (s *BlockTestSuite) randomBytes() []byte { + h := common.NewRandomHash() + return h[:] +} + +func (s *BlockTestSuite) createRandomBlock() *Block { + b := &Block{ + ProposerID: NodeID{common.NewRandomHash()}, + ParentHash: common.NewRandomHash(), + Hash: common.NewRandomHash(), + Position: Position{ + ChainID: rand.Uint32(), + Height: rand.Uint64(), + }, + Acks: common.NewSortedHashes(common.Hashes{ + common.NewRandomHash(), + common.NewRandomHash(), + }), + Timestamp: time.Now().Add(time.Duration(rand.Int())), + Witness: Witness{ + Height: rand.Uint64(), + Timestamp: time.Now().Add(time.Duration(rand.Int())), + Data: s.randomBytes(), + }, + ConsensusHeight: rand.Uint64(), + ConsensusTimestamp: time.Now().Add(time.Duration(rand.Int())), + Payload: s.randomBytes(), + Signature: crypto.Signature{Signature: s.randomBytes()}, + CRSSignature: crypto.Signature{Signature: s.randomBytes()}, + } + return b +} + +func (s *BlockTestSuite) TestCreateRandomBlock() { + b1 := *s.createRandomBlock() + b2 := *s.createRandomBlock() + + v1 := reflect.ValueOf(b1) + v2 := reflect.ValueOf(b2) + for i := 0; i < v1.NumField(); i++ { + f1 := v1.Field(i) + f2 := v2.Field(i) + if reflect.DeepEqual(f1.Interface(), f2.Interface()) { + s.Failf("Non randomized field found", "Field %s is not randomized\n", + v1.Type().Field(i).Name) + } + } +} + func (s *BlockTestSuite) TestSortByHash() { hash := common.Hash{} copy(hash[:], "aaaaaa") @@ -100,6 +153,24 @@ func (s *BlockTestSuite) TestIsAcking() { s.False(b0.IsAcking(common.NewRandomHash())) } +func (s *BlockTestSuite) TestClone() { + b1 := *s.createRandomBlock() + b2 := *b1.Clone() + + // Use reflect here to better understand the error message. + v1 := reflect.ValueOf(b1) + v2 := reflect.ValueOf(b2) + for i := 0; i < v1.NumField(); i++ { + f1 := v1.Field(i) + f2 := v2.Field(i) + if !reflect.DeepEqual(f1.Interface(), f2.Interface()) { + s.Failf("Field Not Equal", "Field %s is not equal.\n-%v\n+%v\n", + v1.Type().Field(i).Name, + f1, f2) + } + } +} + func TestBlock(t *testing.T) { suite.Run(t, new(BlockTestSuite)) } |