2424from test_framework .script import (
2525 CScript ,
2626 CScriptNum ,
27+ OP_RETURN ,
2728)
2829from test_framework .test_framework import BitcoinTestFramework
2930from test_framework .util import (
@@ -96,8 +97,8 @@ async def build_coinbase_test(self, template, ctx, miniwallet):
9697 coinbase_tx .vout = [CTxOut ()]
9798 coinbase_tx .vout [0 ].scriptPubKey = miniwallet .get_output_script ()
9899 coinbase_tx .vout [0 ].nValue = coinbase_res .blockRewardRemaining
99- # Add SegWit OP_RETURN. This is currently always present even for
100- # empty blocks, but this may change .
100+ # Append any required coinbase outputs, such as the witness
101+ # commitment and deployment signalling OP_RETURNs .
101102 for output_data in coinbase_res .requiredOutputs :
102103 output = CTxOut ()
103104 output .deserialize (BytesIO (output_data ))
@@ -396,6 +397,39 @@ async def async_routine():
396397
397398 asyncio .run (capnp .run (async_routine ()))
398399
400+ def run_opreturn_required_outputs_test (self ):
401+ self .log .info ("Running OP_RETURN required_outputs test" )
402+ self .restart_node (0 , extra_args = [
403+ '-vbparams=testdummy:999999999999:999999999999' ,
404+ '-vbparams=testdummy2:0:999999999999' ,
405+ ])
406+
407+ async def async_routine ():
408+ ctx , mining = await self .make_mining_ctx ()
409+
410+ async with AsyncExitStack () as stack :
411+ template = await mining_create_block_template (mining , stack , ctx , self .default_block_create_options )
412+ assert template is not None
413+
414+ coinbase_res = await mining_get_coinbase_tx (template , ctx )
415+ assert_equal (len (coinbase_res .requiredOutputs ), 2 )
416+
417+ signal_output = CTxOut ()
418+ signal_output .deserialize (BytesIO (coinbase_res .requiredOutputs [0 ]))
419+ assert_equal (signal_output .nValue , 0 )
420+ assert_equal (signal_output .scriptPubKey .hex (), CScript ([OP_RETURN , b"BIP-9999" ]).hex ())
421+
422+ witness_output = CTxOut ()
423+ witness_output .deserialize (BytesIO (coinbase_res .requiredOutputs [1 ]))
424+ assert_equal (witness_output .nValue , 0 )
425+ # getblocktemplate only exposes the witness commitment today.
426+ assert_equal (
427+ witness_output .scriptPubKey .hex (),
428+ self .nodes [0 ].getblocktemplate ({"rules" : ["segwit" ]})["default_witness_commitment" ],
429+ )
430+
431+ asyncio .run (capnp .run (async_routine ()))
432+
399433 def run_test (self ):
400434 self .miniwallet = MiniWallet (self .nodes [0 ])
401435 self .default_block_create_options = self .capnp_modules ['mining' ].BlockCreateOptions ()
@@ -404,6 +438,7 @@ def run_test(self):
404438 self .run_block_template_test ()
405439 self .run_coinbase_and_submission_test ()
406440 self .run_ipc_option_override_test ()
441+ self .run_opreturn_required_outputs_test ()
407442
408443
409444if __name__ == '__main__' :
0 commit comments