mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-10 11:57:28 -03:00
ec700f0a76
e4b0dabb21
test: add functional test for tagged MiniWallet instances (Sebastian Falbesoner)3162c917e9
test: fix MiniWallet internal key derivation for tagged instances (Sebastian Falbesoner)c9f7364ab2
test: fix MiniWallet script-path spend (missing parity bit in leaf version) (Sebastian Falbesoner)7774c314fb
test: refactor: return TaprootInfo from P2TR address creation routine (Sebastian Falbesoner) Pull request description: This PR fixes a dormant bug in MiniWallet that exists since support for P2TR was initially added in #23371 (see commit041abfebe4
). In the course of spending the output, the leaf version byte of the control block in the witness stack doesn't set the parity bit, i.e. we were so far just lucky that the used combinations of relevant data (internal pubkey, leaf script / version) didn't result in a tweaked pubkey with odd y-parity. If that was the case, we'd get the following validation error: `mandatory-script-verify-flag-failed (Witness program hash mismatch) (-26)` Since MiniWallets can now optionally be tagged (#29939), resulting in different internal pubkeys, the issue is more prevalent now. Fix it by passing the parity bit, as specified in BIP341. Can be tested with the following patch (fails on master, succeeds on PR): ```diff diff --git a/test/functional/test_framework/mempool_util.py b/test/functional/test_framework/mempool_util.py index 148cc935ed..7ebe858681 100644 --- a/test/functional/test_framework/mempool_util.py +++ b/test/functional/test_framework/mempool_util.py @@ -42,7 +42,7 @@ def fill_mempool(test_framework, node): # Generate UTXOs to flood the mempool # 1 to create a tx initially that will be evicted from the mempool later # 75 transactions each with a fee rate higher than the previous one - ephemeral_miniwallet = MiniWallet(node, tag_name="fill_mempool_ephemeral_wallet") + ephemeral_miniwallet = MiniWallet(node, tag_name="fill_mempool_ephemeral_wallet3") test_framework.generate(ephemeral_miniwallet, 1 + num_of_batches * tx_batch_size) # Mine enough blocks so that the UTXOs are allowed to be spent ``` In addition to that, another bug is fixed where the internal key derivation failed, as not every pseudorandom hash results in a valid x-only pubkey. Fix this by treating the hash result as private key and calculate the x-only public key out of that, to be used then as internal key. Fixes #30528. ACKs for top commit: glozow: ACKe4b0dabb21
rkrux: reACK [e4b0dab
](e4b0dabb21
) hodlinator: ACKe4b0dabb21
Tree-SHA512: a16f33f76bcb1012857cc3129438a9f6badf28aa2b1d25696da0d385ba5866b46de0f1f93ba777ed9263fe6952f98d7d9c44ea0c0170a2bcc86cbef90bf6ac58
67 lines
2.9 KiB
Python
Executable file
67 lines
2.9 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
# Copyright (c) 2024 The Bitcoin Core developers
|
|
# Distributed under the MIT software license, see the accompanying
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
"""Test MiniWallet."""
|
|
import random
|
|
import string
|
|
|
|
from test_framework.blocktools import COINBASE_MATURITY
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import (
|
|
assert_greater_than_or_equal,
|
|
)
|
|
from test_framework.wallet import (
|
|
MiniWallet,
|
|
MiniWalletMode,
|
|
)
|
|
|
|
|
|
class FeatureFrameworkMiniWalletTest(BitcoinTestFramework):
|
|
def set_test_params(self):
|
|
self.num_nodes = 1
|
|
|
|
def test_tx_padding(self):
|
|
"""Verify that MiniWallet's transaction padding (`target_weight` parameter)
|
|
works accurately enough (i.e. at most 3 WUs higher) with all modes."""
|
|
for mode_name, wallet in self.wallets:
|
|
self.log.info(f"Test tx padding with MiniWallet mode {mode_name}...")
|
|
utxo = wallet.get_utxo(mark_as_spent=False)
|
|
for target_weight in [1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 4000000,
|
|
989, 2001, 4337, 13371, 23219, 49153, 102035, 223419, 3999989]:
|
|
tx = wallet.create_self_transfer(utxo_to_spend=utxo, target_weight=target_weight)["tx"]
|
|
self.log.debug(f"-> target weight: {target_weight}, actual weight: {tx.get_weight()}")
|
|
assert_greater_than_or_equal(tx.get_weight(), target_weight)
|
|
assert_greater_than_or_equal(target_weight + 3, tx.get_weight())
|
|
|
|
def test_wallet_tagging(self):
|
|
"""Verify that tagged wallet instances are able to send funds."""
|
|
self.log.info(f"Test tagged wallet instances...")
|
|
node = self.nodes[0]
|
|
untagged_wallet = self.wallets[0][1]
|
|
for i in range(10):
|
|
tag = ''.join(random.choice(string.ascii_letters) for _ in range(20))
|
|
self.log.debug(f"-> ({i}) tag name: {tag}")
|
|
tagged_wallet = MiniWallet(node, tag_name=tag)
|
|
untagged_wallet.send_to(from_node=node, scriptPubKey=tagged_wallet.get_scriptPubKey(), amount=100000)
|
|
tagged_wallet.rescan_utxos()
|
|
tagged_wallet.send_self_transfer(from_node=node)
|
|
self.generate(node, 1) # clear mempool
|
|
|
|
def run_test(self):
|
|
node = self.nodes[0]
|
|
self.wallets = [
|
|
("ADDRESS_OP_TRUE", MiniWallet(node, mode=MiniWalletMode.ADDRESS_OP_TRUE)),
|
|
("RAW_OP_TRUE", MiniWallet(node, mode=MiniWalletMode.RAW_OP_TRUE)),
|
|
("RAW_P2PK", MiniWallet(node, mode=MiniWalletMode.RAW_P2PK)),
|
|
]
|
|
for _, wallet in self.wallets:
|
|
self.generate(wallet, 10)
|
|
self.generate(wallet, COINBASE_MATURITY)
|
|
|
|
self.test_tx_padding()
|
|
self.test_wallet_tagging()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
FeatureFrameworkMiniWalletTest(__file__).main()
|