Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#23117: test: Use assert_equal over assert for e…
Browse files Browse the repository at this point in the history
…asier debugging

fa8f3ba test: pep-8 (MarcoFalke)
fac5708 test: Use assert_equal over assert for easier debugging (MarcoFalke)

Pull request description:

  See #23116

ACKs for top commit:
  jonatack:
    Light code review ACK fa8f3ba
  hebasto:
    ACK fa8f3ba, I have reviewed the code and it looks OK, I agree it can be merged.
  theStack:
    Code-review ACK fa8f3ba

Tree-SHA512: bfdb91fd72144f05c38ed9aa8e40e17f59dcb90ac164b8bd01e241296a6c91a01f79a95a5eb97cc8445fdabe8605ef5b8062219a683613627c776feb7433a460
  • Loading branch information
fanquake committed Sep 28, 2021
2 parents a9d0cec + fa8f3ba commit dccf3d2
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions test/functional/feature_segwit.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
assert_greater_than_or_equal,
assert_is_hex_string,
assert_raises_rpc_error,
try_rpc,
Expand All @@ -54,20 +55,24 @@
P2WPKH = 0
P2WSH = 1


def getutxo(txid):
utxo = {}
utxo["vout"] = 0
utxo["txid"] = txid
return utxo


def find_spendable_utxo(node, min_value):
for utxo in node.listunspent(query_options={'minimumAmount': min_value}):
if utxo['spendable']:
return utxo

raise AssertionError(f"Unspent output equal or higher than {min_value} not found")

txs_mined = {} # txindex from txid to blockhash

txs_mined = {} # txindex from txid to blockhash


class SegWitTest(BitcoinTestFramework):
def set_test_params(self):
Expand Down Expand Up @@ -124,18 +129,18 @@ def run_test(self):
self.log.info("Verify sigops are counted in GBT with pre-BIP141 rules before the fork")
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
tmpl = self.nodes[0].getblocktemplate({'rules': ['segwit']})
assert tmpl['sizelimit'] == 1000000
assert_equal(tmpl['sizelimit'], 1000000)
assert 'weightlimit' not in tmpl
assert tmpl['sigoplimit'] == 20000
assert tmpl['transactions'][0]['hash'] == txid
assert tmpl['transactions'][0]['sigops'] == 2
assert_equal(tmpl['sigoplimit'], 20000)
assert_equal(tmpl['transactions'][0]['hash'], txid)
assert_equal(tmpl['transactions'][0]['sigops'], 2)
assert '!segwit' not in tmpl['rules']
self.generate(self.nodes[0], 1) # block 162

balance_presetup = self.nodes[0].getbalance()
self.pubkey = []
p2sh_ids = [] # p2sh_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE embedded in p2sh
wit_ids = [] # wit_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE via bare witness
p2sh_ids = [] # p2sh_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE embedded in p2sh
wit_ids = [] # wit_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE via bare witness
for i in range(3):
newaddress = self.nodes[i].getnewaddress()
self.pubkey.append(self.nodes[i].getaddressinfo(newaddress)["pubkey"])
Expand Down Expand Up @@ -215,7 +220,7 @@ def run_test(self):
witnesses = coinbase_tx["decoded"]["vin"][0]["txinwitness"]
assert_equal(len(witnesses), 1)
assert_is_hex_string(witnesses[0])
assert_equal(witnesses[0], '00'*32)
assert_equal(witnesses[0], '00' * 32)

self.log.info("Verify witness txs without witness data are invalid after the fork")
self.fail_accept(self.nodes[2], 'non-mandatory-script-verify-flag (Witness program hash mismatch)', wit_ids[NODE_2][P2WPKH][2], sign=False)
Expand All @@ -232,11 +237,11 @@ def run_test(self):
self.log.info("Verify sigops are counted in GBT with BIP141 rules after the fork")
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
tmpl = self.nodes[0].getblocktemplate({'rules': ['segwit']})
assert tmpl['sizelimit'] >= 3999577 # actual maximum size is lower due to minimum mandatory non-witness data
assert tmpl['weightlimit'] == 4000000
assert tmpl['sigoplimit'] == 80000
assert tmpl['transactions'][0]['txid'] == txid
assert tmpl['transactions'][0]['sigops'] == 8
assert_greater_than_or_equal(tmpl['sizelimit'], 3999577) # actual maximum size is lower due to minimum mandatory non-witness data
assert_equal(tmpl['weightlimit'], 4000000)
assert_equal(tmpl['sigoplimit'], 80000)
assert_equal(tmpl['transactions'][0]['txid'], txid)
assert_equal(tmpl['transactions'][0]['sigops'], 8)
assert '!segwit' in tmpl['rules']

self.generate(self.nodes[0], 1) # Mine a block to clear the gbt cache
Expand Down Expand Up @@ -356,7 +361,7 @@ def run_test(self):

for i in compressed_spendable_address:
v = self.nodes[0].getaddressinfo(i)
if (v['isscript']):
if v['isscript']:
[bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
# p2sh multisig with compressed keys should always be spendable
spendable_anytime.extend([p2sh])
Expand All @@ -375,7 +380,7 @@ def run_test(self):

for i in uncompressed_spendable_address:
v = self.nodes[0].getaddressinfo(i)
if (v['isscript']):
if v['isscript']:
[bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
# p2sh multisig with uncompressed keys should always be spendable
spendable_anytime.extend([p2sh])
Expand All @@ -394,7 +399,7 @@ def run_test(self):

for i in compressed_solvable_address:
v = self.nodes[0].getaddressinfo(i)
if (v['isscript']):
if v['isscript']:
# Multisig without private is not seen after addmultisigaddress, but seen after importaddress
[bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
solvable_after_importaddress.extend([bare, p2sh, p2wsh, p2sh_p2wsh])
Expand All @@ -407,7 +412,7 @@ def run_test(self):

for i in uncompressed_solvable_address:
v = self.nodes[0].getaddressinfo(i)
if (v['isscript']):
if v['isscript']:
[bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
# Base uncompressed multisig without private is not seen after addmultisigaddress, but seen after importaddress
solvable_after_importaddress.extend([bare, p2sh])
Expand Down Expand Up @@ -446,7 +451,7 @@ def run_test(self):
importlist = []
for i in compressed_spendable_address + uncompressed_spendable_address + compressed_solvable_address + uncompressed_solvable_address:
v = self.nodes[0].getaddressinfo(i)
if (v['isscript']):
if v['isscript']:
bare = bytes.fromhex(v['hex'])
importlist.append(bare.hex())
importlist.append(script_to_p2wsh_script(bare).hex())
Expand Down Expand Up @@ -509,7 +514,7 @@ def run_test(self):

for i in compressed_spendable_address:
v = self.nodes[0].getaddressinfo(i)
if (v['isscript']):
if v['isscript']:
[bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
premature_witaddress.append(script_to_p2sh(p2wsh))
else:
Expand All @@ -519,7 +524,7 @@ def run_test(self):

for i in uncompressed_spendable_address + uncompressed_solvable_address:
v = self.nodes[0].getaddressinfo(i)
if (v['isscript']):
if v['isscript']:
[bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
# P2WSH and P2SH(P2WSH) multisig with uncompressed keys are never seen
unseen_anytime.extend([p2wsh, p2sh_p2wsh])
Expand All @@ -530,7 +535,7 @@ def run_test(self):

for i in compressed_solvable_address:
v = self.nodes[0].getaddressinfo(i)
if (v['isscript']):
if v['isscript']:
[bare, p2sh, p2wsh, p2sh_p2wsh] = self.p2sh_address_to_script(v)
premature_witaddress.append(script_to_p2sh(p2wsh))
else:
Expand Down Expand Up @@ -597,13 +602,13 @@ def mine_and_test_listunspent(self, script_list, ismine):
watchcount = 0
spendcount = 0
for i in self.nodes[0].listunspent():
if (i['txid'] == txid):
if i['txid'] == txid:
watchcount += 1
if i['spendable']:
spendcount += 1
if (ismine == 2):
if ismine == 2:
assert_equal(spendcount, len(script_list))
elif (ismine == 1):
elif ismine == 1:
assert_equal(watchcount, len(script_list))
assert_equal(spendcount, 0)
else:
Expand All @@ -615,7 +620,7 @@ def p2sh_address_to_script(self, v):
p2sh = CScript(bytes.fromhex(v['scriptPubKey']))
p2wsh = script_to_p2wsh_script(bare)
p2sh_p2wsh = script_to_p2sh_script(p2wsh)
return([bare, p2sh, p2wsh, p2sh_p2wsh])
return [bare, p2sh, p2wsh, p2sh_p2wsh]

def p2pkh_address_to_script(self, v):
pubkey = bytes.fromhex(v['pubkey'])
Expand Down

0 comments on commit dccf3d2

Please sign in to comment.