mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-15 08:31:49 +01:00
Merge bitcoin/bitcoin#32421: test: refactor: overhaul (w)txid determination for CTransaction objects
4ef6253017test: avoid unneeded (w)txid hex -> integer conversions (Sebastian Falbesoner)472f3770aescripted-diff: test: rename CTransaction `.getwtxid()` -> `wtxid_hex` for consistency (Sebastian Falbesoner)81af4334e8test: rename CTransaction `.sha256` -> `.txid_int` for consistency (Sebastian Falbesoner)ce83924237test: rename CTransaction `.rehash()`/`.hash` -> `.txid_hex` for consistency (Sebastian Falbesoner)e9cdaefb0atest: introduce and use CTransaction `.wtxid_int` property (Sebastian Falbesoner)9b3dce24a3test: remove bare CTransaction `.rehash()`/`.calc_sha256()` calls (Sebastian Falbesoner)a2724e3ea3test: remove txid caching in CTransaction class (Sebastian Falbesoner) Pull request description: In the functional test framework, determining a (w)txid for a `CTransaction` instance is currently rather confusing and footgunny due to inconsistent naming/interfaces (see table below) and statefulness involved. This PR aims to improve that by: * removing the (w)txid caching mechanism, in order to avoid the need to call additional rehashing functions (`.rehash()`/`.calculate_sha256()`, see first two commits and https://github.com/bitcoin/bitcoin/pull/32050#discussion_r1993286997). This change in theory decreases the performance, as the involved serialization and hashing involved might be called more often than previously, but I couldn't find a functional test where this leads to a measurable run-time increase on my machine. * introduce consistent naming that shows the type of the returned txid, i.e. hex string vs. test-framework-internal representation [currently integers] (see remaining commits) Summary table showing (w)txid determaination before/after this PR: | Task | master | PR | |:-----------------------|:-----------------------|:-------------| | get TXID (hex string) | `.rehash()` / `.hash`[1] | `.txid_hex` | | get TXID (integer) | `.sha256`[1] | `.txid_int` | | get WTXID (hex string) | `.getwtxid()` | `.wtxid_hex` | | get WTXID (integer) | `.calc_sha256(True)` | `.wtxid_int` | Unfortunately, most renames can't be done with a scripted-diff, as the property names (`.hash`, `.sha256`) are also used for blocks and other message types. The PR is rather invasive and touches a lot of files, but I think it's worth to do it, also to make life easier for new contributors. Future tasks like e.g. doing the same overhaul for block (header) objects or getting rid of the integer representation (see https://github.com/bitcoin/bitcoin/pull/32050) become easier should become easier after this one. [1] = returned value might be out-of-date, if rehashing function wasn't called after modification ACKs for top commit: maflcko: re-ACK4ef6253017🏈 achow101: ACK4ef6253017marcofleon: code review ACK4ef6253017Tree-SHA512: 4b472c31d169966b6f6878911a8404d25bf3e503b6e8ef30f36a7415d21ad4bc1265083af2d3ead6edfcd9fac9ccb0a8be57e1b0739ad431b836413070d7d583
This commit is contained in:
@@ -40,8 +40,7 @@ def signet_txs(block, challenge):
|
||||
txs[0].vout[-1].scriptPubKey += CScriptOp.encode_op_pushdata(SIGNET_HEADER)
|
||||
hashes = []
|
||||
for tx in txs:
|
||||
tx.rehash()
|
||||
hashes.append(ser_uint256(tx.sha256))
|
||||
hashes.append(ser_uint256(tx.txid_int))
|
||||
mroot = block.get_merkle_root(hashes)
|
||||
|
||||
sd = b""
|
||||
@@ -55,12 +54,11 @@ def signet_txs(block, challenge):
|
||||
to_spend.nLockTime = 0
|
||||
to_spend.vin = [CTxIn(COutPoint(0, 0xFFFFFFFF), b"\x00" + CScriptOp.encode_op_pushdata(sd), 0)]
|
||||
to_spend.vout = [CTxOut(0, challenge)]
|
||||
to_spend.rehash()
|
||||
|
||||
spend = CTransaction()
|
||||
spend.version = 0
|
||||
spend.nLockTime = 0
|
||||
spend.vin = [CTxIn(COutPoint(to_spend.sha256, 0), b"", 0)]
|
||||
spend.vin = [CTxIn(COutPoint(to_spend.txid_int, 0), b"", 0)]
|
||||
spend.vout = [CTxOut(0, b"\x6a")]
|
||||
|
||||
return spend, to_spend
|
||||
@@ -88,7 +86,6 @@ def finish_block(block, signet_solution, grind_cmd):
|
||||
pass # Don't need to add a signet commitment if there's no signet signature needed
|
||||
else:
|
||||
block.vtx[0].vout[-1].scriptPubKey += CScriptOp.encode_op_pushdata(SIGNET_HEADER + signet_solution)
|
||||
block.vtx[0].rehash()
|
||||
block.hashMerkleRoot = block.calc_merkle_root()
|
||||
if grind_cmd is None:
|
||||
block.solve()
|
||||
@@ -111,7 +108,6 @@ def new_block(tmpl, reward_spk, *, blocktime=None, poolid=None):
|
||||
cbtx.vin = [CTxIn(COutPoint(0, 0xffffffff), scriptSig, MAX_SEQUENCE_NONFINAL)]
|
||||
cbtx.vout = [CTxOut(tmpl["coinbasevalue"], reward_spk)]
|
||||
cbtx.vin[0].nSequence = 2**32-2
|
||||
cbtx.rehash()
|
||||
|
||||
block = CBlock()
|
||||
block.nVersion = tmpl["version"]
|
||||
@@ -130,7 +126,6 @@ def new_block(tmpl, reward_spk, *, blocktime=None, poolid=None):
|
||||
block.vtx[0].wit.vtxinwit = [cbwit]
|
||||
block.vtx[0].vout.append(CTxOut(0, bytes(get_witness_script(witroot, witnonce))))
|
||||
|
||||
block.vtx[0].rehash()
|
||||
block.hashMerkleRoot = block.calc_merkle_root()
|
||||
|
||||
return block
|
||||
|
||||
Reference in New Issue
Block a user