Merge bitcoin/bitcoin#22048: test: MiniWallet: introduce enum type for output mode

6cebac598e5e85eadd60eb1274d7f33d63ce1108 test: MiniWallet: introduce enum type for output mode (Sebastian Falbesoner)

Pull request description:

  This is a follow-up PR to #21945 which lifted the number of MiniWallet's tx output modes from 2 to 3 (by adding P2PK Support).
  Since the current way of specifying the mode on the ctor via two booleans is ugly and error-prone (see table in comment https://github.com/bitcoin/bitcoin/pull/21945#issuecomment-842526575), a new Enum type `MiniWalletMode` is introduced that can hold the following values:

  - ADDRESS_OP_TRUE
  - RAW_OP_TRUE
  - RAW_P2PK

  Also adds documentation that should guide the user on which mode is useful for what etc. with a summary table. (Can also be split up in a separate commit or shortened if that is desired, maybe it's considered to be too verbose).

ACKs for top commit:
  MarcoFalke:
    cr ACK 6cebac598e5e85eadd60eb1274d7f33d63ce1108

Tree-SHA512: cbbc10806d9d9e62829548094415e9f1a281cd247b9a9fc7f7f33b923c723aa03e7bc3024623a77fb1f7da4d73455fa8244840f746980d32acdad97ee12100da
This commit is contained in:
MarcoFalke 2021-05-25 07:26:18 +02:00 committed by Konstantin Akimov
parent f4cd20b115
commit d5a8d5e6a0
No known key found for this signature in database
GPG Key ID: 2176C4A5D01EA524
3 changed files with 39 additions and 8 deletions

View File

@ -29,7 +29,10 @@ from test_framework.util import (
assert_equal, assert_equal,
assert_raises_rpc_error, assert_raises_rpc_error,
) )
from test_framework.wallet import MiniWallet from test_framework.wallet import (
MiniWallet,
MiniWalletMode,
)
CLTV_HEIGHT = 1351 CLTV_HEIGHT = 1351
@ -101,7 +104,7 @@ class BIP65Test(BitcoinTestFramework):
def run_test(self): def run_test(self):
peer = self.nodes[0].add_p2p_connection(P2PInterface()) peer = self.nodes[0].add_p2p_connection(P2PInterface())
wallet = MiniWallet(self.nodes[0], raw_script=True) wallet = MiniWallet(self.nodes[0], mode=MiniWalletMode.RAW_OP_TRUE)
self.test_cltv_info(is_active=False) self.test_cltv_info(is_active=False)

View File

@ -55,7 +55,10 @@ from test_framework.util import (
assert_equal, assert_equal,
softfork_active, softfork_active,
) )
from test_framework.wallet import MiniWallet from test_framework.wallet import (
MiniWallet,
MiniWalletMode,
)
TESTING_TX_COUNT = 83 # Number of testing transactions: 1 BIP113 tx, 16 BIP68 txs, 66 BIP112 txs (see comments above) TESTING_TX_COUNT = 83 # Number of testing transactions: 1 BIP113 tx, 16 BIP68 txs, 66 BIP112 txs (see comments above)
COINBASE_BLOCK_COUNT = TESTING_TX_COUNT # Number of coinbase blocks we need to generate as inputs for our txs COINBASE_BLOCK_COUNT = TESTING_TX_COUNT # Number of coinbase blocks we need to generate as inputs for our txs
@ -187,7 +190,7 @@ class BIP68_112_113Test(BitcoinTestFramework):
def run_test(self): def run_test(self):
self.helper_peer = self.nodes[0].add_p2p_connection(P2PDataStore()) self.helper_peer = self.nodes[0].add_p2p_connection(P2PDataStore())
self.miniwallet = MiniWallet(self.nodes[0], use_p2pk=True) self.miniwallet = MiniWallet(self.nodes[0], mode=MiniWalletMode.RAW_P2PK)
self.log.info("Generate blocks in the past for coinbase outputs.") self.log.info("Generate blocks in the past for coinbase outputs.")
self.coinbase_blocks = self.miniwallet.generate(COINBASE_BLOCK_COUNT) # blocks generated for inputs self.coinbase_blocks = self.miniwallet.generate(COINBASE_BLOCK_COUNT) # blocks generated for inputs

View File

@ -5,6 +5,7 @@
"""A limited-functionality wallet, which may replace a real wallet in tests""" """A limited-functionality wallet, which may replace a real wallet in tests"""
from decimal import Decimal from decimal import Decimal
from enum import Enum
from test_framework.address import ADDRESS_BCRT1_P2SH_OP_TRUE from test_framework.address import ADDRESS_BCRT1_P2SH_OP_TRUE
from test_framework.key import ECKey from test_framework.key import ECKey
from typing import Optional from typing import Optional
@ -30,22 +31,46 @@ from test_framework.util import (
) )
class MiniWalletMode(Enum):
"""Determines the transaction type the MiniWallet is creating and spending.
For most purposes, the default mode ADDRESS_OP_TRUE should be sufficient;
it simply uses a fixed P2SH address whose coins are spent with a
witness stack of OP_TRUE, i.e. following an anyone-can-spend policy.
However, if the transactions need to be modified by the user (e.g. prepending
scriptSig for testing opcodes that are activated by a soft-fork), or the txs
should contain an actual signature, the raw modes RAW_OP_TRUE and RAW_P2PK
can be useful. Summary of modes:
| output | | tx is | can modify | needs
mode | description | address | standard | scriptSig | signing
----------------+-------------------+-----------+----------+------------+----------
ADDRESS_OP_TRUE | anyone-can-spend | bech32 | yes | no | no
RAW_OP_TRUE | anyone-can-spend | - (raw) | no | yes | no
RAW_P2PK | pay-to-public-key | - (raw) | yes | yes | yes
"""
ADDRESS_OP_TRUE = 1
RAW_OP_TRUE = 2
RAW_P2PK = 3
class MiniWallet: class MiniWallet:
def __init__(self, test_node, *, raw_script=False, use_p2pk=False): def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
self._test_node = test_node self._test_node = test_node
self._utxos = [] self._utxos = []
self._priv_key = None self._priv_key = None
self._address = None self._address = None
if raw_script: assert isinstance(mode, MiniWalletMode)
if mode == MiniWalletMode.RAW_OP_TRUE:
self._scriptPubKey = bytes(CScript([OP_TRUE])) self._scriptPubKey = bytes(CScript([OP_TRUE]))
elif use_p2pk: elif mode == MiniWalletMode.RAW_P2PK:
# use simple deterministic private key (k=1) # use simple deterministic private key (k=1)
self._priv_key = ECKey() self._priv_key = ECKey()
self._priv_key.set((1).to_bytes(32, 'big'), True) self._priv_key.set((1).to_bytes(32, 'big'), True)
pub_key = self._priv_key.get_pubkey() pub_key = self._priv_key.get_pubkey()
self._scriptPubKey = bytes(CScript([pub_key.get_bytes(), OP_CHECKSIG])) self._scriptPubKey = bytes(CScript([pub_key.get_bytes(), OP_CHECKSIG]))
else: elif mode == MiniWalletMode.ADDRESS_OP_TRUE:
self._address = ADDRESS_BCRT1_P2SH_OP_TRUE self._address = ADDRESS_BCRT1_P2SH_OP_TRUE
self._scriptPubKey = hex_str_to_bytes(self._test_node.validateaddress(self._address)['scriptPubKey']) self._scriptPubKey = hex_str_to_bytes(self._test_node.validateaddress(self._address)['scriptPubKey'])