Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions qa/rpc-tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def initialize_datadir(dirname, n):
if not os.path.isdir(datadir):
os.makedirs(datadir)
with open(os.path.join(datadir, "bitcoin.conf"), 'w') as f:
f.write("testnet=0\n");
f.write("regtest=1\n");
f.write("rpcuser=rt\n");
f.write("rpcpassword=rt\n");
Expand Down
34 changes: 13 additions & 21 deletions qa/rpc-tests/wallet.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@
#
# Exercise the wallet. Ported from wallet.sh.
# Does the following:
# a) creates 3 nodes, with an empty chain (no blocks).
# b) node0 mines a block
# c) node1 mines 101 blocks, so now nodes 0 and 1 have 50btc, node2 has none.
# d) node0 sends 21 btc to node2, in two transactions (11 btc, then 10 btc).
# e) node0 mines a block, collects the fee on the second transaction
# f) node1 mines 100 blocks, to mature node0's just-mined block
# g) check that node0 has 100-21, node2 has 21
# h) node0 should now have 2 unspent outputs; send these to node2 via raw tx broadcast by node1
# i) have node1 mine a block
# j) check balances - node0 should have 0, node2 should have 100
#

from test_framework import BitcoinTestFramework
Expand Down Expand Up @@ -45,9 +35,10 @@ def run_test (self):
self.nodes[1].setgenerate(True, 101)
self.sync_all()

assert_equal(self.nodes[0].getbalance(), 50)
assert_equal(self.nodes[1].getbalance(), 50)
assert_equal(self.nodes[2].getbalance(), 0)
genesis_balance = 10500000
assert_equal(self.nodes[0].getbalance(), genesis_balance)
assert_equal(self.nodes[1].getbalance(), genesis_balance)
assert_equal(self.nodes[2].getbalance(), genesis_balance)

# Send 21 BTC from 0 to 2 using sendtoaddress call.
# Second transaction will be child of first, and will require a fee
Expand All @@ -62,9 +53,8 @@ def run_test (self):
self.nodes[1].setgenerate(True, 100)
self.sync_all()

# node0 should end up with 100 btc in block rewards plus fees, but
# minus the 21 plus fees sent to node2
assert_equal(self.nodes[0].getbalance(), 100-21)
assert_equal(self.nodes[0].getbalance(), genesis_balance - 21)
assert_equal(self.nodes[1].getbalance(), 0)
assert_equal(self.nodes[2].getbalance(), 21)

# Node0 should have two unspent outputs.
Expand All @@ -75,12 +65,14 @@ def run_test (self):

# create both transactions
txns_to_send = []
for utxo in node0utxos:
fee = Decimal(20000)/Decimal(100000000)
for utxo in node0utxos:
inputs = []
outputs = {}
inputs.append({ "txid" : utxo["txid"], "vout" : utxo["vout"]})
outputs[self.nodes[2].getnewaddress("from1")] = utxo["amount"]
inputs.append({ "txid" : utxo["txid"], "vout" : utxo["vout"], "nValue": utxo["amount"]})
outputs[self.nodes[2].getnewaddress("from1")] = utxo["amount"] - fee/len(node0utxos)
raw_tx = self.nodes[0].createrawtransaction(inputs, outputs)
raw_tx = self.nodes[0].blindrawtransaction(raw_tx)
txns_to_send.append(self.nodes[0].signrawtransaction(raw_tx))

# Have node 1 (miner) send the transactions
Expand All @@ -92,8 +84,8 @@ def run_test (self):
self.sync_all()

assert_equal(self.nodes[0].getbalance(), 0)
assert_equal(self.nodes[2].getbalance(), 100)
assert_equal(self.nodes[2].getbalance("from1"), 100-21)
assert_equal(self.nodes[2].getbalance(), genesis_balance - fee)
assert_equal(self.nodes[2].getbalance("from1"), genesis_balance - fee - 21)


if __name__ == '__main__':
Expand Down
82 changes: 45 additions & 37 deletions qa/rpc-tests/walletbackup.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@
4 nodes. 1 2 and 3 send transactions between each other,
fourth node is a miner.
1 2 3 each mine a block to start, then
Miner creates 100 blocks so 1 2 3 each have 50 mature
coins to spend.
Then 5 iterations of 1/2/3 sending coins amongst
5 iterations of 1/2/3 sending coins amongst
themselves to get transactions in the wallets,
and the miner mining one block.

Wallets are backed up using dumpwallet/backupwallet.
Then 5 more iterations of transactions and mining a block.

Miner then generates 101 more blocks, so any
transaction fees paid mature.

Sanity check:
Sum(1,2,3,4 balances) == 114*50
Sum(1,2,3,4 balances) == genesis_balance

1/2/3 are shutdown, and their wallets erased.
Then restore using wallet.dat backup. And
Expand Down Expand Up @@ -95,43 +92,51 @@ def stop_three(self):
stop_node(self.nodes[2], 2)

def erase_three(self):
os.remove(self.options.tmpdir + "/node0/regtest/wallet.dat")
os.remove(self.options.tmpdir + "/node1/regtest/wallet.dat")
os.remove(self.options.tmpdir + "/node2/regtest/wallet.dat")
os.remove(self.options.tmpdir + "/node0/alpharegtest/wallet.dat")
os.remove(self.options.tmpdir + "/node1/alpharegtest/wallet.dat")
os.remove(self.options.tmpdir + "/node2/alpharegtest/wallet.dat")

def run_test(self):
logging.info("Generating initial blockchain")
self.nodes[0].setgenerate(True, 1)
sync_blocks(self.nodes)
self.nodes[1].setgenerate(True, 1)
sync_blocks(self.nodes)
self.nodes[2].setgenerate(True, 1)
sync_blocks(self.nodes)
self.nodes[3].setgenerate(True, 100)
sync_blocks(self.nodes)

assert_equal(self.nodes[0].getbalance(), 50)
genesis_balance = 10500000
assert_equal(self.nodes[0].getbalance(), genesis_balance)
assert_equal(self.nodes[1].getbalance(), genesis_balance)
assert_equal(self.nodes[2].getbalance(), genesis_balance)
assert_equal(self.nodes[3].getbalance(), genesis_balance)

self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 50)
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 50)
self.nodes[0].setgenerate(True, 101)
self.sync_all()

assert_equal(self.nodes[0].getbalance(), genesis_balance - 100)
assert_equal(self.nodes[1].getbalance(), 50)
assert_equal(self.nodes[2].getbalance(), 50)
assert_equal(self.nodes[3].getbalance(), 0)


logging.info("Creating transactions")
# Five rounds of sending each other transactions.
for i in range(5):
self.do_one_round()

logging.info("Backing up")
tmpdir = self.options.tmpdir
self.nodes[0].backupwallet(tmpdir + "/node0/wallet.bak")
self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.dump")
self.nodes[1].backupwallet(tmpdir + "/node1/wallet.bak")
self.nodes[1].dumpwallet(tmpdir + "/node1/wallet.dump")
self.nodes[2].backupwallet(tmpdir + "/node2/wallet.bak")
self.nodes[2].dumpwallet(tmpdir + "/node2/wallet.dump")

logging.info("More transactions")
for i in range(5):
self.do_one_round()
blinding_keys = [{}, {}, {}]

def backup(i):
self.nodes[i].backupwallet(tmpdir + "/node%s/wallet.bak" % i)
self.nodes[i].dumpwallet(tmpdir + "/node%s/wallet.dump" % i)
# Export blinding keys
for addrs in self.nodes[i].listaddressgroupings():
for addr in addrs:
blinding_keys[i][addr[0]] = self.nodes[i].dumpblindingkey(addr[0])
backup(0)
backup(1)
backup(2)

# Generate 101 more blocks, so any fees paid mature
self.nodes[3].setgenerate(True, 101)
Expand All @@ -143,9 +148,7 @@ def run_test(self):
balance3 = self.nodes[3].getbalance()
total = balance0 + balance1 + balance2 + balance3

# At this point, there are 214 blocks (103 for setup, then 10 rounds, then 101.)
# 114 are mature, so the sum of all wallets should be 114 * 50 = 5700.
assert_equal(total, 5700)
assert_equal(total, genesis_balance)

##
# Test restoring spender wallets from backups
Expand All @@ -155,13 +158,13 @@ def run_test(self):
self.erase_three()

# Start node2 with no chain
shutil.rmtree(self.options.tmpdir + "/node2/regtest/blocks")
shutil.rmtree(self.options.tmpdir + "/node2/regtest/chainstate")
shutil.rmtree(self.options.tmpdir + "/node2/alpharegtest/blocks")
shutil.rmtree(self.options.tmpdir + "/node2/alpharegtest/chainstate")

# Restore wallets from backup
shutil.copyfile(tmpdir + "/node0/wallet.bak", tmpdir + "/node0/regtest/wallet.dat")
shutil.copyfile(tmpdir + "/node1/wallet.bak", tmpdir + "/node1/regtest/wallet.dat")
shutil.copyfile(tmpdir + "/node2/wallet.bak", tmpdir + "/node2/regtest/wallet.dat")
shutil.copyfile(tmpdir + "/node0/wallet.bak", tmpdir + "/node0/alpharegtest/wallet.dat")
shutil.copyfile(tmpdir + "/node1/wallet.bak", tmpdir + "/node1/alpharegtest/wallet.dat")
shutil.copyfile(tmpdir + "/node2/wallet.bak", tmpdir + "/node2/alpharegtest/wallet.dat")

logging.info("Re-starting nodes")
self.start_three()
Expand All @@ -176,19 +179,24 @@ def run_test(self):
self.erase_three()

#start node2 with no chain
shutil.rmtree(self.options.tmpdir + "/node2/regtest/blocks")
shutil.rmtree(self.options.tmpdir + "/node2/regtest/chainstate")
shutil.rmtree(self.options.tmpdir + "/node2/alpharegtest/blocks")
shutil.rmtree(self.options.tmpdir + "/node2/alpharegtest/chainstate")

self.start_three()

assert_equal(self.nodes[0].getbalance(), 0)
assert_equal(self.nodes[1].getbalance(), 0)
assert_equal(self.nodes[0].getbalance(), genesis_balance)
assert_equal(self.nodes[1].getbalance(), genesis_balance)
assert_equal(self.nodes[2].getbalance(), 0)

self.nodes[0].importwallet(tmpdir + "/node0/wallet.dump")
self.nodes[1].importwallet(tmpdir + "/node1/wallet.dump")
self.nodes[2].importwallet(tmpdir + "/node2/wallet.dump")

# import blinding keys, one for for each address
for i, per_node_keys in enumerate(blinding_keys):
for addr in per_node_keys:
self.nodes[i].importblindingkey(addr, per_node_keys[addr])

sync_blocks(self.nodes)

assert_equal(self.nodes[0].getbalance(), balance0)
Expand Down