[DEV-244] Remove BlockHeader.NumParentBlocks and use a method instead (#121)

This commit is contained in:
Ori Newman 2018-11-06 11:26:59 +02:00 committed by Svarog
parent 8a234bf4a3
commit 05d46e7c01
13 changed files with 118 additions and 136 deletions

View File

@ -163,13 +163,12 @@ func newBlockNode(blockHeader *wire.BlockHeader, parents blockSet, phantomK uint
func (node *blockNode) Header() *wire.BlockHeader {
// No lock is needed because all accessed fields are immutable.
return &wire.BlockHeader{
Version: node.version,
NumParentBlocks: byte(len(node.parents)),
ParentHashes: node.ParentHashes(),
MerkleRoot: node.merkleRoot,
Timestamp: time.Unix(node.timestamp, 0),
Bits: node.bits,
Nonce: node.nonce,
Version: node.version,
ParentHashes: node.ParentHashes(),
MerkleRoot: node.merkleRoot,
Timestamp: time.Unix(node.timestamp, 0),
Bits: node.bits,
Nonce: node.nonce,
}
}

View File

@ -509,13 +509,12 @@ func (g *testGenerator) nextBlock(blockName string, spend *spendableOut, mungers
block := wire.MsgBlock{
Header: wire.BlockHeader{
Version: 1,
NumParentBlocks: 1, // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
ParentHashes: []daghash.Hash{g.tip.BlockHash()}, // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
MerkleRoot: calcMerkleRoot(txns),
Bits: g.params.PowLimitBits,
Timestamp: ts,
Nonce: 0, // To be solved.
Version: 1,
ParentHashes: []daghash.Hash{g.tip.BlockHash()}, // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
MerkleRoot: calcMerkleRoot(txns),
Bits: g.params.PowLimitBits,
Timestamp: ts,
Nonce: 0, // To be solved.
},
Transactions: txns,
}

View File

@ -54,13 +54,12 @@ var (
// as the public transaction ledger for the regression test network.
regTestGenesisBlock = wire.MsgBlock{
Header: wire.BlockHeader{
Version: 1,
NumParentBlocks: 0,
ParentHashes: []daghash.Hash{},
MerkleRoot: *newHashFromStr("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"),
Timestamp: time.Unix(0x5b28c636, 0), // 2018-06-19 09:00:38 +0000 UTC
Bits: 0x207fffff, // 545259519 [7fffff0000000000000000000000000000000000000000000000000000000000]
Nonce: 1,
Version: 1,
ParentHashes: []daghash.Hash{},
MerkleRoot: *newHashFromStr("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"),
Timestamp: time.Unix(0x5b28c636, 0), // 2018-06-19 09:00:38 +0000 UTC
Bits: 0x207fffff, // 545259519 [7fffff0000000000000000000000000000000000000000000000000000000000]
Nonce: 1,
},
Transactions: []*wire.MsgTx{{
Version: 1,

View File

@ -431,7 +431,7 @@ func checkBlockHeaderSanity(header *wire.BlockHeader, powLimit *big.Int, timeSou
//checkBlockParentsOrder ensures that the block's parents are ordered by hash
func checkBlockParentsOrder(header *wire.BlockHeader) error {
sortedHashes := make([]daghash.Hash, 0, len(header.ParentHashes))
sortedHashes := make([]daghash.Hash, 0, header.NumParentBlocks())
for _, hash := range header.ParentHashes {
sortedHashes = append(sortedHashes, hash)
}

View File

@ -169,8 +169,7 @@ func TestCheckBlockSanity(t *testing.T) {
var invalidParentsOrderBlock = wire.MsgBlock{
Header: wire.BlockHeader{
Version: 1,
NumParentBlocks: 2,
Version: 1,
ParentHashes: []daghash.Hash{
[32]byte{ // Make go vet happy.
0x4b, 0xb0, 0x75, 0x35, 0xdf, 0xd5, 0x8e, 0x0b,
@ -610,8 +609,7 @@ func TestValidateParents(t *testing.T) {
// test Block operations.
var Block100000 = wire.MsgBlock{
Header: wire.BlockHeader{
Version: 1,
NumParentBlocks: 2,
Version: 1,
ParentHashes: []daghash.Hash{
[32]byte{ // Make go vet happy.
0x16, 0x5e, 0x38, 0xe8, 0xb3, 0x91, 0x45, 0x95,

View File

@ -78,13 +78,12 @@ var genesisMerkleRoot = daghash.Hash([daghash.HashSize]byte{ // Make go vet happ
// public transaction ledger for the main network.
var genesisBlock = wire.MsgBlock{
Header: wire.BlockHeader{
Version: 1,
NumParentBlocks: 0,
ParentHashes: []daghash.Hash{},
MerkleRoot: genesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
Timestamp: time.Unix(0x5bbe076c, 0), // 2018-10-10 14:06:36 +0000 UTC
Bits: 0x1e00ffff, // 503382015 [000000ffff000000000000000000000000000000000000000000000000000000]
Nonce: 0x80000000000d8796, // 9223372036855662486
Version: 1,
ParentHashes: []daghash.Hash{},
MerkleRoot: genesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
Timestamp: time.Unix(0x5bbe076c, 0), // 2018-10-10 14:06:36 +0000 UTC
Bits: 0x1e00ffff, // 503382015 [000000ffff000000000000000000000000000000000000000000000000000000]
Nonce: 0x80000000000d8796, // 9223372036855662486
},
Transactions: []*wire.MsgTx{&genesisCoinbaseTx},
}
@ -107,13 +106,12 @@ var regTestGenesisMerkleRoot = genesisMerkleRoot
// as the public transaction ledger for the regression test network.
var regTestGenesisBlock = wire.MsgBlock{
Header: wire.BlockHeader{
Version: 1,
NumParentBlocks: 0,
ParentHashes: []daghash.Hash{},
MerkleRoot: regTestGenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
Timestamp: time.Unix(0x5bbe0d4b, 0), // 2018-06-19 09:00:38 +0000 UTC
Bits: 0x207fffff, // 545259519 [7fffff0000000000000000000000000000000000000000000000000000000000]
Nonce: 0x00000000,
Version: 1,
ParentHashes: []daghash.Hash{},
MerkleRoot: regTestGenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
Timestamp: time.Unix(0x5bbe0d4b, 0), // 2018-06-19 09:00:38 +0000 UTC
Bits: 0x207fffff, // 545259519 [7fffff0000000000000000000000000000000000000000000000000000000000]
Nonce: 0x00000000,
},
Transactions: []*wire.MsgTx{&genesisCoinbaseTx},
}
@ -136,13 +134,12 @@ var testNet3GenesisMerkleRoot = genesisMerkleRoot
// serves as the public transaction ledger for the test network (version 3).
var testNet3GenesisBlock = wire.MsgBlock{
Header: wire.BlockHeader{
Version: 1,
NumParentBlocks: 0,
ParentHashes: []daghash.Hash{},
MerkleRoot: testNet3GenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
Timestamp: time.Unix(0x5bbe0e49, 0), // 2018-06-19 09:04:06 +0000 UTC
Bits: 0x1e00ffff, // 503382015 [000000ffff000000000000000000000000000000000000000000000000000000]
Nonce: 0xc00000000032560b, // 2150570811
Version: 1,
ParentHashes: []daghash.Hash{},
MerkleRoot: testNet3GenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
Timestamp: time.Unix(0x5bbe0e49, 0), // 2018-06-19 09:04:06 +0000 UTC
Bits: 0x1e00ffff, // 503382015 [000000ffff000000000000000000000000000000000000000000000000000000]
Nonce: 0xc00000000032560b, // 2150570811
},
Transactions: []*wire.MsgTx{&genesisCoinbaseTx},
}
@ -165,13 +162,12 @@ var simNetGenesisMerkleRoot = genesisMerkleRoot
// as the public transaction ledger for the simulation test network.
var simNetGenesisBlock = wire.MsgBlock{
Header: wire.BlockHeader{
Version: 1,
NumParentBlocks: 0,
ParentHashes: []daghash.Hash{},
MerkleRoot: simNetGenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
Timestamp: time.Unix(0x5bbe00fe, 0), // 2018-10-10 13:39:10 +0000 UTC
Bits: 0x207fffff, // 545259519 [7fffff0000000000000000000000000000000000000000000000000000000000]
Nonce: 0xdffffffffffffffc, // 1610612733
Version: 1,
ParentHashes: []daghash.Hash{},
MerkleRoot: simNetGenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
Timestamp: time.Unix(0x5bbe00fe, 0), // 2018-10-10 13:39:10 +0000 UTC
Bits: 0x207fffff, // 545259519 [7fffff0000000000000000000000000000000000000000000000000000000000]
Nonce: 0xdffffffffffffffc, // 1610612733
},
Transactions: []*wire.MsgTx{&genesisCoinbaseTx},
}

View File

@ -44,13 +44,12 @@ func generateBlocks(out *os.File, numBlocks int) {
func generateBlock(parent *wire.MsgBlock) *wire.MsgBlock {
return &wire.MsgBlock{
Header: wire.BlockHeader{
Version: 1,
NumParentBlocks: 1,
ParentHashes: []daghash.Hash{parent.BlockHash()},
MerkleRoot: genesisMerkleRoot,
Timestamp: time.Unix(0x5b28c4c8, 0), // 2018-06-19 08:54:32 +0000 UTC
Bits: 0x1e00ffff, // 503382015 [000000ffff000000000000000000000000000000000000000000000000000000]
Nonce: 0xc0192550, // 2148484547
Version: 1,
ParentHashes: []daghash.Hash{parent.BlockHash()},
MerkleRoot: genesisMerkleRoot,
Timestamp: time.Unix(0x5b28c4c8, 0), // 2018-06-19 08:54:32 +0000 UTC
Bits: 0x1e00ffff, // 503382015 [000000ffff000000000000000000000000000000000000000000000000000000]
Nonce: 0xc0192550, // 2148484547
},
Transactions: []*wire.MsgTx{&genesisCoinbaseTx},
}

View File

@ -715,12 +715,11 @@ mempoolLoop:
merkles := blockdag.BuildMerkleTreeStore(blockTxns)
var msgBlock wire.MsgBlock
msgBlock.Header = wire.BlockHeader{
Version: nextBlockVersion,
NumParentBlocks: byte(len(g.dag.TipHashes())),
ParentHashes: g.dag.TipHashes(),
MerkleRoot: *merkles[len(merkles)-1],
Timestamp: ts,
Bits: reqDifficulty,
Version: nextBlockVersion,
ParentHashes: g.dag.TipHashes(),
MerkleRoot: *merkles[len(merkles)-1],
Timestamp: ts,
Bits: reqDifficulty,
}
for _, tx := range blockTxns {
if err := msgBlock.AddTransaction(tx.MsgTx()); err != nil {

View File

@ -304,8 +304,7 @@ func TestBlockErrors(t *testing.T) {
// test Block operations.
var Block100000 = wire.MsgBlock{
Header: wire.BlockHeader{
Version: 1,
NumParentBlocks: 2,
Version: 1,
ParentHashes: []daghash.Hash{
[32]byte{ // Make go vet happy.
0x82, 0xdc, 0xbd, 0xe6, 0x88, 0x37, 0x74, 0x5b,

View File

@ -34,9 +34,6 @@ type BlockHeader struct {
// Version of the block. This is not the same as the protocol version.
Version int32
// Number of entries in ParentHashes
NumParentBlocks byte
// Hashes of the parent block headers in the blockDAG.
ParentHashes []daghash.Hash
@ -53,13 +50,18 @@ type BlockHeader struct {
Nonce uint64
}
// NumParentBlocks return the number of entries in ParentHashes
func (h *BlockHeader) NumParentBlocks() byte {
return byte(len(h.ParentHashes))
}
// BlockHash computes the block identifier hash for the given block header.
func (h *BlockHeader) BlockHash() daghash.Hash {
// Encode the header and double sha256 everything prior to the number of
// transactions. Ignore the error returns since there is no way the
// encode could fail except being out of memory which would cause a
// run-time panic.
buf := bytes.NewBuffer(make([]byte, 0, BaseBlockHeaderPayload+len(h.ParentHashes)))
buf := bytes.NewBuffer(make([]byte, 0, BaseBlockHeaderPayload+h.NumParentBlocks()))
_ = writeBlockHeader(buf, 0, h)
return daghash.DoubleHashH(buf.Bytes())
@ -67,7 +69,7 @@ func (h *BlockHeader) BlockHash() daghash.Hash {
// SelectedParentHash returns the hash of the selected block header.
func (h *BlockHeader) SelectedParentHash() *daghash.Hash {
if h.NumParentBlocks == 0 {
if h.NumParentBlocks() == 0 {
return nil
}
@ -76,7 +78,7 @@ func (h *BlockHeader) SelectedParentHash() *daghash.Hash {
// IsGenesis returns true iff this block is a genesis block
func (h *BlockHeader) IsGenesis() bool {
return h.NumParentBlocks == 0
return h.NumParentBlocks() == 0
}
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
@ -118,7 +120,7 @@ func (h *BlockHeader) Serialize(w io.Writer) error {
// SerializeSize returns the number of bytes it would take to serialize the
// block header.
func (h *BlockHeader) SerializeSize() int {
return BaseBlockHeaderPayload + int(h.NumParentBlocks)*daghash.HashSize
return BaseBlockHeaderPayload + int(h.NumParentBlocks())*daghash.HashSize
}
// NewBlockHeader returns a new BlockHeader using the provided version, previous
@ -130,13 +132,12 @@ func NewBlockHeader(version int32, parentHashes []daghash.Hash, merkleRootHash *
// Limit the timestamp to one second precision since the protocol
// doesn't support better.
return &BlockHeader{
Version: version,
NumParentBlocks: byte(len(parentHashes)),
ParentHashes: parentHashes,
MerkleRoot: *merkleRootHash,
Timestamp: time.Unix(time.Now().Unix(), 0),
Bits: bits,
Nonce: nonce,
Version: version,
ParentHashes: parentHashes,
MerkleRoot: *merkleRootHash,
Timestamp: time.Unix(time.Now().Unix(), 0),
Bits: bits,
Nonce: nonce,
}
}
@ -144,13 +145,14 @@ func NewBlockHeader(version int32, parentHashes []daghash.Hash, merkleRootHash *
// decoding block headers stored to disk, such as in a database, as opposed to
// decoding from the wire.
func readBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error {
err := readElements(r, &bh.Version, &bh.NumParentBlocks)
var numParentBlocks byte
err := readElements(r, &bh.Version, &numParentBlocks)
if err != nil {
return err
}
bh.ParentHashes = make([]daghash.Hash, bh.NumParentBlocks)
for i := byte(0); i < bh.NumParentBlocks; i++ {
bh.ParentHashes = make([]daghash.Hash, numParentBlocks)
for i := byte(0); i < numParentBlocks; i++ {
err := readElement(r, &bh.ParentHashes[i])
if err != nil {
return err
@ -164,6 +166,6 @@ func readBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error {
// opposed to encoding for the wire.
func writeBlockHeader(w io.Writer, pver uint32, bh *BlockHeader) error {
sec := int64(bh.Timestamp.Unix())
return writeElements(w, bh.Version, bh.NumParentBlocks, &bh.ParentHashes, &bh.MerkleRoot,
return writeElements(w, bh.Version, bh.NumParentBlocks(), &bh.ParentHashes, &bh.MerkleRoot,
sec, bh.Bits, bh.Nonce)
}

View File

@ -55,13 +55,12 @@ func TestBlockHeaderWire(t *testing.T) {
// baseBlockHdr is used in the various tests as a baseline BlockHeader.
bits := uint32(0x1d00ffff)
baseBlockHdr := &BlockHeader{
Version: 1,
NumParentBlocks: 2,
ParentHashes: []daghash.Hash{mainNetGenesisHash, simNetGenesisHash},
MerkleRoot: mainNetGenesisMerkleRoot,
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
Bits: bits,
Nonce: nonce,
Version: 1,
ParentHashes: []daghash.Hash{mainNetGenesisHash, simNetGenesisHash},
MerkleRoot: mainNetGenesisMerkleRoot,
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
Bits: bits,
Nonce: nonce,
}
// baseBlockHdrEncoded is the wire encoded bytes of baseBlockHdr.
@ -194,13 +193,12 @@ func TestBlockHeaderSerialize(t *testing.T) {
// baseBlockHdr is used in the various tests as a baseline BlockHeader.
bits := uint32(0x1d00ffff)
baseBlockHdr := &BlockHeader{
Version: 1,
NumParentBlocks: 2,
ParentHashes: []daghash.Hash{mainNetGenesisHash, simNetGenesisHash},
MerkleRoot: mainNetGenesisMerkleRoot,
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
Bits: bits,
Nonce: nonce,
Version: 1,
ParentHashes: []daghash.Hash{mainNetGenesisHash, simNetGenesisHash},
MerkleRoot: mainNetGenesisMerkleRoot,
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
Bits: bits,
Nonce: nonce,
}
// baseBlockHdrEncoded is the wire encoded bytes of baseBlockHdr.
@ -274,23 +272,21 @@ func TestBlockHeaderSerializeSize(t *testing.T) {
bits := uint32(0x1d00ffff)
timestamp := time.Unix(0x495fab29, 0) // 2009-01-03 12:15:05 -0600 CST
baseBlockHdr := &BlockHeader{
Version: 1,
NumParentBlocks: 2,
ParentHashes: []daghash.Hash{mainNetGenesisHash, simNetGenesisHash},
MerkleRoot: mainNetGenesisMerkleRoot,
Timestamp: timestamp,
Bits: bits,
Nonce: nonce,
Version: 1,
ParentHashes: []daghash.Hash{mainNetGenesisHash, simNetGenesisHash},
MerkleRoot: mainNetGenesisMerkleRoot,
Timestamp: timestamp,
Bits: bits,
Nonce: nonce,
}
genesisBlockHdr := &BlockHeader{
Version: 1,
NumParentBlocks: 0,
ParentHashes: []daghash.Hash{},
MerkleRoot: mainNetGenesisMerkleRoot,
Timestamp: timestamp,
Bits: bits,
Nonce: nonce,
Version: 1,
ParentHashes: []daghash.Hash{},
MerkleRoot: mainNetGenesisMerkleRoot,
Timestamp: timestamp,
Bits: bits,
Nonce: nonce,
}
tests := []struct {
in *BlockHeader // Block header to encode
@ -321,22 +317,20 @@ func TestIsGenesis(t *testing.T) {
timestamp := time.Unix(0x495fab29, 0) // 2009-01-03 12:15:05 -0600 CST
baseBlockHdr := &BlockHeader{
Version: 1,
NumParentBlocks: 2,
ParentHashes: []daghash.Hash{mainNetGenesisHash, simNetGenesisHash},
MerkleRoot: mainNetGenesisMerkleRoot,
Timestamp: timestamp,
Bits: bits,
Nonce: nonce,
Version: 1,
ParentHashes: []daghash.Hash{mainNetGenesisHash, simNetGenesisHash},
MerkleRoot: mainNetGenesisMerkleRoot,
Timestamp: timestamp,
Bits: bits,
Nonce: nonce,
}
genesisBlockHdr := &BlockHeader{
Version: 1,
NumParentBlocks: 0,
ParentHashes: []daghash.Hash{},
MerkleRoot: mainNetGenesisMerkleRoot,
Timestamp: timestamp,
Bits: bits,
Nonce: nonce,
Version: 1,
ParentHashes: []daghash.Hash{},
MerkleRoot: mainNetGenesisMerkleRoot,
Timestamp: timestamp,
Bits: bits,
Nonce: nonce,
}
tests := []struct {

View File

@ -491,10 +491,9 @@ func TestBlockSerializeSize(t *testing.T) {
// blockOne is the first block in the mainnet block chain.
var blockOne = MsgBlock{
Header: BlockHeader{
Version: 1,
NumParentBlocks: 2,
ParentHashes: []daghash.Hash{mainNetGenesisHash, simNetGenesisHash},
MerkleRoot: daghash.Hash(mainNetGenesisMerkleRoot),
Version: 1,
ParentHashes: []daghash.Hash{mainNetGenesisHash, simNetGenesisHash},
MerkleRoot: daghash.Hash(mainNetGenesisMerkleRoot),
Timestamp: time.Unix(0x4966bc61, 0), // 2009-01-08 20:54:25 -0600 CST
Bits: 0x1d00ffff, // 486604799

View File

@ -337,9 +337,8 @@ func TestMerkleBlockOverflowErrors(t *testing.T) {
// where the first transaction matches.
var merkleBlockOne = MsgMerkleBlock{
Header: BlockHeader{
Version: 1,
NumParentBlocks: 2,
ParentHashes: []daghash.Hash{mainNetGenesisHash, simNetGenesisHash},
Version: 1,
ParentHashes: []daghash.Hash{mainNetGenesisHash, simNetGenesisHash},
MerkleRoot: daghash.Hash([daghash.HashSize]byte{ // Make go vet happy.
0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,