/ test / functional / test_framework / messages.py
messages.py
   1  #!/usr/bin/env python3
   2  # Copyright (c) 2010 ArtForz -- public domain half-a-node
   3  # Copyright (c) 2012 Jeff Garzik
   4  # Copyright (c) 2010-2022 The Bitcoin Core developers
   5  # Distributed under the MIT software license, see the accompanying
   6  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
   7  """Bitcoin test framework primitive and message structures
   8  
   9  CBlock, CTransaction, CBlockHeader, CTxIn, CTxOut, etc....:
  10      data structures that should map to corresponding structures in
  11      bitcoin/primitives
  12  
  13  msg_block, msg_tx, msg_headers, etc.:
  14      data structures that represent network messages
  15  
  16  ser_*, deser_*: functions that handle serialization/deserialization.
  17  
  18  Classes use __slots__ to ensure extraneous attributes aren't accidentally added
  19  by tests, compromising their intended effect.
  20  """
  21  from base64 import b32decode, b32encode
  22  import copy
  23  import hashlib
  24  from io import BytesIO
  25  import math
  26  import random
  27  import socket
  28  import time
  29  import unittest
  30  
  31  from test_framework.crypto.siphash import siphash256
  32  from test_framework.util import (
  33      assert_equal,
  34      assert_not_equal,
  35  )
  36  
  37  MAX_LOCATOR_SZ = 101
  38  MAX_BLOCK_WEIGHT = 4000000
  39  DEFAULT_BLOCK_RESERVED_WEIGHT = 8000
  40  MINIMUM_BLOCK_RESERVED_WEIGHT = 2000
  41  MAX_BLOOM_FILTER_SIZE = 36000
  42  MAX_BLOOM_HASH_FUNCS = 50
  43  
  44  COIN = 100000000  # 1 btc in satoshis
  45  MAX_MONEY = 21000000 * COIN
  46  
  47  MAX_BIP125_RBF_SEQUENCE = 0xfffffffd  # Sequence number that is rbf-opt-in (BIP 125) and csv-opt-out (BIP 68)
  48  MAX_SEQUENCE_NONFINAL = 0xfffffffe  # Sequence number that is csv-opt-out (BIP 68)
  49  SEQUENCE_FINAL = 0xffffffff  # Sequence number that disables nLockTime if set for every input of a tx
  50  
  51  MAX_PROTOCOL_MESSAGE_LENGTH = 4000000  # Maximum length of incoming protocol messages
  52  MAX_HEADERS_RESULTS = 2000  # Number of headers sent in one getheaders result
  53  MAX_INV_SIZE = 50000  # Maximum number of entries in an 'inv' protocol message
  54  
  55  NODE_NONE = 0
  56  NODE_NETWORK = (1 << 0)
  57  NODE_BLOOM = (1 << 2)
  58  NODE_WITNESS = (1 << 3)
  59  NODE_COMPACT_FILTERS = (1 << 6)
  60  NODE_NETWORK_LIMITED = (1 << 10)
  61  NODE_P2P_V2 = (1 << 11)
  62  
  63  MSG_TX = 1
  64  MSG_BLOCK = 2
  65  MSG_FILTERED_BLOCK = 3
  66  MSG_CMPCT_BLOCK = 4
  67  MSG_WTX = 5
  68  MSG_WITNESS_FLAG = 1 << 30
  69  MSG_TYPE_MASK = 0xffffffff >> 2
  70  MSG_WITNESS_TX = MSG_TX | MSG_WITNESS_FLAG
  71  
  72  FILTER_TYPE_BASIC = 0
  73  
  74  WITNESS_SCALE_FACTOR = 4
  75  
  76  DEFAULT_ANCESTOR_LIMIT = 25    # default max number of in-mempool ancestors
  77  DEFAULT_DESCENDANT_LIMIT = 25  # default max number of in-mempool descendants
  78  DEFAULT_CLUSTER_LIMIT = 64     # default max number of transactions in a cluster
  79  
  80  
  81  # Default setting for -datacarriersize.
  82  MAX_OP_RETURN_RELAY = 100_000
  83  
  84  
  85  DEFAULT_MEMPOOL_EXPIRY_HOURS = 336  # hours
  86  
  87  TX_MIN_STANDARD_VERSION = 1
  88  TX_MAX_STANDARD_VERSION = 3
  89  
  90  MAGIC_BYTES = {
  91      "mainnet": b"\xf9\xbe\xb4\xd9",
  92      "testnet4": b"\x1c\x16\x3f\x28",
  93      "regtest": b"\xfa\xbf\xb5\xda",
  94      "signet": b"\x0a\x03\xcf\x40",
  95  }
  96  
  97  def sha256(s):
  98      return hashlib.sha256(s).digest()
  99  
 100  
 101  def sha3(s):
 102      return hashlib.sha3_256(s).digest()
 103  
 104  
 105  def hash256(s):
 106      return sha256(sha256(s))
 107  
 108  
 109  def ser_compact_size(l):
 110      r = b""
 111      if l < 253:
 112          r = l.to_bytes(1, "little")
 113      elif l < 0x10000:
 114          r = (253).to_bytes(1, "little") + l.to_bytes(2, "little")
 115      elif l < 0x100000000:
 116          r = (254).to_bytes(1, "little") + l.to_bytes(4, "little")
 117      else:
 118          r = (255).to_bytes(1, "little") + l.to_bytes(8, "little")
 119      return r
 120  
 121  
 122  def deser_compact_size(f):
 123      nit = int.from_bytes(f.read(1), "little")
 124      if nit == 253:
 125          nit = int.from_bytes(f.read(2), "little")
 126      elif nit == 254:
 127          nit = int.from_bytes(f.read(4), "little")
 128      elif nit == 255:
 129          nit = int.from_bytes(f.read(8), "little")
 130      return nit
 131  
 132  
 133  def ser_varint(l):
 134      r = b""
 135      while True:
 136          r = bytes([(l & 0x7f) | (0x80 if len(r) > 0 else 0x00)]) + r
 137          if l <= 0x7f:
 138              return r
 139          l = (l >> 7) - 1
 140  
 141  
 142  def deser_varint(f):
 143      n = 0
 144      while True:
 145          dat = f.read(1)[0]
 146          n = (n << 7) | (dat & 0x7f)
 147          if (dat & 0x80) > 0:
 148              n += 1
 149          else:
 150              return n
 151  
 152  
 153  def deser_string(f):
 154      nit = deser_compact_size(f)
 155      return f.read(nit)
 156  
 157  
 158  def ser_string(s):
 159      return ser_compact_size(len(s)) + s
 160  
 161  
 162  def deser_uint256(f):
 163      return int.from_bytes(f.read(32), 'little')
 164  
 165  
 166  def ser_uint256(u):
 167      return u.to_bytes(32, 'little')
 168  
 169  
 170  def uint256_from_str(s):
 171      return int.from_bytes(s[:32], 'little')
 172  
 173  
 174  def uint256_from_compact(c):
 175      nbytes = (c >> 24) & 0xFF
 176      v = (c & 0xFFFFFF) << (8 * (nbytes - 3))
 177      return v
 178  
 179  
 180  # deser_function_name: Allow for an alternate deserialization function on the
 181  # entries in the vector.
 182  def deser_vector(f, c, deser_function_name=None):
 183      nit = deser_compact_size(f)
 184      r = []
 185      for _ in range(nit):
 186          t = c()
 187          if deser_function_name:
 188              getattr(t, deser_function_name)(f)
 189          else:
 190              t.deserialize(f)
 191          r.append(t)
 192      return r
 193  
 194  
 195  # ser_function_name: Allow for an alternate serialization function on the
 196  # entries in the vector (we use this for serializing the vector of transactions
 197  # for a witness block).
 198  def ser_vector(l, ser_function_name=None):
 199      r = ser_compact_size(len(l))
 200      for i in l:
 201          if ser_function_name:
 202              r += getattr(i, ser_function_name)()
 203          else:
 204              r += i.serialize()
 205      return r
 206  
 207  
 208  def deser_uint256_vector(f):
 209      nit = deser_compact_size(f)
 210      r = []
 211      for _ in range(nit):
 212          t = deser_uint256(f)
 213          r.append(t)
 214      return r
 215  
 216  
 217  def ser_uint256_vector(l):
 218      r = ser_compact_size(len(l))
 219      for i in l:
 220          r += ser_uint256(i)
 221      return r
 222  
 223  
 224  def deser_string_vector(f):
 225      nit = deser_compact_size(f)
 226      r = []
 227      for _ in range(nit):
 228          t = deser_string(f)
 229          r.append(t)
 230      return r
 231  
 232  
 233  def ser_string_vector(l):
 234      r = ser_compact_size(len(l))
 235      for sv in l:
 236          r += ser_string(sv)
 237      return r
 238  
 239  
 240  def deser_block_spent_outputs(f):
 241      nit = deser_compact_size(f)
 242      return [deser_vector(f, CTxOut) for _ in range(nit)]
 243  
 244  
 245  def from_hex(obj, hex_string):
 246      """Deserialize from a hex string representation (e.g. from RPC)
 247  
 248      Note that there is no complementary helper like e.g. `to_hex` for the
 249      inverse operation. To serialize a message object to a hex string, simply
 250      use obj.serialize().hex()"""
 251      obj.deserialize(BytesIO(bytes.fromhex(hex_string)))
 252      return obj
 253  
 254  
 255  def tx_from_hex(hex_string):
 256      """Deserialize from hex string to a transaction object"""
 257      return from_hex(CTransaction(), hex_string)
 258  
 259  
 260  def malleate_tx_to_invalid_witness(tx):
 261      """
 262      Create a malleated version of the tx where the witness is replaced with garbage data.
 263      Returns a CTransaction object.
 264      """
 265      tx_bad_wit = tx_from_hex(tx["hex"])
 266      tx_bad_wit.wit.vtxinwit = [CTxInWitness()]
 267      # Add garbage data to witness 0. We cannot simply strip the witness, as the node would
 268      # classify it as a transaction in which the witness was missing rather than wrong.
 269      tx_bad_wit.wit.vtxinwit[0].scriptWitness.stack = [b'garbage']
 270  
 271      assert_equal(tx["txid"], tx_bad_wit.txid_hex)
 272      assert_not_equal(tx["wtxid"], tx_bad_wit.wtxid_hex)
 273  
 274      return tx_bad_wit
 275  
 276  
 277  # like from_hex, but without the hex part
 278  def from_binary(cls, stream):
 279      """deserialize a binary stream (or bytes object) into an object"""
 280      # handle bytes object by turning it into a stream
 281      was_bytes = isinstance(stream, bytes)
 282      if was_bytes:
 283          stream = BytesIO(stream)
 284      obj = cls()
 285      obj.deserialize(stream)
 286      if was_bytes:
 287          assert len(stream.read()) == 0
 288      return obj
 289  
 290  
 291  # Objects that map to bitcoind objects, which can be serialized/deserialized
 292  
 293  
 294  class CAddress:
 295      __slots__ = ("net", "ip", "nServices", "port", "time")
 296  
 297      # see https://github.com/bitcoin/bips/blob/master/bip-0155.mediawiki
 298      NET_IPV4 = 1
 299      NET_IPV6 = 2
 300      NET_TORV3 = 4
 301      NET_I2P = 5
 302      NET_CJDNS = 6
 303  
 304      ADDRV2_NET_NAME = {
 305          NET_IPV4: "IPv4",
 306          NET_IPV6: "IPv6",
 307          NET_TORV3: "TorV3",
 308          NET_I2P: "I2P",
 309          NET_CJDNS: "CJDNS"
 310      }
 311  
 312      ADDRV2_ADDRESS_LENGTH = {
 313          NET_IPV4: 4,
 314          NET_IPV6: 16,
 315          NET_TORV3: 32,
 316          NET_I2P: 32,
 317          NET_CJDNS: 16
 318      }
 319  
 320      I2P_PAD = "===="
 321  
 322      def __init__(self):
 323          self.time = 0
 324          self.nServices = 1
 325          self.net = self.NET_IPV4
 326          self.ip = "0.0.0.0"
 327          self.port = 0
 328  
 329      def __eq__(self, other):
 330          return self.net == other.net and self.ip == other.ip and self.nServices == other.nServices and self.port == other.port and self.time == other.time
 331  
 332      def deserialize(self, f, *, with_time=True):
 333          """Deserialize from addrv1 format (pre-BIP155)"""
 334          if with_time:
 335              # VERSION messages serialize CAddress objects without time
 336              self.time = int.from_bytes(f.read(4), "little")
 337          self.nServices = int.from_bytes(f.read(8), "little")
 338          # We only support IPv4 which means skip 12 bytes and read the next 4 as IPv4 address.
 339          f.read(12)
 340          self.net = self.NET_IPV4
 341          self.ip = socket.inet_ntoa(f.read(4))
 342          self.port = int.from_bytes(f.read(2), "big")
 343  
 344      def serialize(self, *, with_time=True):
 345          """Serialize in addrv1 format (pre-BIP155)"""
 346          assert self.net == self.NET_IPV4
 347          r = b""
 348          if with_time:
 349              # VERSION messages serialize CAddress objects without time
 350              r += self.time.to_bytes(4, "little")
 351          r += self.nServices.to_bytes(8, "little")
 352          r += b"\x00" * 10 + b"\xff" * 2
 353          r += socket.inet_aton(self.ip)
 354          r += self.port.to_bytes(2, "big")
 355          return r
 356  
 357      def deserialize_v2(self, f):
 358          """Deserialize from addrv2 format (BIP155)"""
 359          self.time = int.from_bytes(f.read(4), "little")
 360  
 361          self.nServices = deser_compact_size(f)
 362  
 363          self.net = int.from_bytes(f.read(1), "little")
 364          assert self.net in self.ADDRV2_NET_NAME
 365  
 366          address_length = deser_compact_size(f)
 367          assert address_length == self.ADDRV2_ADDRESS_LENGTH[self.net]
 368  
 369          addr_bytes = f.read(address_length)
 370          if self.net == self.NET_IPV4:
 371              self.ip = socket.inet_ntoa(addr_bytes)
 372          elif self.net == self.NET_IPV6:
 373              self.ip = socket.inet_ntop(socket.AF_INET6, addr_bytes)
 374          elif self.net == self.NET_TORV3:
 375              prefix = b".onion checksum"
 376              version = bytes([3])
 377              checksum = sha3(prefix + addr_bytes + version)[:2]
 378              self.ip = b32encode(addr_bytes + checksum + version).decode("ascii").lower() + ".onion"
 379          elif self.net == self.NET_I2P:
 380              self.ip = b32encode(addr_bytes)[0:-len(self.I2P_PAD)].decode("ascii").lower() + ".b32.i2p"
 381          elif self.net == self.NET_CJDNS:
 382              self.ip = socket.inet_ntop(socket.AF_INET6, addr_bytes)
 383          else:
 384              raise Exception("Address type not supported")
 385  
 386          self.port = int.from_bytes(f.read(2), "big")
 387  
 388      def serialize_v2(self):
 389          """Serialize in addrv2 format (BIP155)"""
 390          assert self.net in self.ADDRV2_NET_NAME
 391          r = b""
 392          r += self.time.to_bytes(4, "little")
 393          r += ser_compact_size(self.nServices)
 394          r += self.net.to_bytes(1, "little")
 395          r += ser_compact_size(self.ADDRV2_ADDRESS_LENGTH[self.net])
 396          if self.net == self.NET_IPV4:
 397              r += socket.inet_aton(self.ip)
 398          elif self.net == self.NET_IPV6:
 399              r += socket.inet_pton(socket.AF_INET6, self.ip)
 400          elif self.net == self.NET_TORV3:
 401              sfx = ".onion"
 402              assert self.ip.endswith(sfx)
 403              r += b32decode(self.ip[0:-len(sfx)], True)[0:32]
 404          elif self.net == self.NET_I2P:
 405              sfx = ".b32.i2p"
 406              assert self.ip.endswith(sfx)
 407              r += b32decode(self.ip[0:-len(sfx)] + self.I2P_PAD, True)
 408          elif self.net == self.NET_CJDNS:
 409              r += socket.inet_pton(socket.AF_INET6, self.ip)
 410          else:
 411              raise Exception("Address type not supported")
 412          r += self.port.to_bytes(2, "big")
 413          return r
 414  
 415      def __repr__(self):
 416          return ("CAddress(nServices=%i net=%s addr=%s port=%i)"
 417                  % (self.nServices, self.ADDRV2_NET_NAME[self.net], self.ip, self.port))
 418  
 419  
 420  class CInv:
 421      __slots__ = ("hash", "type")
 422  
 423      typemap = {
 424          0: "Error",
 425          MSG_TX: "TX",
 426          MSG_BLOCK: "Block",
 427          MSG_TX | MSG_WITNESS_FLAG: "WitnessTx",
 428          MSG_BLOCK | MSG_WITNESS_FLAG: "WitnessBlock",
 429          MSG_FILTERED_BLOCK: "filtered Block",
 430          MSG_CMPCT_BLOCK: "CompactBlock",
 431          MSG_WTX: "WTX",
 432      }
 433  
 434      def __init__(self, t=0, h=0):
 435          self.type = t
 436          self.hash = h
 437  
 438      def deserialize(self, f):
 439          self.type = int.from_bytes(f.read(4), "little")
 440          self.hash = deser_uint256(f)
 441  
 442      def serialize(self):
 443          r = b""
 444          r += self.type.to_bytes(4, "little")
 445          r += ser_uint256(self.hash)
 446          return r
 447  
 448      def __repr__(self):
 449          return "CInv(type=%s hash=%064x)" \
 450              % (self.typemap[self.type], self.hash)
 451  
 452      def __eq__(self, other):
 453          return isinstance(other, CInv) and self.hash == other.hash and self.type == other.type
 454  
 455  
 456  class CBlockLocator:
 457      __slots__ = ("nVersion", "vHave")
 458  
 459      def __init__(self):
 460          self.vHave = []
 461  
 462      def deserialize(self, f):
 463          int.from_bytes(f.read(4), "little", signed=True)  # Ignore version field.
 464          self.vHave = deser_uint256_vector(f)
 465  
 466      def serialize(self):
 467          r = b""
 468          r += (0).to_bytes(4, "little", signed=True)  # Bitcoin Core ignores the version field. Set it to 0.
 469          r += ser_uint256_vector(self.vHave)
 470          return r
 471  
 472      def __repr__(self):
 473          return "CBlockLocator(vHave=%s)" % (repr(self.vHave))
 474  
 475  
 476  class COutPoint:
 477      __slots__ = ("hash", "n")
 478  
 479      def __init__(self, hash=0, n=0):
 480          self.hash = hash
 481          self.n = n
 482  
 483      def deserialize(self, f):
 484          self.hash = deser_uint256(f)
 485          self.n = int.from_bytes(f.read(4), "little")
 486  
 487      def serialize(self):
 488          r = b""
 489          r += ser_uint256(self.hash)
 490          r += self.n.to_bytes(4, "little")
 491          return r
 492  
 493      def __repr__(self):
 494          return "COutPoint(hash=%064x n=%i)" % (self.hash, self.n)
 495  
 496  
 497  class CTxIn:
 498      __slots__ = ("nSequence", "prevout", "scriptSig")
 499  
 500      def __init__(self, outpoint=None, scriptSig=b"", nSequence=0):
 501          if outpoint is None:
 502              self.prevout = COutPoint()
 503          else:
 504              self.prevout = outpoint
 505          self.scriptSig = scriptSig
 506          self.nSequence = nSequence
 507  
 508      def deserialize(self, f):
 509          self.prevout = COutPoint()
 510          self.prevout.deserialize(f)
 511          self.scriptSig = deser_string(f)
 512          self.nSequence = int.from_bytes(f.read(4), "little")
 513  
 514      def serialize(self):
 515          r = b""
 516          r += self.prevout.serialize()
 517          r += ser_string(self.scriptSig)
 518          r += self.nSequence.to_bytes(4, "little")
 519          return r
 520  
 521      def __repr__(self):
 522          return "CTxIn(prevout=%s scriptSig=%s nSequence=%i)" \
 523              % (repr(self.prevout), self.scriptSig.hex(),
 524                 self.nSequence)
 525  
 526  
 527  class CTxOut:
 528      __slots__ = ("nValue", "scriptPubKey")
 529  
 530      def __init__(self, nValue=0, scriptPubKey=b""):
 531          self.nValue = nValue
 532          self.scriptPubKey = scriptPubKey
 533  
 534      def deserialize(self, f):
 535          self.nValue = int.from_bytes(f.read(8), "little", signed=True)
 536          self.scriptPubKey = deser_string(f)
 537  
 538      def serialize(self):
 539          r = b""
 540          r += self.nValue.to_bytes(8, "little", signed=True)
 541          r += ser_string(self.scriptPubKey)
 542          return r
 543  
 544      def __repr__(self):
 545          return "CTxOut(nValue=%i.%08i scriptPubKey=%s)" \
 546              % (self.nValue // COIN, self.nValue % COIN,
 547                 self.scriptPubKey.hex())
 548  
 549  
 550  class CScriptWitness:
 551      __slots__ = ("stack",)
 552  
 553      def __init__(self):
 554          # stack is a vector of strings
 555          self.stack = []
 556  
 557      def __repr__(self):
 558          return "CScriptWitness(%s)" % \
 559                 (",".join([x.hex() for x in self.stack]))
 560  
 561      def is_null(self):
 562          if self.stack:
 563              return False
 564          return True
 565  
 566  
 567  class CTxInWitness:
 568      __slots__ = ("scriptWitness",)
 569  
 570      def __init__(self):
 571          self.scriptWitness = CScriptWitness()
 572  
 573      def deserialize(self, f):
 574          self.scriptWitness.stack = deser_string_vector(f)
 575  
 576      def serialize(self):
 577          return ser_string_vector(self.scriptWitness.stack)
 578  
 579      def __repr__(self):
 580          return repr(self.scriptWitness)
 581  
 582      def is_null(self):
 583          return self.scriptWitness.is_null()
 584  
 585  
 586  class CTxWitness:
 587      __slots__ = ("vtxinwit",)
 588  
 589      def __init__(self):
 590          self.vtxinwit = []
 591  
 592      def deserialize(self, f):
 593          for i in range(len(self.vtxinwit)):
 594              self.vtxinwit[i].deserialize(f)
 595  
 596      def serialize(self):
 597          r = b""
 598          # This is different than the usual vector serialization --
 599          # we omit the length of the vector, which is required to be
 600          # the same length as the transaction's vin vector.
 601          for x in self.vtxinwit:
 602              r += x.serialize()
 603          return r
 604  
 605      def __repr__(self):
 606          return "CTxWitness(%s)" % \
 607                 (';'.join([repr(x) for x in self.vtxinwit]))
 608  
 609      def is_null(self):
 610          for x in self.vtxinwit:
 611              if not x.is_null():
 612                  return False
 613          return True
 614  
 615  
 616  class CTransaction:
 617      __slots__ = ("nLockTime", "version", "vin", "vout", "wit")
 618  
 619      def __init__(self, tx=None):
 620          if tx is None:
 621              self.version = 2
 622              self.vin = []
 623              self.vout = []
 624              self.wit = CTxWitness()
 625              self.nLockTime = 0
 626          else:
 627              self.version = tx.version
 628              self.vin = copy.deepcopy(tx.vin)
 629              self.vout = copy.deepcopy(tx.vout)
 630              self.nLockTime = tx.nLockTime
 631              self.wit = copy.deepcopy(tx.wit)
 632  
 633      def deserialize(self, f):
 634          self.version = int.from_bytes(f.read(4), "little")
 635          self.vin = deser_vector(f, CTxIn)
 636          flags = 0
 637          if len(self.vin) == 0:
 638              flags = int.from_bytes(f.read(1), "little")
 639              # Not sure why flags can't be zero, but this
 640              # matches the implementation in bitcoind
 641              if (flags != 0):
 642                  self.vin = deser_vector(f, CTxIn)
 643                  self.vout = deser_vector(f, CTxOut)
 644          else:
 645              self.vout = deser_vector(f, CTxOut)
 646          if flags != 0:
 647              self.wit.vtxinwit = [CTxInWitness() for _ in range(len(self.vin))]
 648              self.wit.deserialize(f)
 649          else:
 650              self.wit = CTxWitness()
 651          self.nLockTime = int.from_bytes(f.read(4), "little")
 652  
 653      def serialize_without_witness(self):
 654          r = b""
 655          r += self.version.to_bytes(4, "little")
 656          r += ser_vector(self.vin)
 657          r += ser_vector(self.vout)
 658          r += self.nLockTime.to_bytes(4, "little")
 659          return r
 660  
 661      # Only serialize with witness when explicitly called for
 662      def serialize_with_witness(self):
 663          flags = 0
 664          if not self.wit.is_null():
 665              flags |= 1
 666          r = b""
 667          r += self.version.to_bytes(4, "little")
 668          if flags:
 669              dummy = []
 670              r += ser_vector(dummy)
 671              r += flags.to_bytes(1, "little")
 672          r += ser_vector(self.vin)
 673          r += ser_vector(self.vout)
 674          if flags & 1:
 675              if (len(self.wit.vtxinwit) != len(self.vin)):
 676                  # vtxinwit must have the same length as vin
 677                  self.wit.vtxinwit = self.wit.vtxinwit[:len(self.vin)]
 678                  for _ in range(len(self.wit.vtxinwit), len(self.vin)):
 679                      self.wit.vtxinwit.append(CTxInWitness())
 680              r += self.wit.serialize()
 681          r += self.nLockTime.to_bytes(4, "little")
 682          return r
 683  
 684      # Regular serialization is with witness -- must explicitly
 685      # call serialize_without_witness to exclude witness data.
 686      def serialize(self):
 687          return self.serialize_with_witness()
 688  
 689      @property
 690      def wtxid_hex(self):
 691          """Return wtxid (transaction hash with witness) as hex string."""
 692          return hash256(self.serialize())[::-1].hex()
 693  
 694      @property
 695      def wtxid_int(self):
 696          """Return wtxid (transaction hash with witness) as integer."""
 697          return uint256_from_str(hash256(self.serialize_with_witness()))
 698  
 699      @property
 700      def txid_hex(self):
 701          """Return txid (transaction hash without witness) as hex string."""
 702          return hash256(self.serialize_without_witness())[::-1].hex()
 703  
 704      @property
 705      def txid_int(self):
 706          """Return txid (transaction hash without witness) as integer."""
 707          return uint256_from_str(hash256(self.serialize_without_witness()))
 708  
 709      def is_valid(self):
 710          for tout in self.vout:
 711              if tout.nValue < 0 or tout.nValue > 21000000 * COIN:
 712                  return False
 713          return True
 714  
 715      # Calculate the transaction weight using witness and non-witness
 716      # serialization size (does NOT use sigops).
 717      def get_weight(self):
 718          with_witness_size = len(self.serialize_with_witness())
 719          without_witness_size = len(self.serialize_without_witness())
 720          return (WITNESS_SCALE_FACTOR - 1) * without_witness_size + with_witness_size
 721  
 722      def get_vsize(self):
 723          return math.ceil(self.get_weight() / WITNESS_SCALE_FACTOR)
 724  
 725      def __repr__(self):
 726          return "CTransaction(version=%i vin=%s vout=%s wit=%s nLockTime=%i)" \
 727              % (self.version, repr(self.vin), repr(self.vout), repr(self.wit), self.nLockTime)
 728  
 729  
 730  class CBlockHeader:
 731      __slots__ = ("hashMerkleRoot", "hashPrevBlock", "nBits", "nNonce",
 732                   "nTime", "nVersion")
 733  
 734      def __init__(self, header=None):
 735          if header is None:
 736              self.set_null()
 737          else:
 738              self.nVersion = header.nVersion
 739              self.hashPrevBlock = header.hashPrevBlock
 740              self.hashMerkleRoot = header.hashMerkleRoot
 741              self.nTime = header.nTime
 742              self.nBits = header.nBits
 743              self.nNonce = header.nNonce
 744  
 745      def set_null(self):
 746          self.nVersion = 4
 747          self.hashPrevBlock = 0
 748          self.hashMerkleRoot = 0
 749          self.nTime = 0
 750          self.nBits = 0
 751          self.nNonce = 0
 752  
 753      def deserialize(self, f):
 754          self.nVersion = int.from_bytes(f.read(4), "little", signed=True)
 755          self.hashPrevBlock = deser_uint256(f)
 756          self.hashMerkleRoot = deser_uint256(f)
 757          self.nTime = int.from_bytes(f.read(4), "little")
 758          self.nBits = int.from_bytes(f.read(4), "little")
 759          self.nNonce = int.from_bytes(f.read(4), "little")
 760  
 761      def serialize(self):
 762          return self._serialize_header()
 763  
 764      def _serialize_header(self):
 765          r = b""
 766          r += self.nVersion.to_bytes(4, "little", signed=True)
 767          r += ser_uint256(self.hashPrevBlock)
 768          r += ser_uint256(self.hashMerkleRoot)
 769          r += self.nTime.to_bytes(4, "little")
 770          r += self.nBits.to_bytes(4, "little")
 771          r += self.nNonce.to_bytes(4, "little")
 772          return r
 773  
 774      @property
 775      def hash_hex(self):
 776          """Return block header hash as hex string."""
 777          return hash256(self._serialize_header())[::-1].hex()
 778  
 779      @property
 780      def hash_int(self):
 781          """Return block header hash as integer."""
 782          return uint256_from_str(hash256(self._serialize_header()))
 783  
 784      def __repr__(self):
 785          return "CBlockHeader(nVersion=%i hashPrevBlock=%064x hashMerkleRoot=%064x nTime=%s nBits=%08x nNonce=%08x)" \
 786              % (self.nVersion, self.hashPrevBlock, self.hashMerkleRoot,
 787                 time.ctime(self.nTime), self.nBits, self.nNonce)
 788  
 789  BLOCK_HEADER_SIZE = len(CBlockHeader().serialize())
 790  assert_equal(BLOCK_HEADER_SIZE, 80)
 791  
 792  class CBlock(CBlockHeader):
 793      __slots__ = ("vtx",)
 794  
 795      def __init__(self, header=None):
 796          super().__init__(header)
 797          self.vtx = []
 798  
 799      def deserialize(self, f):
 800          super().deserialize(f)
 801          self.vtx = deser_vector(f, CTransaction)
 802  
 803      def serialize(self, with_witness=True):
 804          r = b""
 805          r += super().serialize()
 806          if with_witness:
 807              r += ser_vector(self.vtx, "serialize_with_witness")
 808          else:
 809              r += ser_vector(self.vtx, "serialize_without_witness")
 810          return r
 811  
 812      # Calculate the merkle root given a vector of transaction hashes
 813      @classmethod
 814      def get_merkle_root(cls, hashes):
 815          while len(hashes) > 1:
 816              newhashes = []
 817              for i in range(0, len(hashes), 2):
 818                  i2 = min(i+1, len(hashes)-1)
 819                  newhashes.append(hash256(hashes[i] + hashes[i2]))
 820              hashes = newhashes
 821          return uint256_from_str(hashes[0])
 822  
 823      def calc_merkle_root(self):
 824          hashes = []
 825          for tx in self.vtx:
 826              hashes.append(ser_uint256(tx.txid_int))
 827          return self.get_merkle_root(hashes)
 828  
 829      def calc_witness_merkle_root(self):
 830          # For witness root purposes, the hash of the
 831          # coinbase, with witness, is defined to be 0...0
 832          hashes = [ser_uint256(0)]
 833  
 834          for tx in self.vtx[1:]:
 835              # Calculate the hashes with witness data
 836              hashes.append(ser_uint256(tx.wtxid_int))
 837  
 838          return self.get_merkle_root(hashes)
 839  
 840      def is_valid(self):
 841          target = uint256_from_compact(self.nBits)
 842          if self.hash_int > target:
 843              return False
 844          for tx in self.vtx:
 845              if not tx.is_valid():
 846                  return False
 847          if self.calc_merkle_root() != self.hashMerkleRoot:
 848              return False
 849          return True
 850  
 851      def solve(self):
 852          target = uint256_from_compact(self.nBits)
 853          while self.hash_int > target:
 854              self.nNonce += 1
 855  
 856      # Calculate the block weight using witness and non-witness
 857      # serialization size (does NOT use sigops).
 858      def get_weight(self):
 859          with_witness_size = len(self.serialize(with_witness=True))
 860          without_witness_size = len(self.serialize(with_witness=False))
 861          return (WITNESS_SCALE_FACTOR - 1) * without_witness_size + with_witness_size
 862  
 863      def __repr__(self):
 864          return "CBlock(nVersion=%i hashPrevBlock=%064x hashMerkleRoot=%064x nTime=%s nBits=%08x nNonce=%08x vtx=%s)" \
 865              % (self.nVersion, self.hashPrevBlock, self.hashMerkleRoot,
 866                 time.ctime(self.nTime), self.nBits, self.nNonce, repr(self.vtx))
 867  
 868  
 869  class PrefilledTransaction:
 870      __slots__ = ("index", "tx")
 871  
 872      def __init__(self, index=0, tx = None):
 873          self.index = index
 874          self.tx = tx
 875  
 876      def deserialize(self, f):
 877          self.index = deser_compact_size(f)
 878          self.tx = CTransaction()
 879          self.tx.deserialize(f)
 880  
 881      def serialize(self, with_witness=True):
 882          r = b""
 883          r += ser_compact_size(self.index)
 884          if with_witness:
 885              r += self.tx.serialize_with_witness()
 886          else:
 887              r += self.tx.serialize_without_witness()
 888          return r
 889  
 890      def serialize_without_witness(self):
 891          return self.serialize(with_witness=False)
 892  
 893      def serialize_with_witness(self):
 894          return self.serialize(with_witness=True)
 895  
 896      def __repr__(self):
 897          return "PrefilledTransaction(index=%d, tx=%s)" % (self.index, repr(self.tx))
 898  
 899  
 900  # This is what we send on the wire, in a cmpctblock message.
 901  class P2PHeaderAndShortIDs:
 902      __slots__ = ("header", "nonce", "prefilled_txn", "prefilled_txn_length",
 903                   "shortids", "shortids_length")
 904  
 905      def __init__(self):
 906          self.header = CBlockHeader()
 907          self.nonce = 0
 908          self.shortids_length = 0
 909          self.shortids = []
 910          self.prefilled_txn_length = 0
 911          self.prefilled_txn = []
 912  
 913      def deserialize(self, f):
 914          self.header.deserialize(f)
 915          self.nonce = int.from_bytes(f.read(8), "little")
 916          self.shortids_length = deser_compact_size(f)
 917          for _ in range(self.shortids_length):
 918              # shortids are defined to be 6 bytes in the spec, so append
 919              # two zero bytes and read it in as an 8-byte number
 920              self.shortids.append(int.from_bytes(f.read(6) + b'\x00\x00', "little"))
 921          self.prefilled_txn = deser_vector(f, PrefilledTransaction)
 922          self.prefilled_txn_length = len(self.prefilled_txn)
 923  
 924      # When using version 2 compact blocks, we must serialize with_witness.
 925      def serialize(self, with_witness=False):
 926          r = b""
 927          r += self.header.serialize()
 928          r += self.nonce.to_bytes(8, "little")
 929          r += ser_compact_size(self.shortids_length)
 930          for x in self.shortids:
 931              # We only want the first 6 bytes
 932              r += x.to_bytes(8, "little")[0:6]
 933          if with_witness:
 934              r += ser_vector(self.prefilled_txn, "serialize_with_witness")
 935          else:
 936              r += ser_vector(self.prefilled_txn, "serialize_without_witness")
 937          return r
 938  
 939      def __repr__(self):
 940          return "P2PHeaderAndShortIDs(header=%s, nonce=%d, shortids_length=%d, shortids=%s, prefilled_txn_length=%d, prefilledtxn=%s" % (repr(self.header), self.nonce, self.shortids_length, repr(self.shortids), self.prefilled_txn_length, repr(self.prefilled_txn))
 941  
 942  
 943  # P2P version of the above that will use witness serialization (for compact
 944  # block version 2)
 945  class P2PHeaderAndShortWitnessIDs(P2PHeaderAndShortIDs):
 946      __slots__ = ()
 947      def serialize(self):
 948          return super().serialize(with_witness=True)
 949  
 950  # Calculate the BIP 152-compact blocks shortid for a given transaction hash
 951  def calculate_shortid(k0, k1, tx_hash):
 952      expected_shortid = siphash256(k0, k1, tx_hash)
 953      expected_shortid &= 0x0000ffffffffffff
 954      return expected_shortid
 955  
 956  
 957  # This version gets rid of the array lengths, and reinterprets the differential
 958  # encoding into indices that can be used for lookup.
 959  class HeaderAndShortIDs:
 960      __slots__ = ("header", "nonce", "prefilled_txn", "shortids", "use_witness")
 961  
 962      def __init__(self, p2pheaders_and_shortids = None):
 963          self.header = CBlockHeader()
 964          self.nonce = 0
 965          self.shortids = []
 966          self.prefilled_txn = []
 967          self.use_witness = False
 968  
 969          if p2pheaders_and_shortids is not None:
 970              self.header = p2pheaders_and_shortids.header
 971              self.nonce = p2pheaders_and_shortids.nonce
 972              self.shortids = p2pheaders_and_shortids.shortids
 973              last_index = -1
 974              for x in p2pheaders_and_shortids.prefilled_txn:
 975                  self.prefilled_txn.append(PrefilledTransaction(x.index + last_index + 1, x.tx))
 976                  last_index = self.prefilled_txn[-1].index
 977  
 978      def to_p2p(self):
 979          if self.use_witness:
 980              ret = P2PHeaderAndShortWitnessIDs()
 981          else:
 982              ret = P2PHeaderAndShortIDs()
 983          ret.header = self.header
 984          ret.nonce = self.nonce
 985          ret.shortids_length = len(self.shortids)
 986          ret.shortids = self.shortids
 987          ret.prefilled_txn_length = len(self.prefilled_txn)
 988          ret.prefilled_txn = []
 989          last_index = -1
 990          for x in self.prefilled_txn:
 991              ret.prefilled_txn.append(PrefilledTransaction(x.index - last_index - 1, x.tx))
 992              last_index = x.index
 993          return ret
 994  
 995      def get_siphash_keys(self):
 996          header_nonce = self.header.serialize()
 997          header_nonce += self.nonce.to_bytes(8, "little")
 998          hash_header_nonce_as_str = sha256(header_nonce)
 999          key0 = int.from_bytes(hash_header_nonce_as_str[0:8], "little")
1000          key1 = int.from_bytes(hash_header_nonce_as_str[8:16], "little")
1001          return [ key0, key1 ]
1002  
1003      # Version 2 compact blocks use wtxid in shortids (rather than txid)
1004      def initialize_from_block(self, block, nonce=0, prefill_list=None, use_witness=False):
1005          if prefill_list is None:
1006              prefill_list = [0]
1007          self.header = CBlockHeader(block)
1008          self.nonce = nonce
1009          self.prefilled_txn = [ PrefilledTransaction(i, block.vtx[i]) for i in prefill_list ]
1010          self.shortids = []
1011          self.use_witness = use_witness
1012          [k0, k1] = self.get_siphash_keys()
1013          for i in range(len(block.vtx)):
1014              if i not in prefill_list:
1015                  tx_hash = block.vtx[i].txid_int
1016                  if use_witness:
1017                      tx_hash = block.vtx[i].wtxid_int
1018                  self.shortids.append(calculate_shortid(k0, k1, tx_hash))
1019  
1020      def __repr__(self):
1021          return "HeaderAndShortIDs(header=%s, nonce=%d, shortids=%s, prefilledtxn=%s" % (repr(self.header), self.nonce, repr(self.shortids), repr(self.prefilled_txn))
1022  
1023  
1024  class BlockTransactionsRequest:
1025      __slots__ = ("blockhash", "indexes")
1026  
1027      def __init__(self, blockhash=0, indexes = None):
1028          self.blockhash = blockhash
1029          self.indexes = indexes if indexes is not None else []
1030  
1031      def deserialize(self, f):
1032          self.blockhash = deser_uint256(f)
1033          indexes_length = deser_compact_size(f)
1034          for _ in range(indexes_length):
1035              self.indexes.append(deser_compact_size(f))
1036  
1037      def serialize(self):
1038          r = b""
1039          r += ser_uint256(self.blockhash)
1040          r += ser_compact_size(len(self.indexes))
1041          for x in self.indexes:
1042              r += ser_compact_size(x)
1043          return r
1044  
1045      # helper to set the differentially encoded indexes from absolute ones
1046      def from_absolute(self, absolute_indexes):
1047          self.indexes = []
1048          last_index = -1
1049          for x in absolute_indexes:
1050              self.indexes.append(x-last_index-1)
1051              last_index = x
1052  
1053      def to_absolute(self):
1054          absolute_indexes = []
1055          last_index = -1
1056          for x in self.indexes:
1057              absolute_indexes.append(x+last_index+1)
1058              last_index = absolute_indexes[-1]
1059          return absolute_indexes
1060  
1061      def __repr__(self):
1062          return "BlockTransactionsRequest(hash=%064x indexes=%s)" % (self.blockhash, repr(self.indexes))
1063  
1064  
1065  class BlockTransactions:
1066      __slots__ = ("blockhash", "transactions")
1067  
1068      def __init__(self, blockhash=0, transactions = None):
1069          self.blockhash = blockhash
1070          self.transactions = transactions if transactions is not None else []
1071  
1072      def deserialize(self, f):
1073          self.blockhash = deser_uint256(f)
1074          self.transactions = deser_vector(f, CTransaction)
1075  
1076      def serialize(self, with_witness=True):
1077          r = b""
1078          r += ser_uint256(self.blockhash)
1079          if with_witness:
1080              r += ser_vector(self.transactions, "serialize_with_witness")
1081          else:
1082              r += ser_vector(self.transactions, "serialize_without_witness")
1083          return r
1084  
1085      def __repr__(self):
1086          return "BlockTransactions(hash=%064x transactions=%s)" % (self.blockhash, repr(self.transactions))
1087  
1088  
1089  class CPartialMerkleTree:
1090      __slots__ = ("nTransactions", "vBits", "vHash")
1091  
1092      def __init__(self):
1093          self.nTransactions = 0
1094          self.vHash = []
1095          self.vBits = []
1096  
1097      def deserialize(self, f):
1098          self.nTransactions = int.from_bytes(f.read(4), "little")
1099          self.vHash = deser_uint256_vector(f)
1100          vBytes = deser_string(f)
1101          self.vBits = []
1102          for i in range(len(vBytes) * 8):
1103              self.vBits.append(vBytes[i//8] & (1 << (i % 8)) != 0)
1104  
1105      def serialize(self):
1106          r = b""
1107          r += self.nTransactions.to_bytes(4, "little")
1108          r += ser_uint256_vector(self.vHash)
1109          vBytesArray = bytearray([0x00] * ((len(self.vBits) + 7)//8))
1110          for i in range(len(self.vBits)):
1111              vBytesArray[i // 8] |= self.vBits[i] << (i % 8)
1112          r += ser_string(bytes(vBytesArray))
1113          return r
1114  
1115      def __repr__(self):
1116          return "CPartialMerkleTree(nTransactions=%d, vHash=%s, vBits=%s)" % (self.nTransactions, repr(self.vHash), repr(self.vBits))
1117  
1118  
1119  class CMerkleBlock:
1120      __slots__ = ("header", "txn")
1121  
1122      def __init__(self):
1123          self.header = CBlockHeader()
1124          self.txn = CPartialMerkleTree()
1125  
1126      def deserialize(self, f):
1127          self.header.deserialize(f)
1128          self.txn.deserialize(f)
1129  
1130      def serialize(self):
1131          r = b""
1132          r += self.header.serialize()
1133          r += self.txn.serialize()
1134          return r
1135  
1136      def __repr__(self):
1137          return "CMerkleBlock(header=%s, txn=%s)" % (repr(self.header), repr(self.txn))
1138  
1139  
1140  # Objects that correspond to messages on the wire
1141  class msg_version:
1142      __slots__ = ("addrFrom", "addrTo", "nNonce", "relay", "nServices",
1143                   "nStartingHeight", "nTime", "nVersion", "strSubVer")
1144      msgtype = b"version"
1145  
1146      def __init__(self):
1147          self.nVersion = 0
1148          self.nServices = 0
1149          self.nTime = int(time.time())
1150          self.addrTo = CAddress()
1151          self.addrFrom = CAddress()
1152          self.nNonce = random.getrandbits(64)
1153          self.strSubVer = ''
1154          self.nStartingHeight = -1
1155          self.relay = 0
1156  
1157      def deserialize(self, f):
1158          self.nVersion = int.from_bytes(f.read(4), "little", signed=True)
1159          self.nServices = int.from_bytes(f.read(8), "little")
1160          self.nTime = int.from_bytes(f.read(8), "little", signed=True)
1161          self.addrTo = CAddress()
1162          self.addrTo.deserialize(f, with_time=False)
1163  
1164          self.addrFrom = CAddress()
1165          self.addrFrom.deserialize(f, with_time=False)
1166          self.nNonce = int.from_bytes(f.read(8), "little")
1167          self.strSubVer = deser_string(f).decode('utf-8')
1168  
1169          self.nStartingHeight = int.from_bytes(f.read(4), "little", signed=True)
1170  
1171          # Relay field is optional for version 70001 onwards
1172          # But, unconditionally check it to match behaviour in bitcoind
1173          self.relay = int.from_bytes(f.read(1), "little")  # f.read(1) may return an empty b''
1174  
1175      def serialize(self):
1176          r = b""
1177          r += self.nVersion.to_bytes(4, "little", signed=True)
1178          r += self.nServices.to_bytes(8, "little")
1179          r += self.nTime.to_bytes(8, "little", signed=True)
1180          r += self.addrTo.serialize(with_time=False)
1181          r += self.addrFrom.serialize(with_time=False)
1182          r += self.nNonce.to_bytes(8, "little")
1183          r += ser_string(self.strSubVer.encode('utf-8'))
1184          r += self.nStartingHeight.to_bytes(4, "little", signed=True)
1185          r += self.relay.to_bytes(1, "little")
1186          return r
1187  
1188      def __repr__(self):
1189          return 'msg_version(nVersion=%i nServices=%i nTime=%s addrTo=%s addrFrom=%s nNonce=0x%016X strSubVer=%s nStartingHeight=%i relay=%i)' \
1190              % (self.nVersion, self.nServices, time.ctime(self.nTime),
1191                 repr(self.addrTo), repr(self.addrFrom), self.nNonce,
1192                 self.strSubVer, self.nStartingHeight, self.relay)
1193  
1194  
1195  class msg_verack:
1196      __slots__ = ()
1197      msgtype = b"verack"
1198  
1199      def __init__(self):
1200          pass
1201  
1202      def deserialize(self, f):
1203          pass
1204  
1205      def serialize(self):
1206          return b""
1207  
1208      def __repr__(self):
1209          return "msg_verack()"
1210  
1211  
1212  class msg_addr:
1213      __slots__ = ("addrs",)
1214      msgtype = b"addr"
1215  
1216      def __init__(self):
1217          self.addrs = []
1218  
1219      def deserialize(self, f):
1220          self.addrs = deser_vector(f, CAddress)
1221  
1222      def serialize(self):
1223          return ser_vector(self.addrs)
1224  
1225      def __repr__(self):
1226          return "msg_addr(addrs=%s)" % (repr(self.addrs))
1227  
1228  
1229  class msg_addrv2:
1230      __slots__ = ("addrs",)
1231      msgtype = b"addrv2"
1232  
1233      def __init__(self):
1234          self.addrs = []
1235  
1236      def deserialize(self, f):
1237          self.addrs = deser_vector(f, CAddress, "deserialize_v2")
1238  
1239      def serialize(self):
1240          return ser_vector(self.addrs, "serialize_v2")
1241  
1242      def __repr__(self):
1243          return "msg_addrv2(addrs=%s)" % (repr(self.addrs))
1244  
1245  
1246  class msg_sendaddrv2:
1247      __slots__ = ()
1248      msgtype = b"sendaddrv2"
1249  
1250      def __init__(self):
1251          pass
1252  
1253      def deserialize(self, f):
1254          pass
1255  
1256      def serialize(self):
1257          return b""
1258  
1259      def __repr__(self):
1260          return "msg_sendaddrv2()"
1261  
1262  
1263  class msg_inv:
1264      __slots__ = ("inv",)
1265      msgtype = b"inv"
1266  
1267      def __init__(self, inv=None):
1268          if inv is None:
1269              self.inv = []
1270          else:
1271              self.inv = inv
1272  
1273      def deserialize(self, f):
1274          self.inv = deser_vector(f, CInv)
1275  
1276      def serialize(self):
1277          return ser_vector(self.inv)
1278  
1279      def __repr__(self):
1280          return "msg_inv(inv=%s)" % (repr(self.inv))
1281  
1282  
1283  class msg_getdata:
1284      __slots__ = ("inv",)
1285      msgtype = b"getdata"
1286  
1287      def __init__(self, inv=None):
1288          self.inv = inv if inv is not None else []
1289  
1290      def deserialize(self, f):
1291          self.inv = deser_vector(f, CInv)
1292  
1293      def serialize(self):
1294          return ser_vector(self.inv)
1295  
1296      def __repr__(self):
1297          return "msg_getdata(inv=%s)" % (repr(self.inv))
1298  
1299  
1300  class msg_getblocks:
1301      __slots__ = ("locator", "hashstop")
1302      msgtype = b"getblocks"
1303  
1304      def __init__(self):
1305          self.locator = CBlockLocator()
1306          self.hashstop = 0
1307  
1308      def deserialize(self, f):
1309          self.locator = CBlockLocator()
1310          self.locator.deserialize(f)
1311          self.hashstop = deser_uint256(f)
1312  
1313      def serialize(self):
1314          r = b""
1315          r += self.locator.serialize()
1316          r += ser_uint256(self.hashstop)
1317          return r
1318  
1319      def __repr__(self):
1320          return "msg_getblocks(locator=%s hashstop=%064x)" \
1321              % (repr(self.locator), self.hashstop)
1322  
1323  
1324  class msg_tx:
1325      __slots__ = ("tx",)
1326      msgtype = b"tx"
1327  
1328      def __init__(self, tx=None):
1329          if tx is None:
1330              self.tx = CTransaction()
1331          else:
1332              self.tx = tx
1333  
1334      def deserialize(self, f):
1335          self.tx.deserialize(f)
1336  
1337      def serialize(self):
1338          return self.tx.serialize_with_witness()
1339  
1340      def __repr__(self):
1341          return "msg_tx(tx=%s)" % (repr(self.tx))
1342  
1343  class msg_wtxidrelay:
1344      __slots__ = ()
1345      msgtype = b"wtxidrelay"
1346  
1347      def __init__(self):
1348          pass
1349  
1350      def deserialize(self, f):
1351          pass
1352  
1353      def serialize(self):
1354          return b""
1355  
1356      def __repr__(self):
1357          return "msg_wtxidrelay()"
1358  
1359  
1360  class msg_no_witness_tx(msg_tx):
1361      __slots__ = ()
1362  
1363      def serialize(self):
1364          return self.tx.serialize_without_witness()
1365  
1366  
1367  class msg_block:
1368      __slots__ = ("block",)
1369      msgtype = b"block"
1370  
1371      def __init__(self, block=None):
1372          if block is None:
1373              self.block = CBlock()
1374          else:
1375              self.block = block
1376  
1377      def deserialize(self, f):
1378          self.block.deserialize(f)
1379  
1380      def serialize(self):
1381          return self.block.serialize()
1382  
1383      def __repr__(self):
1384          return "msg_block(block=%s)" % (repr(self.block))
1385  
1386  
1387  # Generic type to control the raw bytes sent over the wire.
1388  # The msgtype and the data must be provided.
1389  class msg_generic:
1390      __slots__ = ("msgtype", "data")
1391  
1392      def __init__(self, msgtype, data=None):
1393          self.msgtype = msgtype
1394          self.data = data
1395  
1396      def serialize(self):
1397          return self.data
1398  
1399      def __repr__(self):
1400          return "msg_generic()"
1401  
1402  
1403  class msg_no_witness_block(msg_block):
1404      __slots__ = ()
1405      def serialize(self):
1406          return self.block.serialize(with_witness=False)
1407  
1408  
1409  class msg_getaddr:
1410      __slots__ = ()
1411      msgtype = b"getaddr"
1412  
1413      def __init__(self):
1414          pass
1415  
1416      def deserialize(self, f):
1417          pass
1418  
1419      def serialize(self):
1420          return b""
1421  
1422      def __repr__(self):
1423          return "msg_getaddr()"
1424  
1425  
1426  class msg_ping:
1427      __slots__ = ("nonce",)
1428      msgtype = b"ping"
1429  
1430      def __init__(self, nonce=0):
1431          self.nonce = nonce
1432  
1433      def deserialize(self, f):
1434          self.nonce = int.from_bytes(f.read(8), "little")
1435  
1436      def serialize(self):
1437          r = b""
1438          r += self.nonce.to_bytes(8, "little")
1439          return r
1440  
1441      def __repr__(self):
1442          return "msg_ping(nonce=%08x)" % self.nonce
1443  
1444  
1445  class msg_pong:
1446      __slots__ = ("nonce",)
1447      msgtype = b"pong"
1448  
1449      def __init__(self, nonce=0):
1450          self.nonce = nonce
1451  
1452      def deserialize(self, f):
1453          self.nonce = int.from_bytes(f.read(8), "little")
1454  
1455      def serialize(self):
1456          r = b""
1457          r += self.nonce.to_bytes(8, "little")
1458          return r
1459  
1460      def __repr__(self):
1461          return "msg_pong(nonce=%08x)" % self.nonce
1462  
1463  
1464  class msg_mempool:
1465      __slots__ = ()
1466      msgtype = b"mempool"
1467  
1468      def __init__(self):
1469          pass
1470  
1471      def deserialize(self, f):
1472          pass
1473  
1474      def serialize(self):
1475          return b""
1476  
1477      def __repr__(self):
1478          return "msg_mempool()"
1479  
1480  
1481  class msg_notfound:
1482      __slots__ = ("vec", )
1483      msgtype = b"notfound"
1484  
1485      def __init__(self, vec=None):
1486          self.vec = vec or []
1487  
1488      def deserialize(self, f):
1489          self.vec = deser_vector(f, CInv)
1490  
1491      def serialize(self):
1492          return ser_vector(self.vec)
1493  
1494      def __repr__(self):
1495          return "msg_notfound(vec=%s)" % (repr(self.vec))
1496  
1497  
1498  class msg_sendheaders:
1499      __slots__ = ()
1500      msgtype = b"sendheaders"
1501  
1502      def __init__(self):
1503          pass
1504  
1505      def deserialize(self, f):
1506          pass
1507  
1508      def serialize(self):
1509          return b""
1510  
1511      def __repr__(self):
1512          return "msg_sendheaders()"
1513  
1514  
1515  # getheaders message has
1516  # number of entries
1517  # vector of hashes
1518  # hash_stop (hash of last desired block header, 0 to get as many as possible)
1519  class msg_getheaders:
1520      __slots__ = ("hashstop", "locator",)
1521      msgtype = b"getheaders"
1522  
1523      def __init__(self):
1524          self.locator = CBlockLocator()
1525          self.hashstop = 0
1526  
1527      def deserialize(self, f):
1528          self.locator = CBlockLocator()
1529          self.locator.deserialize(f)
1530          self.hashstop = deser_uint256(f)
1531  
1532      def serialize(self):
1533          r = b""
1534          r += self.locator.serialize()
1535          r += ser_uint256(self.hashstop)
1536          return r
1537  
1538      def __repr__(self):
1539          return "msg_getheaders(locator=%s, stop=%064x)" \
1540              % (repr(self.locator), self.hashstop)
1541  
1542  
1543  # headers message has
1544  # <count> <vector of block headers>
1545  class msg_headers:
1546      __slots__ = ("headers",)
1547      msgtype = b"headers"
1548  
1549      def __init__(self, headers=None):
1550          self.headers = headers if headers is not None else []
1551  
1552      def deserialize(self, f):
1553          # comment in bitcoind indicates these should be deserialized as blocks
1554          blocks = deser_vector(f, CBlock)
1555          for x in blocks:
1556              self.headers.append(CBlockHeader(x))
1557  
1558      def serialize(self):
1559          blocks = [CBlock(x) for x in self.headers]
1560          return ser_vector(blocks)
1561  
1562      def __repr__(self):
1563          return "msg_headers(headers=%s)" % repr(self.headers)
1564  
1565  
1566  class msg_merkleblock:
1567      __slots__ = ("merkleblock",)
1568      msgtype = b"merkleblock"
1569  
1570      def __init__(self, merkleblock=None):
1571          if merkleblock is None:
1572              self.merkleblock = CMerkleBlock()
1573          else:
1574              self.merkleblock = merkleblock
1575  
1576      def deserialize(self, f):
1577          self.merkleblock.deserialize(f)
1578  
1579      def serialize(self):
1580          return self.merkleblock.serialize()
1581  
1582      def __repr__(self):
1583          return "msg_merkleblock(merkleblock=%s)" % (repr(self.merkleblock))
1584  
1585  
1586  class msg_filterload:
1587      __slots__ = ("data", "nHashFuncs", "nTweak", "nFlags")
1588      msgtype = b"filterload"
1589  
1590      def __init__(self, data=b'00', nHashFuncs=0, nTweak=0, nFlags=0):
1591          self.data = data
1592          self.nHashFuncs = nHashFuncs
1593          self.nTweak = nTweak
1594          self.nFlags = nFlags
1595  
1596      def deserialize(self, f):
1597          self.data = deser_string(f)
1598          self.nHashFuncs = int.from_bytes(f.read(4), "little")
1599          self.nTweak = int.from_bytes(f.read(4), "little")
1600          self.nFlags = int.from_bytes(f.read(1), "little")
1601  
1602      def serialize(self):
1603          r = b""
1604          r += ser_string(self.data)
1605          r += self.nHashFuncs.to_bytes(4, "little")
1606          r += self.nTweak.to_bytes(4, "little")
1607          r += self.nFlags.to_bytes(1, "little")
1608          return r
1609  
1610      def __repr__(self):
1611          return "msg_filterload(data={}, nHashFuncs={}, nTweak={}, nFlags={})".format(
1612              self.data, self.nHashFuncs, self.nTweak, self.nFlags)
1613  
1614  
1615  class msg_filteradd:
1616      __slots__ = ("data")
1617      msgtype = b"filteradd"
1618  
1619      def __init__(self, data):
1620          self.data = data
1621  
1622      def deserialize(self, f):
1623          self.data = deser_string(f)
1624  
1625      def serialize(self):
1626          r = b""
1627          r += ser_string(self.data)
1628          return r
1629  
1630      def __repr__(self):
1631          return "msg_filteradd(data={})".format(self.data)
1632  
1633  
1634  class msg_filterclear:
1635      __slots__ = ()
1636      msgtype = b"filterclear"
1637  
1638      def __init__(self):
1639          pass
1640  
1641      def deserialize(self, f):
1642          pass
1643  
1644      def serialize(self):
1645          return b""
1646  
1647      def __repr__(self):
1648          return "msg_filterclear()"
1649  
1650  
1651  class msg_feefilter:
1652      __slots__ = ("feerate",)
1653      msgtype = b"feefilter"
1654  
1655      def __init__(self, feerate=0):
1656          self.feerate = feerate
1657  
1658      def deserialize(self, f):
1659          self.feerate = int.from_bytes(f.read(8), "little")
1660  
1661      def serialize(self):
1662          r = b""
1663          r += self.feerate.to_bytes(8, "little")
1664          return r
1665  
1666      def __repr__(self):
1667          return "msg_feefilter(feerate=%08x)" % self.feerate
1668  
1669  
1670  class msg_sendcmpct:
1671      __slots__ = ("announce", "version")
1672      msgtype = b"sendcmpct"
1673  
1674      def __init__(self, announce=False, version=1):
1675          self.announce = announce
1676          self.version = version
1677  
1678      def deserialize(self, f):
1679          self.announce = bool(int.from_bytes(f.read(1), "little"))
1680          self.version = int.from_bytes(f.read(8), "little")
1681  
1682      def serialize(self):
1683          r = b""
1684          r += int(self.announce).to_bytes(1, "little")
1685          r += self.version.to_bytes(8, "little")
1686          return r
1687  
1688      def __repr__(self):
1689          return "msg_sendcmpct(announce=%s, version=%lu)" % (self.announce, self.version)
1690  
1691  
1692  class msg_cmpctblock:
1693      __slots__ = ("header_and_shortids",)
1694      msgtype = b"cmpctblock"
1695  
1696      def __init__(self, header_and_shortids = None):
1697          self.header_and_shortids = header_and_shortids
1698  
1699      def deserialize(self, f):
1700          self.header_and_shortids = P2PHeaderAndShortIDs()
1701          self.header_and_shortids.deserialize(f)
1702  
1703      def serialize(self):
1704          r = b""
1705          r += self.header_and_shortids.serialize()
1706          return r
1707  
1708      def __repr__(self):
1709          return "msg_cmpctblock(HeaderAndShortIDs=%s)" % repr(self.header_and_shortids)
1710  
1711  
1712  class msg_getblocktxn:
1713      __slots__ = ("block_txn_request",)
1714      msgtype = b"getblocktxn"
1715  
1716      def __init__(self):
1717          self.block_txn_request = None
1718  
1719      def deserialize(self, f):
1720          self.block_txn_request = BlockTransactionsRequest()
1721          self.block_txn_request.deserialize(f)
1722  
1723      def serialize(self):
1724          r = b""
1725          r += self.block_txn_request.serialize()
1726          return r
1727  
1728      def __repr__(self):
1729          return "msg_getblocktxn(block_txn_request=%s)" % (repr(self.block_txn_request))
1730  
1731  
1732  class msg_blocktxn:
1733      __slots__ = ("block_transactions",)
1734      msgtype = b"blocktxn"
1735  
1736      def __init__(self):
1737          self.block_transactions = BlockTransactions()
1738  
1739      def deserialize(self, f):
1740          self.block_transactions.deserialize(f)
1741  
1742      def serialize(self):
1743          r = b""
1744          r += self.block_transactions.serialize()
1745          return r
1746  
1747      def __repr__(self):
1748          return "msg_blocktxn(block_transactions=%s)" % (repr(self.block_transactions))
1749  
1750  
1751  class msg_no_witness_blocktxn(msg_blocktxn):
1752      __slots__ = ()
1753  
1754      def serialize(self):
1755          return self.block_transactions.serialize(with_witness=False)
1756  
1757  
1758  class msg_getcfilters:
1759      __slots__ = ("filter_type", "start_height", "stop_hash")
1760      msgtype =  b"getcfilters"
1761  
1762      def __init__(self, filter_type=None, start_height=None, stop_hash=None):
1763          self.filter_type = filter_type
1764          self.start_height = start_height
1765          self.stop_hash = stop_hash
1766  
1767      def deserialize(self, f):
1768          self.filter_type = int.from_bytes(f.read(1), "little")
1769          self.start_height = int.from_bytes(f.read(4), "little")
1770          self.stop_hash = deser_uint256(f)
1771  
1772      def serialize(self):
1773          r = b""
1774          r += self.filter_type.to_bytes(1, "little")
1775          r += self.start_height.to_bytes(4, "little")
1776          r += ser_uint256(self.stop_hash)
1777          return r
1778  
1779      def __repr__(self):
1780          return "msg_getcfilters(filter_type={:#x}, start_height={}, stop_hash={:x})".format(
1781              self.filter_type, self.start_height, self.stop_hash)
1782  
1783  class msg_cfilter:
1784      __slots__ = ("filter_type", "block_hash", "filter_data")
1785      msgtype =  b"cfilter"
1786  
1787      def __init__(self, filter_type=None, block_hash=None, filter_data=None):
1788          self.filter_type = filter_type
1789          self.block_hash = block_hash
1790          self.filter_data = filter_data
1791  
1792      def deserialize(self, f):
1793          self.filter_type = int.from_bytes(f.read(1), "little")
1794          self.block_hash = deser_uint256(f)
1795          self.filter_data = deser_string(f)
1796  
1797      def serialize(self):
1798          r = b""
1799          r += self.filter_type.to_bytes(1, "little")
1800          r += ser_uint256(self.block_hash)
1801          r += ser_string(self.filter_data)
1802          return r
1803  
1804      def __repr__(self):
1805          return "msg_cfilter(filter_type={:#x}, block_hash={:x})".format(
1806              self.filter_type, self.block_hash)
1807  
1808  class msg_getcfheaders:
1809      __slots__ = ("filter_type", "start_height", "stop_hash")
1810      msgtype =  b"getcfheaders"
1811  
1812      def __init__(self, filter_type=None, start_height=None, stop_hash=None):
1813          self.filter_type = filter_type
1814          self.start_height = start_height
1815          self.stop_hash = stop_hash
1816  
1817      def deserialize(self, f):
1818          self.filter_type = int.from_bytes(f.read(1), "little")
1819          self.start_height = int.from_bytes(f.read(4), "little")
1820          self.stop_hash = deser_uint256(f)
1821  
1822      def serialize(self):
1823          r = b""
1824          r += self.filter_type.to_bytes(1, "little")
1825          r += self.start_height.to_bytes(4, "little")
1826          r += ser_uint256(self.stop_hash)
1827          return r
1828  
1829      def __repr__(self):
1830          return "msg_getcfheaders(filter_type={:#x}, start_height={}, stop_hash={:x})".format(
1831              self.filter_type, self.start_height, self.stop_hash)
1832  
1833  class msg_cfheaders:
1834      __slots__ = ("filter_type", "stop_hash", "prev_header", "hashes")
1835      msgtype =  b"cfheaders"
1836  
1837      def __init__(self, filter_type=None, stop_hash=None, prev_header=None, hashes=None):
1838          self.filter_type = filter_type
1839          self.stop_hash = stop_hash
1840          self.prev_header = prev_header
1841          self.hashes = hashes
1842  
1843      def deserialize(self, f):
1844          self.filter_type = int.from_bytes(f.read(1), "little")
1845          self.stop_hash = deser_uint256(f)
1846          self.prev_header = deser_uint256(f)
1847          self.hashes = deser_uint256_vector(f)
1848  
1849      def serialize(self):
1850          r = b""
1851          r += self.filter_type.to_bytes(1, "little")
1852          r += ser_uint256(self.stop_hash)
1853          r += ser_uint256(self.prev_header)
1854          r += ser_uint256_vector(self.hashes)
1855          return r
1856  
1857      def __repr__(self):
1858          return "msg_cfheaders(filter_type={:#x}, stop_hash={:x})".format(
1859              self.filter_type, self.stop_hash)
1860  
1861  class msg_getcfcheckpt:
1862      __slots__ = ("filter_type", "stop_hash")
1863      msgtype =  b"getcfcheckpt"
1864  
1865      def __init__(self, filter_type=None, stop_hash=None):
1866          self.filter_type = filter_type
1867          self.stop_hash = stop_hash
1868  
1869      def deserialize(self, f):
1870          self.filter_type = int.from_bytes(f.read(1), "little")
1871          self.stop_hash = deser_uint256(f)
1872  
1873      def serialize(self):
1874          r = b""
1875          r += self.filter_type.to_bytes(1, "little")
1876          r += ser_uint256(self.stop_hash)
1877          return r
1878  
1879      def __repr__(self):
1880          return "msg_getcfcheckpt(filter_type={:#x}, stop_hash={:x})".format(
1881              self.filter_type, self.stop_hash)
1882  
1883  class msg_cfcheckpt:
1884      __slots__ = ("filter_type", "stop_hash", "headers")
1885      msgtype =  b"cfcheckpt"
1886  
1887      def __init__(self, filter_type=None, stop_hash=None, headers=None):
1888          self.filter_type = filter_type
1889          self.stop_hash = stop_hash
1890          self.headers = headers
1891  
1892      def deserialize(self, f):
1893          self.filter_type = int.from_bytes(f.read(1), "little")
1894          self.stop_hash = deser_uint256(f)
1895          self.headers = deser_uint256_vector(f)
1896  
1897      def serialize(self):
1898          r = b""
1899          r += self.filter_type.to_bytes(1, "little")
1900          r += ser_uint256(self.stop_hash)
1901          r += ser_uint256_vector(self.headers)
1902          return r
1903  
1904      def __repr__(self):
1905          return "msg_cfcheckpt(filter_type={:#x}, stop_hash={:x})".format(
1906              self.filter_type, self.stop_hash)
1907  
1908  class msg_sendtxrcncl:
1909      __slots__ = ("version", "salt")
1910      msgtype = b"sendtxrcncl"
1911  
1912      def __init__(self):
1913          self.version = 0
1914          self.salt = 0
1915  
1916      def deserialize(self, f):
1917          self.version = int.from_bytes(f.read(4), "little")
1918          self.salt = int.from_bytes(f.read(8), "little")
1919  
1920      def serialize(self):
1921          r = b""
1922          r += self.version.to_bytes(4, "little")
1923          r += self.salt.to_bytes(8, "little")
1924          return r
1925  
1926      def __repr__(self):
1927          return "msg_sendtxrcncl(version=%lu, salt=%lu)" %\
1928              (self.version, self.salt)
1929  
1930  class TestFrameworkScript(unittest.TestCase):
1931      def test_addrv2_encode_decode(self):
1932          def check_addrv2(ip, net):
1933              addr = CAddress()
1934              addr.net, addr.ip = net, ip
1935              ser = addr.serialize_v2()
1936              actual = CAddress()
1937              actual.deserialize_v2(BytesIO(ser))
1938              self.assertEqual(actual, addr)
1939  
1940          check_addrv2("1.65.195.98", CAddress.NET_IPV4)
1941          check_addrv2("2001:41f0::62:6974:636f:696e", CAddress.NET_IPV6)
1942          check_addrv2("2bqghnldu6mcug4pikzprwhtjjnsyederctvci6klcwzepnjd46ikjyd.onion", CAddress.NET_TORV3)
1943          check_addrv2("255fhcp6ajvftnyo7bwz3an3t4a4brhopm3bamyh2iu5r3gnr2rq.b32.i2p", CAddress.NET_I2P)
1944          check_addrv2("fc32:17ea:e415:c3bf:9808:149d:b5a2:c9aa", CAddress.NET_CJDNS)
1945  
1946      def test_varint_encode_decode(self):
1947          def check_varint(num, expected_encoding_hex):
1948              expected_encoding = bytes.fromhex(expected_encoding_hex)
1949              self.assertEqual(ser_varint(num), expected_encoding)
1950              self.assertEqual(deser_varint(BytesIO(expected_encoding)), num)
1951  
1952          # test cases from serialize_tests.cpp:varint_bitpatterns
1953          check_varint(0, "00")
1954          check_varint(0x7f, "7f")
1955          check_varint(0x80, "8000")
1956          check_varint(0x1234, "a334")
1957          check_varint(0xffff, "82fe7f")
1958          check_varint(0x123456, "c7e756")
1959          check_varint(0x80123456, "86ffc7e756")
1960          check_varint(0xffffffff, "8efefefe7f")
1961          check_varint(0xffffffffffffffff, "80fefefefefefefefe7f")