haskoin-core-0.3.0: Implementation of the core Bitcoin protocol features.

Safe HaskellNone
LanguageHaskell98

Network.Haskoin.Block

Contents

Description

This package provides block and block-related types.

Synopsis

Blocks

data Block Source

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.

Constructors

Block 

Fields

blockHeader :: !BlockHeader

Header information for this block.

blockCoinbaseTx :: !CoinbaseTx

Coinbase transaction of this block.

blockTxns :: ![Tx]

List of transactions pertaining to this block.

data GetBlocks 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.

Constructors

GetBlocks 

Fields

getBlocksVersion :: !Word32

The protocol version

getBlocksLocator :: !BlockLocator

Block locator object. It is a list of block hashes from the most recent block back to the genesis block. The list is dense at first and sparse towards the end.

getBlocksHashStop :: !BlockHash

Hash of the last desired block. If set to zero, the maximum number of block hashes is returned (500).

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.

Constructors

BlockHeader 

Fields

blockVersion :: !Word32

Block version information, based on the version of the software creating this block.

prevBlock :: !BlockHash

Hash of the previous block (parent) referenced by this block.

merkleRoot :: !Hash256

Root of the merkle tree of all transactions pertaining to this block.

blockTimestamp :: !Word32

Unix timestamp recording when this block was created

blockBits :: !Word32

The difficulty target being used for this block

bhNonce :: !Word32

A random nonce used to generate this block. Additional randomness is included in the coinbase transaction of this block.

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.

Constructors

GetHeaders 

Fields

getHeadersVersion :: !Word32

The protocol version

getHeadersBL :: !BlockLocator

Block locator object. It is a list of block hashes from the most recent block back to the Genesis block. The list is dense at first and sparse towards the end.

getHeadersHashStop :: !BlockHash

Hash of the last desired block header. When set to zero, the maximum number of block headers is returned (2000)

data Headers Source

The Headers type is used to return a list of block headers in response to a GetHeaders message.

Constructors

Headers 

Fields

headersList :: ![BlockHeaderCount]

List of block headers with respective transaction counts

type BlockHeaderCount = (BlockHeader, VarInt) Source

BlockHeader type with a transaction count as VarInt

Merkle Blocks

data MerkleBlock Source

Constructors

MerkleBlock 

Fields

merkleHeader :: !BlockHeader

Header information for this merkle block.

merkleTotalTxns :: !Word32

Number of transactions in the block (including unmatched transactions).

mHashes :: ![Hash256]

Hashes in depth-first order. They are used to rebuild a partial merkle tree.

mFlags :: ![Bool]

Flag bits, packed per 8 in a byte. Least significant bit first. Flag bits are used to rebuild a partial merkle tree.

calcTreeHeight Source

Arguments

:: Int

Number of transactions (leaf nodes).

-> Int

Height of the merkle tree.

Computes the height of a merkle tree.

calcTreeWidth Source

Arguments

:: 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.

buildMerkleRoot Source

Arguments

:: [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.

calcHash Source

Arguments

:: 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.

buildPartialMerkle Source

Arguments

:: [(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.

extractMatches Source

Arguments

:: 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.