2016-05-06 11:23:48 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-12-31 18:50:11 +01:00
|
|
|
# Copyright (c) 2014-2020 The Bitcoin Core developers
|
2014-11-19 21:36:10 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Test the wallet accounts properly when there is a double-spend conflict."""
|
2021-04-12 16:58:49 +02:00
|
|
|
from decimal import Decimal
|
2014-11-19 21:36:10 +01:00
|
|
|
|
2015-05-02 12:53:35 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2021-04-12 16:58:49 +02:00
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
find_output,
|
|
|
|
)
|
2014-11-19 21:36:10 +01:00
|
|
|
|
2021-02-25 09:48:28 +01:00
|
|
|
|
2014-11-19 21:36:10 +01:00
|
|
|
class TxnMallTest(BitcoinTestFramework):
|
2017-09-01 18:47:13 +02:00
|
|
|
def set_test_params(self):
|
2021-02-25 09:48:28 +01:00
|
|
|
self.num_nodes = 3
|
2019-12-09 19:52:38 +01:00
|
|
|
self.supports_cli = False
|
2016-05-20 15:16:51 +02:00
|
|
|
|
2018-09-13 12:33:15 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2014-11-19 21:36:10 +01:00
|
|
|
def add_options(self, parser):
|
2021-07-04 00:41:23 +02:00
|
|
|
parser.add_argument("--mineblock", dest="mine_block", default=False, action="store_true",
|
|
|
|
help="Test double-spend of 1-confirmed transaction")
|
2014-11-19 21:36:10 +01:00
|
|
|
|
|
|
|
def setup_network(self):
|
|
|
|
# Start with split network:
|
2017-05-02 20:02:55 +02:00
|
|
|
super().setup_network()
|
2022-09-24 14:36:35 +02:00
|
|
|
self.disconnect_nodes(1, 2)
|
2014-11-19 21:36:10 +01:00
|
|
|
|
|
|
|
def run_test(self):
|
2016-03-06 16:14:39 +01:00
|
|
|
# All nodes should start with 12,500 DASH:
|
|
|
|
starting_balance = 12500
|
2019-02-25 17:44:18 +01:00
|
|
|
|
|
|
|
# All nodes should be out of IBD.
|
|
|
|
# If the nodes are not all out of IBD, that can interfere with
|
|
|
|
# blockchain sync later in the test when nodes are connected, due to
|
|
|
|
# timing issues.
|
|
|
|
for n in self.nodes:
|
|
|
|
assert n.getblockchaininfo()["initialblockdownload"] == False
|
|
|
|
|
2021-02-25 09:48:28 +01:00
|
|
|
for i in range(3):
|
2014-11-19 21:36:10 +01:00
|
|
|
assert_equal(self.nodes[i].getbalance(), starting_balance)
|
Merge #12987: tests/tools: Enable additional Python flake8 rules for automatic linting via Travis
643aad17fa Enable additional flake8 rules (practicalswift)
f020aca297 Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift)
Pull request description:
Enabled rules:
```
* E242: tab after ','
* E266: too many leading '#' for block comment
* E401: multiple imports on one line
* E402: module level import not at top of file
* E701: multiple statements on one line (colon)
* E901: SyntaxError: invalid syntax
* E902: TokenError: EOF in multi-line string
* F821: undefined name 'Foo'
* W293: blank line contains whitespace
* W606: 'async' and 'await' are reserved keywords starting with Python 3.7
```
Note to reviewers:
* In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR.
* Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes.
Before this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
5 E266 too many leading '#' for block comment
4 E401 multiple imports on one line
6 E402 module level import not at top of file
5 E701 multiple statements on one line (colon)
1 F812 list comprehension redefines 'n' from line 159
4 F821 undefined name 'ConnectionRefusedError'
28 W293 blank line contains whitespace
```
After this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
$
```
Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a
Signed-off-by: pasta <pasta@dashboost.org>
2018-04-16 17:49:49 +02:00
|
|
|
|
2018-05-11 00:08:57 +02:00
|
|
|
# Assign coins to foo and bar addresses:
|
|
|
|
node0_address_foo = self.nodes[0].getnewaddress()
|
|
|
|
fund_foo_txid = self.nodes[0].sendtoaddress(node0_address_foo, 12190)
|
2015-03-11 22:29:06 +01:00
|
|
|
fund_foo_tx = self.nodes[0].gettransaction(fund_foo_txid)
|
|
|
|
|
2018-05-11 00:08:57 +02:00
|
|
|
node0_address_bar = self.nodes[0].getnewaddress()
|
|
|
|
fund_bar_txid = self.nodes[0].sendtoaddress(node0_address_bar, 290)
|
2015-03-11 22:29:06 +01:00
|
|
|
fund_bar_tx = self.nodes[0].gettransaction(fund_bar_txid)
|
|
|
|
|
2018-05-11 00:08:57 +02:00
|
|
|
assert_equal(self.nodes[0].getbalance(),
|
|
|
|
starting_balance + fund_foo_tx["fee"] + fund_bar_tx["fee"])
|
2014-11-19 21:36:10 +01:00
|
|
|
|
|
|
|
# Coins are sent to node1_address
|
2018-05-11 00:08:57 +02:00
|
|
|
node1_address = self.nodes[1].getnewaddress()
|
2014-11-19 21:36:10 +01:00
|
|
|
|
2016-03-06 16:14:39 +01:00
|
|
|
# First: use raw transaction API to send 12400 DASH to node1_address,
|
2014-11-19 21:36:10 +01:00
|
|
|
# but don't broadcast:
|
2015-03-11 22:29:06 +01:00
|
|
|
doublespend_fee = Decimal('-.02')
|
|
|
|
rawtx_input_0 = {}
|
|
|
|
rawtx_input_0["txid"] = fund_foo_txid
|
2016-03-06 16:14:39 +01:00
|
|
|
rawtx_input_0["vout"] = find_output(self.nodes[0], fund_foo_txid, 12190)
|
2015-03-11 22:29:06 +01:00
|
|
|
rawtx_input_1 = {}
|
|
|
|
rawtx_input_1["txid"] = fund_bar_txid
|
2016-03-06 16:14:39 +01:00
|
|
|
rawtx_input_1["vout"] = find_output(self.nodes[0], fund_bar_txid, 290)
|
2015-03-11 22:29:06 +01:00
|
|
|
inputs = [rawtx_input_0, rawtx_input_1]
|
|
|
|
change_address = self.nodes[0].getnewaddress()
|
2014-11-19 21:36:10 +01:00
|
|
|
outputs = {}
|
2016-03-06 16:14:39 +01:00
|
|
|
outputs[node1_address] = 12400
|
|
|
|
outputs[change_address] = 12480 - 12400 + doublespend_fee
|
2014-11-19 21:36:10 +01:00
|
|
|
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
|
2018-02-20 03:29:22 +01:00
|
|
|
doublespend = self.nodes[0].signrawtransactionwithwallet(rawtx)
|
2014-11-19 21:36:10 +01:00
|
|
|
assert_equal(doublespend["complete"], True)
|
|
|
|
|
2016-03-06 16:14:39 +01:00
|
|
|
# Create two spends using 1 500 DASH coin each
|
2018-05-11 00:08:57 +02:00
|
|
|
txid1 = self.nodes[0].sendtoaddress(node1_address, 400)
|
|
|
|
txid2 = self.nodes[0].sendtoaddress(node1_address, 200)
|
Merge #12987: tests/tools: Enable additional Python flake8 rules for automatic linting via Travis
643aad17fa Enable additional flake8 rules (practicalswift)
f020aca297 Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift)
Pull request description:
Enabled rules:
```
* E242: tab after ','
* E266: too many leading '#' for block comment
* E401: multiple imports on one line
* E402: module level import not at top of file
* E701: multiple statements on one line (colon)
* E901: SyntaxError: invalid syntax
* E902: TokenError: EOF in multi-line string
* F821: undefined name 'Foo'
* W293: blank line contains whitespace
* W606: 'async' and 'await' are reserved keywords starting with Python 3.7
```
Note to reviewers:
* In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR.
* Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes.
Before this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
5 E266 too many leading '#' for block comment
4 E401 multiple imports on one line
6 E402 module level import not at top of file
5 E701 multiple statements on one line (colon)
1 F812 list comprehension redefines 'n' from line 159
4 F821 undefined name 'ConnectionRefusedError'
28 W293 blank line contains whitespace
```
After this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
$
```
Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a
Signed-off-by: pasta <pasta@dashboost.org>
2018-04-16 17:49:49 +02:00
|
|
|
|
2014-11-19 21:36:10 +01:00
|
|
|
# Have node0 mine a block:
|
|
|
|
if (self.options.mine_block):
|
2024-09-26 21:17:04 +02:00
|
|
|
self.generate(self.nodes[0], 1, sync_fun=lambda: self.sync_blocks(self.nodes[0:2]))
|
2014-11-19 21:36:10 +01:00
|
|
|
|
|
|
|
tx1 = self.nodes[0].gettransaction(txid1)
|
|
|
|
tx2 = self.nodes[0].gettransaction(txid2)
|
|
|
|
|
2016-03-06 16:14:39 +01:00
|
|
|
# Node0's balance should be starting balance, plus 500DASH for another
|
|
|
|
# matured block, minus 400, minus 200, and minus transaction fees:
|
2015-03-11 22:29:06 +01:00
|
|
|
expected = starting_balance + fund_foo_tx["fee"] + fund_bar_tx["fee"]
|
Merge #12987: tests/tools: Enable additional Python flake8 rules for automatic linting via Travis
643aad17fa Enable additional flake8 rules (practicalswift)
f020aca297 Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift)
Pull request description:
Enabled rules:
```
* E242: tab after ','
* E266: too many leading '#' for block comment
* E401: multiple imports on one line
* E402: module level import not at top of file
* E701: multiple statements on one line (colon)
* E901: SyntaxError: invalid syntax
* E902: TokenError: EOF in multi-line string
* F821: undefined name 'Foo'
* W293: blank line contains whitespace
* W606: 'async' and 'await' are reserved keywords starting with Python 3.7
```
Note to reviewers:
* In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR.
* Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes.
Before this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
5 E266 too many leading '#' for block comment
4 E401 multiple imports on one line
6 E402 module level import not at top of file
5 E701 multiple statements on one line (colon)
1 F812 list comprehension redefines 'n' from line 159
4 F821 undefined name 'ConnectionRefusedError'
28 W293 blank line contains whitespace
```
After this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
$
```
Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a
Signed-off-by: pasta <pasta@dashboost.org>
2018-04-16 17:49:49 +02:00
|
|
|
if self.options.mine_block:
|
|
|
|
expected += 500
|
2014-11-19 21:36:10 +01:00
|
|
|
expected += tx1["amount"] + tx1["fee"]
|
|
|
|
expected += tx2["amount"] + tx2["fee"]
|
|
|
|
assert_equal(self.nodes[0].getbalance(), expected)
|
|
|
|
|
|
|
|
if self.options.mine_block:
|
|
|
|
assert_equal(tx1["confirmations"], 1)
|
|
|
|
assert_equal(tx2["confirmations"], 1)
|
2018-05-11 00:08:57 +02:00
|
|
|
# Node1's balance should be both transaction amounts:
|
|
|
|
assert_equal(self.nodes[1].getbalance(), starting_balance - tx1["amount"] - tx2["amount"])
|
2014-11-19 21:36:10 +01:00
|
|
|
else:
|
|
|
|
assert_equal(tx1["confirmations"], 0)
|
|
|
|
assert_equal(tx2["confirmations"], 0)
|
Merge #12987: tests/tools: Enable additional Python flake8 rules for automatic linting via Travis
643aad17fa Enable additional flake8 rules (practicalswift)
f020aca297 Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift)
Pull request description:
Enabled rules:
```
* E242: tab after ','
* E266: too many leading '#' for block comment
* E401: multiple imports on one line
* E402: module level import not at top of file
* E701: multiple statements on one line (colon)
* E901: SyntaxError: invalid syntax
* E902: TokenError: EOF in multi-line string
* F821: undefined name 'Foo'
* W293: blank line contains whitespace
* W606: 'async' and 'await' are reserved keywords starting with Python 3.7
```
Note to reviewers:
* In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR.
* Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes.
Before this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
5 E266 too many leading '#' for block comment
4 E401 multiple imports on one line
6 E402 module level import not at top of file
5 E701 multiple statements on one line (colon)
1 F812 list comprehension redefines 'n' from line 159
4 F821 undefined name 'ConnectionRefusedError'
28 W293 blank line contains whitespace
```
After this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
$
```
Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a
Signed-off-by: pasta <pasta@dashboost.org>
2018-04-16 17:49:49 +02:00
|
|
|
|
2015-03-11 22:29:06 +01:00
|
|
|
# Now give doublespend and its parents to miner:
|
|
|
|
self.nodes[2].sendrawtransaction(fund_foo_tx["hex"])
|
|
|
|
self.nodes[2].sendrawtransaction(fund_bar_tx["hex"])
|
2015-11-26 18:42:07 +01:00
|
|
|
doublespend_txid = self.nodes[2].sendrawtransaction(doublespend["hex"])
|
2014-11-19 21:36:10 +01:00
|
|
|
# ... mine a block...
|
2024-09-26 21:17:04 +02:00
|
|
|
self.generate(self.nodes[2], 1, sync_fun=self.no_op)
|
2014-11-19 21:36:10 +01:00
|
|
|
|
|
|
|
# Reconnect the split network, and sync chain:
|
2022-09-24 14:36:35 +02:00
|
|
|
self.connect_nodes(1, 2)
|
2024-10-01 21:25:52 +02:00
|
|
|
self.generate(self.nodes[2], 1) # Mine another block to make sure we sync
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks()
|
2015-11-26 18:42:07 +01:00
|
|
|
assert_equal(self.nodes[0].gettransaction(doublespend_txid)["confirmations"], 2)
|
2014-11-19 21:36:10 +01:00
|
|
|
|
|
|
|
# Re-fetch transaction info:
|
|
|
|
tx1 = self.nodes[0].gettransaction(txid1)
|
|
|
|
tx2 = self.nodes[0].gettransaction(txid2)
|
2015-11-26 18:42:07 +01:00
|
|
|
|
2014-11-19 21:36:10 +01:00
|
|
|
# Both transactions should be conflicted
|
2015-11-26 18:42:07 +01:00
|
|
|
assert_equal(tx1["confirmations"], -2)
|
|
|
|
assert_equal(tx2["confirmations"], -2)
|
2014-11-19 21:36:10 +01:00
|
|
|
|
2016-03-06 16:14:39 +01:00
|
|
|
# Node0's total balance should be starting balance, plus 1000DASH for
|
|
|
|
# two more matured blocks, minus 12400 for the double-spend, plus fees (which are
|
2015-03-11 22:29:06 +01:00
|
|
|
# negative):
|
2016-03-06 16:14:39 +01:00
|
|
|
expected = starting_balance + 1000 - 12400 + fund_foo_tx["fee"] + fund_bar_tx["fee"] + doublespend_fee
|
2014-11-19 21:36:10 +01:00
|
|
|
assert_equal(self.nodes[0].getbalance(), expected)
|
2018-05-11 00:08:57 +02:00
|
|
|
|
|
|
|
# Node1's balance should be its initial balance (12500 for 25 block rewards) plus the doublespend:
|
|
|
|
assert_equal(self.nodes[1].getbalance(), 12500 + 12400)
|
2014-11-19 21:36:10 +01:00
|
|
|
|
2021-02-25 09:48:28 +01:00
|
|
|
|
2014-11-19 21:36:10 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
TxnMallTest().main()
|