Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc_wallet_standardness.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2019 The Bitcoin developers | # Copyright (c) 2019 The Bitcoin developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Test the response of wallet to a variety of weird / nonstandard coins | """Test the response of wallet to a variety of weird / nonstandard coins | ||||
that it might try to spend.""" | that it might try to spend.""" | ||||
from decimal import Decimal | from decimal import Decimal | ||||
from test_framework.messages import ( | |||||
CTransaction, | |||||
CTxOut, | |||||
FromHex, | |||||
ToHex, | |||||
) | |||||
from test_framework.script import ( | from test_framework.script import ( | ||||
CScript, | CScript, | ||||
OP_1, | OP_1, | ||||
OP_5, | OP_5, | ||||
OP_CHECKSIG, | OP_CHECKSIG, | ||||
OP_CHECKMULTISIG, | OP_CHECKMULTISIG, | ||||
OP_DUP, | OP_DUP, | ||||
OP_EQUALVERIFY, | OP_EQUALVERIFY, | ||||
OP_HASH160, | OP_HASH160, | ||||
OP_PUSHDATA1, | OP_PUSHDATA1, | ||||
hash160, | hash160, | ||||
) | ) | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_raises_rpc_error, | assert_raises_rpc_error, | ||||
assert_equal, | assert_equal, | ||||
sync_blocks, | |||||
) | |||||
from test_framework.messages import ( | |||||
CTransaction, | |||||
CTxOut, | |||||
FromHex, | |||||
ToHex, | |||||
) | ) | ||||
SATOSHI = Decimal('0.00000001') | SATOSHI = Decimal('0.00000001') | ||||
class WalletStandardnessTest(BitcoinTestFramework): | class WalletStandardnessTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.num_nodes = 2 | self.num_nodes = 2 | ||||
self.extra_args = [['-acceptnonstdtxn=0'], ['-acceptnonstdtxn=1']] | self.extra_args = [['-acceptnonstdtxn=0'], ['-acceptnonstdtxn=1']] | ||||
def skip_test_if_missing_module(self): | def skip_test_if_missing_module(self): | ||||
self.skip_if_no_wallet() | self.skip_if_no_wallet() | ||||
def run_test(self): | def run_test(self): | ||||
std_node, nonstd_node = self.nodes | std_node, nonstd_node = self.nodes | ||||
address_nonstd = nonstd_node.getnewaddress() | address_nonstd = nonstd_node.getnewaddress() | ||||
# make and mature some coins for the nonstandard node | # make and mature some coins for the nonstandard node | ||||
nonstd_node.generate(120) | nonstd_node.generate(120) | ||||
sync_blocks(self.nodes) | self.sync_blocks() | ||||
def fund_and_test_wallet(scriptPubKey, shouldBeStandard, shouldBeInWallet, | def fund_and_test_wallet(scriptPubKey, shouldBeStandard, shouldBeInWallet, | ||||
amount=10000, spendfee=500, nonstd_error="scriptpubkey (code 64)", sign_error=None): | amount=10000, spendfee=500, nonstd_error="scriptpubkey (code 64)", sign_error=None): | ||||
""" Get the nonstandard node to fund a transaction, test its | """ Get the nonstandard node to fund a transaction, test its | ||||
standardness by trying to broadcast on the standard node, then | standardness by trying to broadcast on the standard node, then | ||||
mine it and see if it ended up in the standard node's wallet. | mine it and see if it ended up in the standard node's wallet. | ||||
Finally, it attempts to spend the coin. | Finally, it attempts to spend the coin. | ||||
""" | """ | ||||
Show All 29 Lines | def run_test(self): | ||||
# make sure it's in nonstandard node's mempool, then mine it | # make sure it's in nonstandard node's mempool, then mine it | ||||
nonstd_node.sendrawtransaction(rawtx) | nonstd_node.sendrawtransaction(rawtx) | ||||
assert txid in nonstd_node.getrawmempool() | assert txid in nonstd_node.getrawmempool() | ||||
[blockhash] = nonstd_node.generate(1) | [blockhash] = nonstd_node.generate(1) | ||||
# make sure it was mined | # make sure it was mined | ||||
assert txid in nonstd_node.getblock(blockhash)["tx"] | assert txid in nonstd_node.getblock(blockhash)["tx"] | ||||
sync_blocks(self.nodes) | self.sync_blocks() | ||||
wallet_outpoints = {(entry['txid'], entry['vout']) | wallet_outpoints = {(entry['txid'], entry['vout']) | ||||
for entry in std_node.listunspent()} | for entry in std_node.listunspent()} | ||||
# calculate wallet balance change just as a double check | # calculate wallet balance change just as a double check | ||||
balance_change = std_node.getbalance() - balance_initial | balance_change = std_node.getbalance() - balance_initial | ||||
if shouldBeInWallet: | if shouldBeInWallet: | ||||
assert (txid, 0) in wallet_outpoints | assert (txid, 0) in wallet_outpoints | ||||
Show All 15 Lines | def run_test(self): | ||||
signresult = std_node.signrawtransactionwithwallet(spendtx) | signresult = std_node.signrawtransactionwithwallet(spendtx) | ||||
if sign_error is None: | if sign_error is None: | ||||
assert_equal(signresult['complete'], True) | assert_equal(signresult['complete'], True) | ||||
txid = std_node.sendrawtransaction(signresult['hex']) | txid = std_node.sendrawtransaction(signresult['hex']) | ||||
[blockhash] = std_node.generate(1) | [blockhash] = std_node.generate(1) | ||||
# make sure it was mined | # make sure it was mined | ||||
assert txid in std_node.getblock(blockhash)["tx"] | assert txid in std_node.getblock(blockhash)["tx"] | ||||
sync_blocks(self.nodes) | self.sync_blocks() | ||||
else: | else: | ||||
assert_equal(signresult['complete'], False) | assert_equal(signresult['complete'], False) | ||||
assert_equal(signresult['errors'][0]['error'], sign_error) | assert_equal(signresult['errors'][0]['error'], sign_error) | ||||
# we start with an empty wallet | # we start with an empty wallet | ||||
assert_equal(std_node.getbalance(), 0) | assert_equal(std_node.getbalance(), 0) | ||||
address = std_node.getnewaddress() | address = std_node.getnewaddress() | ||||
▲ Show 20 Lines • Show All 55 Lines • Show Last 20 Lines |