add write to block

pull/61/head
Minh Doan 6 years ago
parent 4454200ed2
commit 67b371a440
  1. 5
      blockchain/block.go
  2. 14
      blockchain/block_test.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

@ -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.")
}
}

Loading…
Cancel
Save