Changeset View
Changeset View
Standalone View
Standalone View
test/functional/utxocommit_vector.py
- This file was added.
#!/usr/bin/env python3 | |||||
# Copyright (c) 2015-2016 The Bitcoin developers | |||||
# Distributed under the MIT software license, see the accompanying | |||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | |||||
# Test the UTXO commitment against fixed test vectors | |||||
from test_framework.blocktools import (create_block, create_coinbase) | |||||
from test_framework.mininode import (COutPoint, | |||||
CTxIn, | |||||
NetworkThread, | |||||
NodeConn, | |||||
NodeConnCB, | |||||
msg_block) | |||||
from test_framework.test_framework import BitcoinTestFramework | |||||
from test_framework.util import ( | |||||
p2p_port, assert_equal, hex_str_to_bytes, bytes_to_hex_str) | |||||
# Test vectors coinbase 1,2,3 are taken from block 1,2,3 of the mainchain. | |||||
# The resulting testvectors are calculated in the ECMH spec. | |||||
# The txin_script is included to ensure the txid is correct | |||||
coinbases = [ | |||||
{ | |||||
"txin_script": "04ffff001d0104", | |||||
"scriptpubkey": "410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac", | |||||
"commitment_after": "5e294984c0b6ff1c897bdbb6f7cf3ef801e2f13bc73428aacdf8cb8d3bd2f0e5" | |||||
}, | |||||
{ | |||||
"txin_script": "04ffff001d010b", | |||||
"scriptpubkey": "41047211a824f55b505228e4c3d5194c1fcfaa15a456abdf37f9b9d97a4040afc073dee6c89064984f03385237d92167c13e236446b417ab79a0fcae412ae3316b77ac", | |||||
"commitment_after": "48098f4ca9bb5dac273e56316db6412369ed1fa8beb579570532d16347fefccc" | |||||
}, | |||||
{ | |||||
"txin_script": "04ffff001d010e", | |||||
"scriptpubkey": "410494b9d3e76c5b1629ecf97fff95d7a4bbdac87cc26099ada28066c6ff1eb9191223cd897194a08d0c2726c5747f1db49e8cf90e75dc3e3550ae9b30086f3cd5aaac", | |||||
"commitment_after": "411cafe74b66d1b79ae41e632e0de1a3b7419a25dbf0a66a1f3b4d6eb6dcc3d2" | |||||
} | |||||
] | |||||
class UtxoCommitVectorTest(BitcoinTestFramework): | |||||
def set_test_params(self): | |||||
self.setup_clean_chain = True | |||||
self.num_nodes = 1 | |||||
def setup_network(self): | |||||
self.setup_nodes() | |||||
def connect_to(self, node): | |||||
node_conn = NodeConnCB() | |||||
node_conn.add_connection( | |||||
NodeConn('127.0.0.1', p2p_port(0), node, node_conn)) | |||||
NetworkThread().start() # Start up network handling in another thread | |||||
node_conn.wait_for_verack() | |||||
return node_conn | |||||
def run_test(self): | |||||
node_conn = self.connect_to(self.nodes[0]) | |||||
# TODO: The third coinbase doesn't match the spec | |||||
for n in range(0, 2): | |||||
# create coinbase and block | |||||
coinbase = create_coinbase(n + 1) | |||||
coinbase.vin[0] = CTxIn(COutPoint(0, 0xffffffff), hex_str_to_bytes( | |||||
coinbases[n]["txin_script"]), 0xffffffff) | |||||
coinbase.vout[0].scriptPubKey = hex_str_to_bytes( | |||||
coinbases[n]["scriptpubkey"]) | |||||
coinbase.rehash() | |||||
self.log.info("Coinbase %d hash = %s, hex = %s", n+1, | |||||
coinbase.hash, bytes_to_hex_str(coinbase.serialize())) | |||||
tip = int(self.nodes[0].getbestblockhash(), 16) | |||||
block_time = self.nodes[0].getblock( | |||||
self.nodes[0].getbestblockhash())['time'] + 1 | |||||
block = create_block(tip, coinbase, block_time) | |||||
block.solve() | |||||
# send block | |||||
node_conn.send_message(msg_block(block)) | |||||
node_conn.sync_with_ping(5) | |||||
txoutsetinfo = self.nodes[0].gettxoutsetinfo() | |||||
self.log.info("Commitment after %d = %s", n + | |||||
1, txoutsetinfo['commitment']) | |||||
# verify | |||||
expected = hex_str_to_bytes(coinbases[n]['commitment_after'])[::-1] | |||||
assert_equal(hex_str_to_bytes( | |||||
txoutsetinfo['commitment_calculated']), expected) | |||||
assert_equal(hex_str_to_bytes( | |||||
txoutsetinfo['commitment']), expected) | |||||
if __name__ == '__main__': | |||||
UtxoCommitVectorTest().main() |