add test for gob encoding

pull/69/head
Minh Doan 6 years ago
parent 01f2769ac9
commit ef46b60e05
  1. 19
      blockchain/blockchain.go
  2. 3
      blockchain/blockchain_test.go
  3. 33
      p2p/helper_test.go

@ -15,6 +15,15 @@ type Blockchain struct {
const genesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks" const genesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"
func (bc *Blockchain) FindBlock(blockHash [32]byte) *Block {
for _, block := range bc.Blocks {
if bytes.Compare(block.Hash[:], blockHash[:]) == 0 {
return block
}
}
return nil
}
// GetLatestBlock gests the latest block at the end of the chain // GetLatestBlock gests the latest block at the end of the chain
func (bc *Blockchain) GetLatestBlock() *Block { func (bc *Blockchain) GetLatestBlock() *Block {
if len(bc.Blocks) == 0 { if len(bc.Blocks) == 0 {
@ -196,6 +205,16 @@ func CreateBlockchain(address [20]byte, shardId uint32) *Blockchain {
return &bc return &bc
} }
// Creat testing genesis block.
func CreateTestingGenesisBlock() *Block {
priIntOne := 111
shardId := uint32(1)
testAddressOne := pki.GetAddressFromInt(priIntOne)
cbtx := NewCoinbaseTX(testAddressOne, genesisCoinbaseData, shardId)
genesis := NewGenesisBlock(cbtx, shardId)
return genesis
}
// Create state block based on the utxos. // Create state block based on the utxos.
func (bc *Blockchain) CreateStateBlock(utxoPool *UTXOPool) *Block { func (bc *Blockchain) CreateStateBlock(utxoPool *UTXOPool) *Block {
var numBlocks int32 = 0 var numBlocks int32 = 0

@ -1,8 +1,9 @@
package blockchain package blockchain
import ( import (
"github.com/simple-rules/harmony-benchmark/crypto/pki"
"testing" "testing"
"github.com/simple-rules/harmony-benchmark/crypto/pki"
) )
var ( var (

@ -1,9 +1,15 @@
package p2p package p2p_test
import ( import (
"bufio" "bufio"
"bytes"
"encoding/gob"
"net" "net"
"reflect"
"testing" "testing"
"github.com/simple-rules/harmony-benchmark/blockchain"
"github.com/simple-rules/harmony-benchmark/p2p"
) )
func setUpTestServer(times int, t *testing.T, conCreated chan struct{}) { func setUpTestServer(times int, t *testing.T, conCreated chan struct{}) {
@ -18,11 +24,11 @@ func setUpTestServer(times int, t *testing.T, conCreated chan struct{}) {
) )
for times > 0 { for times > 0 {
times-- times--
data, err := ReadMessageContent(conn) data, err := p2p.ReadMessageContent(conn)
if err != nil { if err != nil {
t.Fatalf("error when ReadMessageContent %v", err) t.Fatalf("error when ReadMessageContent %v", err)
} }
data = CreateMessage(byte(1), data) data = p2p.CreateMessage(byte(1), data)
w.Write(data) w.Write(data)
w.Flush() w.Flush()
} }
@ -40,9 +46,9 @@ func TestNewNewNode(t *testing.T) {
times-- times--
myMsg := "minhdoan" myMsg := "minhdoan"
SendMessageContent(conn, []byte(myMsg)) p2p.SendMessageContent(conn, []byte(myMsg))
data, err := ReadMessageContent(conn) data, err := p2p.ReadMessageContent(conn)
if err != nil { if err != nil {
t.Error("got an error when trying to receive an expected message from server.") t.Error("got an error when trying to receive an expected message from server.")
} }
@ -51,3 +57,20 @@ func TestNewNewNode(t *testing.T) {
} }
} }
} }
func TestGobEncode(t *testing.T) {
block := blockchain.CreateTestingGenesisBlock()
var tmp bytes.Buffer
enc := gob.NewEncoder(&tmp)
enc.Encode(*block)
tmp2 := bytes.NewBuffer(tmp.Bytes())
dec := gob.NewDecoder(tmp2)
var block2 blockchain.Block
dec.Decode(&block2)
if !reflect.DeepEqual(*block, block2) {
t.Error("Error in GobEncode")
}
}

Loading…
Cancel
Save