[tests] Fix flake8 warnings in several wallet functional tests

This commit fixes flake8 warnings in the following functional tests:

- wallet_listreceivedby.py
- wallet_basic.py
- wallet_txn_clone.py
- wallet_listsinceblock.py
- wallet_import_rescan.py
- wallet_listtransactions.py
- wallet_importprunedfunds.py
- wallet_txn_doublspend.py
This commit is contained in:
John Newbery 2018-04-24 14:55:53 -05:00
parent 3186ad4a81
commit a533834d50
8 changed files with 231 additions and 211 deletions

View file

@ -3,8 +3,20 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test the wallet.""" """Test the wallet."""
from decimal import Decimal
import time
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import * from test_framework.util import (
assert_array_result,
assert_equal,
assert_fee_amount,
assert_raises_rpc_error,
connect_nodes_bi,
sync_blocks,
sync_mempools,
wait_until,
)
class WalletTest(BitcoinTestFramework): class WalletTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
@ -216,7 +228,7 @@ class WalletTest(BitcoinTestFramework):
assert_equal(self.nodes[0].getunconfirmedbalance(), 1) assert_equal(self.nodes[0].getunconfirmedbalance(), 1)
# check if we can list zero value tx as available coins # check if we can list zero value tx as available coins
#1. create rawtx # 1. create raw_tx
# 2. hex-changed one output to 0.0 # 2. hex-changed one output to 0.0
# 3. sign and send # 3. sign and send
# 4. check if recipient (node0) can list the zero value tx # 4. check if recipient (node0) can list the zero value tx
@ -224,21 +236,20 @@ class WalletTest(BitcoinTestFramework):
inputs = [{"txid": usp[0]['txid'], "vout": usp[0]['vout']}] inputs = [{"txid": usp[0]['txid'], "vout": usp[0]['vout']}]
outputs = {self.nodes[1].getnewaddress(): 49.998, self.nodes[0].getnewaddress(): 11.11} outputs = {self.nodes[1].getnewaddress(): 49.998, self.nodes[0].getnewaddress(): 11.11}
rawTx = self.nodes[1].createrawtransaction(inputs, outputs).replace("c0833842", "00000000") #replace 11.11 with 0.0 (int32) raw_tx = self.nodes[1].createrawtransaction(inputs, outputs).replace("c0833842", "00000000") # replace 11.11 with 0.0 (int32)
decRawTx = self.nodes[1].decoderawtransaction(rawTx) signed_raw_tx = self.nodes[1].signrawtransactionwithwallet(raw_tx)
signedRawTx = self.nodes[1].signrawtransactionwithwallet(rawTx) decoded_raw_tx = self.nodes[1].decoderawtransaction(signed_raw_tx['hex'])
decRawTx = self.nodes[1].decoderawtransaction(signedRawTx['hex']) zero_value_txid = decoded_raw_tx['txid']
zeroValueTxid= decRawTx['txid'] self.nodes[1].sendrawtransaction(signed_raw_tx['hex'])
self.nodes[1].sendrawtransaction(signedRawTx['hex'])
self.sync_all() self.sync_all()
self.nodes[1].generate(1) # mine a block self.nodes[1].generate(1) # mine a block
self.sync_all() self.sync_all()
unspentTxs = self.nodes[0].listunspent() #zero value tx must be in listunspents output unspent_txs = self.nodes[0].listunspent() # zero value tx must be in listunspents output
found = False found = False
for uTx in unspentTxs: for uTx in unspent_txs:
if uTx['txid'] == zeroValueTxid: if uTx['txid'] == zero_value_txid:
found = True found = True
assert_equal(uTx['amount'], Decimal('0')) assert_equal(uTx['amount'], Decimal('0'))
assert(found) assert(found)
@ -253,22 +264,22 @@ class WalletTest(BitcoinTestFramework):
connect_nodes_bi(self.nodes, 0, 2) connect_nodes_bi(self.nodes, 0, 2)
self.sync_all([self.nodes[0:3]]) self.sync_all([self.nodes[0:3]])
txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2) txid_not_broadcast = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2)
txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted) tx_obj_not_broadcast = self.nodes[0].gettransaction(txid_not_broadcast)
self.nodes[1].generate(1) # mine a block, tx should not be in there self.nodes[1].generate(1) # mine a block, tx should not be in there
self.sync_all([self.nodes[0:3]]) self.sync_all([self.nodes[0:3]])
assert_equal(self.nodes[2].getbalance(), node_2_bal) # should not be changed because tx was not broadcasted assert_equal(self.nodes[2].getbalance(), node_2_bal) # should not be changed because tx was not broadcasted
# now broadcast from another node, mine a block, sync, and check the balance # now broadcast from another node, mine a block, sync, and check the balance
self.nodes[1].sendrawtransaction(txObjNotBroadcasted['hex']) self.nodes[1].sendrawtransaction(tx_obj_not_broadcast['hex'])
self.nodes[1].generate(1) self.nodes[1].generate(1)
self.sync_all([self.nodes[0:3]]) self.sync_all([self.nodes[0:3]])
node_2_bal += 2 node_2_bal += 2
txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted) tx_obj_not_broadcast = self.nodes[0].gettransaction(txid_not_broadcast)
assert_equal(self.nodes[2].getbalance(), node_2_bal) assert_equal(self.nodes[2].getbalance(), node_2_bal)
# create another tx # create another tx
txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2) self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2)
# restart the nodes with -walletbroadcast=1 # restart the nodes with -walletbroadcast=1
self.stop_nodes() self.stop_nodes()
@ -288,18 +299,18 @@ class WalletTest(BitcoinTestFramework):
assert_equal(self.nodes[2].getbalance(), node_2_bal) assert_equal(self.nodes[2].getbalance(), node_2_bal)
# send a tx with value in a string (PR#6380 +) # send a tx with value in a string (PR#6380 +)
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "2") txid = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "2")
txObj = self.nodes[0].gettransaction(txId) tx_obj = self.nodes[0].gettransaction(txid)
assert_equal(txObj['amount'], Decimal('-2')) assert_equal(tx_obj['amount'], Decimal('-2'))
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "0.0001") txid = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "0.0001")
txObj = self.nodes[0].gettransaction(txId) tx_obj = self.nodes[0].gettransaction(txid)
assert_equal(txObj['amount'], Decimal('-0.0001')) assert_equal(tx_obj['amount'], Decimal('-0.0001'))
# check if JSON parser can handle scientific notation in strings # check if JSON parser can handle scientific notation in strings
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1e-4") txid = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1e-4")
txObj = self.nodes[0].gettransaction(txId) tx_obj = self.nodes[0].gettransaction(txid)
assert_equal(txObj['amount'], Decimal('-0.0001')) assert_equal(tx_obj['amount'], Decimal('-0.0001'))
# This will raise an exception because the amount type is wrong # This will raise an exception because the amount type is wrong
assert_raises_rpc_error(-3, "Invalid amount", self.nodes[0].sendtoaddress, self.nodes[2].getnewaddress(), "1f-4") assert_raises_rpc_error(-3, "Invalid amount", self.nodes[0].sendtoaddress, self.nodes[2].getnewaddress(), "1f-4")
@ -335,13 +346,13 @@ class WalletTest(BitcoinTestFramework):
{"spendable": True}) {"spendable": True})
# Mine a block from node0 to an address from node1 # Mine a block from node0 to an address from node1
cbAddr = self.nodes[1].getnewaddress() coinbase_addr = self.nodes[1].getnewaddress()
blkHash = self.nodes[0].generatetoaddress(1, cbAddr)[0] block_hash = self.nodes[0].generatetoaddress(1, coinbase_addr)[0]
cbTxId = self.nodes[0].getblock(blkHash)['tx'][0] coinbase_txid = self.nodes[0].getblock(block_hash)['tx'][0]
self.sync_all([self.nodes[0:3]]) self.sync_all([self.nodes[0:3]])
# Check that the txid and balance is found by node1 # Check that the txid and balance is found by node1
self.nodes[1].gettransaction(cbTxId) self.nodes[1].gettransaction(coinbase_txid)
# check if wallet or blockchain maintenance changes the balance # check if wallet or blockchain maintenance changes the balance
self.sync_all([self.nodes[0:3]]) self.sync_all([self.nodes[0:3]])

View file

@ -3,8 +3,13 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test the importprunedfunds and removeprunedfunds RPCs.""" """Test the importprunedfunds and removeprunedfunds RPCs."""
from decimal import Decimal
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import * from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
)
class ImportPrunedFundsTest(BitcoinTestFramework): class ImportPrunedFundsTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):

View file

@ -112,8 +112,8 @@ class ReceivedByTest(BitcoinTestFramework):
self.log.info("listreceivedbylabel + getreceivedbylabel Test") self.log.info("listreceivedbylabel + getreceivedbylabel Test")
# set pre-state # set pre-state
addrArr = self.nodes[1].getnewaddress() address = self.nodes[1].getnewaddress()
label = self.nodes[1].getaccount(addrArr) label = self.nodes[1].getaccount(address)
received_by_label_json = [r for r in self.nodes[1].listreceivedbylabel() if r["label"] == label][0] received_by_label_json = [r for r in self.nodes[1].listreceivedbylabel() if r["label"] == label][0]
balance_by_label = self.nodes[1].getreceivedbylabel(label) balance_by_label = self.nodes[1].getreceivedbylabel(label)

View file

@ -150,26 +150,26 @@ class ListSinceBlockTest (BitcoinTestFramework):
# send from nodes[1] using utxo to nodes[0] # send from nodes[1] using utxo to nodes[0]
change = '%.8f' % (float(utxo['amount']) - 1.0003) change = '%.8f' % (float(utxo['amount']) - 1.0003)
recipientDict = { recipient_dict = {
self.nodes[0].getnewaddress(): 1, self.nodes[0].getnewaddress(): 1,
self.nodes[1].getnewaddress(): change, self.nodes[1].getnewaddress(): change,
} }
utxoDicts = [{ utxo_dicts = [{
'txid': utxo['txid'], 'txid': utxo['txid'],
'vout': utxo['vout'], 'vout': utxo['vout'],
}] }]
txid1 = self.nodes[1].sendrawtransaction( txid1 = self.nodes[1].sendrawtransaction(
self.nodes[1].signrawtransactionwithwallet( self.nodes[1].signrawtransactionwithwallet(
self.nodes[1].createrawtransaction(utxoDicts, recipientDict))['hex']) self.nodes[1].createrawtransaction(utxo_dicts, recipient_dict))['hex'])
# send from nodes[2] using utxo to nodes[3] # send from nodes[2] using utxo to nodes[3]
recipientDict2 = { recipient_dict2 = {
self.nodes[3].getnewaddress(): 1, self.nodes[3].getnewaddress(): 1,
self.nodes[2].getnewaddress(): change, self.nodes[2].getnewaddress(): change,
} }
self.nodes[2].sendrawtransaction( self.nodes[2].sendrawtransaction(
self.nodes[2].signrawtransactionwithwallet( self.nodes[2].signrawtransactionwithwallet(
self.nodes[2].createrawtransaction(utxoDicts, recipientDict2))['hex']) self.nodes[2].createrawtransaction(utxo_dicts, recipient_dict2))['hex'])
# generate on both sides # generate on both sides
lastblockhash = self.nodes[1].generate(3)[2] lastblockhash = self.nodes[1].generate(3)[2]
@ -225,16 +225,16 @@ class ListSinceBlockTest (BitcoinTestFramework):
utxos = self.nodes[2].listunspent() utxos = self.nodes[2].listunspent()
utxo = utxos[0] utxo = utxos[0]
change = '%.8f' % (float(utxo['amount']) - 1.0003) change = '%.8f' % (float(utxo['amount']) - 1.0003)
recipientDict = { recipient_dict = {
self.nodes[0].getnewaddress(): 1, self.nodes[0].getnewaddress(): 1,
self.nodes[2].getnewaddress(): change, self.nodes[2].getnewaddress(): change,
} }
utxoDicts = [{ utxo_dicts = [{
'txid': utxo['txid'], 'txid': utxo['txid'],
'vout': utxo['vout'], 'vout': utxo['vout'],
}] }]
signedtxres = self.nodes[2].signrawtransactionwithwallet( signedtxres = self.nodes[2].signrawtransactionwithwallet(
self.nodes[2].createrawtransaction(utxoDicts, recipientDict)) self.nodes[2].createrawtransaction(utxo_dicts, recipient_dict))
assert signedtxres['complete'] assert signedtxres['complete']
signedtx = signedtxres['hex'] signedtx = signedtxres['hex']

View file

@ -3,13 +3,20 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test the listtransactions API.""" """Test the listtransactions API."""
from decimal import Decimal
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.mininode import CTransaction, COIN
from io import BytesIO from io import BytesIO
def txFromHex(hexstring): from test_framework.mininode import CTransaction, COIN
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_array_result,
assert_equal,
bytes_to_hex_str,
hex_str_to_bytes,
sync_mempools,
)
def tx_from_hex(hexstring):
tx = CTransaction() tx = CTransaction()
f = BytesIO(hex_str_to_bytes(hexstring)) f = BytesIO(hex_str_to_bytes(hexstring))
tx.deserialize(f) tx.deserialize(f)
@ -146,7 +153,7 @@ class ListTransactionsTest(BitcoinTestFramework):
inputs = [{"txid": txid_2, "vout": utxo_to_use["vout"]}] inputs = [{"txid": txid_2, "vout": utxo_to_use["vout"]}]
outputs = {self.nodes[1].getnewaddress(): 0.998} outputs = {self.nodes[1].getnewaddress(): 0.998}
tx3 = self.nodes[0].createrawtransaction(inputs, outputs) tx3 = self.nodes[0].createrawtransaction(inputs, outputs)
tx3_modified = txFromHex(tx3) tx3_modified = tx_from_hex(tx3)
tx3_modified.vin[0].nSequence = 0 tx3_modified.vin[0].nSequence = 0
tx3 = bytes_to_hex_str(tx3_modified.serialize()) tx3 = bytes_to_hex_str(tx3_modified.serialize())
tx3_signed = self.nodes[0].signrawtransactionwithwallet(tx3)['hex'] tx3_signed = self.nodes[0].signrawtransactionwithwallet(tx3)['hex']
@ -197,7 +204,5 @@ class ListTransactionsTest(BitcoinTestFramework):
assert_equal(self.nodes[0].gettransaction(txid_3b)["bip125-replaceable"], "no") assert_equal(self.nodes[0].gettransaction(txid_3b)["bip125-replaceable"], "no")
assert_equal(self.nodes[0].gettransaction(txid_4)["bip125-replaceable"], "unknown") assert_equal(self.nodes[0].gettransaction(txid_4)["bip125-replaceable"], "unknown")
if __name__ == '__main__': if __name__ == '__main__':
ListTransactionsTest().main() ListTransactionsTest().main()

View file

@ -5,7 +5,12 @@
"""Test the wallet accounts properly when there are cloned transactions with malleated scriptsigs.""" """Test the wallet accounts properly when there are cloned transactions with malleated scriptsigs."""
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import * from test_framework.util import (
assert_equal,
connect_nodes,
disconnect_nodes,
sync_blocks,
)
class TxnMallTest(BitcoinTestFramework): class TxnMallTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
@ -71,8 +76,7 @@ class TxnMallTest(BitcoinTestFramework):
pos0 = 2 * (4 + 1 + 36 + 1 + 4 + 1) pos0 = 2 * (4 + 1 + 36 + 1 + 4 + 1)
hex40 = "00286bee00000000" hex40 = "00286bee00000000"
output_len = 16 + 2 + 2 * int("0x" + clone_raw[pos0 + 16:pos0 + 16 + 2], 0) output_len = 16 + 2 + 2 * int("0x" + clone_raw[pos0 + 16:pos0 + 16 + 2], 0)
if (rawtx1["vout"][0]["value"] == 40 and clone_raw[pos0 : pos0 + 16] != hex40 or if (rawtx1["vout"][0]["value"] == 40 and clone_raw[pos0:pos0 + 16] != hex40 or rawtx1["vout"][0]["value"] != 40 and clone_raw[pos0:pos0 + 16] == hex40):
rawtx1["vout"][0]["value"] != 40 and clone_raw[pos0 : pos0 + 16] == hex40):
output0 = clone_raw[pos0:pos0 + output_len] output0 = clone_raw[pos0:pos0 + output_len]
output1 = clone_raw[pos0 + output_len:pos0 + 2 * output_len] output1 = clone_raw[pos0 + output_len:pos0 + 2 * output_len]
clone_raw = clone_raw[:pos0] + output1 + output0 + clone_raw[pos0 + 2 * output_len:] clone_raw = clone_raw[:pos0] + output1 + output0 + clone_raw[pos0 + 2 * output_len:]
@ -153,16 +157,11 @@ class TxnMallTest(BitcoinTestFramework):
# "bar" should have been debited by (possibly unconfirmed) tx2 # "bar" should have been debited by (possibly unconfirmed) tx2
assert_equal(self.nodes[0].getbalance("bar", 0), 29 + tx2["amount"] + tx2["fee"]) assert_equal(self.nodes[0].getbalance("bar", 0), 29 + tx2["amount"] + tx2["fee"])
# "" should have starting balance, less funding txes, plus subsidies # "" should have starting balance, less funding txes, plus subsidies
assert_equal(self.nodes[0].getbalance("", 0), starting_balance assert_equal(self.nodes[0].getbalance("", 0),
- 1219 starting_balance - 1219 + fund_foo_tx["fee"] - 29 + fund_bar_tx["fee"] + 100)
+ fund_foo_tx["fee"]
- 29
+ fund_bar_tx["fee"]
+ 100)
# Node1's "from0" account balance # Node1's "from0" account balance
assert_equal(self.nodes[1].getbalance("from0", 0), -(tx1["amount"] + tx2["amount"])) assert_equal(self.nodes[1].getbalance("from0", 0), -(tx1["amount"] + tx2["amount"]))
if __name__ == '__main__': if __name__ == '__main__':
TxnMallTest().main() TxnMallTest().main()

View file

@ -3,9 +3,16 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test the wallet accounts properly when there is a double-spend conflict.""" """Test the wallet accounts properly when there is a double-spend conflict."""
from decimal import Decimal
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import * from test_framework.util import (
assert_equal,
connect_nodes,
disconnect_nodes,
find_output,
sync_blocks,
)
class TxnMallTest(BitcoinTestFramework): class TxnMallTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
@ -128,18 +135,11 @@ class TxnMallTest(BitcoinTestFramework):
# fees (which are negative) # fees (which are negative)
assert_equal(self.nodes[0].getbalance("foo"), 1219) assert_equal(self.nodes[0].getbalance("foo"), 1219)
assert_equal(self.nodes[0].getbalance("bar"), 29) assert_equal(self.nodes[0].getbalance("bar"), 29)
assert_equal(self.nodes[0].getbalance(""), starting_balance assert_equal(self.nodes[0].getbalance(""),
-1219 starting_balance - 1219 - 29 - 1240 + 100 + fund_foo_tx["fee"] + fund_bar_tx["fee"] + doublespend_fee)
- 29
-1240
+ 100
+ fund_foo_tx["fee"]
+ fund_bar_tx["fee"]
+ doublespend_fee)
# Node1's "from0" account balance should be just the doublespend: # Node1's "from0" account balance should be just the doublespend:
assert_equal(self.nodes[1].getbalance("from0"), 1240) assert_equal(self.nodes[1].getbalance("from0"), 1240)
if __name__ == '__main__': if __name__ == '__main__':
TxnMallTest().main() TxnMallTest().main()