Skip to content

Commit

Permalink
Fix functional/feature_segwit.py file
Browse files Browse the repository at this point in the history
  • Loading branch information
janus committed Sep 18, 2024
1 parent 1f93c0e commit 8bcafc7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion test/functional/feature_dbcrash.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def set_test_params(self):

# Node3 is a normal node with default args, except will mine full blocks
# and txs with "dust" outputs
self.node3_args = ["-blockmaxweight=4000000", "-dustrelayfee=0"]
self.node3_args = ["-blockmaxweight=400000", "-dustrelayfee=0"]
self.extra_args = [self.node0_args, self.node1_args, self.node2_args, self.node3_args]

def setup_network(self):
Expand Down
76 changes: 51 additions & 25 deletions test/functional/feature_segwit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
send_to_witness,
witness_script,
)
from test_framework.descriptors import descsum_create
from test_framework.messages import (
COIN,
COutPoint,
Expand Down Expand Up @@ -48,10 +49,10 @@
assert_raises_rpc_error,
try_rpc,
)

from test_framework.wallet_util import (
get_generate_key,
)

NODE_0 = 0
NODE_2 = 2
P2WPKH = 0
Expand Down Expand Up @@ -112,7 +113,7 @@ def setup_network(self):
self.sync_all()

def success_mine(self, node, txid, sign, redeem_script=""):
send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script)
send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("199.998"), sign, redeem_script)
block = self.generate(node, 1)
assert_equal(len(node.getblock(block[0])["tx"]), 2)
self.sync_blocks()
Expand All @@ -126,12 +127,12 @@ 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_equal(tmpl['sizelimit'], 100000)
assert 'weightlimit' not in tmpl
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']
assert_equal(tmpl['sizelimit'], 400000)
#assert 'weightlimit' not in tmpl Commented out because Bitgesell defaults to segwit
assert_equal(tmpl['sigoplimit'], 80000)
#assert_equal(tmpl['transactions'][0]['hash'], txid) Commented out because Bitgesell defaults to segwit
assert_equal(tmpl['transactions'][0]['sigops'], 9)
#assert '!segwit' not in tmpl['rules'] Commented out because Bitgesell defaults to segwit
self.generate(self.nodes[0], 1) # block 162

balance_presetup = self.nodes[0].getbalance()
Expand All @@ -147,6 +148,31 @@ def run_test(self):
bip173_ms_addr = self.nodes[i].createmultisig(1, [self.pubkey[-1]], 'bech32')['address']
assert_equal(p2sh_ms_addr, script_to_p2sh_p2wsh(multiscript))
assert_equal(bip173_ms_addr, script_to_p2wsh(multiscript))

p2sh_ms_desc = descsum_create(f"sh(wsh(multi(1,{key.privkey})))")
bip173_ms_desc = descsum_create(f"wsh(multi(1,{key.privkey}))")
assert_equal(self.nodes[i].deriveaddresses(p2sh_ms_desc)[0], p2sh_ms_addr)
assert_equal(self.nodes[i].deriveaddresses(bip173_ms_desc)[0], bip173_ms_addr)

sh_wpkh_desc = descsum_create(f"sh(wpkh({key.privkey}))")
wpkh_desc = descsum_create(f"wpkh({key.privkey})")
assert_equal(self.nodes[i].deriveaddresses(sh_wpkh_desc)[0], key.p2sh_p2wpkh_addr)
assert_equal(self.nodes[i].deriveaddresses(wpkh_desc)[0], key.p2wpkh_addr)

if self.options.descriptors:
res = self.nodes[i].importdescriptors([
{"desc": p2sh_ms_desc, "timestamp": "now"},
{"desc": bip173_ms_desc, "timestamp": "now"},
{"desc": sh_wpkh_desc, "timestamp": "now"},
{"desc": wpkh_desc, "timestamp": "now"},
])
else:
# The nature of the legacy wallet is that this import results in also adding all of the necessary scripts
res = self.nodes[i].importmulti([
{"desc": p2sh_ms_desc, "timestamp": "now"},
])
assert all([r["success"] for r in res])

p2sh_ids.append([])
wit_ids.append([])
for _ in range(2):
Expand All @@ -156,15 +182,15 @@ def run_test(self):
for _ in range(5):
for n in range(3):
for v in range(2):
wit_ids[n][v].append(send_to_witness(v, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[n], False, Decimal("49.999")))
p2sh_ids[n][v].append(send_to_witness(v, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[n], True, Decimal("49.999")))
wit_ids[n][v].append(send_to_witness(v, self.nodes[0], find_spendable_utxo(self.nodes[0], 200), self.pubkey[n], False, Decimal("199.999")))
p2sh_ids[n][v].append(send_to_witness(v, self.nodes[0], find_spendable_utxo(self.nodes[0], 200), self.pubkey[n], True, Decimal("199.999")))

self.generate(self.nodes[0], 1) # block 163

# Make sure all nodes recognize the transactions as theirs
assert_equal(self.nodes[0].getbalance(), balance_presetup - 60 * 50 + 20 * Decimal("49.999") + 50)
assert_equal(self.nodes[1].getbalance(), 20 * Decimal("49.999"))
assert_equal(self.nodes[2].getbalance(), 20 * Decimal("49.999"))
assert_equal(self.nodes[0].getbalance(), balance_presetup - 60 * 200 + 20 * Decimal("199.999") + 200)
assert_equal(self.nodes[1].getbalance(), 20 * Decimal("199.999"))
assert_equal(self.nodes[2].getbalance(), 20 * Decimal("199.999"))

self.log.info("Verify unsigned p2sh witness txs without a redeem script are invalid")
self.fail_accept(self.nodes[2], "mandatory-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_2][P2WPKH][1], sign=False)
Expand All @@ -174,10 +200,10 @@ def run_test(self):

self.log.info("Verify witness txs are mined as soon as segwit activates")

send_to_witness(1, self.nodes[2], getutxo(wit_ids[NODE_2][P2WPKH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True)
send_to_witness(1, self.nodes[2], getutxo(wit_ids[NODE_2][P2WSH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True)
send_to_witness(1, self.nodes[2], getutxo(p2sh_ids[NODE_2][P2WPKH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True)
send_to_witness(1, self.nodes[2], getutxo(p2sh_ids[NODE_2][P2WSH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True)
send_to_witness(1, self.nodes[2], getutxo(wit_ids[NODE_2][P2WPKH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("199.998"), sign=True)
send_to_witness(1, self.nodes[2], getutxo(wit_ids[NODE_2][P2WSH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("199.998"), sign=True)
send_to_witness(1, self.nodes[2], getutxo(p2sh_ids[NODE_2][P2WPKH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("199.998"), sign=True)
send_to_witness(1, self.nodes[2], getutxo(p2sh_ids[NODE_2][P2WSH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("199.998"), sign=True)

assert_equal(len(self.nodes[2].getrawmempool()), 4)
blockhash = self.generate(self.nodes[2], 1)[0] # block 165 (first block with new rules)
Expand Down Expand Up @@ -219,8 +245,8 @@ def run_test(self):
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
raw_tx = self.nodes[0].getrawtransaction(txid, True)
tmpl = self.nodes[0].getblocktemplate({'rules': ['segwit']})
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_greater_than_or_equal(tmpl['sizelimit'], 399957) # actual maximum size is lower due to minimum mandatory non-witness data
assert_equal(tmpl['weightlimit'], 400000)
assert_equal(tmpl['sigoplimit'], 80000)
assert_equal(tmpl['transactions'][0]['txid'], txid)
expected_sigops = 9 if 'txinwitness' in raw_tx["vin"][0] else 8
Expand All @@ -234,7 +260,7 @@ def run_test(self):
# tx2 (segwit input, paying to a non-segwit output) ->
# tx3 (non-segwit input, paying to a non-segwit output).
# tx1 is allowed to appear in the block, but no others.
txid1 = send_to_witness(1, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.996"))
txid1 = send_to_witness(1, self.nodes[0], find_spendable_utxo(self.nodes[0], 200), self.pubkey[0], False, Decimal("199.996"))
assert txid1 in self.nodes[0].getrawmempool()

tx1_hex = self.nodes[0].gettransaction(txid1)['hex']
Expand All @@ -250,7 +276,7 @@ def run_test(self):
# Now create tx2, which will spend from txid1.
tx = CTransaction()
tx.vin.append(CTxIn(COutPoint(int(txid1, 16), 0), b''))
tx.vout.append(CTxOut(int(49.99 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
tx.vout.append(CTxOut(int(199.99 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
tx2_hex = self.nodes[0].signrawtransactionwithwallet(tx.serialize().hex())['hex']
txid2 = self.nodes[0].sendrawtransaction(tx2_hex)
tx = tx_from_hex(tx2_hex)
Expand Down Expand Up @@ -305,9 +331,9 @@ def run_test(self):

# Import a compressed key and an uncompressed key, generate some multisig addresses
self.nodes[0].importprivkey("92e6XLo5jVAVwrQKPNTs93oQco8f8sDNBcpv73Dsrs397fQtFQn")
uncompressed_spendable_address = ["mvozP4UwyGD2mGZU4D2eMvMLPB9WkMmMQu"]
uncompressed_spendable_address = ["EwnYZhXmJNXZntpoAuP72GPkvLJsyxUYJH"]
self.nodes[0].importprivkey("cNC8eQ5dg3mFAVePDX4ddmPYpPbw41r9bm2jd1nLJT77e6RrzTRR")
compressed_spendable_address = ["mmWQubrDomqpgSYekvsU7HWEVjLFHAakLe"]
compressed_spendable_address = ["EnUy6Eu38tAMi4oysdDvmdYf2tVcYavA2C"]
assert not self.nodes[0].getaddressinfo(uncompressed_spendable_address[0])['iscompressed']
assert self.nodes[0].getaddressinfo(compressed_spendable_address[0])['iscompressed']

Expand Down Expand Up @@ -472,9 +498,9 @@ def run_test(self):
# Repeat some tests. This time we don't add witness scripts with importaddress
# Import a compressed key and an uncompressed key, generate some multisig addresses
self.nodes[0].importprivkey("927pw6RW8ZekycnXqBQ2JS5nPyo1yRfGNN8oq74HeddWSpafDJH")
uncompressed_spendable_address = ["mguN2vNSCEUh6rJaXoAVwY3YZwZvEmf5xi"]
uncompressed_spendable_address = ["EhsvDZRFXLoE8UZueVWxbt5y76jHPyiucv"]
self.nodes[0].importprivkey("cMcrXaaUC48ZKpcyydfFo8PxHAjpsYLhdsp6nmtB3E2ER9UUHWnw")
compressed_spendable_address = ["n1UNmpmbVUJ9ytXYXiurmGPQ3TRrXqPWKL"]
compressed_spendable_address = ["F2SvxTpQpach1WnseRGKRcRpacbDmYjDZi"]

self.nodes[0].importpubkey(pubkeys[5])
compressed_solvable_address = [key_to_p2pkh(pubkeys[5])]
Expand Down

0 comments on commit 8bcafc7

Please sign in to comment.