@@ -452,7 +452,7 @@ def deserialize(self, f):
452
452
else :
453
453
self .vout = deser_vector (f , CTxOut )
454
454
if flags != 0 :
455
- self .wit .vtxinwit = [CTxInWitness ()] * len (self .vin )
455
+ self .wit .vtxinwit = [CTxInWitness () for i in range ( len (self .vin ))]
456
456
self .wit .deserialize (f )
457
457
self .nLockTime = struct .unpack ("<I" , f .read (4 ))[0 ]
458
458
self .sha256 = None
@@ -518,8 +518,8 @@ def is_valid(self):
518
518
return True
519
519
520
520
def __repr__ (self ):
521
- return "CTransaction(nVersion=%i vin=%s vout=%s nLockTime=%i)" \
522
- % (self .nVersion , repr (self .vin ), repr (self .vout ), self .nLockTime )
521
+ return "CTransaction(nVersion=%i vin=%s vout=%s wit=%s nLockTime=%i)" \
522
+ % (self .nVersion , repr (self .vin ), repr (self .vout ), repr ( self . wit ), self .nLockTime )
523
523
524
524
525
525
class CBlockHeader (object ):
@@ -755,6 +755,9 @@ def serialize(self, with_witness=False):
755
755
r += self .tx .serialize_without_witness ()
756
756
return r
757
757
758
+ def serialize_with_witness (self ):
759
+ return self .serialize (with_witness = True )
760
+
758
761
def __repr__ (self ):
759
762
return "PrefilledTransaction(index=%d, tx=%s)" % (self .index , repr (self .tx ))
760
763
@@ -779,6 +782,7 @@ def deserialize(self, f):
779
782
self .prefilled_txn = deser_vector (f , PrefilledTransaction )
780
783
self .prefilled_txn_length = len (self .prefilled_txn )
781
784
785
+ # When using version 2 compact blocks, we must serialize with_witness.
782
786
def serialize (self , with_witness = False ):
783
787
r = b""
784
788
r += self .header .serialize ()
@@ -787,12 +791,20 @@ def serialize(self, with_witness=False):
787
791
for x in self .shortids :
788
792
# We only want the first 6 bytes
789
793
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 )
791
798
return r
792
799
793
800
def __repr__ (self ):
794
801
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 ))
795
802
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 )
796
808
797
809
# Calculate the BIP 152-compact blocks shortid for a given transaction hash
798
810
def calculate_shortid (k0 , k1 , tx_hash ):
@@ -808,6 +820,7 @@ def __init__(self, p2pheaders_and_shortids = None):
808
820
self .nonce = 0
809
821
self .shortids = []
810
822
self .prefilled_txn = []
823
+ self .use_witness = False
811
824
812
825
if p2pheaders_and_shortids != None :
813
826
self .header = p2pheaders_and_shortids .header
@@ -819,7 +832,10 @@ def __init__(self, p2pheaders_and_shortids = None):
819
832
last_index = self .prefilled_txn [- 1 ].index
820
833
821
834
def to_p2p (self ):
822
- ret = P2PHeaderAndShortIDs ()
835
+ if self .use_witness :
836
+ ret = P2PHeaderAndShortWitnessIDs ()
837
+ else :
838
+ ret = P2PHeaderAndShortIDs ()
823
839
ret .header = self .header
824
840
ret .nonce = self .nonce
825
841
ret .shortids_length = len (self .shortids )
@@ -840,15 +856,20 @@ def get_siphash_keys(self):
840
856
key1 = struct .unpack ("<Q" , hash_header_nonce_as_str [8 :16 ])[0 ]
841
857
return [ key0 , key1 ]
842
858
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 ):
844
861
self .header = CBlockHeader (block )
845
862
self .nonce = nonce
846
863
self .prefilled_txn = [ PrefilledTransaction (i , block .vtx [i ]) for i in prefill_list ]
847
864
self .shortids = []
865
+ self .use_witness = use_witness
848
866
[k0 , k1 ] = self .get_siphash_keys ()
849
867
for i in range (len (block .vtx )):
850
868
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 ))
852
873
853
874
def __repr__ (self ):
854
875
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):
1424
1445
def __repr__ (self ):
1425
1446
return "msg_blocktxn(block_transactions=%s)" % (repr (self .block_transactions ))
1426
1447
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
+
1427
1454
# This is what a callback should look like for NodeConn
1428
1455
# Reimplement the on_* functions to provide handling for events
1429
1456
class NodeConnCB (object ):
0 commit comments