From c947b00d36fc97ef7a0de7965ddb8914d0b90414 Mon Sep 17 00:00:00 2001 From: George Tankersley Date: Fri, 14 Sep 2018 15:02:47 -0400 Subject: [PATCH] parser: clarify difference between block hash and block header hash --- parser/block.go | 27 ++++++++++++++------------- parser/block_test.go | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/parser/block.go b/parser/block.go index a98e15e..e693f58 100644 --- a/parser/block.go +++ b/parser/block.go @@ -64,13 +64,6 @@ type EquihashSize struct { Size uint16 // always 1344 } -func ReadBlockHeader(blockHeader *BlockHeader, data []byte) error { - if blockHeader.rawBlockHeader == nil { - blockHeader.rawBlockHeader = new(rawBlockHeader) - } - return blockHeader.UnmarshalBinary(data) -} - func (hdr *rawBlockHeader) MarshalBinary() ([]byte, error) { serBytes := make([]byte, 0, SER_BLOCK_HEADER_SIZE) serBuf := bytes.NewBuffer(serBytes) @@ -89,12 +82,12 @@ func (hdr *rawBlockHeader) UnmarshalBinary(data []byte) error { type BlockHeader struct { *rawBlockHeader - cachedBlockHash []byte + cachedHash []byte } -func (hdr *BlockHeader) GetBlockHash() []byte { - if hdr.cachedBlockHash != nil { - return hdr.cachedBlockHash +func (hdr *BlockHeader) GetBlockHeaderHash() []byte { + if hdr.cachedHash != nil { + return hdr.cachedHash } serializedHeader, err := hdr.MarshalBinary() @@ -107,11 +100,19 @@ func (hdr *BlockHeader) GetBlockHash() []byte { digest := sha256.Sum256(serializedHeader) digest = sha256.Sum256(digest[:]) - hdr.cachedBlockHash = digest[:] - return hdr.cachedBlockHash + hdr.cachedHash = digest[:] + return hdr.cachedHash } func (hdr *BlockHeader) GetSerializedSize() int { // TODO: Make this dynamic. Low priority; it's unlikely to change. return SER_BLOCK_HEADER_SIZE } + +func ReadBlockHeader(blockHeader *BlockHeader, data []byte) error { + if blockHeader.rawBlockHeader == nil { + blockHeader.rawBlockHeader = new(rawBlockHeader) + } + return blockHeader.UnmarshalBinary(data) +} + diff --git a/parser/block_test.go b/parser/block_test.go index a23661e..5b25fcf 100644 --- a/parser/block_test.go +++ b/parser/block_test.go @@ -73,7 +73,7 @@ func TestBlockHeader(t *testing.T) { break } - hash := blockHeader.GetBlockHash() + hash := blockHeader.GetBlockHeaderHash() // This is not necessarily true for anything but our current test cases. for _, b := range hash[28:] {