Safe Haskell | None |
---|---|
Language | Haskell98 |
This package provides block and block-related types.
- data Block = Block {
- blockHeader :: !BlockHeader
- blockCoinbaseTx :: !CoinbaseTx
- blockTxns :: ![Tx]
- type BlockLocator = [BlockHash]
- data GetBlocks = GetBlocks {}
- headerHash :: BlockHeader -> BlockHash
- data BlockHeader = BlockHeader {
- blockVersion :: !Word32
- prevBlock :: !BlockHash
- merkleRoot :: !Hash256
- blockTimestamp :: !Word32
- blockBits :: !Word32
- bhNonce :: !Word32
- data GetHeaders = GetHeaders {}
- data Headers = Headers {
- headersList :: ![BlockHeaderCount]
- type BlockHeaderCount = (BlockHeader, VarInt)
- newtype BlockHash = BlockHash {}
- blockHashToHex :: BlockHash -> ByteString
- hexToBlockHash :: ByteString -> Maybe BlockHash
- data MerkleBlock = MerkleBlock {
- merkleHeader :: !BlockHeader
- merkleTotalTxns :: !Word32
- mHashes :: ![Hash256]
- mFlags :: ![Bool]
- type MerkleRoot = Hash256
- type FlagBits = [Bool]
- type PartialMerkleTree = [Hash256]
- calcTreeHeight :: Int -> Int
- calcTreeWidth :: Int -> Int -> Int
- buildMerkleRoot :: [TxHash] -> MerkleRoot
- calcHash :: Int -> Int -> [TxHash] -> Hash256
- buildPartialMerkle :: [(TxHash, Bool)] -> (FlagBits, PartialMerkleTree)
- extractMatches :: FlagBits -> PartialMerkleTree -> Int -> Either String (MerkleRoot, [TxHash])
- decodeCompact :: Word32 -> Integer
- encodeCompact :: Integer -> Word32
Blocks
Data type describing a block in the bitcoin protocol. Blocks are sent in
response to GetData
messages that are requesting information from a
block hash.
Block | |
|
type BlockLocator = [BlockHash] Source
Data type representing a GetBlocks message request. It is used in the
bitcoin protocol to retrieve blocks from a peer by providing it a
BlockLocator
object. The BlockLocator
is a sparse list of block hashes
from the caller node with the purpose of informing the receiving node
about the state of the caller's blockchain. The receiver node will detect
a wrong branch in the caller's main chain and send the caller appropriate
Blocks
. The response to a GetBlocks
message is an Inv
message
containing the list of block hashes pertaining to the request.
GetBlocks | |
|
Block Headers
headerHash :: BlockHeader -> BlockHash Source
Compute the hash of a block header
data BlockHeader Source
Data type recording information on a Block
. The hash of a block is
defined as the hash of this data structure. The block mining process
involves finding a partial hash collision by varying the nonce in the
BlockHeader
and/or additional randomness in the CoinbaseTx
of this
Block
. Variations in the CoinbaseTx
will result in different merkle
roots in the BlockHeader
.
BlockHeader | |
|
data GetHeaders Source
Similar to the GetBlocks
message type but for retrieving block headers
only. The response to a GetHeaders
request is a Headers
message
containing a list of block headers pertaining to the request. A maximum of
2000 block headers can be returned. GetHeaders
is used by thin (SPV)
clients to exclude block contents when synchronizing the blockchain.
GetHeaders | |
|
The Headers
type is used to return a list of block headers in
response to a GetHeaders
message.
Headers | |
|
type BlockHeaderCount = (BlockHeader, VarInt) Source
BlockHeader
type with a transaction count as VarInt
Merkle Blocks
data MerkleBlock Source
MerkleBlock | |
|
type MerkleRoot = Hash256 Source
type PartialMerkleTree = [Hash256] Source
Computes the height of a merkle tree.
:: Int | Number of transactions (leaf nodes). |
-> Int | Height at which we want to compute the width. |
-> Int | Width of the merkle tree. |
Computes the width of a merkle tree at a specific height. The transactions are at height 0.
:: [TxHash] | List of transaction hashes (leaf nodes). |
-> MerkleRoot | Root of the merkle tree. |
Computes the root of a merkle tree from a list of leaf node hashes.
:: Int | Height of the node in the merkle tree. |
-> Int | Position of the node (0 for the leftmost node). |
-> [TxHash] | Transaction hashes of the merkle tree (leaf nodes). |
-> Hash256 | Hash of the node at the specified position. |
Computes the hash of a specific node in a merkle tree.
:: [(TxHash, Bool)] | List of transactions hashes forming the leaves of the merkle tree and a bool indicating if that transaction should be included in the partial merkle tree. |
-> (FlagBits, PartialMerkleTree) | Flag bits (used to parse the partial merkle tree) and the partial merkle tree. |
Build a partial merkle tree.
:: FlagBits | Flag bits (produced by buildPartialMerkle). |
-> PartialMerkleTree | Partial merkle tree. |
-> Int | Number of transaction at height 0 (leaf nodes). |
-> Either String (MerkleRoot, [TxHash]) | Merkle root and the list of matching transaction hashes. |
Extracts the matching hashes from a partial merkle tree. This will return
the list of transaction hashes that have been included (set to True) in
a call to buildPartialMerkle
.
Difficulty Target
decodeCompact :: Word32 -> Integer Source
Decode the compact number used in the difficulty target of a block into an Integer.
As described in the Satoshi reference implementation srcbignum.h:
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar to a floating point format. The most significant 8 bits are the unsigned exponent of base 256. This exponent can be thought of as "number of bytes of N". The lower 23 bits are the mantissa. Bit number 24 (0x800000) represents the sign of N.
N = (-1^sign) * mantissa * 256^(exponent-3)
encodeCompact :: Integer -> Word32 Source
Encode an Integer to the compact number format used in the difficulty target of a block.