Copyright | No rights reserved |
---|---|
License | UNLICENSE |
Maintainer | xenog@protonmail.com |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
Most functions relating to blocks are exported by this module.
Synopsis
- data Block = Block {
- blockHeader :: !BlockHeader
- blockTxns :: ![Tx]
- type BlockHeight = Word32
- type Timestamp = Word32
- data BlockHeader = BlockHeader {
- blockVersion :: !Word32
- prevBlock :: !BlockHash
- merkleRoot :: !Hash256
- blockTimestamp :: !Timestamp
- blockBits :: !Word32
- bhNonce :: !Word32
- headerHash :: BlockHeader -> BlockHash
- type BlockLocator = [BlockHash]
- data GetBlocks = GetBlocks {}
- data GetHeaders = GetHeaders {}
- type BlockHeaderCount = (BlockHeader, VarInt)
- newtype BlockHash = BlockHash {}
- blockHashToHex :: BlockHash -> Text
- hexToBlockHash :: Text -> Maybe BlockHash
- newtype Headers = Headers {}
- decodeCompact :: Word32 -> (Integer, Bool)
- encodeCompact :: Integer -> Word32
- type BlockWork = Integer
- class Monad m => BlockHeaders m where
- addBlockHeader :: BlockNode -> m ()
- getBlockHeader :: BlockHash -> m (Maybe BlockNode)
- getBestBlockHeader :: m BlockNode
- setBestBlockHeader :: BlockNode -> m ()
- addBlockHeaders :: [BlockNode] -> m ()
- data BlockNode
- = BlockNode {
- nodeHeader :: !BlockHeader
- nodeHeight :: !BlockHeight
- nodeWork :: !BlockWork
- nodeSkip :: !BlockHash
- | GenesisNode {
- nodeHeader :: !BlockHeader
- nodeHeight :: !BlockHeight
- nodeWork :: !BlockWork
- = BlockNode {
- data HeaderMemory = HeaderMemory {}
- type BlockMap = HashMap ShortBlockHash ShortByteString
- getAncestor :: BlockHeaders m => BlockHeight -> BlockNode -> m (Maybe BlockNode)
- isGenesis :: BlockNode -> Bool
- initialChain :: Network -> HeaderMemory
- genesisMap :: Network -> BlockMap
- genesisNode :: Network -> BlockNode
- genesisBlock :: Network -> Block
- connectBlocks :: BlockHeaders m => Network -> Timestamp -> [BlockHeader] -> m (Either String [BlockNode])
- connectBlock :: BlockHeaders m => Network -> Timestamp -> BlockHeader -> m (Either String BlockNode)
- parentBlock :: BlockHeaders m => BlockHeader -> m (Maybe BlockNode)
- splitPoint :: BlockHeaders m => BlockNode -> BlockNode -> m BlockNode
- blockLocator :: BlockHeaders m => BlockNode -> m BlockLocator
- data MerkleBlock = MerkleBlock {}
- type MerkleRoot = Hash256
- type FlagBits = [Bool]
- type PartialMerkleTree = [Hash256]
- buildMerkleRoot :: [TxHash] -> MerkleRoot
- buildPartialMerkle :: [(TxHash, Bool)] -> (FlagBits, PartialMerkleTree)
- merkleBlockTxs :: Network -> MerkleBlock -> Either String [TxHash]
- testMerkleRoot :: Network -> MerkleBlock -> Bool
Documentation
Block header and transactions.
Block | |
|
Instances
Eq Block Source # | |
Read Block Source # | |
Show Block Source # | |
Generic Block Source # | |
Hashable Block Source # | |
Defined in Network.Haskoin.Block.Common | |
Serialize Block Source # | |
type Rep Block Source # | |
Defined in Network.Haskoin.Block.Common type Rep Block = D1 (MetaData "Block" "Network.Haskoin.Block.Common" "haskoin-core-0.9.0-9luCRjUjDcyCwjYBmjo0zV" False) (C1 (MetaCons "Block" PrefixI True) (S1 (MetaSel (Just "blockHeader") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 BlockHeader) :*: S1 (MetaSel (Just "blockTxns") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 [Tx]))) |
type BlockHeight = Word32 Source #
Height of a block in the block chain, starting at 0 for Genesis.
data BlockHeader Source #
Data type recording information of a Block
. The hash of a block is
defined as the hash of this data structure, serialized. The block mining
process involves finding a partial hash collision by varying the nonce in the
BlockHeader
and/or additional entropy in the coinbase Transaction
of this
Block
. Variations in the coinbase will result in different merkle roots in
the BlockHeader
.
BlockHeader | |
|
Instances
headerHash :: BlockHeader -> BlockHash Source #
Compute hash of BlockHeader
.
type BlockLocator = [BlockHash] Source #
A block locator is a set of block headers, denser towards the best block and sparser towards the genesis block. It starts at the highest block known. It is used by a node to synchronize against the network. When the locator is provided to a peer, it will send back block hashes starting from the first block in the locator that it recognizes.
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 response to a GetBlocks
message is an Inv
message containing a list of block hashes that the peer believes this node is
missing. The number of block hashes in that inv message will end at the stop
block hash, at at the tip of the chain, or after 500 entries, whichever comes
earlier.
GetBlocks | |
|
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. A maximum of 2000 block headers can be
returned. GetHeaders
is used by simplified payment verification (SPV)
clients to exclude block contents when synchronizing the block chain.
GetHeaders | |
|
Instances
Eq GetHeaders Source # | |
Defined in Network.Haskoin.Block.Common (==) :: GetHeaders -> GetHeaders -> Bool # (/=) :: GetHeaders -> GetHeaders -> Bool # | |
Show GetHeaders Source # | |
Defined in Network.Haskoin.Block.Common showsPrec :: Int -> GetHeaders -> ShowS # show :: GetHeaders -> String # showList :: [GetHeaders] -> ShowS # | |
Serialize GetHeaders Source # | |
Defined in Network.Haskoin.Block.Common put :: Putter GetHeaders # get :: Get GetHeaders # |
type BlockHeaderCount = (BlockHeader, VarInt) Source #
BlockHeader
type with a transaction count as VarInt
Block header hash. To be serialized reversed for display purposes.
Instances
Eq BlockHash Source # | |
Ord BlockHash Source # | |
Defined in Network.Haskoin.Block.Common | |
Read BlockHash Source # | |
Show BlockHash Source # | |
IsString BlockHash Source # | |
Defined in Network.Haskoin.Block.Common fromString :: String -> BlockHash # | |
Generic BlockHash Source # | |
Hashable BlockHash Source # | |
Defined in Network.Haskoin.Block.Common | |
ToJSON BlockHash Source # | |
Defined in Network.Haskoin.Block.Common | |
FromJSON BlockHash Source # | |
Serialize BlockHash Source # | |
type Rep BlockHash Source # | |
Defined in Network.Haskoin.Block.Common |
blockHashToHex :: BlockHash -> Text Source #
Block hashes are reversed with respect to the in-memory byte order in a block hash when displayed.
hexToBlockHash :: Text -> Maybe BlockHash Source #
Convert a human-readable hex block hash into a BlockHash
. Bytes are
reversed as normal.
The Headers
type is used to return a list of block headers in
response to a GetHeaders
message.
Headers | |
|
Decode the compact number used in the difficulty target of a block.
The compact format is a representation of a whole number \(N\) using an unsigned 32-bit 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 the number of bytes of \(N\). The lower 23 bits are the mantissa. Bit number 24 represents the sign of \(N\).
\[ N = -1^{sign} \times mantissa \times 256^{exponent-3} \]
encodeCompact :: Integer -> Word32 Source #
Encode an Integer
to the compact number format used in the difficulty
target of a block.
Block Header Chain
class Monad m => BlockHeaders m where Source #
Typeclass for block header chain storage monad.
addBlockHeader :: BlockNode -> m () Source #
Add a new BlockNode
to the chain. Does not validate.
getBlockHeader :: BlockHash -> m (Maybe BlockNode) Source #
getBestBlockHeader :: m BlockNode Source #
Locate the BlockNode
for the highest block in the chain
setBestBlockHeader :: BlockNode -> m () Source #
Set the highest block in the chain.
addBlockHeaders :: [BlockNode] -> m () Source #
Add a continuous bunch of block headers the chain. Does not validate.
Instances
Monad m => BlockHeaders (StateT HeaderMemory m) Source # | |
Defined in Network.Haskoin.Block.Headers addBlockHeader :: BlockNode -> StateT HeaderMemory m () Source # getBlockHeader :: BlockHash -> StateT HeaderMemory m (Maybe BlockNode) Source # getBestBlockHeader :: StateT HeaderMemory m BlockNode Source # setBestBlockHeader :: BlockNode -> StateT HeaderMemory m () Source # addBlockHeaders :: [BlockNode] -> StateT HeaderMemory m () Source # |
Data structure representing a block header and its position in the block chain.
BlockNode | non-Genesis block header |
| |
GenesisNode | Genesis block header |
|
Instances
data HeaderMemory Source #
Memory-based header tree.
Instances
type BlockMap = HashMap ShortBlockHash ShortByteString Source #
Memory-based map to a serialized BlockNode
data structure.
ShortByteString
is used to avoid memory fragmentation and make the data
structure compact.
getAncestor :: BlockHeaders m => BlockHeight -> BlockNode -> m (Maybe BlockNode) Source #
Get the ancestor of the provided BlockNode
at the specified
BlockHeight
.
initialChain :: Network -> HeaderMemory Source #
Initialize memory-based chain.
genesisMap :: Network -> BlockMap Source #
Initialize map for memory-based chain.
:: BlockHeaders m | |
=> Network | |
-> Timestamp | current time |
-> [BlockHeader] | |
-> m (Either String [BlockNode]) |
Validate a list of continuous block headers and import them to the
block chain. Return Left
on failure with error information.
:: BlockHeaders m | |
=> Network | |
-> Timestamp | current time |
-> BlockHeader | |
-> m (Either String BlockNode) |
Validate and connect single block header to the block chain. Return Left
if fails
to be validated.
parentBlock :: BlockHeaders m => BlockHeader -> m (Maybe BlockNode) Source #
Block's parent. If the block header is in the store, its parent must also be there. No block header get deleted or pruned from the store.
splitPoint :: BlockHeaders m => BlockNode -> BlockNode -> m BlockNode Source #
Find the last common block ancestor between provided block headers.
blockLocator :: BlockHeaders m => BlockNode -> m BlockLocator Source #
Get block locator.
Merkle Blocks
data MerkleBlock Source #
Filtered block: a block with a partial Merkle tree that only includes the transactions that pass a bloom filter that was negotiated.
MerkleBlock | |
|
Instances
type MerkleRoot = Hash256 Source #
Hash of the block's Merkle root.
type FlagBits = [Bool] Source #
Bits that are used to rebuild partial merkle tree transaction hash list.
type PartialMerkleTree = [Hash256] Source #
Partial Merkle tree for a filtered block.
:: [TxHash] | transaction hashes (leaf nodes) |
-> MerkleRoot | root of the Merkle tree |
Computes the root of a Merkle tree from a list of leaf node hashes.
:: [(TxHash, Bool)] | transaction hash and whether to include |
-> (FlagBits, PartialMerkleTree) | flag bits and partial Merkle tree |
Build a partial Merkle tree. Provide a list of tuples with all transaction hashes in the block, and whether the transaction is to be included in the partial tree. Returns a flag bits structure and the computed partial Merkle tree.
merkleBlockTxs :: Network -> MerkleBlock -> Either String [TxHash] Source #
Get matching transactions from Merkle block.
testMerkleRoot :: Network -> MerkleBlock -> Bool Source #
Check if Merkle block root is valid against the block header.