Skip to content

Commit fa826db

Browse files
author
MarcoFalke
committed
scripted-diff: test: Use int.to_bytes over struct packing
-BEGIN VERIFY SCRIPT- sed -i --regexp-extended 's!struct.pack\(.<?B., (.*)\)!\1.to_bytes(1, "little")!g' $( git grep -l struct.pack ) sed -i --regexp-extended 's!struct.pack\(.<I., (.*)\)!\1.to_bytes(4, "little")!g' $( git grep -l struct.pack ) sed -i --regexp-extended 's!struct.pack\(.<H., (.*)\)!\1.to_bytes(2, "little")!g' $( git grep -l struct.pack ) sed -i --regexp-extended 's!struct.pack\(.<i., (.*)\)!\1.to_bytes(4, "little", signed=True)!g' $( git grep -l struct.pack ) sed -i --regexp-extended 's!struct.pack\(.<q., (.*)\)!\1.to_bytes(8, "little", signed=True)!g' $( git grep -l struct.pack ) sed -i --regexp-extended 's!struct.pack\(.>H., (.*)\)!\1.to_bytes(2, "big")!g' $( git grep -l struct.pack ) -END VERIFY SCRIPT-
1 parent faf2a97 commit fa826db

File tree

6 files changed

+32
-32
lines changed

6 files changed

+32
-32
lines changed

contrib/seeds/generate-seeds.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def parse_spec(s):
115115
def ser_compact_size(l):
116116
r = b""
117117
if l < 253:
118-
r = struct.pack("B", l)
118+
r = l.to_bytes(1, "little")
119119
elif l < 0x10000:
120120
r = struct.pack("<BH", 253, l)
121121
elif l < 0x100000000:
@@ -129,10 +129,10 @@ def bip155_serialize(spec):
129129
Serialize (networkID, addr, port) tuple to BIP155 binary format.
130130
'''
131131
r = b""
132-
r += struct.pack('B', spec[0].value)
132+
r += spec[0].value.to_bytes(1, "little")
133133
r += ser_compact_size(len(spec[1]))
134134
r += spec[1]
135-
r += struct.pack('>H', spec[2])
135+
r += spec[2].to_bytes(2, "big")
136136
return r
137137

138138
def process_nodes(g, f, structname):

contrib/signet/miner

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def signet_txs(block, challenge):
5252
mroot = block.get_merkle_root(hashes)
5353

5454
sd = b""
55-
sd += struct.pack("<i", block.nVersion)
55+
sd += block.nVersion.to_bytes(4, "little", signed=True)
5656
sd += ser_uint256(block.hashPrevBlock)
5757
sd += ser_uint256(mroot)
58-
sd += struct.pack("<I", block.nTime)
58+
sd += block.nTime.to_bytes(4, "little")
5959

6060
to_spend = CTransaction()
6161
to_spend.nVersion = 0

test/functional/feature_addrman.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ def serialize_addrman(
2828
tried = []
2929
INCOMPATIBILITY_BASE = 32
3030
r = MAGIC_BYTES[net_magic]
31-
r += struct.pack("B", format)
32-
r += struct.pack("B", (INCOMPATIBILITY_BASE + lowest_compatible))
31+
r += format.to_bytes(1, "little")
32+
r += (INCOMPATIBILITY_BASE + lowest_compatible).to_bytes(1, "little")
3333
r += ser_uint256(bucket_key)
34-
r += struct.pack("<i", (len_new or len(new)))
35-
r += struct.pack("<i", (len_tried or len(tried)))
34+
r += (len_new or len(new)).to_bytes(4, "little", signed=True)
35+
r += (len_tried or len(tried)).to_bytes(4, "little", signed=True)
3636
ADDRMAN_NEW_BUCKET_COUNT = 1 << 10
37-
r += struct.pack("<i", (ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30)))
37+
r += (ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30)).to_bytes(4, "little", signed=True)
3838
for _ in range(ADDRMAN_NEW_BUCKET_COUNT):
39-
r += struct.pack("<i", (0))
39+
r += (0).to_bytes(4, "little", signed=True)
4040
checksum = hash256(r)
4141
r += mock_checksum or checksum
4242
return r

test/functional/p2p_segwit.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,16 +1165,16 @@ def serialize_with_witness(self):
11651165
if not self.wit.is_null():
11661166
flags |= 1
11671167
r = b""
1168-
r += struct.pack("<i", self.nVersion)
1168+
r += self.nVersion.to_bytes(4, "little", signed=True)
11691169
if flags:
11701170
dummy = []
11711171
r += ser_vector(dummy)
1172-
r += struct.pack("<B", flags)
1172+
r += flags.to_bytes(1, "little")
11731173
r += ser_vector(self.vin)
11741174
r += ser_vector(self.vout)
11751175
if flags & 1:
11761176
r += self.wit.serialize()
1177-
r += struct.pack("<I", self.nLockTime)
1177+
r += self.nLockTime.to_bytes(4, "little")
11781178
return r
11791179

11801180
tx2 = BrokenCTransaction()
@@ -1976,11 +1976,11 @@ def test_superfluous_witness(self):
19761976
def serialize_with_bogus_witness(tx):
19771977
flags = 3
19781978
r = b""
1979-
r += struct.pack("<i", tx.nVersion)
1979+
r += tx.nVersion.to_bytes(4, "little", signed=True)
19801980
if flags:
19811981
dummy = []
19821982
r += ser_vector(dummy)
1983-
r += struct.pack("<B", flags)
1983+
r += flags.to_bytes(1, "little")
19841984
r += ser_vector(tx.vin)
19851985
r += ser_vector(tx.vout)
19861986
if flags & 1:
@@ -1990,7 +1990,7 @@ def serialize_with_bogus_witness(tx):
19901990
for _ in range(len(tx.wit.vtxinwit), len(tx.vin)):
19911991
tx.wit.vtxinwit.append(CTxInWitness())
19921992
r += tx.wit.serialize()
1993-
r += struct.pack("<I", tx.nLockTime)
1993+
r += tx.nLockTime.to_bytes(4, "little")
19941994
return r
19951995

19961996
class msg_bogus_tx(msg_tx):

test/functional/test_framework/p2p.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def build_message(self, message, is_decoy=False):
411411
tmsg = self.magic_bytes
412412
tmsg += msgtype
413413
tmsg += b"\x00" * (12 - len(msgtype))
414-
tmsg += struct.pack("<I", len(data))
414+
tmsg += len(data).to_bytes(4, "little")
415415
th = sha256(data)
416416
h = sha256(th)
417417
tmsg += h[:4]

test/functional/test_framework/script.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ def encode_op_pushdata(d):
5858
elif len(d) <= 0xff:
5959
return b'\x4c' + bytes([len(d)]) + d # OP_PUSHDATA1
6060
elif len(d) <= 0xffff:
61-
return b'\x4d' + struct.pack('<H', len(d)) + d # OP_PUSHDATA2
61+
return b'\x4d' + len(d).to_bytes(2, "little") + d # OP_PUSHDATA2
6262
elif len(d) <= 0xffffffff:
63-
return b'\x4e' + struct.pack('<I', len(d)) + d # OP_PUSHDATA4
63+
return b'\x4e' + len(d).to_bytes(4, "little") + d # OP_PUSHDATA4
6464
else:
6565
raise ValueError("Data too long to encode in a PUSHDATA op")
6666

@@ -670,7 +670,7 @@ def LegacySignatureMsg(script, txTo, inIdx, hashtype):
670670
txtmp.vin.append(tmp)
671671

672672
s = txtmp.serialize_without_witness()
673-
s += struct.pack("<I", hashtype)
673+
s += hashtype.to_bytes(4, "little")
674674

675675
return (s, None)
676676

@@ -726,7 +726,7 @@ def SegwitV0SignatureMsg(script, txTo, inIdx, hashtype, amount):
726726
if (not (hashtype & SIGHASH_ANYONECANPAY) and (hashtype & 0x1f) != SIGHASH_SINGLE and (hashtype & 0x1f) != SIGHASH_NONE):
727727
serialize_sequence = bytes()
728728
for i in txTo.vin:
729-
serialize_sequence += struct.pack("<I", i.nSequence)
729+
serialize_sequence += i.nSequence.to_bytes(4, "little")
730730
hashSequence = uint256_from_str(hash256(serialize_sequence))
731731

732732
if ((hashtype & 0x1f) != SIGHASH_SINGLE and (hashtype & 0x1f) != SIGHASH_NONE):
@@ -739,16 +739,16 @@ def SegwitV0SignatureMsg(script, txTo, inIdx, hashtype, amount):
739739
hashOutputs = uint256_from_str(hash256(serialize_outputs))
740740

741741
ss = bytes()
742-
ss += struct.pack("<i", txTo.nVersion)
742+
ss += txTo.nVersion.to_bytes(4, "little", signed=True)
743743
ss += ser_uint256(hashPrevouts)
744744
ss += ser_uint256(hashSequence)
745745
ss += txTo.vin[inIdx].prevout.serialize()
746746
ss += ser_string(script)
747-
ss += struct.pack("<q", amount)
748-
ss += struct.pack("<I", txTo.vin[inIdx].nSequence)
747+
ss += amount.to_bytes(8, "little", signed=True)
748+
ss += txTo.vin[inIdx].nSequence.to_bytes(4, "little")
749749
ss += ser_uint256(hashOutputs)
750750
ss += txTo.nLockTime.to_bytes(4, "little")
751-
ss += struct.pack("<I", hashtype)
751+
ss += hashtype.to_bytes(4, "little")
752752
return ss
753753

754754
def SegwitV0SignatureHash(*args, **kwargs):
@@ -818,8 +818,8 @@ def TaprootSignatureMsg(txTo, spent_utxos, hash_type, input_index = 0, scriptpat
818818
in_type = hash_type & SIGHASH_ANYONECANPAY
819819
spk = spent_utxos[input_index].scriptPubKey
820820
ss = bytes([0, hash_type]) # epoch, hash_type
821-
ss += struct.pack("<i", txTo.nVersion)
822-
ss += struct.pack("<I", txTo.nLockTime)
821+
ss += txTo.nVersion.to_bytes(4, "little", signed=True)
822+
ss += txTo.nLockTime.to_bytes(4, "little")
823823
if in_type != SIGHASH_ANYONECANPAY:
824824
ss += BIP341_sha_prevouts(txTo)
825825
ss += BIP341_sha_amounts(spent_utxos)
@@ -835,11 +835,11 @@ def TaprootSignatureMsg(txTo, spent_utxos, hash_type, input_index = 0, scriptpat
835835
ss += bytes([spend_type])
836836
if in_type == SIGHASH_ANYONECANPAY:
837837
ss += txTo.vin[input_index].prevout.serialize()
838-
ss += struct.pack("<q", spent_utxos[input_index].nValue)
838+
ss += spent_utxos[input_index].nValue.to_bytes(8, "little", signed=True)
839839
ss += ser_string(spk)
840-
ss += struct.pack("<I", txTo.vin[input_index].nSequence)
840+
ss += txTo.vin[input_index].nSequence.to_bytes(4, "little")
841841
else:
842-
ss += struct.pack("<I", input_index)
842+
ss += input_index.to_bytes(4, "little")
843843
if (spend_type & 1):
844844
ss += sha256(ser_string(annex))
845845
if out_type == SIGHASH_SINGLE:
@@ -850,7 +850,7 @@ def TaprootSignatureMsg(txTo, spent_utxos, hash_type, input_index = 0, scriptpat
850850
if (scriptpath):
851851
ss += TaggedHash("TapLeaf", bytes([leaf_ver]) + ser_string(script))
852852
ss += bytes([0])
853-
ss += struct.pack("<i", codeseparator_pos)
853+
ss += codeseparator_pos.to_bytes(4, "little", signed=True)
854854
assert len(ss) == 175 - (in_type == SIGHASH_ANYONECANPAY) * 49 - (out_type != SIGHASH_ALL and out_type != SIGHASH_SINGLE) * 32 + (annex is not None) * 32 + scriptpath * 37
855855
return ss
856856

0 commit comments

Comments
 (0)