diff options
| author | Ross Nicoll <[email protected]> | 2017-08-13 13:31:12 +0100 |
|---|---|---|
| committer | Ross Nicoll <[email protected]> | 2018-09-19 19:22:45 +0100 |
| commit | bc8cca48968dfa3f60b5eae6a2b92bdd2870eee3 (patch) | |
| tree | c885ee6a4370350c227b741d8284302974af050f /qa/rpc-tests | |
| parent | Update DB version to 5.1 (diff) | |
| download | discoin-bc8cca48968dfa3f60b5eae6a2b92bdd2870eee3.tar.xz discoin-bc8cca48968dfa3f60b5eae6a2b92bdd2870eee3.zip | |
Merge AuxPoW support from Namecore
Changes are as below:
Wrap CBlockHeader::nVersion into a new class (CBlockVersion). This allows to take care of interpreting the field into a base version, auxpow flag and the chain ID.
Update getauxblock.py for new 'generate' RPC call.
Add 'auxpow' to block JSON.
Accept auxpow as PoW verification.
Add unit tests for auxpow verification.
Add check for memory-layout of CBlockVersion.
Weaken auxpow chain ID checks for the testnet.
Allow Params() to overrule when to check the auxpow chain ID and for legacy blocks. Use this to disable the checks on testnet.
Introduce CPureBlockHeader.
Split the block header part that is used by auxpow and the "real" block header (that uses auxpow) to resolve the cyclic dependency between the two.
Differentiate between uint256 and arith_uint256.
This change was done upstream, modify the auxpow code.
Add missing lock in auxpow_tests.
Fix REST header check for auxpow headers.
Those can be longer, thus take that into account. Also perform the check actually on an auxpow header.
Correctly set the coinbase for getauxblock results.
Call IncrementExtraNonce in getauxblock so that the coinbase is actually initialised with the stuff it should be. (BIP30 block height and COINBASE_FLAGS.)
Implement getauxblock plus regression test.
Turn auxpow test into FIXTURE test.
This allows using of the Params() calls.
Move CMerkleTx code to auxpow.cpp.
Otherwise we get linker errors when building without wallet.
Fix rebase with BIP66.
Update the code to handle BIP66's nVersion=3.
Enforce that auxpow parent blocks have no auxpow block version.
This is for compatibility with namecoind. See also https://github.com/namecoin/namecoin/pull/199.
Move auxpow-related parameters to Consensus::Params.
Diffstat (limited to 'qa/rpc-tests')
| -rwxr-xr-x | qa/rpc-tests/getauxblock.py | 123 | ||||
| -rwxr-xr-x | qa/rpc-tests/rest.py | 21 | ||||
| -rw-r--r-- | qa/rpc-tests/test_framework/auxpow.py | 111 |
3 files changed, 247 insertions, 8 deletions
diff --git a/qa/rpc-tests/getauxblock.py b/qa/rpc-tests/getauxblock.py new file mode 100755 index 000000000..dfb57be8d --- /dev/null +++ b/qa/rpc-tests/getauxblock.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python +# Copyright (c) 2014-2015 Daniel Kraft +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +# Test the "getauxblock" merge-mining RPC interface. + +from test_framework.test_framework import BitcoinTestFramework +from test_framework.util import * + +from test_framework import auxpow + +class GetAuxBlockTest (BitcoinTestFramework): + + def run_test (self): + BitcoinTestFramework.run_test (self) + + # Generate a block so that we are not "downloading blocks". + self.nodes[0].generate (1) + + # Compare basic data of getauxblock to getblocktemplate. + auxblock = self.nodes[0].getauxblock () + blocktemplate = self.nodes[0].getblocktemplate () + assert_equal (auxblock['coinbasevalue'], blocktemplate['coinbasevalue']) + assert_equal (auxblock['bits'], blocktemplate['bits']) + assert_equal (auxblock['height'], blocktemplate['height']) + assert_equal (auxblock['previousblockhash'], blocktemplate['previousblockhash']) + + # Compare target and take byte order into account. + target = auxblock['_target'] + reversedTarget = auxpow.reverseHex (target) + assert_equal (reversedTarget, blocktemplate['target']) + + # Verify data that can be found in another way. + assert_equal (auxblock['chainid'], 1) + assert_equal (auxblock['height'], self.nodes[0].getblockcount () + 1) + assert_equal (auxblock['previousblockhash'], self.nodes[0].getblockhash (auxblock['height'] - 1)) + + # Calling again should give the same block. + auxblock2 = self.nodes[0].getauxblock () + assert_equal (auxblock2, auxblock) + + # If we receive a new block, the old hash will be replaced. + self.sync_all () + self.nodes[1].generate (1) + self.sync_all () + auxblock2 = self.nodes[0].getauxblock () + assert auxblock['hash'] != auxblock2['hash'] + try: + self.nodes[0].getauxblock (auxblock['hash'], "x") + raise AssertionError ("invalid block hash accepted") + except JSONRPCException as exc: + assert_equal (exc.error['code'], -8) + + # Invalid format for auxpow. + try: + self.nodes[0].getauxblock (auxblock2['hash'], "x") + raise AssertionError ("malformed auxpow accepted") + except JSONRPCException as exc: + assert_equal (exc.error['code'], -1) + + # Invalidate the block again, send a transaction and query for the + # auxblock to solve that contains the transaction. + self.nodes[0].generate (1) + addr = self.nodes[1].getnewaddress () + txid = self.nodes[0].sendtoaddress (addr, 1) + self.sync_all () + assert_equal (self.nodes[1].getrawmempool (), [txid]) + auxblock = self.nodes[0].getauxblock () + blocktemplate = self.nodes[0].getblocktemplate () + target = blocktemplate['target'] + + # Compute invalid auxpow. + apow = auxpow.computeAuxpow (auxblock['hash'], target, False) + res = self.nodes[0].getauxblock (auxblock['hash'], apow) + assert not res + + # Compute and submit valid auxpow. + apow = auxpow.computeAuxpow (auxblock['hash'], target, True) + res = self.nodes[0].getauxblock (auxblock['hash'], apow) + assert res + + # Make sure that the block is indeed accepted. + self.sync_all () + assert_equal (self.nodes[1].getrawmempool (), []) + height = self.nodes[1].getblockcount () + assert_equal (height, auxblock['height']) + assert_equal (self.nodes[1].getblockhash (height), auxblock['hash']) + + # Call getblock and verify the auxpow field. + data = self.nodes[1].getblock (auxblock['hash']) + assert 'auxpow' in data + auxJson = data['auxpow'] + assert_equal (auxJson['index'], 0) + assert_equal (auxJson['parentblock'], apow[-160:]) + + # Check that previous blocks don't have 'auxpow' in their getblock JSON. + oldHash = self.nodes[1].getblockhash (100) + data = self.nodes[1].getblock (oldHash) + assert 'auxpow' not in data + + # Check that it paid correctly to the first node. + t = self.nodes[0].listtransactions ("", 1) + assert_equal (len (t), 1) + t = t[0] + assert_equal (t['category'], "immature") + assert_equal (t['blockhash'], auxblock['hash']) + assert t['generated'] + assert t['amount'] >= Decimal ("25") + assert_equal (t['confirmations'], 1) + + # Verify the coinbase script. Ensure that it includes the block height + # to make the coinbase tx unique. The expected block height is around + # 200, so that the serialisation of the CScriptNum ends in an extra 00. + # The vector has length 2, which makes up for 02XX00 as the serialised + # height. Check this. + blk = self.nodes[1].getblock (auxblock['hash']) + tx = self.nodes[1].getrawtransaction (blk['tx'][0], 1) + coinbase = tx['vin'][0]['coinbase'] + assert_equal ("02%02x00" % auxblock['height'], coinbase[0 : 6]) + +if __name__ == '__main__': + GetAuxBlockTest ().main () diff --git a/qa/rpc-tests/rest.py b/qa/rpc-tests/rest.py index b769cd71f..99a1c49b0 100755 --- a/qa/rpc-tests/rest.py +++ b/qa/rpc-tests/rest.py @@ -8,6 +8,7 @@ # +from test_framework import auxpow from test_framework.test_framework import BitcoinTestFramework from test_framework.util import * from struct import * @@ -203,8 +204,10 @@ class RESTTest (BitcoinTestFramework): response = http_post_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True) assert_equal(response.status, 200) #must be a 200 because we are within the limits - self.nodes[0].generate(1) #generate block to not affect upcoming tests + # Generate a block to not affect upcoming tests. + auxpow.mineAuxpowBlock(self.nodes[0]) #generate self.sync_all() + bb_hash = self.nodes[0].getbestblockhash() ################ # /rest/block/ # @@ -219,24 +222,26 @@ class RESTTest (BitcoinTestFramework): # compare with block header response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"bin", True) assert_equal(response_header.status, 200) - assert_equal(int(response_header.getheader('content-length')), 80) + headerLen = int(response_header.getheader('content-length')) + assert_greater_than(headerLen, 80) response_header_str = response_header.read() - assert_equal(response_str[0:80], response_header_str) + assert_equal(response_str[0:headerLen], response_header_str) # check block hex format response_hex = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True) assert_equal(response_hex.status, 200) assert_greater_than(int(response_hex.getheader('content-length')), 160) - response_hex_str = response_hex.read() - assert_equal(encode(response_str, "hex_codec")[0:160], response_hex_str[0:160]) + response_hex_str = response_hex.read().strip() + assert_equal(encode(response_str, "hex_codec"), response_hex_str) # compare with hex block header response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True) assert_equal(response_header_hex.status, 200) assert_greater_than(int(response_header_hex.getheader('content-length')), 160) - response_header_hex_str = response_header_hex.read() - assert_equal(response_hex_str[0:160], response_header_hex_str[0:160]) - assert_equal(encode(response_header_str, "hex_codec")[0:160], response_header_hex_str[0:160]) + response_header_hex_str = response_header_hex.read().strip() + headerLen = len (response_header_hex_str) + assert_equal(response_hex_str[0:headerLen], response_header_hex_str) + assert_equal(encode(response_header_str, "hex_codec"), response_header_hex_str) # check json format block_json_string = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+'json') diff --git a/qa/rpc-tests/test_framework/auxpow.py b/qa/rpc-tests/test_framework/auxpow.py new file mode 100644 index 000000000..7027a712b --- /dev/null +++ b/qa/rpc-tests/test_framework/auxpow.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python +# Copyright (c) 2014 Daniel Kraft +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +# General code for auxpow testing. This includes routines to +# solve an auxpow and to generate auxpow blocks. + +import binascii +import hashlib + +def computeAuxpow (block, target, ok): + """ + Build an auxpow object (serialised as hex string) that solves + (ok = True) or doesn't solve (ok = False) the block. + """ + + # Start by building the merge-mining coinbase. The merkle tree + # consists only of the block hash as root. + coinbase = "fabe" + binascii.hexlify ("m" * 2) + coinbase += block + coinbase += "01000000" + ("00" * 4) + + # Construct "vector" of transaction inputs. + vin = "01" + vin += ("00" * 32) + ("ff" * 4) + vin += ("%02x" % (len (coinbase) / 2)) + coinbase + vin += ("ff" * 4) + + # Build up the full coinbase transaction. It consists only + # of the input and has no outputs. + tx = "01000000" + vin + "00" + ("00" * 4) + txHash = doubleHashHex (tx) + + # Construct the parent block header. It need not be valid, just good + # enough for auxpow purposes. + header = "01000000" + header += "00" * 32 + header += reverseHex (txHash) + header += "00" * 4 + header += "00" * 4 + header += "00" * 4 + + # Mine the block. + (header, blockhash) = mineBlock (header, target, ok) + + # Build the MerkleTx part of the auxpow. + auxpow = tx + auxpow += blockhash + auxpow += "00" + auxpow += "00" * 4 + + # Extend to full auxpow. + auxpow += "00" + auxpow += "00" * 4 + auxpow += header + + return auxpow + +def mineAuxpowBlock (node): + """ + Mine an auxpow block on the given RPC connection. + """ + + auxblock = node.getauxblock () + target = reverseHex (auxblock['_target']) + apow = computeAuxpow (auxblock['hash'], target, True) + res = node.getauxblock (auxblock['hash'], apow) + assert res + +def mineBlock (header, target, ok): + """ + Given a block header, update the nonce until it is ok (or not) + for the given target. + """ + + data = bytearray (binascii.unhexlify (header)) + while True: + assert data[79] < 255 + data[79] += 1 + hexData = binascii.hexlify (data) + + blockhash = doubleHashHex (hexData) + if (ok and blockhash < target) or ((not ok) and blockhash > target): + break + + return (hexData, blockhash) + +def doubleHashHex (data): + """ + Perform Bitcoin's Double-SHA256 hash on the given hex string. + """ + + hasher = hashlib.sha256 () + hasher.update (binascii.unhexlify (data)) + data = hasher.digest () + + hasher = hashlib.sha256 () + hasher.update (data) + + return reverseHex (hasher.hexdigest ()) + +def reverseHex (data): + """ + Flip byte order in the given data (hex string). + """ + + b = bytearray (binascii.unhexlify (data)) + b.reverse () + + return binascii.hexlify (b) |