Retrieving a MerkleBlock#
For the merkleblock message documentation on the reference page, an actual merkle block was retrieved from the network and manually processed. This section walks through each step of the process, demonstrating basic network communication and merkle block processing.
#!/usr/bin/env python3
from time import sleep
from hashlib import sha256
import struct
import sys
network_string = bytes.fromhex("bf0c6bbd") # Mainnet
def send(msg,payload):
## Command is ASCII text, null padded to 12 bytes
command = msg.encode() + ( ( 12 - len(msg) ) * b"\00" )
## Payload length is a uint32_t
payload_raw = bytes.fromhex(payload)
payload_len = struct.pack("<I", len(payload_raw))
## Checksum is first 4 bytes of SHA256(SHA256(<payload>))
checksum = sha256(sha256(payload_raw).digest()).digest()[:4]
sys.stdout.buffer.write(
network_string
+ command
+ payload_len
+ checksum
+ payload_raw
)
sys.stdout.flush()
To connect to the P2P network, the trivial Python function above was developed to compute message headers and send payloads decoded from hex. The message header starts with the mainnet start string. To connect to a testnet or regtest node instead, change it to cee2caff or fcc1b7dc and connect to that network’s port (19999 or 19899).
## Create a version message
send("version",
"62120100" # ........................ Protocol Version: 70242
+ "0000000000000000" # ................ Services: Headers Only (SPV)
+ "c6925e5400000000" # ................ Time: 1415484102
+ "00000000000000000000000000000000"
+ "0000ffff7f000001270f" # ............ Receiver IP Address/Port
+ "00000000000000000000000000000000"
+ "0000ffff7f000001270f" # ............ Sender IP Address/Port
+ "0000000000000000" # ................ Nonce (not used here)
+ "17" # .............................. Bytes in version string
+ "2f4461736820446f6373204578616d70"
+ "6c653a312e302f" # .................. Version string
+ "93050500" # ........................ Starting block height: 329107
+ "00" # .............................. Relay transactions: false
)
Peers on the network will not accept any requests until you send them a version message. Dash Core disconnects peers whose protocol version is below its minimum supported version (MIN_PEER_PROTO_VERSION in src/version.h). The receiving node will reply with their version message and a verack message.
sleep(1)
send("verack", "")
We’re not going to validate their version message with this simple script, but we will sleep a short bit and send back our own verack message as if we had accepted their version message.
send("filterload",
"02" # ........ Filter bytes: 2
+ "b50f" # ....... Filter: 1010 1101 1111 0000
+ "0b000000" # ... nHashFuncs: 11
+ "00000000" # ... nTweak: 0/none
+ "00" # ......... nFlags: BLOOM_UPDATE_NONE
)
We set a bloom filter with the filterload message. This filter is described in the preceding sections.
## Block header hash from the command line, converted from RPC byte
## order to internal byte order
block_hash = bytes.fromhex(sys.argv[1])[::-1].hex()
send("getdata",
"01" # ................................. Number of inventories: 1
+ "03000000" # ........................... Inventory type: filtered block
+ block_hash # ........................... Block header hash
)
We request a merkle block for transactions matching our filter, completing our script. The node only returns blocks it has, so the script takes the block header hash as a command-line argument. RPCs such as getbestblockhash display hashes in RPC byte order, and P2P messages use internal byte order, so the script reverses the bytes. The node replies with a merkleblock message even if no transactions in the block match the filter.
To run the script, we simply pipe it to the Unix netcat command or one of its many clones, one of which is available for practically any platform. For example, with the original netcat and using hexdump (hd) to display the output:
## Connect to the Dash Core peer running on localhost
python3 get-merkle.py "$(dash-cli getbestblockhash)" | nc localhost 9999 | hd
The Parsing a MerkleBlock section walks through part of an example response.