2017-12-21 20:33:47 +01:00
|
|
|
#!/usr/bin/env python3
|
2016-05-12 22:16:44 +02:00
|
|
|
# Copyright (c) 2014-2015 The Bitcoin Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#
|
|
|
|
# Test txindex generation and fetching
|
|
|
|
#
|
|
|
|
|
|
|
|
import binascii
|
|
|
|
|
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
|
|
|
from test_framework.messages import COutPoint, CTransaction, CTxIn, CTxOut
|
|
|
|
from test_framework.script import CScript, OP_CHECKSIG, OP_DUP, OP_EQUALVERIFY, OP_HASH160
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2022-09-24 14:36:35 +02:00
|
|
|
from test_framework.util import assert_equal
|
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
|
|
|
|
|
|
|
|
2016-05-12 22:16:44 +02:00
|
|
|
class TxIndexTest(BitcoinTestFramework):
|
|
|
|
|
2019-09-24 00:56:31 +02:00
|
|
|
def set_test_params(self):
|
2018-04-18 13:48:59 +02:00
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.num_nodes = 4
|
2016-05-12 22:16:44 +02:00
|
|
|
|
2018-09-13 12:33:15 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2016-05-12 22:16:44 +02:00
|
|
|
def setup_network(self):
|
2019-09-24 00:56:31 +02:00
|
|
|
self.add_nodes(self.num_nodes)
|
2016-05-12 22:16:44 +02:00
|
|
|
# Nodes 0/1 are "wallet" nodes
|
2019-09-24 00:56:31 +02:00
|
|
|
self.start_node(0)
|
|
|
|
self.start_node(1, ["-txindex"])
|
2016-05-12 22:16:44 +02:00
|
|
|
# Nodes 2/3 are used for testing
|
2019-09-24 00:56:31 +02:00
|
|
|
self.start_node(2, ["-txindex"])
|
|
|
|
self.start_node(3, ["-txindex"])
|
2022-09-24 14:36:35 +02:00
|
|
|
self.connect_nodes(0, 1)
|
|
|
|
self.connect_nodes(0, 2)
|
|
|
|
self.connect_nodes(0, 3)
|
2016-05-12 22:16:44 +02:00
|
|
|
self.sync_all()
|
2018-11-02 16:41:29 +01:00
|
|
|
self.import_deterministic_coinbase_privkeys()
|
2016-05-12 22:16:44 +02:00
|
|
|
|
|
|
|
def run_test(self):
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("Mining blocks...")
|
2016-05-12 22:16:44 +02:00
|
|
|
self.nodes[0].generate(105)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
chain_height = self.nodes[1].getblockcount()
|
|
|
|
assert_equal(chain_height, 105)
|
|
|
|
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("Testing transaction index...")
|
2016-05-12 22:16:44 +02:00
|
|
|
|
2019-05-21 10:03:15 +02:00
|
|
|
addressHash = binascii.unhexlify("C5E4FB9171C22409809A3E8047A29C83886E325D")
|
2016-05-12 22:16:44 +02:00
|
|
|
scriptPubKey = CScript([OP_DUP, OP_HASH160, addressHash, OP_EQUALVERIFY, OP_CHECKSIG])
|
|
|
|
unspent = self.nodes[0].listunspent()
|
|
|
|
tx = CTransaction()
|
2019-05-21 10:03:15 +02:00
|
|
|
tx_fee_sat = 1000
|
|
|
|
amount = int(unspent[0]["amount"] * 100000000) - tx_fee_sat
|
2016-05-12 22:16:44 +02:00
|
|
|
tx.vin = [CTxIn(COutPoint(int(unspent[0]["txid"], 16), unspent[0]["vout"]))]
|
|
|
|
tx.vout = [CTxOut(amount, scriptPubKey)]
|
|
|
|
tx.rehash()
|
|
|
|
|
2021-08-27 21:03:02 +02:00
|
|
|
signed_tx = self.nodes[0].signrawtransactionwithwallet(tx.serialize().hex())
|
2021-12-12 07:27:44 +01:00
|
|
|
txid = self.nodes[0].sendrawtransaction(signed_tx["hex"], 0)
|
2016-05-12 22:16:44 +02:00
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# Check verbose raw transaction results
|
2019-05-21 10:03:15 +02:00
|
|
|
verbose = self.nodes[3].getrawtransaction(txid, 1)
|
2020-06-14 03:58:03 +02:00
|
|
|
assert_equal(verbose["vout"][0]["valueSat"], 50000000000 - tx_fee_sat)
|
|
|
|
assert_equal(verbose["vout"][0]["value"] * 100000000, 50000000000 - tx_fee_sat)
|
2016-05-12 22:16:44 +02:00
|
|
|
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("Passed")
|
2016-05-12 22:16:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
TxIndexTest().main()
|