Skip to content

Commit 5352e5e

Browse files
committed
Merge #10223: Tests: Refactor to create witness script creation function
c39a6b9 Tests: Refactor to create witness script creation function (Jimmy Song) Tree-SHA512: 1dde621c811ea1a2719acb9a9b84825d3f520234da7fc4045da13754d4a6e6736de2fd508a2b6e64226ad95c7e634bf76d36bd0dcd1b37c63e7b1e172ee0816c
2 parents f3db4c6 + c39a6b9 commit 5352e5e

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

test/functional/p2p-segwit.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from test_framework.test_framework import BitcoinTestFramework
99
from test_framework.util import *
1010
from test_framework.script import *
11-
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment, WITNESS_COMMITMENT_HEADER
11+
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment, get_witness_script, WITNESS_COMMITMENT_HEADER
1212
from test_framework.key import CECKey, CPubKey
1313
import time
1414
import random
@@ -1721,15 +1721,10 @@ def test_getblocktemplate_before_lockin(self):
17211721
assert('default_witness_commitment' in gbt_results)
17221722
witness_commitment = gbt_results['default_witness_commitment']
17231723

1724-
# TODO: this duplicates some code from blocktools.py, would be nice
1725-
# to refactor.
17261724
# Check that default_witness_commitment is present.
1727-
block = CBlock()
1728-
witness_root = block.get_merkle_root([ser_uint256(0), ser_uint256(txid)])
1729-
check_commitment = uint256_from_str(hash256(ser_uint256(witness_root)+ser_uint256(0)))
1730-
from test_framework.blocktools import WITNESS_COMMITMENT_HEADER
1731-
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(check_commitment)
1732-
script = CScript([OP_RETURN, output_data])
1725+
witness_root = CBlock.get_merkle_root([ser_uint256(0),
1726+
ser_uint256(txid)])
1727+
script = get_witness_script(witness_root, 0)
17331728
assert_equal(witness_commitment, bytes_to_hex_str(script))
17341729

17351730
# undo mocktime

test/functional/test_framework/blocktools.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,26 @@ def create_block(hashprev, coinbase, nTime=None):
2525
# From BIP141
2626
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
2727

28+
29+
def get_witness_script(witness_root, witness_nonce):
30+
witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root)+ser_uint256(witness_nonce)))
31+
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment)
32+
return CScript([OP_RETURN, output_data])
33+
34+
2835
# According to BIP141, blocks with witness rules active must commit to the
2936
# hash of all in-block transactions including witness.
3037
def add_witness_commitment(block, nonce=0):
3138
# First calculate the merkle root of the block's
3239
# transactions, with witnesses.
3340
witness_nonce = nonce
3441
witness_root = block.calc_witness_merkle_root()
35-
witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root)+ser_uint256(witness_nonce)))
3642
# witness_nonce should go to coinbase witness.
3743
block.vtx[0].wit.vtxinwit = [CTxInWitness()]
3844
block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(witness_nonce)]
3945

4046
# witness commitment is the last OP_RETURN output in coinbase
41-
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment)
42-
block.vtx[0].vout.append(CTxOut(0, CScript([OP_RETURN, output_data])))
47+
block.vtx[0].vout.append(CTxOut(0, get_witness_script(witness_root, witness_nonce)))
4348
block.vtx[0].rehash()
4449
block.hashMerkleRoot = block.calc_merkle_root()
4550
block.rehash()

test/functional/test_framework/mininode.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ def serialize(self, with_witness=False):
610610
return r
611611

612612
# Calculate the merkle root given a vector of transaction hashes
613-
def get_merkle_root(self, hashes):
613+
@classmethod
614+
def get_merkle_root(cls, hashes):
614615
while len(hashes) > 1:
615616
newhashes = []
616617
for i in range(0, len(hashes), 2):

0 commit comments

Comments
 (0)