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" "log"
"time" "time"
"github.com/simple-rules/harmony-benchmark/db"
"github.com/simple-rules/harmony-benchmark/utils" "github.com/simple-rules/harmony-benchmark/utils"
) )
@ -69,6 +70,10 @@ func (b *Block) HashTransactions() []byte {
return txHash[:] 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 // CalculateBlockHash returns a hash of the block
func (b *Block) CalculateBlockHash() []byte { func (b *Block) CalculateBlockHash() []byte {
var hashes [][]byte var hashes [][]byte

@ -1,7 +1,7 @@
package blockchain package blockchain
import ( import (
"bytes" "reflect"
"testing" "testing"
) )
@ -15,15 +15,7 @@ func TestBlockSerialize(t *testing.T) {
serializedValue := block.Serialize() serializedValue := block.Serialize()
deserializedBlock := DeserializeBlock(serializedValue) deserializedBlock := DeserializeBlock(serializedValue)
if block.Timestamp != deserializedBlock.Timestamp { if !reflect.DeepEqual(block, deserializedBlock) {
t.Errorf("Serialize or Deserialize incorrect at TimeStamp.") t.Errorf("Original block and the deserialized block not equal.")
}
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.")
} }
} }

Loading…
Cancel
Save