btcd学习笔记
比特币中的merketree,btcd是通过数组实现的merketree,把交易看成二叉树的叶子节点,每两个交易的hash计算hash256,计算得到上一层的节点,反复计算,直到求解到root节点,该root节点即比特币区块头中的MerkleRoot
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 // Merkle tree reference to hash of all transactions for the block. MerkleRoot chainhash.Hash // Time the block was created. This is, unfortunately, encoded as a // uint32 on the wire and therefore is limited to 2106. Timestamp time.Time // Difficulty target for the block. Bits uint32 // Nonce used to generate the block. Nonce uint32 }
区块链头包括版本信息,前一个区块的哈希值,当前区块中所有交易的merkleroot,时间戳信息,当前区块链的挖矿难度,以及Nonce,挖矿软件不断尝试随机数Nonce,以确保生成符合难度的当前区块的哈希值。每一个hash值是256bit即32个字节,所以比特币的链头部一共需要80个字节