mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-02 09:15:04 +02:00
Merge bitcoin/bitcoin#22363: test: refactor: use script_util helpers for creating P2{PKH,SH,WPKH,WSH} scripts
905d672b74test: use script_util helpers for creating P2W{PKH,SH} scripts (Sebastian Falbesoner)285a65ccfdtest: use script_util helpers for creating P2SH scripts (Sebastian Falbesoner)b57b633b94test: use script_util helpers for creating P2PKH scripts (Sebastian Falbesoner)61b6a017a9test: wallet util: fix multisig P2SH-P2WSH script creation (Sebastian Falbesoner) Pull request description: PR #18788 (commit08067aebfd) introduced functions to generate output scripts for various types. This PR replaces all manual CScript creations in the P2PKH, P2SH, P2WPKH, P2WSH formats with those helpers in order to increase readability and maintainability over the functional test codebase. The first commit fixes a bug in the wallet_util helper module w.r.t. to P2SH-P2WSH script creation (the result is not used in any test so far, hence it can still be seen as refactoring). The following table shows a summary of the output script patterns tackled in this PR: | Type | master branch | PR branch | | ---------- | ------------- | ------------- | | P2PKH | `CScript([OP_DUP, OP_HASH160, hash160(key), OP_EQUALVERIFY, OP_CHECKSIG])` | `key_to_p2pkh_script(key)` | | | `CScript([OP_DUP, OP_HASH160, keyhash, OP_EQUALVERIFY, OP_CHECKSIG])` | `keyhash_to_p2pkh_script(keyhash)` | | P2SH | `CScript([OP_HASH160, hash160(script), OP_EQUAL])` | `script_to_p2sh_script(script)` | | P2WPKH | `CScript([OP_0, hash160(key)])` | `key_to_p2wpkh_script(key)` | | P2WSH | `CScript([OP_0, sha256(script)])` | `script_to_p2wsh_script(script)` | Note that the `key_to_...` helpers can't be used if an invalid key size (not 33 or 65 bytes) is passed, which is the case in some rare instances where the scripts still have to be created manually. Possible follow-up ideas: * further simplify by identifying P2SH-wrapped scripts and using `key_to_p2sh_p2wpkh_script()` and `script_to_p2sh_p2wsh_script()` helpers * introduce and use `key_to_p2pk_script()` helper for P2PK scripts ACKs for top commit: rajarshimaitra: tACK905d672b74LarryRuane: tACK905d672b740xB10C: ACK905d672b74MarcoFalke: review ACK905d672b74🕹 Tree-SHA512: 7ccfe69699bc81168ac122b03536720013355c1b2fbb088355b616015318644c4d1cd27e20c4f56c89ad083ae609add4bc838cf6316794d0edb0ce9cf7fa0fd8
This commit is contained in:
@@ -29,27 +29,32 @@ from test_framework.messages import (
|
||||
CTxOut,
|
||||
MAX_MONEY,
|
||||
)
|
||||
from test_framework import script as sc
|
||||
from test_framework.blocktools import create_tx_with_script, MAX_BLOCK_SIGOPS
|
||||
from test_framework.script import (
|
||||
CScript,
|
||||
OP_CAT,
|
||||
OP_SUBSTR,
|
||||
OP_LEFT,
|
||||
OP_RIGHT,
|
||||
OP_INVERT,
|
||||
OP_AND,
|
||||
OP_OR,
|
||||
OP_XOR,
|
||||
OP_2MUL,
|
||||
OP_0,
|
||||
OP_2DIV,
|
||||
OP_MUL,
|
||||
OP_2MUL,
|
||||
OP_AND,
|
||||
OP_CAT,
|
||||
OP_CHECKSIG,
|
||||
OP_DIV,
|
||||
OP_MOD,
|
||||
OP_INVERT,
|
||||
OP_LEFT,
|
||||
OP_LSHIFT,
|
||||
OP_RSHIFT
|
||||
OP_MOD,
|
||||
OP_MUL,
|
||||
OP_OR,
|
||||
OP_RIGHT,
|
||||
OP_RSHIFT,
|
||||
OP_SUBSTR,
|
||||
OP_TRUE,
|
||||
OP_XOR,
|
||||
)
|
||||
basic_p2sh = sc.CScript([sc.OP_HASH160, sc.hash160(sc.CScript([sc.OP_0])), sc.OP_EQUAL])
|
||||
from test_framework.script_util import (
|
||||
script_to_p2sh_script,
|
||||
)
|
||||
basic_p2sh = script_to_p2sh_script(CScript([OP_0]))
|
||||
|
||||
|
||||
class BadTxTemplate:
|
||||
@@ -116,7 +121,7 @@ class SizeTooSmall(BadTxTemplate):
|
||||
def get_tx(self):
|
||||
tx = CTransaction()
|
||||
tx.vin.append(self.valid_txin)
|
||||
tx.vout.append(CTxOut(0, sc.CScript([sc.OP_TRUE])))
|
||||
tx.vout.append(CTxOut(0, CScript([OP_TRUE])))
|
||||
tx.calc_sha256()
|
||||
return tx
|
||||
|
||||
@@ -230,7 +235,7 @@ class TooManySigops(BadTxTemplate):
|
||||
expect_disconnect = False
|
||||
|
||||
def get_tx(self):
|
||||
lotsa_checksigs = sc.CScript([sc.OP_CHECKSIG] * (MAX_BLOCK_SIGOPS))
|
||||
lotsa_checksigs = CScript([OP_CHECKSIG] * (MAX_BLOCK_SIGOPS))
|
||||
return create_tx_with_script(
|
||||
self.spend_tx, 0,
|
||||
script_pub_key=lotsa_checksigs,
|
||||
|
||||
Reference in New Issue
Block a user