Merge #10223: Tests: Refactor to create witness script creation function

c39a6b9 Tests: Refactor to create witness script creation function (Jimmy Song)

Tree-SHA512: 1dde621c811ea1a2719acb9a9b84825d3f520234da7fc4045da13754d4a6e6736de2fd508a2b6e64226ad95c7e634bf76d36bd0dcd1b37c63e7b1e172ee0816c
This commit is contained in:
Wladimir J. van der Laan 2017-04-21 17:27:54 +02:00
commit 5352e5e75d
No known key found for this signature in database
GPG key ID: 74810B012346C9A6
3 changed files with 14 additions and 13 deletions

View file

@ -8,7 +8,7 @@ from test_framework.mininode import *
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import * from test_framework.util import *
from test_framework.script import * from test_framework.script import *
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment, WITNESS_COMMITMENT_HEADER from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment, get_witness_script, WITNESS_COMMITMENT_HEADER
from test_framework.key import CECKey, CPubKey from test_framework.key import CECKey, CPubKey
import time import time
import random import random
@ -1721,15 +1721,10 @@ class SegWitTest(BitcoinTestFramework):
assert('default_witness_commitment' in gbt_results) assert('default_witness_commitment' in gbt_results)
witness_commitment = gbt_results['default_witness_commitment'] witness_commitment = gbt_results['default_witness_commitment']
# TODO: this duplicates some code from blocktools.py, would be nice
# to refactor.
# Check that default_witness_commitment is present. # Check that default_witness_commitment is present.
block = CBlock() witness_root = CBlock.get_merkle_root([ser_uint256(0),
witness_root = block.get_merkle_root([ser_uint256(0), ser_uint256(txid)]) ser_uint256(txid)])
check_commitment = uint256_from_str(hash256(ser_uint256(witness_root)+ser_uint256(0))) script = get_witness_script(witness_root, 0)
from test_framework.blocktools import WITNESS_COMMITMENT_HEADER
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(check_commitment)
script = CScript([OP_RETURN, output_data])
assert_equal(witness_commitment, bytes_to_hex_str(script)) assert_equal(witness_commitment, bytes_to_hex_str(script))
# undo mocktime # undo mocktime

View file

@ -25,6 +25,13 @@ def create_block(hashprev, coinbase, nTime=None):
# From BIP141 # From BIP141
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed" WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
def get_witness_script(witness_root, witness_nonce):
witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root)+ser_uint256(witness_nonce)))
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment)
return CScript([OP_RETURN, output_data])
# According to BIP141, blocks with witness rules active must commit to the # According to BIP141, blocks with witness rules active must commit to the
# hash of all in-block transactions including witness. # hash of all in-block transactions including witness.
def add_witness_commitment(block, nonce=0): def add_witness_commitment(block, nonce=0):
@ -32,14 +39,12 @@ def add_witness_commitment(block, nonce=0):
# transactions, with witnesses. # transactions, with witnesses.
witness_nonce = nonce witness_nonce = nonce
witness_root = block.calc_witness_merkle_root() witness_root = block.calc_witness_merkle_root()
witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root)+ser_uint256(witness_nonce)))
# witness_nonce should go to coinbase witness. # witness_nonce should go to coinbase witness.
block.vtx[0].wit.vtxinwit = [CTxInWitness()] block.vtx[0].wit.vtxinwit = [CTxInWitness()]
block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(witness_nonce)] block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(witness_nonce)]
# witness commitment is the last OP_RETURN output in coinbase # witness commitment is the last OP_RETURN output in coinbase
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment) block.vtx[0].vout.append(CTxOut(0, get_witness_script(witness_root, witness_nonce)))
block.vtx[0].vout.append(CTxOut(0, CScript([OP_RETURN, output_data])))
block.vtx[0].rehash() block.vtx[0].rehash()
block.hashMerkleRoot = block.calc_merkle_root() block.hashMerkleRoot = block.calc_merkle_root()
block.rehash() block.rehash()

View file

@ -610,7 +610,8 @@ class CBlock(CBlockHeader):
return r return r
# Calculate the merkle root given a vector of transaction hashes # Calculate the merkle root given a vector of transaction hashes
def get_merkle_root(self, hashes): @classmethod
def get_merkle_root(cls, hashes):
while len(hashes) > 1: while len(hashes) > 1:
newhashes = [] newhashes = []
for i in range(0, len(hashes), 2): for i in range(0, len(hashes), 2):