Skip to content

Commit

Permalink
Signed blocks by default for custom chains
Browse files Browse the repository at this point in the history
  • Loading branch information
instagibbs committed Dec 19, 2018
1 parent 06e5551 commit 6be1277
Show file tree
Hide file tree
Showing 16 changed files with 223 additions and 288 deletions.
2 changes: 1 addition & 1 deletion src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ class CCustomParams : public CRegTestParams {
consensus.signblockscript = CScript(sign_bytes.begin(), sign_bytes.end());
// Default signature size is the size of dummy push, and single 72 byte DER signature
consensus.max_block_signature_size = gArgs.GetArg("-con_max_block_sig_size", 74);
g_signed_blocks = gArgs.GetBoolArg("-con_signed_blocks", false);
g_signed_blocks = gArgs.GetBoolArg("-con_signed_blocks", true);

// Custom chains connect coinbase outputs to db by default
consensus.connect_genesis_outputs = gArgs.GetArg("-con_connect_coinbase", true);
Expand Down
1 change: 1 addition & 0 deletions test/bitcoin_functional/functional/test_framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ def initialize_datadir(dirname, n, chain):
f.write("con_connect_coinbase=0\n")
f.write("anyonecanspendaremine=0\n")
f.write("con_blockheightinheader=0\n")
f.write("con_signed_blocks=0\n")
f.write("walletrbf=0\n") # Default is 1 in Elements
f.write("con_bip34height=100000000\n")
f.write("con_bip65height=1351\n")
Expand Down
228 changes: 114 additions & 114 deletions test/functional/data/rpc_getblockstats.json

Large diffs are not rendered by default.

34 changes: 16 additions & 18 deletions test/functional/feature_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
CTxIn,
CTxOut,
MAX_BLOCK_BASE_SIZE,
uint256_from_compact,
uint256_from_str,
)
from test_framework.mininode import P2PDataStore
Expand Down Expand Up @@ -533,7 +532,6 @@ def run_test(self):
b44 = CBlock()
b44.nTime = self.tip.nTime + 1
b44.hashPrevBlock = self.tip.sha256
b44.nBits = 0x207fffff
b44.vtx.append(coinbase)
b44.block_height = height
b44.hashMerkleRoot = b44.calc_merkle_root()
Expand All @@ -548,7 +546,6 @@ def run_test(self):
b45 = CBlock()
b45.nTime = self.tip.nTime + 1
b45.hashPrevBlock = self.tip.sha256
b45.nBits = 0x207fffff
b45.vtx.append(non_coinbase)
b45.block_height = height+1
b45.hashMerkleRoot = b45.calc_merkle_root()
Expand All @@ -564,7 +561,6 @@ def run_test(self):
b46 = CBlock()
b46.nTime = b44.nTime + 1
b46.hashPrevBlock = b44.sha256
b46.nBits = 0x207fffff
b46.vtx = []
b46.block_height = height+1
b46.hashMerkleRoot = 0
Expand All @@ -575,14 +571,15 @@ def run_test(self):
self.blocks[46] = b46
self.sync_blocks([b46], False, 16, b'bad-blk-length', reconnect=True)

self.log.info("Reject a block with invalid work")
self.move_tip(44)
b47 = self.next_block(47, solve=False)
target = uint256_from_compact(b47.nBits)
while b47.sha256 < target:
b47.nNonce += 1
b47.rehash()
self.sync_blocks([b47], False, request_block=False)
# No testing PoW
# self.log.info("Reject a block with invalid work")
# self.move_tip(44)
# b47 = self.next_block(47, solve=False)
# target = uint256_from_compact(b47.nBits)
# while b47.sha256 < target:
# b47.nNonce += 1
# b47.rehash()
# self.sync_blocks([b47], False, request_block=False)

self.log.info("Reject a block with a timestamp >2 hours in the future")
self.move_tip(44)
Expand All @@ -598,12 +595,13 @@ def run_test(self):
b49.solve()
self.sync_blocks([b49], False, 16, b'bad-txnmrklroot', reconnect=True)

self.log.info("Reject a block with incorrect POW limit")
self.move_tip(44)
b50 = self.next_block(50)
b50.nBits = b50.nBits - 1
b50.solve()
self.sync_blocks([b50], False, request_block=False, reconnect=True)
# No testing PoW
# self.log.info("Reject a block with incorrect POW limit")
# self.move_tip(44)
# b50 = self.next_block(50)
# b50.nBits = b50.nBits - 1
# b50.solve()
# self.sync_blocks([b50], False, request_block=False, reconnect=True)

self.log.info("Reject a block with two coinbase transactions")
self.move_tip(44)
Expand Down
4 changes: 2 additions & 2 deletions test/functional/feature_block_subsidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from decimal import Decimal

from test_framework.blocktools import create_coinbase
from test_framework.messages import CBlock
from test_framework.messages import CBlock, CProof
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal

Expand All @@ -34,7 +34,6 @@ def run_test(self):
# Block will have 10 satoshi output, node 1 will ban
addr = self.nodes[0].getnewaddress()
sub_block = self.nodes[0].generatetoaddress(1, addr)

raw_coinbase = self.nodes[0].getrawtransaction(self.nodes[0].getblock(sub_block[0])["tx"][0])
decoded_coinbase = self.nodes[0].decoderawtransaction(raw_coinbase)

Expand Down Expand Up @@ -69,6 +68,7 @@ def run_test(self):
block.nBits = int(tmpl["bits"], 16)
block.nNonce = 0
block.block_height = int(tmpl["height"])
block.proof = CProof(bytearray.fromhex('51'))
block.vtx = [coinbase_tx]

assert_template(self.nodes[0], block, "bad-cb-amount")
Expand Down
4 changes: 2 additions & 2 deletions test/functional/feature_mandatory_coinbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from binascii import b2a_hex

from test_framework.blocktools import create_coinbase
from test_framework.messages import CBlock
from test_framework.messages import CBlock, CProof
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error

Expand Down Expand Up @@ -39,7 +39,6 @@ def run_test(self):
node0.importprivkey(mandatory_privkey)

self.log.info("generatetoaddress: Making blocks of various kinds, checking for rejection")

# Create valid blocks to get out of IBD and get some funds (subsidy goes to permitted addr)
node0.generatetoaddress(101, mandatory_address)

Expand All @@ -62,6 +61,7 @@ def run_test(self):
block.nTime = tmpl["curtime"]
block.nBits = int(tmpl["bits"], 16)
block.nNonce = 0
block.proof = CProof(bytearray.fromhex('51'))
block.vtx = [coinbase_tx]
block.block_height = int(tmpl["height"])

Expand Down
89 changes: 0 additions & 89 deletions test/functional/feature_minchainwork.py

This file was deleted.

1 change: 0 additions & 1 deletion test/functional/interface_bitcoin_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def run_test(self):
assert_equal(cli_get_info['timeoffset'], network_info['timeoffset'])
assert_equal(cli_get_info['connections'], network_info['connections'])
assert_equal(cli_get_info['proxy'], network_info['networks'][0]['proxy'])
assert_equal(cli_get_info['difficulty'], blockchain_info['difficulty'])
assert_equal(cli_get_info['testnet'], blockchain_info['chain'] == "test")
assert_equal(cli_get_info['balance'], wallet_info['balance'])
assert_equal(cli_get_info['keypoololdest'], wallet_info['keypoololdest'])
Expand Down
17 changes: 10 additions & 7 deletions test/functional/interface_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,28 +198,31 @@ def run_test(self):
self.log.info("Test the /block and /headers URIs")
bb_hash = self.nodes[0].getbestblockhash()

# Elements note: We stripped nBits and nNonce from headers when in signed block mode
# Each header has a OP_TRUE challenge script

# Check binary format
response = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ)
assert_greater_than(int(response.getheader('content-length')), 84)
response_bytes = response.read()

# Compare with block header
response_header = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ)
assert_equal(int(response_header.getheader('content-length')), 84)
assert_equal(int(response_header.getheader('content-length')), 79)
response_header_bytes = response_header.read()
assert_equal(response_bytes[:84], response_header_bytes)
assert_equal(response_bytes[:79], response_header_bytes)

# Check block hex format
response_hex = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
assert_greater_than(int(response_hex.getheader('content-length')), 160)
assert_greater_than(int(response_hex.getheader('content-length')), 151)
response_hex_bytes = response_hex.read().strip(b'\n')
assert_equal(binascii.hexlify(response_bytes), response_hex_bytes)

# Compare with hex block header
response_header_hex = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
assert_greater_than(int(response_header_hex.getheader('content-length')), 160)
response_header_hex_bytes = response_header_hex.read(168)
assert_equal(binascii.hexlify(response_bytes[:84]), response_header_hex_bytes)
assert_greater_than(int(response_header_hex.getheader('content-length')), 75*2)
response_header_hex_bytes = response_header_hex.read(79*2)
assert_equal(binascii.hexlify(response_bytes[:79]), response_header_hex_bytes)

# Check json format
block_json_obj = self.test_rest_request("/block/{}".format(bb_hash))
Expand All @@ -232,7 +235,7 @@ def run_test(self):

# Compare with normal RPC block response
rpc_block_json = self.nodes[0].getblock(bb_hash)
for key in ['hash', 'confirmations', 'height', 'version', 'merkleroot', 'time', 'nonce', 'bits', 'difficulty', 'chainwork', 'previousblockhash']:
for key in ['hash', 'confirmations', 'height', 'version', 'merkleroot', 'time', 'previousblockhash']:
assert_equal(json_obj[0][key], rpc_block_json[key])

# See if we can get 5 headers in one response
Expand Down
3 changes: 2 additions & 1 deletion test/functional/interface_zmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def _zmq_test(self):

# Should receive the generated raw block.
block = self.rawblock.receive()
assert_equal(genhashes[x], bytes_to_hex_str(hash256(block[:84])))
# 79 bytes, last byte is saying block solution is "", ellide this for hash
assert_equal(genhashes[x], bytes_to_hex_str(hash256(block[:78])))

self.log.info("Wait for tx from second node")
payment_txid = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1.0)
Expand Down
18 changes: 18 additions & 0 deletions test/functional/mempool_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from test_framework.messages import COIN
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error, satoshi_round, sync_blocks, sync_mempools
import time

MAX_ANCESTORS = 25
MAX_DESCENDANTS = 25
Expand All @@ -34,6 +35,23 @@ def chain_transaction(self, node, parent_txid, vout, value, fee, num_outputs):
return (txid, send_value)

def run_test(self):

# Create transaction with 3-second block delay, should fail to enter the template
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
block = self.nodes[0].getnewblockhex(required_age=3)
self.nodes[0].submitblock(block)
assert(txid in self.nodes[0].getrawmempool())
time.sleep(3)
block = self.nodes[0].getnewblockhex(required_age=3)
self.nodes[0].submitblock(block)
assert(txid not in self.nodes[0].getrawmempool())
# Once more with no delay (default is 0, just testing default arg)
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
block = self.nodes[0].getnewblockhex(required_age=0)
self.nodes[0].submitblock(block)
assert(txid not in self.nodes[0].getrawmempool())
assert_raises_rpc_error(-8, "required_wait must be non-negative.", self.nodes[0].getnewblockhex, -1)

''' Mine some blocks and have them mature. '''
self.nodes[0].generate(101)
utxo = self.nodes[0].listunspent(10)
Expand Down
14 changes: 6 additions & 8 deletions test/functional/mining_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import copy
from binascii import b2a_hex
from decimal import Decimal

from test_framework.blocktools import create_coinbase
from test_framework.messages import CBlock
Expand Down Expand Up @@ -40,8 +39,6 @@ def run_test(self):
assert_equal(mining_info['chain'], self.chain)
assert_equal(mining_info['currentblocktx'], 0)
assert_equal(mining_info['currentblockweight'], 0)
assert_equal(mining_info['difficulty'], Decimal('4.656542373906925E-10'))
assert_equal(mining_info['networkhashps'], Decimal('0.003333333333333334'))
assert_equal(mining_info['pooledtx'], 0)

# Mine a block to leave initial block download
Expand All @@ -61,7 +58,7 @@ def run_test(self):
block.hashPrevBlock = int(tmpl["previousblockhash"], 16)
block.nTime = tmpl["curtime"]
block.block_height = node.getblockcount()+1
block.nBits = int(tmpl["bits"], 16)
block.nBits = 0
block.nNonce = 0
block.vtx = [coinbase_tx]

Expand Down Expand Up @@ -110,10 +107,11 @@ def run_test(self):
bad_block_sn[TX_COUNT_OFFSET] += 1
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(bad_block_sn), 'mode': 'proposal'})

self.log.info("getblocktemplate: Test bad bits")
bad_block = copy.deepcopy(block)
bad_block.nBits = 469762303 # impossible in the real world
assert_template(node, bad_block, 'bad-diffbits')
# No PoW test
# self.log.info("getblocktemplate: Test bad bits")
# bad_block = copy.deepcopy(block)
# bad_block.nBits = 469762303 # impossible in the real world
# assert_template(node, bad_block, 'bad-diffbits')

self.log.info("getblocktemplate: Test bad merkle root")
bad_block = copy.deepcopy(block)
Expand Down
Loading

0 comments on commit 6be1277

Please sign in to comment.