modify DeserializeBlock

pull/69/head
Minh Doan 6 years ago
parent 888a865843
commit 665a427100
  1. 4
      blockchain/block.go
  2. 2
      blockchain/block_test.go
  3. 6
      blockchain/blockchain.go

@ -50,14 +50,14 @@ func (b *Block) Serialize() []byte {
}
// DeserializeBlock deserializes a block
func DeserializeBlock(d []byte) *Block {
func DeserializeBlock(d []byte) (*Block, error) {
var block Block
decoder := gob.NewDecoder(bytes.NewReader(d))
err := decoder.Decode(&block)
if err != nil {
log.Panic(err)
}
return &block
return &block, err
}
// Used for debuging.

@ -13,7 +13,7 @@ func TestBlockSerialize(t *testing.T) {
block := NewGenesisBlock(cbtx, 0)
serializedValue := block.Serialize()
deserializedBlock := DeserializeBlock(serializedValue)
deserializedBlock, _ := DeserializeBlock(serializedValue)
if !reflect.DeepEqual(block, deserializedBlock) {
t.Errorf("Original block and the deserialized block not equal.")

@ -15,7 +15,11 @@ type Blockchain struct {
const genesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"
func (bc *Blockchain) FindBlock(blockHash [32]byte) *Block {
// blockHash should have size of 32.
func (bc *Blockchain) FindBlock(blockHash []byte) *Block {
if len(blockHash) != 32 {
return nil
}
for _, block := range bc.Blocks {
if bytes.Compare(block.Hash[:], blockHash[:]) == 0 {
return block

Loading…
Cancel
Save