diff --git a/blockchain/block.go b/blockchain/block.go index d5f84d0f2..d34f69976 100644 --- a/blockchain/block.go +++ b/blockchain/block.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. diff --git a/blockchain/block_test.go b/blockchain/block_test.go index 7a27a9cdc..968f149e9 100644 --- a/blockchain/block_test.go +++ b/blockchain/block_test.go @@ -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.") diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 5faf28c74..c5709b367 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -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