public class PartialMerkleTree extends Message
A data structure that contains proofs of block inclusion for one or more transactions, in an efficient manner.
The encoding works as follows: we traverse the tree in depth-first order, storing a bit for each traversed node, signifying whether the node is the parent of at least one matched leaf txid (or a matched txid itself). In case we are at the leaf level, or this bit is 0, its merkle node hash is stored, and its children are not explored further. Otherwise, no hash is stored, but we recurse into both (or the only) child branch. During decoding, the same depth-first traversal is performed, consuming bits and hashes as they were written during encoding.
The serialization is fixed and provides a hard guarantee about the encoded size, SIZE <= 10 + ceil(32.25*N) where N represents the number of leaf nodes of the partial tree. N itself is bounded by:
N <= total_transactions
N <= 1 + matched_transactions*tree_height
The serialization format: - uint32 total_transactions (4 bytes) - varint number of hashes (1-3 bytes) - uint256[] hashes in depth-first order (<= 32*N bytes) - varint number of bytes of flag bits (1-3 bytes) - byte[] flag bits, packed per 8 in a byte, least significant bit first (<= 2*N-1 bits) The size constraints follow from this.
Message.LazyParseException
bytes, checksum, cursor, length, MAX_SIZE, offset, params, parsed, parseLazy, parseRetain, protocolVersion, recached, UNKNOWN_LENGTH
Constructor and Description |
---|
PartialMerkleTree(NetworkParameters params,
byte[] payloadBytes,
int offset) |
Modifier and Type | Method and Description |
---|---|
void |
bitcoinSerializeToStream(OutputStream stream)
Serializes this message to the provided stream.
|
Sha256Hash |
getTxnHashAndMerkleRoot(List<Sha256Hash> matchedHashes)
Extracts tx hashes that are in this merkle tree
and returns the merkle root of this tree.
|
protected void |
parseLite()
Perform the most minimal parse possible to calculate the length of the message.
|
adjustLength, bitcoinSerialize, bitcoinSerialize, ensureParsed, getHash, getMessageSize, getParams, isCached, isParsed, isRecached, maybeParse, unCache, unsafeBitcoinSerialize
public PartialMerkleTree(NetworkParameters params, byte[] payloadBytes, int offset) throws ProtocolException
ProtocolException
public void bitcoinSerializeToStream(OutputStream stream) throws IOException
Message
IOException
protected void parseLite()
Message
public Sha256Hash getTxnHashAndMerkleRoot(List<Sha256Hash> matchedHashes) throws VerificationException
matchedHashes
- A list which will contain the matched txn (will be cleared)
Required to be a LinkedHashSet in order to retain order or transactions in the blockProtocolException
- if this partial merkle tree is invalidVerificationException
Copyright © 2014. All rights reserved.