mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08: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"
|
||||
)
|
||||
|
||||
// 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 +
|
||||
// PrevBlock and MerkleRoot hashes.
|
||||
const MaxBlockHeaderPayload = 16 + (chainhash.HashSize * 2)
|
||||
// + NumPrevBlocks 1 byte + MerkleRoot hash.
|
||||
// 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
|
||||
// block (MsgBlock) and headers (MsgHeaders) messages.
|
||||
// block (MsgBlock) and headers (MsgHeader ) messages.
|
||||
type BlockHeader struct {
|
||||
// Version of the block. This is not the same as the protocol version.
|
||||
Version int32
|
||||
|
||||
// Hash of the previous block header in the block chain.
|
||||
PrevBlock chainhash.Hash
|
||||
// Number of entries in PrevBlocks
|
||||
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.
|
||||
MerkleRoot chainhash.Hash
|
||||
@ -50,7 +60,7 @@ func (h *BlockHeader) BlockHash() chainhash.Hash {
|
||||
// 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, MaxBlockHeaderPayload))
|
||||
buf := bytes.NewBuffer(make([]byte, 0, BaseBlockHeaderPayload+len(h.PrevBlocks)))
|
||||
_ = writeBlockHeader(buf, 0, h)
|
||||
|
||||
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
|
||||
// block hash, merkle root hash, difficulty bits, and nonce used to generate the
|
||||
// block with defaults for the remaining fields.
|
||||
func NewBlockHeader(version int32, prevHash, merkleRootHash *chainhash.Hash,
|
||||
// block with defaults or calclulated values for the remaining fields.
|
||||
func NewBlockHeader(version int32, prevHashes []chainhash.Hash, merkleRootHash *chainhash.Hash,
|
||||
bits uint32, nonce uint32) *BlockHeader {
|
||||
|
||||
// Limit the timestamp to one second precision since the protocol
|
||||
// doesn't support better.
|
||||
return &BlockHeader{
|
||||
Version: version,
|
||||
PrevBlock: *prevHash,
|
||||
MerkleRoot: *merkleRootHash,
|
||||
Timestamp: time.Unix(time.Now().Unix(), 0),
|
||||
Bits: bits,
|
||||
Nonce: nonce,
|
||||
Version: version,
|
||||
NumPrevBlocks: byte(len(prevHashes)),
|
||||
PrevBlocks: prevHashes,
|
||||
MerkleRoot: *merkleRootHash,
|
||||
Timestamp: time.Unix(time.Now().Unix(), 0),
|
||||
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 from the wire.
|
||||
func readBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error {
|
||||
return readElements(r, &bh.Version, &bh.PrevBlock, &bh.MerkleRoot,
|
||||
(*uint32Time)(&bh.Timestamp), &bh.Bits, &bh.Nonce)
|
||||
err := readElements(r, &bh.Version, &bh.NumPrevBlocks)
|
||||
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
|
||||
@ -123,6 +146,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 := 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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user