Skip to content

Commit 5e6abe5

Browse files
sdaftuarsipa
authored andcommitted
Update p2p test framework with segwit support
mininode now supports witness transactions/blocks, blocktools has a helper for adding witness commitments to blocks, and script has a function to calculate hashes for signature under sigversion 1, used by segwit.
1 parent e5ffcc1 commit 5e6abe5

File tree

3 files changed

+261
-27
lines changed

3 files changed

+261
-27
lines changed

qa/rpc-tests/test_framework/blocktools.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66

77
from .mininode import *
8-
from .script import CScript, OP_TRUE, OP_CHECKSIG
8+
from .script import CScript, OP_TRUE, OP_CHECKSIG, OP_RETURN
99

1010
# Create a block (with regtest difficulty)
1111
def create_block(hashprev, coinbase, nTime=None):
@@ -22,6 +22,29 @@ def create_block(hashprev, coinbase, nTime=None):
2222
block.calc_sha256()
2323
return block
2424

25+
# From BIP141
26+
WITNESS_COMMITMENT_HEADER = "\xaa\x21\xa9\xed"
27+
28+
# According to BIP141, nVersion=5 blocks must commit to the
29+
# hash of all in-block transactions including witness.
30+
def add_witness_commitment(block, nonce=0L):
31+
# First calculate the merkle root of the block's
32+
# transactions, with witnesses.
33+
witness_nonce = nonce
34+
witness_root = block.calc_witness_merkle_root()
35+
witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root)+ser_uint256(witness_nonce)))
36+
# witness_nonce should go to coinbase witness.
37+
block.vtx[0].wit.vtxinwit = [CTxinWitness()]
38+
block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(witness_nonce)]
39+
40+
# 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])))
43+
block.vtx[0].rehash()
44+
block.hashMerkleRoot = block.calc_merkle_root()
45+
block.rehash()
46+
47+
2548
def serialize_script_num(value):
2649
r = bytearray(0)
2750
if value == 0:

0 commit comments

Comments
 (0)