Copyright | No rights reserved |
---|---|
License | MIT |
Maintainer | jprupp@protonmail.ch |
Stability | experimental |
Portability | POSIX |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Function to deal with Merkle trees inside blocks.
Synopsis
- data MerkleBlock = MerkleBlock {
- header :: !BlockHeader
- txn :: !Word32
- hashes :: !PartialMerkleTree
- flags :: !FlagBits
- type MerkleRoot = Hash256
- type FlagBits = [Bool]
- type PartialMerkleTree = [Hash256]
- buildMerkleRoot :: [TxHash] -> MerkleRoot
- merkleBlockTxs :: Network -> MerkleBlock -> Either String [TxHash]
- testMerkleRoot :: Network -> MerkleBlock -> Bool
- buildPartialMerkle :: [(TxHash, Bool)] -> (FlagBits, PartialMerkleTree)
- decodeMerkleFlags :: [Word8] -> FlagBits
- encodeMerkleFlags :: FlagBits -> [Word8]
- calcTreeHeight :: Int -> Int
- calcTreeWidth :: Int -> Int -> Int
- hash2 :: Hash256 -> Hash256 -> Hash256
- calcHash :: Int -> Int -> [TxHash] -> Hash256
- traverseAndBuild :: Int -> Int -> [(TxHash, Bool)] -> (FlagBits, PartialMerkleTree)
- traverseAndExtract :: Int -> Int -> Int -> FlagBits -> PartialMerkleTree -> Maybe (MerkleRoot, [TxHash], Int, Int)
- extractMatches :: Network -> FlagBits -> PartialMerkleTree -> Int -> Either String (MerkleRoot, [TxHash])
- splitIn :: Int -> [a] -> [[a]]
- boolsToWord8 :: [Bool] -> Word8
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.
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.
:: [(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.
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.
:: Int | height of the node |
-> Int | position of the node (0 for the leftmost node) |
-> [TxHash] | transaction hashes (leaf nodes) |
-> Hash256 | hash of the node at the specified position |
Computes the hash of a specific node in a Merkle tree.
traverseAndBuild :: Int -> Int -> [(TxHash, Bool)] -> (FlagBits, PartialMerkleTree) Source #
Helper function to build partial Merkle tree. Used by buildPartialMerkle
above.
traverseAndExtract :: Int -> Int -> Int -> FlagBits -> PartialMerkleTree -> Maybe (MerkleRoot, [TxHash], Int, Int) Source #
Helper function to extract transaction hashes from partial Merkle tree.
:: Network | |
-> FlagBits | |
-> PartialMerkleTree | |
-> Int | number of transaction at height 0 (leaf nodes) |
-> Either String (MerkleRoot, [TxHash]) | Merkle root and 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
.
splitIn :: Int -> [a] -> [[a]] Source #
Helper function to split a list in chunks Int
length. Last chunk may be
smaller.
boolsToWord8 :: [Bool] -> Word8 Source #
Pack up to eight bools in a byte.