diff --git a/blockchain/block.go b/blockchain/block.go index 4629799f5..895841e29 100644 --- a/blockchain/block.go +++ b/blockchain/block.go @@ -8,6 +8,7 @@ import ( "log" "time" + "github.com/simple-rules/harmony-benchmark/db" "github.com/simple-rules/harmony-benchmark/utils" ) @@ -69,6 +70,10 @@ func (b *Block) HashTransactions() []byte { return txHash[:] } +func (b *Block) Write(db db.Database, key string) error { + return db.Put([]byte(key), b.Serialize()) +} + // CalculateBlockHash returns a hash of the block func (b *Block) CalculateBlockHash() []byte { var hashes [][]byte diff --git a/blockchain/block_test.go b/blockchain/block_test.go index 1e58cb084..7a27a9cdc 100644 --- a/blockchain/block_test.go +++ b/blockchain/block_test.go @@ -1,7 +1,7 @@ package blockchain import ( - "bytes" + "reflect" "testing" ) @@ -15,15 +15,7 @@ func TestBlockSerialize(t *testing.T) { serializedValue := block.Serialize() deserializedBlock := DeserializeBlock(serializedValue) - if block.Timestamp != deserializedBlock.Timestamp { - t.Errorf("Serialize or Deserialize incorrect at TimeStamp.") - } - - if bytes.Compare(block.PrevBlockHash[:], deserializedBlock.PrevBlockHash[:]) != 0 { - t.Errorf("Serialize or Deserialize incorrect at PrevBlockHash.") - } - - if bytes.Compare(block.Hash[:], deserializedBlock.Hash[:]) != 0 { - t.Errorf("Serialize or Deserialize incorrect at Hash.") + if !reflect.DeepEqual(block, deserializedBlock) { + t.Errorf("Original block and the deserialized block not equal.") } }