Skip to content

Commit

Permalink
test: Fetch coinbase address from coinbase UTXOs
Browse files Browse the repository at this point in the history
After upstream PR bitcoin/bitcoin#5994, the first call to getnewaddress after
startup does not return the address being used by the miner.
  • Loading branch information
str4d authored and milesmanley committed May 19, 2019
1 parent f72287b commit 720ae8a
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 44 deletions.
3 changes: 2 additions & 1 deletion qa/rpc-tests/finalsaplingroot.py
Expand Up @@ -9,6 +9,7 @@
from test_framework.util import (
assert_equal,
connect_nodes_bi,
get_coinbase_address,
initialize_chain_clean,
start_nodes,
wait_and_assert_operationid_status,
Expand Down Expand Up @@ -56,7 +57,7 @@ def run_test(self):
assert_equal(blk["finalsaplingroot"], SAPLING_TREE_EMPTY_ROOT)

# Node 0 shields some funds
taddr0 = self.nodes[0].getnewaddress()
taddr0 = get_coinbase_address(self.nodes[0], 100)
saplingAddr0 = self.nodes[0].z_getnewaddress('sapling')
recipients = []
recipients.append({"address": saplingAddr0, "amount": Decimal('20')})
Expand Down
5 changes: 3 additions & 2 deletions qa/rpc-tests/mempool_nu_activation.py
Expand Up @@ -7,7 +7,8 @@

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, initialize_chain_clean, \
start_node, connect_nodes, wait_and_assert_operationid_status
start_node, connect_nodes, wait_and_assert_operationid_status, \
get_coinbase_address
from test_framework.authproxy import JSONRPCException

from decimal import Decimal
Expand Down Expand Up @@ -43,7 +44,7 @@ def run_test(self):
self.sync_all()

# Shield some ZEC
node1_taddr = self.nodes[1].getnewaddress()
node1_taddr = get_coinbase_address(self.nodes[1], 97)
node0_zaddr = self.nodes[0].z_getnewaddress('sprout')
recipients = [{'address': node0_zaddr, 'amount': Decimal('10')}]
myopid = self.nodes[1].z_sendmany(node1_taddr, recipients, 1, Decimal('0'))
Expand Down
5 changes: 3 additions & 2 deletions qa/rpc-tests/mempool_tx_input_limit.py
Expand Up @@ -8,7 +8,8 @@
from test_framework.test_framework import BitcoinTestFramework
from test_framework.authproxy import JSONRPCException
from test_framework.util import assert_equal, initialize_chain_clean, \
start_node, connect_nodes, wait_and_assert_operationid_status
start_node, connect_nodes, wait_and_assert_operationid_status, \
get_coinbase_address

from decimal import Decimal

Expand Down Expand Up @@ -50,7 +51,7 @@ def run_test(self):
node0_zaddr = self.nodes[0].z_getnewaddress('sprout')

# Send three inputs from node 0 taddr to zaddr to get out of coinbase
node0_taddr = self.nodes[0].getnewaddress()
node0_taddr = get_coinbase_address(self.nodes[0], 3)
recipients = []
recipients.append({"address":node0_zaddr, "amount":Decimal('30.0')-Decimal('0.0001')}) # utxo amount less fee
myopid = self.nodes[0].z_sendmany(node0_taddr, recipients)
Expand Down
5 changes: 3 additions & 2 deletions qa/rpc-tests/paymentdisclosure.py
Expand Up @@ -8,7 +8,8 @@
from test_framework.test_framework import BitcoinTestFramework
from test_framework.authproxy import JSONRPCException
from test_framework.util import assert_equal, initialize_chain_clean, \
start_node, connect_nodes_bi, wait_and_assert_operationid_status
start_node, connect_nodes_bi, wait_and_assert_operationid_status, \
get_coinbase_address

from decimal import Decimal

Expand Down Expand Up @@ -48,7 +49,7 @@ def run_test (self):
assert_equal(self.nodes[1].getbalance(), 10)
assert_equal(self.nodes[2].getbalance(), 30)

mytaddr = self.nodes[0].getnewaddress()
mytaddr = get_coinbase_address(self.nodes[0], 4)
myzaddr = self.nodes[0].z_getnewaddress('sprout')

# Check that Node 2 has payment disclosure disabled.
Expand Down
2 changes: 1 addition & 1 deletion qa/rpc-tests/signrawtransaction_offline.py
Expand Up @@ -27,12 +27,12 @@ def run_test(self):

assert_equal(0, len(offline_node.getpeerinfo())) # make sure node 1 has no peers

privkeys = [self.nodes[0].dumpprivkey(self.nodes[0].getnewaddress())]
taddr = self.nodes[0].getnewaddress()

tx = self.nodes[0].listunspent()[0]
txid = tx['txid']
scriptpubkey = tx['scriptPubKey']
privkeys = [self.nodes[0].dumpprivkey(tx['address'])]

create_inputs = [{'txid': txid, 'vout': 0}]
sign_inputs = [{'txid': txid, 'vout': 0, 'scriptPubKey': scriptpubkey, 'amount': 10}]
Expand Down
9 changes: 9 additions & 0 deletions qa/rpc-tests/test_framework/util.py
Expand Up @@ -419,3 +419,12 @@ def wait_and_assert_operationid_status(node, myopid, in_status='success', in_err
return result # if there was an error return the result
else:
return txid # otherwise return the txid

# Find a coinbase address on the node, filtering by the number of UTXOs it has.
# The default cached chain has one address per coinbase output.
def get_coinbase_address(node, expected_utxos):
addrs = [utxo['address'] for utxo in node.listunspent() if utxo['generated']]
assert(len(set(addrs)) > 0)
addrs = [a for a in set(addrs) if addrs.count(a) == expected_utxos]
assert(len(addrs) > 0)
return addrs[0]
5 changes: 3 additions & 2 deletions qa/rpc-tests/wallet_1941.py
Expand Up @@ -10,7 +10,8 @@
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, initialize_chain_clean, \
initialize_datadir, start_nodes, start_node, connect_nodes_bi, \
bitcoind_processes, wait_and_assert_operationid_status
bitcoind_processes, wait_and_assert_operationid_status, \
get_coinbase_address

from decimal import Decimal

Expand Down Expand Up @@ -48,7 +49,7 @@ def run_test (self):
self.nodes[0].setmocktime(starttime)
self.nodes[0].generate(101)

mytaddr = self.nodes[0].getnewaddress() # where coins were mined
mytaddr = get_coinbase_address(self.nodes[0], 1)
myzaddr = self.nodes[0].z_getnewaddress('sprout')

# Send 10 coins to our zaddr.
Expand Down
4 changes: 2 additions & 2 deletions qa/rpc-tests/wallet_anchorfork.py
Expand Up @@ -8,7 +8,7 @@
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, initialize_chain_clean, \
start_nodes, stop_nodes, connect_nodes_bi, \
wait_and_assert_operationid_status, wait_bitcoinds
wait_and_assert_operationid_status, wait_bitcoinds, get_coinbase_address
from decimal import Decimal

class WalletAnchorForkTest (BitcoinTestFramework):
Expand Down Expand Up @@ -45,7 +45,7 @@ def run_test (self):
# At this point in time, commitment tree is the empty root

# Node 0 creates a joinsplit transaction
mytaddr0 = self.nodes[0].getnewaddress()
mytaddr0 = get_coinbase_address(self.nodes[0], 4)
myzaddr0 = self.nodes[0].z_getnewaddress('sprout')
recipients = []
recipients.append({"address":myzaddr0, "amount": Decimal('10.0') - Decimal('0.0001')})
Expand Down
13 changes: 8 additions & 5 deletions qa/rpc-tests/wallet_listnotes.py
Expand Up @@ -6,7 +6,12 @@
import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, start_nodes, wait_and_assert_operationid_status
from test_framework.util import (
assert_equal,
get_coinbase_address,
start_nodes,
wait_and_assert_operationid_status,
)

from decimal import Decimal

Expand Down Expand Up @@ -35,12 +40,10 @@ def run_test(self):
self.sync_all()
assert_equal(201, self.nodes[0].getblockcount())

mining_addr = self.nodes[0].listunspent()[0]['address']

# Shield coinbase funds (must be a multiple of 10, no change allowed pre-sapling)
receive_amount_10 = Decimal('10.0') - Decimal('0.0001')
recipients = [{"address":sproutzaddr, "amount":receive_amount_10}]
myopid = self.nodes[0].z_sendmany(mining_addr, recipients)
myopid = self.nodes[0].z_sendmany(get_coinbase_address(self.nodes[0], 1), recipients)
txid_1 = wait_and_assert_operationid_status(self.nodes[0], myopid)
self.sync_all()

Expand Down Expand Up @@ -119,7 +122,7 @@ def run_test(self):
# so send from coin base)
receive_amount_2 = Decimal('2.0') - Decimal('0.0001')
recipients = [{"address": saplingzaddr, "amount":receive_amount_2}]
myopid = self.nodes[0].z_sendmany(mining_addr, recipients)
myopid = self.nodes[0].z_sendmany(get_coinbase_address(self.nodes[0], 1), recipients)
txid_3 = wait_and_assert_operationid_status(self.nodes[0], myopid)
self.sync_all()
unspent_tx = self.nodes[0].z_listunspent(0)
Expand Down
6 changes: 4 additions & 2 deletions qa/rpc-tests/wallet_nullifiers.py
Expand Up @@ -7,7 +7,8 @@

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_true, bitcoind_processes, \
connect_nodes_bi, start_node, start_nodes, wait_and_assert_operationid_status
connect_nodes_bi, start_node, start_nodes, wait_and_assert_operationid_status, \
get_coinbase_address

from decimal import Decimal

Expand All @@ -22,7 +23,8 @@ def run_test (self):
myzaddr0 = self.nodes[0].z_getnewaddress('sprout')

# send node 0 taddr to zaddr to get out of coinbase
mytaddr = self.nodes[0].getnewaddress()
# Tests using the default cached chain have one address per coinbase output
mytaddr = get_coinbase_address(self.nodes[0], 1)
recipients = []
recipients.append({"address":myzaddr0, "amount":Decimal('10.0')-Decimal('0.0001')}) # utxo amount less fee

Expand Down
5 changes: 3 additions & 2 deletions qa/rpc-tests/wallet_overwintertx.py
Expand Up @@ -7,7 +7,8 @@

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, initialize_chain_clean, \
start_nodes, connect_nodes_bi, wait_and_assert_operationid_status, assert_greater_than
start_nodes, connect_nodes_bi, wait_and_assert_operationid_status, \
assert_greater_than, get_coinbase_address
from test_framework.authproxy import JSONRPCException

from decimal import Decimal
Expand All @@ -34,7 +35,7 @@ def run_test (self):
self.sync_all()
# Node 0 has reward from blocks 1 to 98 which are spendable.

taddr0 = self.nodes[0].getnewaddress()
taddr0 = get_coinbase_address(self.nodes[0], 98)
taddr1 = self.nodes[1].getnewaddress()
taddr2 = self.nodes[2].getnewaddress()
zaddr2 = self.nodes[2].z_getnewaddress('sprout')
Expand Down
3 changes: 2 additions & 1 deletion qa/rpc-tests/wallet_persistence.py
Expand Up @@ -8,6 +8,7 @@
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal, assert_true,
get_coinbase_address,
start_nodes, stop_nodes,
initialize_chain_clean, connect_nodes_bi, wait_bitcoinds,
wait_and_assert_operationid_status
Expand Down Expand Up @@ -58,7 +59,7 @@ def run_test(self):
self.sync_all()

# Node 0 shields funds to Sapling address
taddr0 = self.nodes[0].getnewaddress()
taddr0 = get_coinbase_address(self.nodes[0], 101)
recipients = []
recipients.append({"address": sapling_addr, "amount": Decimal('20')})
myopid = self.nodes[0].z_sendmany(taddr0, recipients, 1, 0)
Expand Down
5 changes: 3 additions & 2 deletions qa/rpc-tests/wallet_protectcoinbase.py
Expand Up @@ -9,7 +9,8 @@
from test_framework.authproxy import JSONRPCException
from test_framework.mininode import COIN
from test_framework.util import assert_equal, initialize_chain_clean, \
start_nodes, connect_nodes_bi, wait_and_assert_operationid_status
start_nodes, connect_nodes_bi, wait_and_assert_operationid_status, \
get_coinbase_address

import sys
import timeit
Expand Down Expand Up @@ -75,7 +76,7 @@ def run_test (self):
assert_equal("Coinbase funds can only be sent to a zaddr" in errorString, True)

# Prepare to send taddr->zaddr
mytaddr = self.nodes[0].getnewaddress()
mytaddr = get_coinbase_address(self.nodes[0], 4)
myzaddr = self.nodes[0].z_getnewaddress('sprout')

# Node 3 will test that watch only address utxos are not selected
Expand Down
20 changes: 11 additions & 9 deletions qa/rpc-tests/wallet_sapling.py
Expand Up @@ -9,6 +9,7 @@
from test_framework.authproxy import JSONRPCException
from test_framework.util import (
assert_equal,
get_coinbase_address,
start_nodes,
wait_and_assert_operationid_status,
)
Expand All @@ -34,18 +35,18 @@ def run_test(self):
self.sync_all()

# Verify RPCs disallow Sapling value transfer if Sapling is not active
tmp_taddr = self.nodes[3].getnewaddress()
tmp_taddr = get_coinbase_address(self.nodes[3], 1)
tmp_zaddr = self.nodes[3].z_getnewaddress('sapling')
try:
recipients = []
recipients.append({"address": tmp_zaddr, "amount": Decimal('20')})
recipients.append({"address": tmp_zaddr, "amount": Decimal('10')})
self.nodes[3].z_sendmany(tmp_taddr, recipients, 1, 0)
raise AssertionError("Should have thrown an exception")
except JSONRPCException as e:
assert_equal("Invalid parameter, Sapling has not activated", e.error['message'])
try:
recipients = []
recipients.append({"address": tmp_taddr, "amount": Decimal('20')})
recipients.append({"address": tmp_taddr, "amount": Decimal('10')})
self.nodes[3].z_sendmany(tmp_zaddr, recipients, 1, 0)
raise AssertionError("Should have thrown an exception")
except JSONRPCException as e:
Expand Down Expand Up @@ -75,9 +76,6 @@ def run_test(self):
self.nodes[2].generate(2)
self.sync_all()

taddr0 = self.nodes[0].getnewaddress()
# Skip over the address containing node 1's coinbase
self.nodes[1].getnewaddress()
taddr1 = self.nodes[1].getnewaddress()
saplingAddr0 = self.nodes[0].z_getnewaddress('sapling')
saplingAddr1 = self.nodes[1].z_getnewaddress('sapling')
Expand All @@ -95,10 +93,9 @@ def run_test(self):

# Node 0 shields some funds
# taddr -> Sapling
# -> taddr (change)
recipients = []
recipients.append({"address": saplingAddr0, "amount": Decimal('20')})
myopid = self.nodes[0].z_sendmany(taddr0, recipients, 1, 0)
recipients.append({"address": saplingAddr0, "amount": Decimal('10')})
myopid = self.nodes[0].z_sendmany(get_coinbase_address(self.nodes[0], 1), recipients, 1, 0)
mytxid = wait_and_assert_operationid_status(self.nodes[0], myopid)

self.sync_all()
Expand All @@ -107,6 +104,11 @@ def run_test(self):
mempool = self.nodes[0].getrawmempool(True)
assert(Decimal(mempool[mytxid]['startingpriority']) == Decimal('1E+16'))

# Shield another coinbase UTXO
myopid = self.nodes[0].z_sendmany(get_coinbase_address(self.nodes[0], 1), recipients, 1, 0)
mytxid = wait_and_assert_operationid_status(self.nodes[0], myopid)

self.sync_all()
self.nodes[2].generate(1)
self.sync_all()

Expand Down
16 changes: 7 additions & 9 deletions qa/rpc-tests/wallet_shieldcoinbase.py
Expand Up @@ -9,7 +9,7 @@
from test_framework.authproxy import JSONRPCException
from test_framework.util import assert_equal, initialize_chain_clean, \
start_node, connect_nodes_bi, sync_blocks, sync_mempools, \
wait_and_assert_operationid_status
wait_and_assert_operationid_status, get_coinbase_address

from decimal import Decimal

Expand Down Expand Up @@ -46,17 +46,13 @@ def run_test (self):
print "Mining blocks..."

self.nodes[0].generate(1)
do_not_shield_taddr = self.nodes[0].getnewaddress()

self.nodes[0].generate(4)
walletinfo = self.nodes[0].getwalletinfo()
assert_equal(walletinfo['immature_balance'], 50)
assert_equal(walletinfo['balance'], 0)
self.sync_all()
self.nodes[2].generate(1)
self.nodes[2].getnewaddress()
self.nodes[2].generate(1)
self.nodes[2].getnewaddress()
self.nodes[2].generate(1)
self.sync_all()
self.nodes[1].generate(101)
Expand All @@ -65,8 +61,10 @@ def run_test (self):
assert_equal(self.nodes[1].getbalance(), 10)
assert_equal(self.nodes[2].getbalance(), 30)

do_not_shield_taddr = get_coinbase_address(self.nodes[0], 1)

# Prepare to send taddr->zaddr
mytaddr = self.nodes[0].getnewaddress()
mytaddr = get_coinbase_address(self.nodes[0], 4)
myzaddr = self.nodes[0].z_getnewaddress(self.addr_type)

# Shielding will fail when trying to spend from watch-only address
Expand Down Expand Up @@ -145,7 +143,7 @@ def run_test (self):
self.sync_all()
self.nodes[1].generate(100)
self.sync_all()
mytaddr = self.nodes[0].getnewaddress()
mytaddr = get_coinbase_address(self.nodes[0], 800)

def verify_locking(first, second, limit):
result = self.nodes[0].z_shieldcoinbase(mytaddr, myzaddr, 0, limit)
Expand Down Expand Up @@ -186,7 +184,7 @@ def verify_locking(first, second, limit):
if self.addr_type == 'sprout':
# Verify maximum number of utxos which node 2 can shield is limited by option -mempooltxinputlimit
# This option is used when the limit parameter is set to 0.
mytaddr = self.nodes[2].getnewaddress()
mytaddr = get_coinbase_address(self.nodes[2], 20)
result = self.nodes[2].z_shieldcoinbase(mytaddr, myzaddr, Decimal('0.0001'), 0)
assert_equal(result["shieldingUTXOs"], Decimal('7'))
assert_equal(result["remainingUTXOs"], Decimal('13'))
Expand All @@ -198,7 +196,7 @@ def verify_locking(first, second, limit):
# Verify maximum number of utxos which node 0 can shield is set by default limit parameter of 50
self.nodes[0].generate(200)
self.sync_all()
mytaddr = self.nodes[0].getnewaddress()
mytaddr = get_coinbase_address(self.nodes[0], 100)
result = self.nodes[0].z_shieldcoinbase(mytaddr, myzaddr, Decimal('0.0001'))
assert_equal(result["shieldingUTXOs"], Decimal('50'))
assert_equal(result["remainingUTXOs"], Decimal('50'))
Expand Down
5 changes: 3 additions & 2 deletions qa/rpc-tests/wallet_treestate.py
Expand Up @@ -7,7 +7,8 @@

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, initialize_chain_clean, \
start_nodes, connect_nodes_bi, wait_and_assert_operationid_status
start_nodes, connect_nodes_bi, wait_and_assert_operationid_status, \
get_coinbase_address

import time
from decimal import Decimal
Expand Down Expand Up @@ -35,7 +36,7 @@ def run_test (self):
self.nodes[1].generate(101)
self.sync_all()

mytaddr = self.nodes[0].getnewaddress() # where coins were mined
mytaddr = get_coinbase_address(self.nodes[0], 100)
myzaddr = self.nodes[0].z_getnewaddress('sprout')

# Spend coinbase utxos to create three notes of 9.99990000 each
Expand Down

0 comments on commit 720ae8a

Please sign in to comment.