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.
Instances of this class are not safe for use by multiple threads.
cursor, length, MAX_SIZE, offset, params, payload, protocolVersion, recached, serializer, UNKNOWN_LENGTH
Constructor and Description |
---|
PartialMerkleTree(NetworkParameters params,
byte[] payloadBytes,
int offset) |
PartialMerkleTree(NetworkParameters params,
byte[] bits,
java.util.List<Sha256Hash> hashes,
int origTxCount)
Constructs a new PMT with the given bit set (little endian) and the raw list of hashes including internal hashes,
taking ownership of the list.
|
Modifier and Type | Method and Description |
---|---|
void |
bitcoinSerializeToStream(java.io.OutputStream stream)
Serializes this message to the provided stream.
|
static PartialMerkleTree |
buildFromLeaves(NetworkParameters params,
byte[] includeBits,
java.util.List<Sha256Hash> allLeafHashes)
Calculates a PMT given the list of leaf hashes and which leaves need to be included.
|
boolean |
equals(java.lang.Object o) |
int |
getTransactionCount() |
Sha256Hash |
getTxnHashAndMerkleRoot(java.util.List<Sha256Hash> matchedHashesOut)
Extracts tx hashes that are in this merkle tree
and returns the merkle root of this tree.
|
int |
hashCode() |
protected void |
parse() |
java.lang.String |
toString() |
adjustLength, bitcoinSerialize, bitcoinSerialize, getHash, getMessageSize, getParams, hasMoreBytes, isCached, isRecached, readByteArray, readBytes, readHash, readInt64, readStr, readUint32, readUint64, readVarInt, readVarInt, unCache, unsafeBitcoinSerialize
public PartialMerkleTree(NetworkParameters params, byte[] payloadBytes, int offset) throws ProtocolException
ProtocolException
public PartialMerkleTree(NetworkParameters params, byte[] bits, java.util.List<Sha256Hash> hashes, int origTxCount)
public static PartialMerkleTree buildFromLeaves(NetworkParameters params, byte[] includeBits, java.util.List<Sha256Hash> allLeafHashes)
public void bitcoinSerializeToStream(java.io.OutputStream stream) throws java.io.IOException
Message
bitcoinSerializeToStream
in class Message
java.io.IOException
protected void parse() throws ProtocolException
parse
in class Message
ProtocolException
public Sha256Hash getTxnHashAndMerkleRoot(java.util.List<Sha256Hash> matchedHashesOut) throws VerificationException
matchedHashesOut
- A list which will contain the matched txn (will be cleared).ProtocolException
- if this partial merkle tree is invalidVerificationException
public int getTransactionCount()
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object