Skip to content

Commit 422fac6

Browse files
sdaftuarsipa
authored andcommitted
[qa] Add support for compactblocks v2 to mininode
1 parent f5b9b8f commit 422fac6

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

qa/rpc-tests/test_framework/mininode.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,9 @@ def serialize(self, with_witness=False):
755755
r += self.tx.serialize_without_witness()
756756
return r
757757

758+
def serialize_with_witness(self):
759+
return self.serialize(with_witness=True)
760+
758761
def __repr__(self):
759762
return "PrefilledTransaction(index=%d, tx=%s)" % (self.index, repr(self.tx))
760763

@@ -779,6 +782,7 @@ def deserialize(self, f):
779782
self.prefilled_txn = deser_vector(f, PrefilledTransaction)
780783
self.prefilled_txn_length = len(self.prefilled_txn)
781784

785+
# When using version 2 compact blocks, we must serialize with_witness.
782786
def serialize(self, with_witness=False):
783787
r = b""
784788
r += self.header.serialize()
@@ -787,12 +791,20 @@ def serialize(self, with_witness=False):
787791
for x in self.shortids:
788792
# We only want the first 6 bytes
789793
r += struct.pack("<Q", x)[0:6]
790-
r += ser_vector(self.prefilled_txn)
794+
if with_witness:
795+
r += ser_vector(self.prefilled_txn, "serialize_with_witness")
796+
else:
797+
r += ser_vector(self.prefilled_txn)
791798
return r
792799

793800
def __repr__(self):
794801
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))
795802

803+
# P2P version of the above that will use witness serialization (for compact
804+
# block version 2)
805+
class P2PHeaderAndShortWitnessIDs(P2PHeaderAndShortIDs):
806+
def serialize(self):
807+
return super(P2PHeaderAndShortWitnessIDs, self).serialize(with_witness=True)
796808

797809
# Calculate the BIP 152-compact blocks shortid for a given transaction hash
798810
def calculate_shortid(k0, k1, tx_hash):
@@ -808,6 +820,7 @@ def __init__(self, p2pheaders_and_shortids = None):
808820
self.nonce = 0
809821
self.shortids = []
810822
self.prefilled_txn = []
823+
self.use_witness = False
811824

812825
if p2pheaders_and_shortids != None:
813826
self.header = p2pheaders_and_shortids.header
@@ -819,7 +832,10 @@ def __init__(self, p2pheaders_and_shortids = None):
819832
last_index = self.prefilled_txn[-1].index
820833

821834
def to_p2p(self):
822-
ret = P2PHeaderAndShortIDs()
835+
if self.use_witness:
836+
ret = P2PHeaderAndShortWitnessIDs()
837+
else:
838+
ret = P2PHeaderAndShortIDs()
823839
ret.header = self.header
824840
ret.nonce = self.nonce
825841
ret.shortids_length = len(self.shortids)
@@ -840,15 +856,20 @@ def get_siphash_keys(self):
840856
key1 = struct.unpack("<Q", hash_header_nonce_as_str[8:16])[0]
841857
return [ key0, key1 ]
842858

843-
def initialize_from_block(self, block, nonce=0, prefill_list = [0]):
859+
# Version 2 compact blocks use wtxid in shortids (rather than txid)
860+
def initialize_from_block(self, block, nonce=0, prefill_list = [0], use_witness = False):
844861
self.header = CBlockHeader(block)
845862
self.nonce = nonce
846863
self.prefilled_txn = [ PrefilledTransaction(i, block.vtx[i]) for i in prefill_list ]
847864
self.shortids = []
865+
self.use_witness = use_witness
848866
[k0, k1] = self.get_siphash_keys()
849867
for i in range(len(block.vtx)):
850868
if i not in prefill_list:
851-
self.shortids.append(calculate_shortid(k0, k1, block.vtx[i].sha256))
869+
tx_hash = block.vtx[i].sha256
870+
if use_witness:
871+
tx_hash = block.vtx[i].calc_sha256(with_witness=True)
872+
self.shortids.append(calculate_shortid(k0, k1, tx_hash))
852873

853874
def __repr__(self):
854875
return "HeaderAndShortIDs(header=%s, nonce=%d, shortids=%s, prefilledtxn=%s" % (repr(self.header), self.nonce, repr(self.shortids), repr(self.prefilled_txn))
@@ -1424,6 +1445,12 @@ def serialize(self):
14241445
def __repr__(self):
14251446
return "msg_blocktxn(block_transactions=%s)" % (repr(self.block_transactions))
14261447

1448+
class msg_witness_blocktxn(msg_blocktxn):
1449+
def serialize(self):
1450+
r = b""
1451+
r += self.block_transactions.serialize(with_witness=True)
1452+
return r
1453+
14271454
# This is what a callback should look like for NodeConn
14281455
# Reimplement the on_* functions to provide handling for events
14291456
class NodeConnCB(object):

0 commit comments

Comments
 (0)