Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc_wallet_standardness.py
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | 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) | ||||
self.sync_blocks() | self.sync_blocks() | ||||
def fund_and_test_wallet(scriptPubKey, shouldBeStandard, shouldBeInWallet, | def fund_and_test_wallet(scriptPubKey, is_standard, expected_in_std_wallet, | ||||
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 | """ | ||||
standardness by trying to broadcast on the standard node, then | Get the nonstandard node to fund a transaction, test its | ||||
mine it and see if it ended up in the standard node's wallet. | standardness by trying to broadcast on the standard node, | ||||
then 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. | ||||
""" | """ | ||||
self.log.info("Trying script {}".format(scriptPubKey.hex(),)) | self.log.info("Trying script {}".format(scriptPubKey.hex(),)) | ||||
# get nonstandard node to fund the script | # get nonstandard node to fund the script | ||||
tx = CTransaction() | tx = CTransaction() | ||||
tx.vout.append(CTxOut(max(amount, 10000), scriptPubKey)) | tx.vout.append(CTxOut(max(amount, 10000), scriptPubKey)) | ||||
rawtx = nonstd_node.fundrawtransaction( | rawtx = nonstd_node.fundrawtransaction( | ||||
ToHex(tx), {'lockUnspents': True, 'changePosition': 1})['hex'] | ToHex(tx), {'lockUnspents': True, 'changePosition': 1})['hex'] | ||||
# fundrawtransaction doesn't like to fund dust outputs, so we | # fundrawtransaction doesn't like to fund dust outputs, so we | ||||
# have to manually override the amount. | # have to manually override the amount. | ||||
FromHex(tx, rawtx) | FromHex(tx, rawtx) | ||||
tx.vout[0].nValue = min(amount, 10000) | tx.vout[0].nValue = min(amount, 10000) | ||||
rawtx = nonstd_node.signrawtransactionwithwallet(ToHex(tx))['hex'] | rawtx = nonstd_node.signrawtransactionwithwallet(ToHex(tx))['hex'] | ||||
# ensure signing process did not disturb scriptPubKey | # ensure signing process did not disturb scriptPubKey | ||||
signedtx = FromHex(CTransaction(), rawtx) | signedtx = FromHex(CTransaction(), rawtx) | ||||
assert_equal(scriptPubKey, signedtx.vout[0].scriptPubKey) | assert_equal(scriptPubKey, signedtx.vout[0].scriptPubKey) | ||||
txid = signedtx.rehash() | txid = signedtx.rehash() | ||||
balance_initial = std_node.getbalance() | balance_initial = std_node.getbalance() | ||||
# try broadcasting it on the standard node | # try broadcasting it on the standard node | ||||
if shouldBeStandard: | if is_standard: | ||||
std_node.sendrawtransaction(rawtx) | std_node.sendrawtransaction(rawtx) | ||||
assert txid in std_node.getrawmempool() | assert txid in std_node.getrawmempool() | ||||
else: | else: | ||||
assert_raises_rpc_error(-26, nonstd_error, | assert_raises_rpc_error(-26, nonstd_error, | ||||
std_node.sendrawtransaction, rawtx) | std_node.sendrawtransaction, rawtx) | ||||
assert txid not in std_node.getrawmempool() | assert txid not in std_node.getrawmempool() | ||||
# 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"] | ||||
self.sync_blocks() | 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 expected_in_std_wallet: | ||||
assert (txid, 0) in wallet_outpoints | assert (txid, 0) in wallet_outpoints | ||||
assert balance_change == amount * SATOSHI | assert balance_change == amount * SATOSHI | ||||
else: | else: | ||||
assert (txid, 0) not in wallet_outpoints | assert (txid, 0) not in wallet_outpoints | ||||
assert balance_change == 0 | assert balance_change == 0 | ||||
# try spending the funds using the wallet. | # try spending the funds using the wallet. | ||||
outamount = (amount - spendfee) * SATOSHI | outamount = (amount - spendfee) * SATOSHI | ||||
▲ Show 20 Lines • Show All 80 Lines • Show Last 20 Lines |