Skip to content

Commit 1349e9e

Browse files
committed
test: Add anchor mempool acceptance test
1 parent 9d89209 commit 1349e9e

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

test/functional/mempool_accept.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
keys_to_multisig_script,
3838
MIN_PADDING,
3939
MIN_STANDARD_TX_NONWITNESS_SIZE,
40+
PAY_TO_ANCHOR,
4041
script_to_p2sh_script,
4142
script_to_p2wsh_script,
4243
)
@@ -389,6 +390,56 @@ def run_test(self):
389390
maxfeerate=0,
390391
)
391392

393+
self.log.info('OP_1 <0x4e73> is able to be created and spent')
394+
anchor_value = 10000
395+
create_anchor_tx = self.wallet.send_to(from_node=node, scriptPubKey=PAY_TO_ANCHOR, amount=anchor_value)
396+
self.generate(node, 1)
397+
398+
# First spend has non-empty witness, will be rejected to prevent third party wtxid malleability
399+
anchor_nonempty_wit_spend = CTransaction()
400+
anchor_nonempty_wit_spend.vin.append(CTxIn(COutPoint(int(create_anchor_tx["txid"], 16), create_anchor_tx["sent_vout"]), b""))
401+
anchor_nonempty_wit_spend.vout.append(CTxOut(anchor_value - int(fee*COIN), script_to_p2wsh_script(CScript([OP_TRUE]))))
402+
anchor_nonempty_wit_spend.wit.vtxinwit.append(CTxInWitness())
403+
anchor_nonempty_wit_spend.wit.vtxinwit[0].scriptWitness.stack.append(b"f")
404+
anchor_nonempty_wit_spend.rehash()
405+
406+
self.check_mempool_result(
407+
result_expected=[{'txid': anchor_nonempty_wit_spend.rehash(), 'allowed': False, 'reject-reason': 'bad-witness-nonstandard'}],
408+
rawtxs=[anchor_nonempty_wit_spend.serialize().hex()],
409+
maxfeerate=0,
410+
)
411+
412+
# Clear witness stuffing
413+
anchor_spend = anchor_nonempty_wit_spend
414+
anchor_spend.wit.vtxinwit[0].scriptWitness.stack = []
415+
anchor_spend.rehash()
416+
417+
self.check_mempool_result(
418+
result_expected=[{'txid': anchor_spend.rehash(), 'allowed': True, 'vsize': anchor_spend.get_vsize(), 'fees': { 'base': Decimal('0.00000700')}}],
419+
rawtxs=[anchor_spend.serialize().hex()],
420+
maxfeerate=0,
421+
)
422+
423+
self.log.info('But cannot be spent if nested sh()')
424+
nested_anchor_tx = self.wallet.create_self_transfer(sequence=SEQUENCE_FINAL)['tx']
425+
nested_anchor_tx.vout[0].scriptPubKey = script_to_p2sh_script(PAY_TO_ANCHOR)
426+
nested_anchor_tx.rehash()
427+
self.generateblock(node, self.wallet.get_address(), [nested_anchor_tx.serialize().hex()])
428+
429+
nested_anchor_spend = CTransaction()
430+
nested_anchor_spend.vin.append(CTxIn(COutPoint(nested_anchor_tx.sha256, 0), b""))
431+
nested_anchor_spend.vin[0].scriptSig = CScript([bytes(PAY_TO_ANCHOR)])
432+
nested_anchor_spend.vout.append(CTxOut(nested_anchor_tx.vout[0].nValue - int(fee*COIN), script_to_p2wsh_script(CScript([OP_TRUE]))))
433+
nested_anchor_spend.rehash()
434+
435+
self.check_mempool_result(
436+
result_expected=[{'txid': nested_anchor_spend.rehash(), 'allowed': False, 'reject-reason': 'non-mandatory-script-verify-flag (Witness version reserved for soft-fork upgrades)'}],
437+
rawtxs=[nested_anchor_spend.serialize().hex()],
438+
maxfeerate=0,
439+
)
440+
# but is consensus-legal
441+
self.generateblock(node, self.wallet.get_address(), [nested_anchor_spend.serialize().hex()])
442+
392443
self.log.info('Spending a confirmed bare multisig is okay')
393444
address = self.wallet.get_address()
394445
tx = tx_from_hex(raw_tx_reference)

test/functional/test_framework/script_util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from test_framework.script import (
99
CScript,
1010
OP_0,
11+
OP_1,
1112
OP_15,
1213
OP_16,
1314
OP_CHECKMULTISIG,
@@ -42,6 +43,8 @@
4243
DUMMY_MIN_OP_RETURN_SCRIPT = CScript([OP_RETURN] + ([OP_0] * (MIN_PADDING - 1)))
4344
assert len(DUMMY_MIN_OP_RETURN_SCRIPT) == MIN_PADDING
4445

46+
PAY_TO_ANCHOR = CScript([OP_1, bytes.fromhex("4e73")])
47+
4548
def key_to_p2pk_script(key):
4649
key = check_key(key)
4750
return CScript([key, OP_CHECKSIG])

0 commit comments

Comments
 (0)