Skip to content

Commit

Permalink
test: Add anchor mempool acceptance test
Browse files Browse the repository at this point in the history
  • Loading branch information
instagibbs committed Jul 22, 2024
1 parent 3989d38 commit 83b1313
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
51 changes: 51 additions & 0 deletions test/functional/mempool_accept.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
keys_to_multisig_script,
MIN_PADDING,
MIN_STANDARD_TX_NONWITNESS_SIZE,
PAY_TO_ANCHOR,
script_to_p2sh_script,
script_to_p2wsh_script,
)
Expand Down Expand Up @@ -389,6 +390,56 @@ def run_test(self):
maxfeerate=0,
)

self.log.info('OP_1 <0x4e73> is able to be created and spent')
true_tx = self.wallet.create_self_transfer(sequence=SEQUENCE_FINAL)['tx']
true_tx.vout[0].scriptPubKey = bytes(PAY_TO_ANCHOR)
true_tx.rehash()
self.generateblock(node, self.wallet.get_address(), [true_tx.serialize().hex()])

# First spend has non-empty witness, will be rejected to prevent third party wtxid malleability
true_tx_spend = CTransaction()
true_tx_spend.vin.append(CTxIn(COutPoint(true_tx.sha256, 0), b""))
true_tx_spend.vout.append(CTxOut(true_tx.vout[0].nValue - int(fee*COIN), script_to_p2wsh_script(CScript([OP_TRUE]))))
true_tx_spend.wit.vtxinwit.append(CTxInWitness())
true_tx_spend.wit.vtxinwit[0].scriptWitness.stack.append(b"f")
true_tx_spend.rehash()

self.check_mempool_result(
result_expected=[{'txid': true_tx_spend.rehash(), 'allowed': False, 'reject-reason': 'bad-witness-nonstandard'}],
rawtxs=[true_tx_spend.serialize().hex()],
maxfeerate=0,
)

# Clear witness stuffing
true_tx_spend.wit.vtxinwit[0].scriptWitness.stack = []
true_tx_spend.rehash()

self.check_mempool_result(
result_expected=[{'txid': true_tx_spend.rehash(), 'allowed': True, 'vsize': true_tx_spend.get_vsize(), 'fees': { 'base': Decimal('0.00000700')}}],
rawtxs=[true_tx_spend.serialize().hex()],
maxfeerate=0,
)

self.log.info('But cannot be spent if nested sh()')
nested_true_tx = self.wallet.create_self_transfer(sequence=SEQUENCE_FINAL)['tx']
nested_true_tx.vout[0].scriptPubKey = script_to_p2sh_script(PAY_TO_ANCHOR)
nested_true_tx.rehash()
self.generateblock(node, self.wallet.get_address(), [nested_true_tx.serialize().hex()])

nested_true_tx_spend = CTransaction()
nested_true_tx_spend.vin.append(CTxIn(COutPoint(nested_true_tx.sha256, 0), b""))
nested_true_tx_spend.vin[0].scriptSig = CScript([bytes(PAY_TO_ANCHOR)])
nested_true_tx_spend.vout.append(CTxOut(true_tx.vout[0].nValue - int(fee*COIN), script_to_p2wsh_script(CScript([OP_TRUE]))))
nested_true_tx_spend.rehash()

self.check_mempool_result(
result_expected=[{'txid': nested_true_tx_spend.rehash(), 'allowed': False, 'reject-reason': 'non-mandatory-script-verify-flag (Witness version reserved for soft-fork upgrades)'}],
rawtxs=[nested_true_tx_spend.serialize().hex()],
maxfeerate=0,
)
# but is consensus-legal
self.generateblock(node, self.wallet.get_address(), [nested_true_tx_spend.serialize().hex()])

self.log.info('Spending a confirmed bare multisig is okay')
address = self.wallet.get_address()
tx = tx_from_hex(raw_tx_reference)
Expand Down
3 changes: 3 additions & 0 deletions test/functional/test_framework/script_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from test_framework.script import (
CScript,
OP_0,
OP_1,
OP_15,
OP_16,
OP_CHECKMULTISIG,
Expand Down Expand Up @@ -42,6 +43,8 @@
DUMMY_MIN_OP_RETURN_SCRIPT = CScript([OP_RETURN] + ([OP_0] * (MIN_PADDING - 1)))
assert len(DUMMY_MIN_OP_RETURN_SCRIPT) == MIN_PADDING

PAY_TO_ANCHOR = CScript([OP_1, bytes.fromhex("4e73")])

def key_to_p2pk_script(key):
key = check_key(key)
return CScript([key, OP_CHECKSIG])
Expand Down

0 comments on commit 83b1313

Please sign in to comment.