#!/usr/bin/env python3 # Copyright (c) 2010 ArtForz -- public domain half-a-node # Copyright (c) 2012 Jeff Garzik # Copyright (c) 2010-2016 The Bitcoin Core developers # Copyright (c) 2017-2022 The Zcash developers # Distributed under the MIT software license, see the accompanying # file COPYING or https://www.opensource.org/licenses/mit-license.php . # # mininode.py - Bitcoin P2P network half-a-node # # This python code was modified from ArtForz' public domain half-a-node, as # found in the mini-node branch of https://github.com/jgarzik/pynode. # # NodeConn: an object which manages p2p connectivity to a bitcoin node # NodeConnCB: a base class that describes the interface for receiving # callbacks with network messages from a NodeConn # CBlock, CTransaction, CBlockHeader, CTxIn, CTxOut, etc....: # data structures that should map to corresponding structures in # bitcoin/primitives # msg_block, msg_tx, msg_headers, etc.: # data structures that represent network messages # ser_*, deser_*: functions that handle serialization/deserialization import struct import socket import asyncore import time import sys import random from binascii import hexlify from io import BytesIO from codecs import encode import hashlib from threading import RLock from threading import Thread import logging import copy from hashlib import blake2b from .equihash import ( gbp_basic, gbp_validate, hash_nonce, zcash_person, ) from .util import bytes_to_hex_str BIP0031_VERSION = 60000 SPROUT_PROTO_VERSION = 170002 # past bip-31 for ping/pong OVERWINTER_PROTO_VERSION = 170003 SAPLING_PROTO_VERSION = 170006 BLOSSOM_PROTO_VERSION = 170008 NU5_PROTO_VERSION = 170050 # NU6_PROTO_VERSION = 170110 MY_SUBVERSION = b"/python-mininode-tester:0.0.3/" SPROUT_VERSION_GROUP_ID = 0x00000000 OVERWINTER_VERSION_GROUP_ID = 0x03C48270 SAPLING_VERSION_GROUP_ID = 0x892F2085 ZIP225_VERSION_GROUP_ID = 0x26A7270A # No transaction format change in Blossom. MAX_INV_SZ = 50000 COIN = 100000000 # 1 zec in zatoshis BLOSSOM_POW_TARGET_SPACING_RATIO = 2 # The placeholder value used for the auth digest of pre-v5 transactions. LEGACY_TX_AUTH_DIGEST = (1 << 256) - 1 # Keep our own socket map for asyncore, so that we can track disconnects # ourselves (to workaround an issue with closing an asyncore socket when # using select) mininode_socket_map = dict() # One lock for synchronizing all data access between the networking thread (see # NetworkThread below) and the thread running the test logic. For simplicity, # NodeConn acquires this lock whenever delivering a message to a NodeConnCB, # and whenever adding anything to the send buffer (in send_message()). This # lock should be acquired in the thread running the test logic to synchronize # access to any data shared with the NodeConnCB or NodeConn. mininode_lock = RLock() # Serialization/deserialization tools def sha256(s): return hashlib.new('sha256', s).digest() def hash256(s): return sha256(sha256(s)) def nuparams(branch_id, height): return '-nuparams=%x:%d' % (branch_id, height) def fundingstream(idx, start_height, end_height, addrs): return '-fundingstream=%d:%d:%d:%s' % (idx, start_height, end_height, ",".join(addrs)) def ser_compactsize(n): if n < 253: return struct.pack("B", n) elif n < 0x10000: return struct.pack(">= 32 return rs def uint256_from_str(s): r = 0 t = struct.unpack("> 24) & 0xFF v = (c & 0xFFFFFF) << (8 * (nbytes - 3)) return v def block_work_from_compact(c): target = uint256_from_compact(c) return 2**256 // (target + 1) def deser_vector(f, c): nit = struct.unpack("H", f.read(2))[0] def serialize(self): r = b"" r += struct.pack("H", self.port) return r def __repr__(self): return "CAddress(nServices=%i ip=%s port=%i)" % (self.nServices, self.ip, self.port) class CInv(object): typemap = { 0: b"Error", 1: b"TX", 2: b"Block", 5: b"WTX", } def __init__(self, t=0, h=0, h_aux=0): self.type = t self.hash = h self.hash_aux = h_aux if self.type == 1: self.hash_aux = LEGACY_TX_AUTH_DIGEST def deserialize(self, f): self.type = struct.unpack(" 0: flags = struct.unpack("B", f.read(1))[0] self.enableSpends = (flags & ORCHARD_FLAGS_ENABLE_SPENDS) != 0 self.enableOutputs = (flags & ORCHARD_FLAGS_ENABLE_OUTPUTS) != 0 self.valueBalance = struct.unpack(" 0: r += struct.pack("B", self.flags()) r += struct.pack(" 0 if has_sapling: self.valueBalance = struct.unpack(" 0: self.anchor = deser_uint256(f) for i in range(len(self.spends)): self.spends[i].zkproof = Groth16Proof() self.spends[i].zkproof.deserialize(f) for i in range(len(self.spends)): self.spends[i].spendAuthSig = RedJubjubSignature() self.spends[i].spendAuthSig.deserialize(f) for i in range(len(self.outputs)): self.outputs[i].zkproof = Groth16Proof() self.outputs[i].zkproof.deserialize(f) if has_sapling: self.bindingSig = RedJubjubSignature() self.bindingSig.deserialize(f) def serialize(self): r = b"" r += ser_vector(self.spends) r += ser_vector(self.outputs) has_sapling = (len(self.spends) + len(self.outputs)) > 0 if has_sapling: r += struct.pack(" 0: r += ser_uint256(self.anchor) for spend in self.spends: r += spend.zkproof.serialize() for spend in self.spends: r += spend.spendAuthSig.serialize() for output in self.outputs: r += output.zkproof.serialize() if has_sapling: r += self.bindingSig.serialize() return r def __repr__(self): return "SaplingBundle(spends=%r, outputs=%r, valueBalance=%i, bindingSig=%064x)" \ % ( self.spends, self.outputs, self.valueBalance, self.bindingSig, ) G1_PREFIX_MASK = 0x02 G2_PREFIX_MASK = 0x0a class ZCProof(object): def __init__(self): self.g_A = None self.g_A_prime = None self.g_B = None self.g_B_prime = None self.g_C = None self.g_C_prime = None self.g_K = None self.g_H = None def deserialize(self, f): def deser_g1(self, f): leadingByte = struct.unpack("> 31) self.nVersion = header & 0x7FFFFFFF self.nVersionGroupId = (struct.unpack("= 2: self.vJoinSplit = deser_vector(f, JSDescription) if len(self.vJoinSplit) > 0: self.joinSplitPubKey = deser_uint256(f) self.joinSplitSig = f.read(64) if isSaplingV4 and not (len(self.shieldedSpends) == 0 and len(self.shieldedOutputs) == 0): self.bindingSig = RedJubjubSignature() self.bindingSig.deserialize(f) self.sha256 = None self.hash = None def serialize(self): header = (int(self.fOverwintered)<<31) | self.nVersion isOverwinterV3 = (self.fOverwintered and self.nVersionGroupId == OVERWINTER_VERSION_GROUP_ID and self.nVersion == 3) isSaplingV4 = (self.fOverwintered and self.nVersionGroupId == SAPLING_VERSION_GROUP_ID and self.nVersion == 4) isNu5V5 = (self.fOverwintered and self.nVersionGroupId == ZIP225_VERSION_GROUP_ID and self.nVersion == 5) if isNu5V5: r = b"" # Common transaction fields r += struct.pack("= 2: r += ser_vector(self.vJoinSplit) if len(self.vJoinSplit) > 0: r += ser_uint256(self.joinSplitPubKey) r += self.joinSplitSig if isSaplingV4 and not (len(self.shieldedSpends) == 0 and len(self.shieldedOutputs) == 0): r += self.bindingSig.serialize() return r def rehash(self): self.sha256 = None self.calc_sha256() def calc_sha256(self): if self.nVersion >= 5: from . import zip244 txid = zip244.txid_digest(self) self.auth_digest = zip244.auth_digest(self) else: txid = hash256(self.serialize()) self.auth_digest = b'\xFF'*32 if self.sha256 is None: self.sha256 = uint256_from_str(txid) self.hash = encode(txid[::-1], 'hex_codec').decode('ascii') self.auth_digest_hex = encode(self.auth_digest[::-1], 'hex_codec').decode('ascii') def is_valid(self): self.calc_sha256() for tout in self.vout: if tout.nValue < 0 or tout.nValue > 21000000 * 100000000: return False return True def __repr__(self): r = ("CTransaction(fOverwintered=%r nVersion=%i nVersionGroupId=0x%08x " "vin=%r vout=%r nLockTime=%i nExpiryHeight=%i " "valueBalance=%i shieldedSpends=%r shieldedOutputs=%r" % (self.fOverwintered, self.nVersion, self.nVersionGroupId, self.vin, self.vout, self.nLockTime, self.nExpiryHeight, self.valueBalance, self.shieldedSpends, self.shieldedOutputs)) if self.nVersion >= 2: r += " vJoinSplit=%r" % (self.vJoinSplit,) if len(self.vJoinSplit) > 0: r += " joinSplitPubKey=%064x joinSplitSig=%s" \ % (self.joinSplitPubKey, bytes_to_hex_str(self.joinSplitSig)) if len(self.shieldedSpends) > 0 or len(self.shieldedOutputs) > 0: r += " bindingSig=%r" % self.bindingSig r += ")" return r class CBlockHeader(object): def __init__(self, header=None): if header is None: self.set_null() else: self.nVersion = header.nVersion self.hashPrevBlock = header.hashPrevBlock self.hashMerkleRoot = header.hashMerkleRoot self.hashBlockCommitments = header.hashBlockCommitments self.nTime = header.nTime self.nBits = header.nBits self.nNonce = header.nNonce self.nSolution = header.nSolution self.sha256 = header.sha256 self.hash = header.hash self.calc_sha256() def set_null(self): self.nVersion = 4 self.hashPrevBlock = 0 self.hashMerkleRoot = 0 self.hashBlockCommitments = 0 self.nTime = 0 self.nBits = 0 self.nNonce = 0 self.nSolution = [] self.sha256 = None self.hash = None def deserialize(self, f): self.nVersion = struct.unpack(" 1: newhashes = [] for i in range(0, len(hashes), 2): i2 = min(i+1, len(hashes)-1) newhashes.append(hash256(hashes[i] + hashes[i2])) hashes = newhashes return uint256_from_str(hashes[0]) def calc_auth_data_root(self): hashes = [] nleaves = 0 for tx in self.vtx: tx.calc_sha256() hashes.append(tx.auth_digest) nleaves += 1 # Continue adding leaves (of zeros) until reaching a power of 2 while nleaves & (nleaves-1) > 0: hashes.append(b'\x00'*32) nleaves += 1 while len(hashes) > 1: newhashes = [] for i in range(0, len(hashes), 2): digest = blake2b(digest_size=32, person=b'ZcashAuthDatHash') digest.update(hashes[i]) digest.update(hashes[i+1]) newhashes.append(digest.digest()) hashes = newhashes return uint256_from_str(hashes[0]) def is_valid(self, n=48, k=5): # H(I||... digest = blake2b(digest_size=(512//n)*n//8, person=zcash_person(n, k)) digest.update(super(CBlock, self).serialize()[:108]) hash_nonce(digest, self.nNonce) if not gbp_validate(self.nSolution, digest, n, k): return False self.calc_sha256() target = uint256_from_compact(self.nBits) if self.sha256 > target: return False for tx in self.vtx: if not tx.is_valid(): return False if self.calc_merkle_root() != self.hashMerkleRoot: return False return True def solve(self, n=48, k=5): target = uint256_from_compact(self.nBits) # H(I||... digest = blake2b(digest_size=(512//n)*n//8, person=zcash_person(n, k)) digest.update(super(CBlock, self).serialize()[:108]) self.nNonce = 0 while True: # H(I||V||... curr_digest = digest.copy() hash_nonce(curr_digest, self.nNonce) # (x_1, x_2, ...) = A(I, V, n, k) solns = gbp_basic(curr_digest, n, k) for soln in solns: assert(gbp_validate(curr_digest, soln, n, k)) self.nSolution = soln self.rehash() if self.sha256 <= target: return self.nNonce += 1 def __repr__(self): return "CBlock(nVersion=%i hashPrevBlock=%064x hashMerkleRoot=%064x hashBlockCommitments=%064x nTime=%s nBits=%08x nNonce=%064x nSolution=%r vtx=%r)" \ % (self.nVersion, self.hashPrevBlock, self.hashMerkleRoot, self.hashBlockCommitments, time.ctime(self.nTime), self.nBits, self.nNonce, self.nSolution, self.vtx) class CUnsignedAlert(object): def __init__(self): self.nVersion = 1 self.nRelayUntil = 0 self.nExpiration = 0 self.nID = 0 self.nCancel = 0 self.setCancel = [] self.nMinVer = 0 self.nMaxVer = 0 self.setSubVer = [] self.nPriority = 0 self.strComment = b"" self.strStatusBar = b"" self.strReserved = b"" def deserialize(self, f): self.nVersion = struct.unpack("= 106: self.addrFrom = CAddress() self.addrFrom.deserialize(f) self.nNonce = struct.unpack("= 209: self.nStartingHeight = struct.unpack(" class msg_headers(object): command = b"headers" def __init__(self): self.headers = [] def deserialize(self, f): # comment in bitcoind indicates these should be deserialized as blocks blocks = deser_vector(f, CBlock) for x in blocks: self.headers.append(CBlockHeader(x)) def serialize(self): blocks = [CBlock(x) for x in self.headers] return ser_vector(blocks) def __repr__(self): return "msg_headers(headers=%s)" % repr(self.headers) class msg_reject(object): command = b"reject" REJECT_MALFORMED = 1 def __init__(self): self.message = b"" self.code = 0 self.reason = b"" self.data = 0 def deserialize(self, f): self.message = deser_string(f) self.code = struct.unpack("= 209: conn.send_message(msg_verack()) conn.ver_send = min(SPROUT_PROTO_VERSION, message.nVersion) if message.nVersion < 209: conn.ver_recv = conn.ver_send def on_verack(self, conn, message): conn.ver_recv = conn.ver_send self.verack_received = True def on_inv(self, conn, message): want = msg_getdata() for i in message.inv: if i.type != 0: want.inv.append(i) if len(want.inv): conn.send_message(want) def on_addr(self, conn, message): pass def on_alert(self, conn, message): pass def on_getdata(self, conn, message): pass def on_notfound(self, conn, message): pass def on_getblocks(self, conn, message): pass def on_tx(self, conn, message): pass def on_block(self, conn, message): pass def on_getaddr(self, conn, message): pass def on_headers(self, conn, message): pass def on_getheaders(self, conn, message): pass def on_ping(self, conn, message): if conn.ver_send > BIP0031_VERSION: conn.send_message(msg_pong(message.nonce)) def on_reject(self, conn, message): pass def on_close(self, conn): pass def on_mempool(self, conn): pass def on_pong(self, conn, message): pass # The actual NodeConn class # This class provides an interface for a p2p connection to a specified node class NodeConn(asyncore.dispatcher): messagemap = { b"version": msg_version, b"verack": msg_verack, b"addr": msg_addr, b"alert": msg_alert, b"inv": msg_inv, b"getdata": msg_getdata, b"notfound": msg_notfound, b"getblocks": msg_getblocks, b"tx": msg_tx, b"block": msg_block, b"getaddr": msg_getaddr, b"ping": msg_ping, b"pong": msg_pong, b"headers": msg_headers, b"getheaders": msg_getheaders, b"reject": msg_reject, b"mempool": msg_mempool } MAGIC_BYTES = { "mainnet": b"\x24\xe9\x27\x64", # mainnet "testnet3": b"\xfa\x1a\xf9\xbf", # testnet3 "regtest": b"\xaa\xe8\x3f\x5f" # regtest } def __init__(self, dstaddr, dstport, rpc, callback, net="regtest", protocol_version=SAPLING_PROTO_VERSION): asyncore.dispatcher.__init__(self, map=mininode_socket_map) self.log = logging.getLogger("NodeConn(%s:%d)" % (dstaddr, dstport)) self.dstaddr = dstaddr self.dstport = dstport self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.sendbuf = b"" self.recvbuf = b"" self.ver_send = 209 self.ver_recv = 209 self.last_sent = 0 self.state = "connecting" self.network = net self.cb = callback self.disconnect = False # stuff version msg into sendbuf vt = msg_version(protocol_version) vt.addrTo.ip = self.dstaddr vt.addrTo.port = self.dstport vt.addrFrom.ip = "0.0.0.0" vt.addrFrom.port = 0 self.send_message(vt, True) print('MiniNode: Connecting to Bitcoin Node IP # ' + dstaddr + ':' \ + str(dstport) + ' using version ' + str(protocol_version)) try: self.connect((dstaddr, dstport)) except: self.handle_close() self.rpc = rpc def show_debug_msg(self, msg): self.log.debug(msg) def handle_connect(self): self.show_debug_msg("MiniNode: Connected & Listening: \n") self.state = b"connected" def handle_close(self): self.show_debug_msg("MiniNode: Closing Connection to %s:%d... " % (self.dstaddr, self.dstport)) self.state = b"closed" self.recvbuf = b"" self.sendbuf = b"" try: self.close() except: pass self.cb.on_close(self) def handle_read(self): try: t = self.recv(8192) if len(t) > 0: self.recvbuf += t self.got_data() except: pass def readable(self): return True def writable(self): with mininode_lock: length = len(self.sendbuf) return (length > 0) def handle_write(self): with mininode_lock: try: sent = self.send(self.sendbuf) except: self.handle_close() return self.sendbuf = self.sendbuf[sent:] def got_data(self): try: while True: if len(self.recvbuf) < 4: return if self.recvbuf[:4] != self.MAGIC_BYTES[self.network]: raise ValueError("got garbage %r" % (self.recvbuf,)) if self.ver_recv < 209: if len(self.recvbuf) < 4 + 12 + 4: return command = self.recvbuf[4:4+12].split(b"\x00", 1)[0] msglen = struct.unpack("= 209: th = sha256(data) h = sha256(th) tmsg += h[:4] tmsg += data with mininode_lock: self.sendbuf += tmsg self.last_sent = time.time() def got_message(self, message): if message.command == b"version": if message.nVersion <= BIP0031_VERSION: self.messagemap[b'ping'] = msg_ping_prebip31 if self.last_sent + 30 * 60 < time.time(): self.send_message(self.messagemap[b'ping']()) self.show_debug_msg("Recv %s" % repr(message)) self.cb.deliver(self, message) def disconnect_node(self): self.disconnect = True class NetworkThread(Thread): def run(self): while mininode_socket_map: # We check for whether to disconnect outside of the asyncore # loop to workaround the behavior of asyncore when using # select disconnected = [] for fd, obj in mininode_socket_map.items(): if obj.disconnect: disconnected.append(obj) [ obj.handle_close() for obj in disconnected ] asyncore.loop(0.1, use_poll=True, map=mininode_socket_map, count=1) # An exception we can raise if we detect a potential disconnect # (p2p or rpc) before the test is complete class EarlyDisconnectError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value)