aboutsummaryrefslogtreecommitdiff
path: root/test/functional/test_framework/messages.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/functional/test_framework/messages.py')
-rwxr-xr-xtest/functional/test_framework/messages.py75
1 files changed, 54 insertions, 21 deletions
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 5a9736a7a3..65d90f8448 100755
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -18,7 +18,7 @@ ser_*, deser_*: functions that handle serialization/deserialization.
Classes use __slots__ to ensure extraneous attributes aren't accidentally added
by tests, compromising their intended effect.
"""
-from codecs import encode
+from base64 import b32decode, b32encode
import copy
import hashlib
from io import BytesIO
@@ -29,17 +29,17 @@ import struct
import time
from test_framework.siphash import siphash256
-from test_framework.util import hex_str_to_bytes, assert_equal
+from test_framework.util import assert_equal
MAX_LOCATOR_SZ = 101
-MAX_BLOCK_BASE_SIZE = 1000000
+MAX_BLOCK_WEIGHT = 4000000
MAX_BLOOM_FILTER_SIZE = 36000
MAX_BLOOM_HASH_FUNCS = 50
COIN = 100000000 # 1 btc in satoshis
MAX_MONEY = 21000000 * COIN
-BIP125_SEQUENCE_NUMBER = 0xfffffffd # Sequence number that is BIP 125 opt-in and BIP 68-opt-out
+BIP125_SEQUENCE_NUMBER = 0xfffffffd # Sequence number that is rbf-opt-in (BIP 125) and csv-opt-out (BIP 68)
MAX_PROTOCOL_MESSAGE_LENGTH = 4000000 # Maximum length of incoming protocol messages
MAX_HEADERS_RESULTS = 2000 # Number of headers sent in one getheaders result
@@ -190,14 +190,20 @@ def ser_string_vector(l):
return r
-# Deserialize from a hex string representation (eg from RPC)
-def FromHex(obj, hex_string):
- obj.deserialize(BytesIO(hex_str_to_bytes(hex_string)))
+def from_hex(obj, hex_string):
+ """Deserialize from a hex string representation (e.g. from RPC)
+
+ Note that there is no complementary helper like e.g. `to_hex` for the
+ inverse operation. To serialize a message object to a hex string, simply
+ use obj.serialize().hex()"""
+ obj.deserialize(BytesIO(bytes.fromhex(hex_string)))
return obj
-# Convert a binary-serializable object to hex (eg for submission via RPC)
-def ToHex(obj):
- return obj.serialize().hex()
+
+def tx_from_hex(hex_string):
+ """Deserialize from hex string to a transaction object"""
+ return from_hex(CTransaction(), hex_string)
+
# Objects that map to bitcoind objects, which can be serialized/deserialized
@@ -207,15 +213,20 @@ class CAddress:
# see https://github.com/bitcoin/bips/blob/master/bip-0155.mediawiki
NET_IPV4 = 1
+ NET_I2P = 5
ADDRV2_NET_NAME = {
- NET_IPV4: "IPv4"
+ NET_IPV4: "IPv4",
+ NET_I2P: "I2P"
}
ADDRV2_ADDRESS_LENGTH = {
- NET_IPV4: 4
+ NET_IPV4: 4,
+ NET_I2P: 32
}
+ I2P_PAD = "===="
+
def __init__(self):
self.time = 0
self.nServices = 1
@@ -223,6 +234,9 @@ class CAddress:
self.ip = "0.0.0.0"
self.port = 0
+ def __eq__(self, other):
+ 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
+
def deserialize(self, f, *, with_time=True):
"""Deserialize from addrv1 format (pre-BIP155)"""
if with_time:
@@ -255,24 +269,33 @@ class CAddress:
self.nServices = deser_compact_size(f)
self.net = struct.unpack("B", f.read(1))[0]
- assert self.net == self.NET_IPV4
+ assert self.net in (self.NET_IPV4, self.NET_I2P)
address_length = deser_compact_size(f)
assert address_length == self.ADDRV2_ADDRESS_LENGTH[self.net]
- self.ip = socket.inet_ntoa(f.read(4))
+ addr_bytes = f.read(address_length)
+ if self.net == self.NET_IPV4:
+ self.ip = socket.inet_ntoa(addr_bytes)
+ else:
+ self.ip = b32encode(addr_bytes)[0:-len(self.I2P_PAD)].decode("ascii").lower() + ".b32.i2p"
self.port = struct.unpack(">H", f.read(2))[0]
def serialize_v2(self):
"""Serialize in addrv2 format (BIP155)"""
- assert self.net == self.NET_IPV4
+ assert self.net in (self.NET_IPV4, self.NET_I2P)
r = b""
r += struct.pack("<I", self.time)
r += ser_compact_size(self.nServices)
r += struct.pack("B", self.net)
r += ser_compact_size(self.ADDRV2_ADDRESS_LENGTH[self.net])
- r += socket.inet_aton(self.ip)
+ if self.net == self.NET_IPV4:
+ r += socket.inet_aton(self.ip)
+ else:
+ sfx = ".b32.i2p"
+ assert self.ip.endswith(sfx)
+ r += b32decode(self.ip[0:-len(sfx)] + self.I2P_PAD, True)
r += struct.pack(">H", self.port)
return r
@@ -584,12 +607,15 @@ class CTransaction:
return False
return True
- # Calculate the virtual transaction size using witness and non-witness
+ # Calculate the transaction weight using witness and non-witness
# serialization size (does NOT use sigops).
- def get_vsize(self):
+ def get_weight(self):
with_witness_size = len(self.serialize_with_witness())
without_witness_size = len(self.serialize_without_witness())
- return math.ceil(((WITNESS_SCALE_FACTOR - 1) * without_witness_size + with_witness_size) / WITNESS_SCALE_FACTOR)
+ return (WITNESS_SCALE_FACTOR - 1) * without_witness_size + with_witness_size
+
+ def get_vsize(self):
+ return math.ceil(self.get_weight() / WITNESS_SCALE_FACTOR)
def __repr__(self):
return "CTransaction(nVersion=%i vin=%s vout=%s wit=%s nLockTime=%i)" \
@@ -615,7 +641,7 @@ class CBlockHeader:
self.calc_sha256()
def set_null(self):
- self.nVersion = 1
+ self.nVersion = 4
self.hashPrevBlock = 0
self.hashMerkleRoot = 0
self.nTime = 0
@@ -654,7 +680,7 @@ class CBlockHeader:
r += struct.pack("<I", self.nBits)
r += struct.pack("<I", self.nNonce)
self.sha256 = uint256_from_str(hash256(r))
- self.hash = encode(hash256(r)[::-1], 'hex_codec').decode('ascii')
+ self.hash = hash256(r)[::-1].hex()
def rehash(self):
self.sha256 = None
@@ -737,6 +763,13 @@ class CBlock(CBlockHeader):
self.nNonce += 1
self.rehash()
+ # Calculate the block weight using witness and non-witness
+ # serialization size (does NOT use sigops).
+ def get_weight(self):
+ with_witness_size = len(self.serialize(with_witness=True))
+ without_witness_size = len(self.serialize(with_witness=False))
+ return (WITNESS_SCALE_FACTOR - 1) * without_witness_size + with_witness_size
+
def __repr__(self):
return "CBlock(nVersion=%i hashPrevBlock=%064x hashMerkleRoot=%064x nTime=%s nBits=%08x nNonce=%08x vtx=%s)" \
% (self.nVersion, self.hashPrevBlock, self.hashMerkleRoot,