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 committed Apr 13, 2018
1 parent 117201d commit 36e5497
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 27 deletions.
5 changes: 3 additions & 2 deletions qa/rpc-tests/mempool_nu_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,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 decimal import Decimal

Expand Down Expand Up @@ -37,7 +38,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()
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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

import time
from decimal import Decimal
Expand Down Expand Up @@ -49,7 +50,7 @@ def run_test(self):
node0_zaddr = self.nodes[0].z_getnewaddress()

# 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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 @@ -46,7 +47,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()

# Check that Node 2 has payment disclosure disabled.
Expand Down
7 changes: 7 additions & 0 deletions qa/rpc-tests/test_framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,10 @@ def wait_and_assert_operationid_status(node, myopid, in_status='success', in_err
assert(in_errormsg is not None)
assert_equal(in_errormsg in errormsg, True)
return txid

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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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 @@ -46,7 +47,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()

# Send 10 coins to our zaddr.
Expand Down
4 changes: 2 additions & 2 deletions qa/rpc-tests/wallet_anchorfork.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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 @@ -43,7 +43,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()
recipients = []
recipients.append({"address":myzaddr0, "amount": Decimal('10.0') - Decimal('0.0001')})
Expand Down
5 changes: 3 additions & 2 deletions qa/rpc-tests/wallet_nullifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

import time
from decimal import Decimal
Expand All @@ -22,7 +22,8 @@ def run_test (self):
myzaddr0 = self.nodes[0].z_getnewaddress()

# 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
myopid = self.nodes[0].z_sendmany(mytaddr, recipients)
Expand Down
5 changes: 3 additions & 2 deletions qa/rpc-tests/wallet_overwintertx.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,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

from decimal import Decimal

Expand All @@ -31,7 +32,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()
Expand Down
5 changes: 3 additions & 2 deletions qa/rpc-tests/wallet_protectcoinbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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 time
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()

# Node 3 will test that watch only address utxos are not selected
Expand Down
16 changes: 7 additions & 9 deletions qa/rpc-tests/wallet_shieldcoinbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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 All @@ -34,17 +34,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 @@ -53,8 +49,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()

# Shielding will fail when trying to spend from watch-only address
Expand Down Expand Up @@ -133,7 +131,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)

# Shielding the 800 utxos will occur over two transactions, since max tx size is 100,000 bytes.
# We don't verify shieldingValue as utxos are not selected in any specific order, so value can change on each test run.
Expand Down Expand Up @@ -166,7 +164,7 @@ def run_test (self):

# 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 @@ -178,7 +176,7 @@ def run_test (self):
# 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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 @@ -34,7 +35,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()

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

0 comments on commit 36e5497

Please sign in to comment.