mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-10-14 00:59:33 +00:00
[DEV-24] changed wire.BlockHeader to have list of previous hashes, instead of only 1
This commit is contained in:
parent
e14712c99e
commit
a655bd9861
@ -12,19 +12,29 @@ import (
|
|||||||
"github.com/daglabs/btcd/chaincfg/chainhash"
|
"github.com/daglabs/btcd/chaincfg/chainhash"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MaxBlockHeaderPayload is the maximum number of bytes a block header can be.
|
// BaseBlockHeaderPayload is the base number of bytes a block header can be,
|
||||||
|
// not including the list of previous block headers.
|
||||||
// Version 4 bytes + Timestamp 4 bytes + Bits 4 bytes + Nonce 4 bytes +
|
// Version 4 bytes + Timestamp 4 bytes + Bits 4 bytes + Nonce 4 bytes +
|
||||||
// PrevBlock and MerkleRoot hashes.
|
// + NumPrevBlocks 1 byte + MerkleRoot hash.
|
||||||
const MaxBlockHeaderPayload = 16 + (chainhash.HashSize * 2)
|
// To get total size of block header len(PrevBlocks) * chainhash.HashSize should be
|
||||||
|
// added to this value
|
||||||
|
const BaseBlockHeaderPayload = 21 + (chainhash.HashSize)
|
||||||
|
|
||||||
|
// MaxBlockHeaderPayload is the maximum number of bytes a block header can be.
|
||||||
|
// BaseBlockHeaderPayload + up to 255 hashes of previous blocks
|
||||||
|
const MaxBlockHeaderPayload = BaseBlockHeaderPayload + (255 * chainhash.HashSize)
|
||||||
|
|
||||||
// BlockHeader defines information about a block and is used in the bitcoin
|
// BlockHeader defines information about a block and is used in the bitcoin
|
||||||
// block (MsgBlock) and headers (MsgHeaders) messages.
|
// block (MsgBlock) and headers (MsgHeader ) messages.
|
||||||
type BlockHeader struct {
|
type BlockHeader struct {
|
||||||
// Version of the block. This is not the same as the protocol version.
|
// Version of the block. This is not the same as the protocol version.
|
||||||
Version int32
|
Version int32
|
||||||
|
|
||||||
// Hash of the previous block header in the block chain.
|
// Number of entries in PrevBlocks
|
||||||
PrevBlock chainhash.Hash
|
NumPrevBlocks byte
|
||||||
|
|
||||||
|
// Hashes of the previous block headers in the blockDAG.
|
||||||
|
PrevBlocks []chainhash.Hash
|
||||||
|
|
||||||
// Merkle tree reference to hash of all transactions for the block.
|
// Merkle tree reference to hash of all transactions for the block.
|
||||||
MerkleRoot chainhash.Hash
|
MerkleRoot chainhash.Hash
|
||||||
@ -50,7 +60,7 @@ func (h *BlockHeader) BlockHash() chainhash.Hash {
|
|||||||
// transactions. Ignore the error returns since there is no way the
|
// transactions. Ignore the error returns since there is no way the
|
||||||
// encode could fail except being out of memory which would cause a
|
// encode could fail except being out of memory which would cause a
|
||||||
// run-time panic.
|
// run-time panic.
|
||||||
buf := bytes.NewBuffer(make([]byte, 0, MaxBlockHeaderPayload))
|
buf := bytes.NewBuffer(make([]byte, 0, BaseBlockHeaderPayload+len(h.PrevBlocks)))
|
||||||
_ = writeBlockHeader(buf, 0, h)
|
_ = writeBlockHeader(buf, 0, h)
|
||||||
|
|
||||||
return chainhash.DoubleHashH(buf.Bytes())
|
return chainhash.DoubleHashH(buf.Bytes())
|
||||||
@ -94,19 +104,20 @@ func (h *BlockHeader) Serialize(w io.Writer) error {
|
|||||||
|
|
||||||
// NewBlockHeader returns a new BlockHeader using the provided version, previous
|
// NewBlockHeader returns a new BlockHeader using the provided version, previous
|
||||||
// block hash, merkle root hash, difficulty bits, and nonce used to generate the
|
// block hash, merkle root hash, difficulty bits, and nonce used to generate the
|
||||||
// block with defaults for the remaining fields.
|
// block with defaults or calclulated values for the remaining fields.
|
||||||
func NewBlockHeader(version int32, prevHash, merkleRootHash *chainhash.Hash,
|
func NewBlockHeader(version int32, prevHashes []chainhash.Hash, merkleRootHash *chainhash.Hash,
|
||||||
bits uint32, nonce uint32) *BlockHeader {
|
bits uint32, nonce uint32) *BlockHeader {
|
||||||
|
|
||||||
// Limit the timestamp to one second precision since the protocol
|
// Limit the timestamp to one second precision since the protocol
|
||||||
// doesn't support better.
|
// doesn't support better.
|
||||||
return &BlockHeader{
|
return &BlockHeader{
|
||||||
Version: version,
|
Version: version,
|
||||||
PrevBlock: *prevHash,
|
NumPrevBlocks: byte(len(prevHashes)),
|
||||||
MerkleRoot: *merkleRootHash,
|
PrevBlocks: prevHashes,
|
||||||
Timestamp: time.Unix(time.Now().Unix(), 0),
|
MerkleRoot: *merkleRootHash,
|
||||||
Bits: bits,
|
Timestamp: time.Unix(time.Now().Unix(), 0),
|
||||||
Nonce: nonce,
|
Bits: bits,
|
||||||
|
Nonce: nonce,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,8 +125,20 @@ func NewBlockHeader(version int32, prevHash, merkleRootHash *chainhash.Hash,
|
|||||||
// decoding block headers stored to disk, such as in a database, as opposed to
|
// decoding block headers stored to disk, such as in a database, as opposed to
|
||||||
// decoding from the wire.
|
// decoding from the wire.
|
||||||
func readBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error {
|
func readBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error {
|
||||||
return readElements(r, &bh.Version, &bh.PrevBlock, &bh.MerkleRoot,
|
err := readElements(r, &bh.Version, &bh.NumPrevBlocks)
|
||||||
(*uint32Time)(&bh.Timestamp), &bh.Bits, &bh.Nonce)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
bh.NumPrevBlocks, err = binarySerializer.Uint8(r)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
bh.PrevBlocks = make([]chainhash.Hash, bh.NumPrevBlocks)
|
||||||
|
for i := byte(0); i < bh.NumPrevBlocks; i++ {
|
||||||
|
readElement(r, &bh.PrevBlocks[i])
|
||||||
|
}
|
||||||
|
return readElements(r, &bh.MerkleRoot, (*uint32Time)(&bh.Timestamp), &bh.Bits, &bh.Nonce)
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeBlockHeader writes a bitcoin block header to w. See Serialize for
|
// writeBlockHeader writes a bitcoin block header to w. See Serialize for
|
||||||
@ -123,6 +146,6 @@ func readBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error {
|
|||||||
// opposed to encoding for the wire.
|
// opposed to encoding for the wire.
|
||||||
func writeBlockHeader(w io.Writer, pver uint32, bh *BlockHeader) error {
|
func writeBlockHeader(w io.Writer, pver uint32, bh *BlockHeader) error {
|
||||||
sec := uint32(bh.Timestamp.Unix())
|
sec := uint32(bh.Timestamp.Unix())
|
||||||
return writeElements(w, bh.Version, &bh.PrevBlock, &bh.MerkleRoot,
|
return writeElements(w, bh.Version, bh.NumPrevBlocks, &bh.PrevBlocks, &bh.MerkleRoot,
|
||||||
sec, bh.Bits, bh.Nonce)
|
sec, bh.Bits, bh.Nonce)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user