parser: tidy up the BlockHeader API
This commit is contained in:
parent
5d3e0308a8
commit
e69779195f
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
@ -16,7 +15,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// A block header as defined in version 2018.0-beta-29 of the Zcash Protocol Spec.
|
// A block header as defined in version 2018.0-beta-29 of the Zcash Protocol Spec.
|
||||||
type RawBlockHeader struct {
|
type rawBlockHeader struct {
|
||||||
// The block version number indicates which set of block validation rules
|
// The block version number indicates which set of block validation rules
|
||||||
// to follow. The current and only defined block version number for Zcash
|
// to follow. The current and only defined block version number for Zcash
|
||||||
// is 4.
|
// is 4.
|
||||||
|
@ -57,30 +56,39 @@ type RawBlockHeader struct {
|
||||||
Solution [EQUIHASH_SIZE]byte
|
Solution [EQUIHASH_SIZE]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// EquihashSize is a concrete instance of Bitcoin's CompactSize encoding.
|
// EquihashSize is a concrete instance of Bitcoin's CompactSize encoding. This
|
||||||
|
// representation is a hack allowing us to use Go's binary parsing. In contexts
|
||||||
|
// outside of Zcash this could be a variable-length field.
|
||||||
type EquihashSize struct {
|
type EquihashSize struct {
|
||||||
SizeTag byte // always the byte value 253
|
SizeTag byte // always the byte value 253
|
||||||
Size uint16 // always 1344
|
Size uint16 // always 1344
|
||||||
}
|
}
|
||||||
|
|
||||||
func readRawBlockHeader(r io.Reader) (*RawBlockHeader, error) {
|
func ReadBlockHeader(blockHeader *BlockHeader, data []byte) error {
|
||||||
var blockHeader RawBlockHeader
|
if blockHeader.rawBlockHeader == nil {
|
||||||
err := binary.Read(r, binary.LittleEndian, &blockHeader)
|
blockHeader.rawBlockHeader = new(rawBlockHeader)
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed reading block header")
|
|
||||||
}
|
}
|
||||||
return &blockHeader, nil
|
return blockHeader.UnmarshalBinary(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hdr *RawBlockHeader) MarshalBinary() ([]byte, error) {
|
func (hdr *rawBlockHeader) MarshalBinary() ([]byte, error) {
|
||||||
serBytes := make([]byte, 0, SER_BLOCK_HEADER_SIZE)
|
serBytes := make([]byte, 0, SER_BLOCK_HEADER_SIZE)
|
||||||
serBuf := bytes.NewBuffer(serBytes)
|
serBuf := bytes.NewBuffer(serBytes)
|
||||||
err := binary.Write(serBuf, binary.LittleEndian, hdr)
|
err := binary.Write(serBuf, binary.LittleEndian, hdr)
|
||||||
return serBytes[:SER_BLOCK_HEADER_SIZE], err
|
return serBytes[:SER_BLOCK_HEADER_SIZE], err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (hdr *rawBlockHeader) UnmarshalBinary(data []byte) error {
|
||||||
|
reader := bytes.NewReader(data)
|
||||||
|
err := binary.Read(reader, binary.LittleEndian, hdr)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed parsing block header")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type BlockHeader struct {
|
type BlockHeader struct {
|
||||||
*RawBlockHeader
|
*rawBlockHeader
|
||||||
cachedBlockHash []byte
|
cachedBlockHash []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,3 +110,8 @@ func (hdr *BlockHeader) GetBlockHash() []byte {
|
||||||
hdr.cachedBlockHash = digest[:]
|
hdr.cachedBlockHash = digest[:]
|
||||||
return hdr.cachedBlockHash
|
return hdr.cachedBlockHash
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (hdr *BlockHeader) GetSerializedSize() int {
|
||||||
|
// TODO: Make this dynamic. Low priority; it's unlikely to change.
|
||||||
|
return SER_BLOCK_HEADER_SIZE
|
||||||
|
}
|
||||||
|
|
|
@ -27,32 +27,32 @@ func TestBlockHeader(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to read the header
|
// Try to read the header
|
||||||
reader := bytes.NewReader(decodedBlockData)
|
blockHeader := &BlockHeader{}
|
||||||
rawHeader, err := readRawBlockHeader(reader)
|
err = ReadBlockHeader(blockHeader, decodedBlockData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Some basic sanity checks
|
// Some basic sanity checks
|
||||||
if rawHeader.Version != 4 {
|
if blockHeader.Version != 4 {
|
||||||
t.Error("Read wrong version in a test block.")
|
t.Error("Read wrong version in a test block.")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if rawHeader.Time < lastBlockTime {
|
if blockHeader.Time < lastBlockTime {
|
||||||
t.Error("Block times not increasing.")
|
t.Error("Block times not increasing.")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
lastBlockTime = rawHeader.Time
|
lastBlockTime = blockHeader.Time
|
||||||
|
|
||||||
if rawHeader.SolutionSize.Size != 1344 {
|
if blockHeader.SolutionSize.Size != 1344 {
|
||||||
t.Error("Got wrong Equihash solution size.")
|
t.Error("Got wrong Equihash solution size.")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// Re-serialize and check for consistency
|
// Re-serialize and check for consistency
|
||||||
serializedHeader, err := rawHeader.MarshalBinary()
|
serializedHeader, err := blockHeader.MarshalBinary()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Error serializing header: %v", err)
|
t.Errorf("Error serializing header: %v", err)
|
||||||
break
|
break
|
||||||
|
@ -73,10 +73,6 @@ func TestBlockHeader(t *testing.T) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
blockHeader := &BlockHeader{
|
|
||||||
rawHeader,
|
|
||||||
nil,
|
|
||||||
}
|
|
||||||
hash := blockHeader.GetBlockHash()
|
hash := blockHeader.GetBlockHash()
|
||||||
|
|
||||||
// This is not necessarily true for anything but our current test cases.
|
// This is not necessarily true for anything but our current test cases.
|
||||||
|
|
Loading…
Reference in New Issue