Merge pull request #41 from str4d/testing-fixes

Testing fixes
This commit is contained in:
str4d 2019-05-30 18:21:24 +01:00 committed by GitHub
commit ce11c2107e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 13 deletions

View File

@ -201,7 +201,7 @@ func handleBlock(db *sql.DB, sequence int, blockData []byte) {
entry := log.WithFields(logrus.Fields{ entry := log.WithFields(logrus.Fields{
"seqnum": sequence, "seqnum": sequence,
"block_height": block.GetHeight(), "block_height": block.GetHeight(),
"block_hash": blockHash, "block_hash": hex.EncodeToString(block.GetDisplayHash()),
"block_version": block.GetVersion(), "block_version": block.GetVersion(),
"tx_count": block.GetTxCount(), "tx_count": block.GetTxCount(),
"sapling": block.HasSaplingTransactions(), "sapling": block.HasSaplingTransactions(),
@ -226,7 +226,7 @@ func handleBlock(db *sql.DB, sequence int, blockData []byte) {
) )
entry = log.WithFields(logrus.Fields{ entry = log.WithFields(logrus.Fields{
"block_height": block.GetHeight(), "block_height": block.GetHeight(),
"block_hash": blockHash, "block_hash": hex.EncodeToString(block.GetDisplayHash()),
"tx_index": index, "tx_index": index,
"tx_size": len(tx.Bytes()), "tx_size": len(tx.Bytes()),
"sapling": tx.HasSaplingTransactions(), "sapling": tx.HasSaplingTransactions(),

View File

@ -7,13 +7,13 @@ import (
"log" "log"
"math/big" "math/big"
"github.com/zcash-hackworks/lightwalletd/parser/internal/bytestring"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/zcash-hackworks/lightwalletd/parser/internal/bytestring"
) )
const ( const (
EQUIHASH_SIZE = 1344 // size of an Equihash solution in bytes serBlockHeaderMinusEquihashSize = 140 // size of a serialized block header minus the Equihash solution
SER_BLOCK_HEADER_SIZE = 1487 // size of a serialized block header equihashSizeMainnet = 1344 // size of a mainnet / testnet Equihash solution in bytes
) )
// 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.
@ -62,8 +62,47 @@ type blockHeader struct {
targetThreshold *big.Int targetThreshold *big.Int
} }
func CompactLengthPrefixedLen(val []byte) int {
length := len(val)
if length < 253 {
return 1 + length
} else if length < 0xffff {
return 1 + 2 + length
} else if length < 0xffff {
return 1 + 4 + length
} else {
return 1 + 8 + length
}
}
func WriteCompactLengthPrefixed(buf *bytes.Buffer, val []byte) error {
length := len(val)
if length < 253 {
binary.Write(buf, binary.LittleEndian, uint8(length))
binary.Write(buf, binary.LittleEndian, val)
} else if length < 0xffff {
binary.Write(buf, binary.LittleEndian, byte(253))
binary.Write(buf, binary.LittleEndian, uint16(length))
binary.Write(buf, binary.LittleEndian, val)
} else if length < 0xffff {
binary.Write(buf, binary.LittleEndian, byte(254))
binary.Write(buf, binary.LittleEndian, uint32(length))
binary.Write(buf, binary.LittleEndian, val)
} else {
binary.Write(buf, binary.LittleEndian, byte(255))
binary.Write(buf, binary.LittleEndian, uint64(length))
binary.Write(buf, binary.LittleEndian, val)
}
return nil
}
func (hdr *rawBlockHeader) GetSize() int {
return serBlockHeaderMinusEquihashSize + CompactLengthPrefixedLen(hdr.Solution)
}
func (hdr *rawBlockHeader) MarshalBinary() ([]byte, error) { func (hdr *rawBlockHeader) MarshalBinary() ([]byte, error) {
backing := make([]byte, 0, SER_BLOCK_HEADER_SIZE) headerSize := hdr.GetSize()
backing := make([]byte, 0, headerSize)
buf := bytes.NewBuffer(backing) buf := bytes.NewBuffer(backing)
binary.Write(buf, binary.LittleEndian, hdr.Version) binary.Write(buf, binary.LittleEndian, hdr.Version)
binary.Write(buf, binary.LittleEndian, hdr.HashPrevBlock) binary.Write(buf, binary.LittleEndian, hdr.HashPrevBlock)
@ -72,11 +111,8 @@ func (hdr *rawBlockHeader) MarshalBinary() ([]byte, error) {
binary.Write(buf, binary.LittleEndian, hdr.Time) binary.Write(buf, binary.LittleEndian, hdr.Time)
binary.Write(buf, binary.LittleEndian, hdr.NBitsBytes) binary.Write(buf, binary.LittleEndian, hdr.NBitsBytes)
binary.Write(buf, binary.LittleEndian, hdr.Nonce) binary.Write(buf, binary.LittleEndian, hdr.Nonce)
// TODO: write a Builder that knows about CompactSize WriteCompactLengthPrefixed(buf, hdr.Solution)
binary.Write(buf, binary.LittleEndian, byte(253)) return backing[:headerSize], nil
binary.Write(buf, binary.LittleEndian, uint16(1344))
binary.Write(buf, binary.LittleEndian, hdr.Solution)
return backing[:SER_BLOCK_HEADER_SIZE], nil
} }
func NewBlockHeader() *blockHeader { func NewBlockHeader() *blockHeader {

View File

@ -91,7 +91,7 @@ func TestBlockHeader(t *testing.T) {
} }
lastBlockTime = blockHeader.Time lastBlockTime = blockHeader.Time
if len(blockHeader.Solution) != EQUIHASH_SIZE { if len(blockHeader.Solution) != equihashSizeMainnet {
t.Error("Got wrong Equihash solution size.") t.Error("Got wrong Equihash solution size.")
break break
} }
@ -103,7 +103,7 @@ func TestBlockHeader(t *testing.T) {
break break
} }
if !bytes.Equal(serializedHeader, blockData[:SER_BLOCK_HEADER_SIZE]) { if !bytes.Equal(serializedHeader, blockData[:serBlockHeaderMinusEquihashSize+3+equihashSizeMainnet]) {
offset := 0 offset := 0
length := 0 length := 0
for i := 0; i < len(serializedHeader); i++ { for i := 0; i < len(serializedHeader); i++ {