2014-11-27 06:01:25 +01:00
|
|
|
#!/usr/bin/env python2
|
2015-12-13 17:58:29 +01:00
|
|
|
# Copyright (c) 2014-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2014-11-19 21:55:40 +01:00
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2015-05-02 12:53:35 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import *
|
2014-11-19 21:55:40 +01:00
|
|
|
|
|
|
|
class WalletTest (BitcoinTestFramework):
|
|
|
|
|
2015-09-21 12:49:13 +02:00
|
|
|
def check_fee_amount(self, curr_balance, balance_with_fee, fee_per_byte, tx_size):
|
|
|
|
"""Return curr_balance after asserting the fee was in range"""
|
|
|
|
fee = balance_with_fee - curr_balance
|
|
|
|
target_fee = fee_per_byte * tx_size
|
|
|
|
if fee < target_fee:
|
|
|
|
raise AssertionError("Fee of %s BTC too low! (Should be %s BTC)"%(str(fee), str(target_fee)))
|
|
|
|
# allow the node's estimation to be at most 2 bytes off
|
|
|
|
if fee > fee_per_byte * (tx_size + 2):
|
|
|
|
raise AssertionError("Fee of %s BTC too high! (Should be %s BTC)"%(str(fee), str(target_fee)))
|
|
|
|
return curr_balance
|
|
|
|
|
2014-11-19 21:55:40 +01:00
|
|
|
def setup_chain(self):
|
|
|
|
print("Initializing test directory "+self.options.tmpdir)
|
2015-03-23 18:47:18 +01:00
|
|
|
initialize_chain_clean(self.options.tmpdir, 4)
|
2014-11-19 21:55:40 +01:00
|
|
|
|
|
|
|
def setup_network(self, split=False):
|
|
|
|
self.nodes = start_nodes(3, self.options.tmpdir)
|
|
|
|
connect_nodes_bi(self.nodes,0,1)
|
|
|
|
connect_nodes_bi(self.nodes,1,2)
|
|
|
|
connect_nodes_bi(self.nodes,0,2)
|
|
|
|
self.is_network_split=False
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
def run_test (self):
|
|
|
|
print "Mining blocks..."
|
|
|
|
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[0].generate(1)
|
2014-11-19 21:55:40 +01:00
|
|
|
|
2014-12-25 08:57:16 +01:00
|
|
|
walletinfo = self.nodes[0].getwalletinfo()
|
|
|
|
assert_equal(walletinfo['immature_balance'], 50)
|
|
|
|
assert_equal(walletinfo['balance'], 0)
|
|
|
|
|
2014-11-19 21:55:40 +01:00
|
|
|
self.sync_all()
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[1].generate(101)
|
2014-11-19 21:55:40 +01:00
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
assert_equal(self.nodes[0].getbalance(), 50)
|
|
|
|
assert_equal(self.nodes[1].getbalance(), 50)
|
|
|
|
assert_equal(self.nodes[2].getbalance(), 0)
|
|
|
|
|
|
|
|
# Send 21 BTC from 0 to 2 using sendtoaddress call.
|
|
|
|
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)
|
|
|
|
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 10)
|
|
|
|
|
2014-12-25 08:57:16 +01:00
|
|
|
walletinfo = self.nodes[0].getwalletinfo()
|
|
|
|
assert_equal(walletinfo['immature_balance'], 0)
|
|
|
|
|
2015-04-28 16:48:28 +02:00
|
|
|
# Have node0 mine a block, thus it will collect its own fee.
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[0].generate(1)
|
2014-11-19 21:55:40 +01:00
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# Have node1 generate 100 blocks (so node0 can recover the fee)
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[1].generate(100)
|
2014-11-19 21:55:40 +01:00
|
|
|
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[2].getbalance(), 21)
|
|
|
|
|
|
|
|
# Node0 should have two unspent outputs.
|
2015-07-14 21:13:16 +02:00
|
|
|
# Create a couple of transactions to send them to node2, submit them through
|
|
|
|
# node1, and make sure both node0 and node2 pick them up properly:
|
2014-11-19 21:55:40 +01:00
|
|
|
node0utxos = self.nodes[0].listunspent(1)
|
|
|
|
assert_equal(len(node0utxos), 2)
|
|
|
|
|
|
|
|
# create both transactions
|
|
|
|
txns_to_send = []
|
2015-07-14 21:13:16 +02:00
|
|
|
for utxo in node0utxos:
|
2014-11-19 21:55:40 +01:00
|
|
|
inputs = []
|
|
|
|
outputs = {}
|
|
|
|
inputs.append({ "txid" : utxo["txid"], "vout" : utxo["vout"]})
|
2015-12-06 20:24:02 +01:00
|
|
|
outputs[self.nodes[2].getnewaddress("from1")] = utxo["amount"] - 3
|
2014-11-19 21:55:40 +01:00
|
|
|
raw_tx = self.nodes[0].createrawtransaction(inputs, outputs)
|
|
|
|
txns_to_send.append(self.nodes[0].signrawtransaction(raw_tx))
|
|
|
|
|
|
|
|
# Have node 1 (miner) send the transactions
|
|
|
|
self.nodes[1].sendrawtransaction(txns_to_send[0]["hex"], True)
|
|
|
|
self.nodes[1].sendrawtransaction(txns_to_send[1]["hex"], True)
|
|
|
|
|
|
|
|
# Have node1 mine a block to confirm transactions:
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[1].generate(1)
|
2014-11-19 21:55:40 +01:00
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
assert_equal(self.nodes[0].getbalance(), 0)
|
2015-12-06 20:24:02 +01:00
|
|
|
assert_equal(self.nodes[2].getbalance(), 94)
|
|
|
|
assert_equal(self.nodes[2].getbalance("from1"), 94-21)
|
2014-11-19 21:55:40 +01:00
|
|
|
|
2015-03-05 14:59:19 +01:00
|
|
|
# Send 10 BTC normal
|
2014-07-23 14:34:36 +02:00
|
|
|
address = self.nodes[0].getnewaddress("test")
|
2015-09-21 12:49:13 +02:00
|
|
|
fee_per_byte = Decimal('0.001') / 1000
|
|
|
|
self.nodes[2].settxfee(fee_per_byte * 1000)
|
2014-07-23 14:34:36 +02:00
|
|
|
txid = self.nodes[2].sendtoaddress(address, 10, "", "", False)
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[2].generate(1)
|
2014-07-23 14:34:36 +02:00
|
|
|
self.sync_all()
|
2015-12-06 20:24:02 +01:00
|
|
|
node_2_bal = self.check_fee_amount(self.nodes[2].getbalance(), Decimal('84'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
|
2015-09-21 12:49:13 +02:00
|
|
|
assert_equal(self.nodes[0].getbalance(), Decimal('10'))
|
2014-07-23 14:34:36 +02:00
|
|
|
|
2015-03-05 14:59:19 +01:00
|
|
|
# Send 10 BTC with subtract fee from amount
|
2014-07-23 14:34:36 +02:00
|
|
|
txid = self.nodes[2].sendtoaddress(address, 10, "", "", True)
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[2].generate(1)
|
2014-07-23 14:34:36 +02:00
|
|
|
self.sync_all()
|
2015-09-21 12:49:13 +02:00
|
|
|
node_2_bal -= Decimal('10')
|
|
|
|
assert_equal(self.nodes[2].getbalance(), node_2_bal)
|
|
|
|
node_0_bal = self.check_fee_amount(self.nodes[0].getbalance(), Decimal('20'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
|
2014-11-19 21:55:40 +01:00
|
|
|
|
2015-03-05 14:59:19 +01:00
|
|
|
# Sendmany 10 BTC
|
|
|
|
txid = self.nodes[2].sendmany('from1', {address: 10}, 0, "", [])
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[2].generate(1)
|
2015-03-05 14:59:19 +01:00
|
|
|
self.sync_all()
|
2015-09-21 12:49:13 +02:00
|
|
|
node_0_bal += Decimal('10')
|
|
|
|
node_2_bal = self.check_fee_amount(self.nodes[2].getbalance(), node_2_bal - Decimal('10'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
|
|
|
|
assert_equal(self.nodes[0].getbalance(), node_0_bal)
|
2015-03-05 14:59:19 +01:00
|
|
|
|
|
|
|
# Sendmany 10 BTC with subtract fee from amount
|
|
|
|
txid = self.nodes[2].sendmany('from1', {address: 10}, 0, "", [address])
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[2].generate(1)
|
2015-03-05 14:59:19 +01:00
|
|
|
self.sync_all()
|
2015-09-21 12:49:13 +02:00
|
|
|
node_2_bal -= Decimal('10')
|
|
|
|
assert_equal(self.nodes[2].getbalance(), node_2_bal)
|
|
|
|
node_0_bal = self.check_fee_amount(self.nodes[0].getbalance(), node_0_bal + Decimal('10'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
|
2015-03-05 14:59:19 +01:00
|
|
|
|
2015-03-23 18:47:18 +01:00
|
|
|
# Test ResendWalletTransactions:
|
|
|
|
# Create a couple of transactions, then start up a fourth
|
|
|
|
# node (nodes[3]) and ask nodes[0] to rebroadcast.
|
|
|
|
# EXPECT: nodes[3] should have those transactions in its mempool.
|
|
|
|
txid1 = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1)
|
|
|
|
txid2 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1)
|
|
|
|
sync_mempools(self.nodes)
|
|
|
|
|
|
|
|
self.nodes.append(start_node(3, self.options.tmpdir))
|
|
|
|
connect_nodes_bi(self.nodes, 0, 3)
|
|
|
|
sync_blocks(self.nodes)
|
|
|
|
|
|
|
|
relayed = self.nodes[0].resendwallettransactions()
|
2015-12-02 18:12:23 +01:00
|
|
|
assert_equal(set(relayed), {txid1, txid2})
|
2015-03-23 18:47:18 +01:00
|
|
|
sync_mempools(self.nodes)
|
|
|
|
|
|
|
|
assert(txid1 in self.nodes[3].getrawmempool())
|
2015-07-14 21:13:16 +02:00
|
|
|
|
2015-04-21 10:27:35 +02:00
|
|
|
#check if we can list zero value tx as available coins
|
|
|
|
#1. create rawtx
|
2015-07-14 21:13:16 +02:00
|
|
|
#2. hex-changed one output to 0.0
|
2015-04-21 10:27:35 +02:00
|
|
|
#3. sign and send
|
|
|
|
#4. check if recipient (node0) can list the zero value tx
|
|
|
|
usp = self.nodes[1].listunspent()
|
|
|
|
inputs = [{"txid":usp[0]['txid'], "vout":usp[0]['vout']}]
|
|
|
|
outputs = {self.nodes[1].getnewaddress(): 49.998, self.nodes[0].getnewaddress(): 11.11}
|
2015-07-14 21:13:16 +02:00
|
|
|
|
2015-04-21 10:27:35 +02:00
|
|
|
rawTx = self.nodes[1].createrawtransaction(inputs, outputs).replace("c0833842", "00000000") #replace 11.11 with 0.0 (int32)
|
|
|
|
decRawTx = self.nodes[1].decoderawtransaction(rawTx)
|
|
|
|
signedRawTx = self.nodes[1].signrawtransaction(rawTx)
|
|
|
|
decRawTx = self.nodes[1].decoderawtransaction(signedRawTx['hex'])
|
|
|
|
zeroValueTxid= decRawTx['txid']
|
|
|
|
sendResp = self.nodes[1].sendrawtransaction(signedRawTx['hex'])
|
2015-07-14 21:13:16 +02:00
|
|
|
|
2015-04-21 10:27:35 +02:00
|
|
|
self.sync_all()
|
|
|
|
self.nodes[1].generate(1) #mine a block
|
|
|
|
self.sync_all()
|
2015-07-14 21:13:16 +02:00
|
|
|
|
2015-04-21 10:27:35 +02:00
|
|
|
unspentTxs = self.nodes[0].listunspent() #zero value tx must be in listunspents output
|
|
|
|
found = False
|
|
|
|
for uTx in unspentTxs:
|
|
|
|
if uTx['txid'] == zeroValueTxid:
|
|
|
|
found = True
|
2015-12-15 17:15:13 +01:00
|
|
|
assert_equal(uTx['amount'], Decimal('0'))
|
2015-04-21 10:27:35 +02:00
|
|
|
assert(found)
|
2015-07-14 21:13:16 +02:00
|
|
|
|
2015-04-01 11:56:52 +02:00
|
|
|
#do some -walletbroadcast tests
|
|
|
|
stop_nodes(self.nodes)
|
|
|
|
wait_bitcoinds()
|
|
|
|
self.nodes = start_nodes(3, self.options.tmpdir, [["-walletbroadcast=0"],["-walletbroadcast=0"],["-walletbroadcast=0"]])
|
|
|
|
connect_nodes_bi(self.nodes,0,1)
|
|
|
|
connect_nodes_bi(self.nodes,1,2)
|
|
|
|
connect_nodes_bi(self.nodes,0,2)
|
|
|
|
self.sync_all()
|
2015-03-23 18:47:18 +01:00
|
|
|
|
2015-12-15 17:15:13 +01:00
|
|
|
txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2)
|
2015-04-01 11:56:52 +02:00
|
|
|
txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted)
|
2015-04-09 17:36:10 +02:00
|
|
|
self.nodes[1].generate(1) #mine a block, tx should not be in there
|
2015-04-01 11:56:52 +02:00
|
|
|
self.sync_all()
|
2015-12-15 17:15:13 +01:00
|
|
|
assert_equal(self.nodes[2].getbalance(), node_2_bal) #should not be changed because tx was not broadcasted
|
2015-07-14 21:13:16 +02:00
|
|
|
|
2015-04-01 11:56:52 +02:00
|
|
|
#now broadcast from another node, mine a block, sync, and check the balance
|
|
|
|
self.nodes[1].sendrawtransaction(txObjNotBroadcasted['hex'])
|
2015-04-09 17:36:10 +02:00
|
|
|
self.nodes[1].generate(1)
|
2015-04-01 11:56:52 +02:00
|
|
|
self.sync_all()
|
2015-12-18 12:35:50 +01:00
|
|
|
node_2_bal += 2
|
2015-04-01 11:56:52 +02:00
|
|
|
txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted)
|
2015-12-18 12:35:50 +01:00
|
|
|
assert_equal(self.nodes[2].getbalance(), node_2_bal)
|
2015-07-14 21:13:16 +02:00
|
|
|
|
2015-04-01 11:56:52 +02:00
|
|
|
#create another tx
|
2015-12-15 17:15:13 +01:00
|
|
|
txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2)
|
2015-07-14 21:13:16 +02:00
|
|
|
|
2015-04-01 11:56:52 +02:00
|
|
|
#restart the nodes with -walletbroadcast=1
|
|
|
|
stop_nodes(self.nodes)
|
|
|
|
wait_bitcoinds()
|
|
|
|
self.nodes = start_nodes(3, self.options.tmpdir)
|
|
|
|
connect_nodes_bi(self.nodes,0,1)
|
|
|
|
connect_nodes_bi(self.nodes,1,2)
|
|
|
|
connect_nodes_bi(self.nodes,0,2)
|
|
|
|
sync_blocks(self.nodes)
|
2015-07-14 21:13:16 +02:00
|
|
|
|
2015-04-09 17:36:10 +02:00
|
|
|
self.nodes[0].generate(1)
|
2015-04-01 11:56:52 +02:00
|
|
|
sync_blocks(self.nodes)
|
2015-12-18 12:35:50 +01:00
|
|
|
node_2_bal += 2
|
2015-07-14 21:13:16 +02:00
|
|
|
|
2015-04-01 11:56:52 +02:00
|
|
|
#tx should be added to balance because after restarting the nodes tx should be broadcastet
|
2015-12-18 12:35:50 +01:00
|
|
|
assert_equal(self.nodes[2].getbalance(), node_2_bal)
|
2015-07-14 21:13:16 +02:00
|
|
|
|
|
|
|
#send a tx with value in a string (PR#6380 +)
|
|
|
|
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "2")
|
|
|
|
txObj = self.nodes[0].gettransaction(txId)
|
2015-12-15 17:15:13 +01:00
|
|
|
assert_equal(txObj['amount'], Decimal('-2'))
|
2015-07-14 21:13:16 +02:00
|
|
|
|
|
|
|
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "0.0001")
|
|
|
|
txObj = self.nodes[0].gettransaction(txId)
|
2015-12-15 17:15:13 +01:00
|
|
|
assert_equal(txObj['amount'], Decimal('-0.0001'))
|
2015-07-14 21:13:16 +02:00
|
|
|
|
|
|
|
#check if JSON parser can handle scientific notation in strings
|
|
|
|
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1e-4")
|
|
|
|
txObj = self.nodes[0].gettransaction(txId)
|
2015-12-15 17:15:13 +01:00
|
|
|
assert_equal(txObj['amount'], Decimal('-0.0001'))
|
2015-07-14 21:13:16 +02:00
|
|
|
|
|
|
|
#this should fail
|
|
|
|
errorString = ""
|
|
|
|
try:
|
|
|
|
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1f-4")
|
|
|
|
except JSONRPCException,e:
|
|
|
|
errorString = e.error['message']
|
|
|
|
|
2015-12-15 17:15:13 +01:00
|
|
|
assert_equal("Invalid amount" in errorString, True)
|
2015-07-14 21:13:16 +02:00
|
|
|
|
|
|
|
errorString = ""
|
|
|
|
try:
|
|
|
|
self.nodes[0].generate("2") #use a string to as block amount parameter must fail because it's not interpreted as amount
|
|
|
|
except JSONRPCException,e:
|
|
|
|
errorString = e.error['message']
|
|
|
|
|
2015-12-15 17:15:13 +01:00
|
|
|
assert_equal("not an integer" in errorString, True)
|
2015-07-14 21:13:16 +02:00
|
|
|
|
2015-12-18 12:35:50 +01:00
|
|
|
#check if wallet or blochchain maintenance changes the balance
|
|
|
|
self.sync_all()
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
balance_nodes = [self.nodes[i].getbalance() for i in range(3)]
|
|
|
|
|
|
|
|
maintenance = [
|
|
|
|
'-rescan',
|
|
|
|
'-reindex',
|
|
|
|
'-zapwallettxes=1',
|
|
|
|
'-zapwallettxes=2',
|
|
|
|
'-salvagewallet',
|
|
|
|
]
|
|
|
|
for m in maintenance:
|
2016-01-16 23:07:18 +01:00
|
|
|
print "check " + m
|
2015-12-18 12:35:50 +01:00
|
|
|
stop_nodes(self.nodes)
|
|
|
|
wait_bitcoinds()
|
|
|
|
self.nodes = start_nodes(3, self.options.tmpdir, [[m]] * 3)
|
|
|
|
connect_nodes_bi(self.nodes,0,1)
|
|
|
|
connect_nodes_bi(self.nodes,1,2)
|
|
|
|
connect_nodes_bi(self.nodes,0,2)
|
|
|
|
self.sync_all()
|
|
|
|
assert_equal(balance_nodes, [self.nodes[i].getbalance() for i in range(3)])
|
2015-07-14 21:13:16 +02:00
|
|
|
|
|
|
|
|
2014-11-19 21:55:40 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
WalletTest ().main ()
|