mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 12:32:48 +01:00
9d33b30a87
dac7a111bdd3b0233d94cf68dae7a8bfc6ac9c64 refactor: test: use _ variable for unused loop counters (Sebastian Falbesoner)
Pull request description:
This tiny PR substitutes Python loops in the form of `for x in range(N): ...` by `for _ in range(N): ...` where applicable. The idea is indicating to the reader that a block (or statement, in list comprehensions) is just repeated N times, and that the loop counter is not used in the body, hence using the throwaway variable. This is already done quite often in the current tests (see e.g. `$ git grep "for _ in range("`). Another alternative would be using `itertools.repeat` (according to Python core developer Raymond Hettinger it's [even faster](https://twitter.com/raymondh/status/1144527183341375488)), but that doesn't seem to be widespread in use and I'm not sure about a readability increase.
The only drawback I see is that whenever one wants to debug loop iterations, one would need to introduce a loop variable again. Reviewing this is basically a no-brainer, since tests would fail immediately if a a substitution has taken place on a loop where the variable is used.
Instances to replace were found by `$ git grep "for.*in range("` and manually checked.
ACKs for top commit:
darosior:
ACK dac7a111bdd3b0233d94cf68dae7a8bfc6ac9c64
instagibbs:
manual inspection ACK dac7a111bd
practicalswift:
ACK dac7a111bdd3b0233d94cf68dae7a8bfc6ac9c64 -- the updated code is easier to reason about since the throwaway nature of a variable is expressed explicitly (using the Pythonic `_` idiom) instead of implicitly. Explicit is better than implicit was we all know by now :)
Tree-SHA512: 5f43ded9ce14e5e00b3876ec445b90acda1842f813149ae7bafa93f3ac3d510bb778e2c701187fd2c73585e6b87797bb2d2987139bd1a9ba7d58775a59392406
98 lines
4.8 KiB
Python
Executable File
98 lines
4.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 2020 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 generateblock rpc.
|
|
'''
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import (
|
|
assert_equal,
|
|
assert_raises_rpc_error,
|
|
)
|
|
|
|
class GenerateBlockTest(BitcoinTestFramework):
|
|
def set_test_params(self):
|
|
self.num_nodes = 1
|
|
|
|
def skip_test_if_missing_module(self):
|
|
self.skip_if_no_wallet()
|
|
|
|
def run_test(self):
|
|
node = self.nodes[0]
|
|
|
|
self.log.info('Generate an empty block to address')
|
|
address = node.getnewaddress()
|
|
hash = node.generateblock(address, [])['hash']
|
|
block = node.getblock(hash, 2)
|
|
assert_equal(len(block['tx']), 1)
|
|
assert_equal(block['tx'][0]['vout'][0]['scriptPubKey']['addresses'][0], address)
|
|
|
|
self.log.info('Generate an empty block to a descriptor')
|
|
hash = node.generateblock('addr(' + address + ')', [])['hash']
|
|
block = node.getblock(hash, 2)
|
|
assert_equal(len(block['tx']), 1)
|
|
assert_equal(block['tx'][0]['vout'][0]['scriptPubKey']['addresses'][0], address)
|
|
|
|
self.log.info('Generate an empty block to a combo descriptor with compressed pubkey')
|
|
combo_key = '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'
|
|
combo_address = 'yWziQMcwmKjRdzi7eWjwiQX8EjWcd6dSg6'
|
|
hash = node.generateblock('combo(' + combo_key + ')', [])['hash']
|
|
block = node.getblock(hash, 2)
|
|
assert_equal(len(block['tx']), 1)
|
|
assert_equal(block['tx'][0]['vout'][0]['scriptPubKey']['addresses'][0], combo_address)
|
|
|
|
# Generate 110 blocks to spend
|
|
node.generatetoaddress(110, address)
|
|
|
|
# Generate some extra mempool transactions to verify they don't get mined
|
|
for _ in range(10):
|
|
node.sendtoaddress(address, 0.001)
|
|
|
|
self.log.info('Generate block with txid')
|
|
txid = node.sendtoaddress(address, 1)
|
|
hash = node.generateblock(address, [txid])['hash']
|
|
block = node.getblock(hash, 1)
|
|
assert_equal(len(block['tx']), 2)
|
|
assert_equal(block['tx'][1], txid)
|
|
|
|
self.log.info('Generate block with raw tx')
|
|
utxos = node.listunspent(addresses=[address])
|
|
raw = node.createrawtransaction([{'txid':utxos[0]['txid'], 'vout':utxos[0]['vout']}],[{address:1}])
|
|
signed_raw = node.signrawtransactionwithwallet(raw)['hex']
|
|
hash = node.generateblock(address, [signed_raw])['hash']
|
|
block = node.getblock(hash, 1)
|
|
assert_equal(len(block['tx']), 2)
|
|
txid = block['tx'][1]
|
|
assert_equal(node.gettransaction(txid)['hex'], signed_raw)
|
|
|
|
self.log.info('Fail to generate block with out of order txs')
|
|
raw1 = node.createrawtransaction([{'txid':txid, 'vout':0}],[{address:0.9999}])
|
|
signed_raw1 = node.signrawtransactionwithwallet(raw1)['hex']
|
|
txid1 = node.sendrawtransaction(signed_raw1)
|
|
raw2 = node.createrawtransaction([{'txid':txid1, 'vout':0}],[{address:0.999}])
|
|
signed_raw2 = node.signrawtransactionwithwallet(raw2)['hex']
|
|
assert_raises_rpc_error(-25, 'TestBlockValidity failed: bad-txns-inputs-missingorspent', node.generateblock, address, [signed_raw2, txid1])
|
|
|
|
self.log.info('Fail to generate block with txid not in mempool')
|
|
missing_txid = '0000000000000000000000000000000000000000000000000000000000000000'
|
|
assert_raises_rpc_error(-5, 'Transaction ' + missing_txid + ' not in mempool.', node.generateblock, address, [missing_txid])
|
|
|
|
self.log.info('Fail to generate block with invalid raw tx')
|
|
invalid_raw_tx = '0000'
|
|
assert_raises_rpc_error(-22, 'Transaction decode failed for ' + invalid_raw_tx, node.generateblock, address, [invalid_raw_tx])
|
|
|
|
self.log.info('Fail to generate block with invalid address/descriptor')
|
|
assert_raises_rpc_error(-5, 'Invalid address or descriptor', node.generateblock, '1234', [])
|
|
|
|
self.log.info('Fail to generate block with a ranged descriptor')
|
|
ranged_descriptor = 'pkh(tpubD6NzVbkrYhZ4XgiXtGrdW5XDAPFCL9h7we1vwNCpn8tGbBcgfVYjXyhWo4E1xkh56hjod1RhGjxbaTLV3X4FyWuejifB9jusQ46QzG87VKp/0/*)'
|
|
assert_raises_rpc_error(-8, 'Ranged descriptor not accepted. Maybe pass through deriveaddresses first?', node.generateblock, ranged_descriptor, [])
|
|
|
|
self.log.info('Fail to generate block with a descriptor missing a private key')
|
|
child_descriptor = 'pkh(tpubD6NzVbkrYhZ4XgiXtGrdW5XDAPFCL9h7we1vwNCpn8tGbBcgfVYjXyhWo4E1xkh56hjod1RhGjxbaTLV3X4FyWuejifB9jusQ46QzG87VKp/0\'/0)'
|
|
assert_raises_rpc_error(-5, 'Cannot derive script without private keys', node.generateblock, child_descriptor, [])
|
|
|
|
if __name__ == '__main__':
|
|
GenerateBlockTest().main()
|