Skip to content

Commit

Permalink
Merge pull request #445 from gandrewstone/dev_validateblocktemplate
Browse files Browse the repository at this point in the history
add validateblocktemplate RPC call
  • Loading branch information
gandrewstone committed Apr 24, 2017
2 parents e1fe522 + 9a54c60 commit b22d70e
Show file tree
Hide file tree
Showing 7 changed files with 460 additions and 8 deletions.
12 changes: 12 additions & 0 deletions doc/miner.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,15 @@ When you restart bitcoind, the memory pool starts empty. If a block is found qu
`bitcoin-cli addnode <new node's IP:port> onetry`
`bitcoin-cli pushtx <new node's IP:port>`
```

Validating unsolved blocks
--------------------------

Bitcoin Unlimited can be used to validate block templates received from other Bitcoin Unlimited releases or other bitcoin clients. This ensures that Bitcoin Unlimited will accept the block once it is mined, allowing miners to deploy multiple clients in their mining networks. Note that this API will return an error if the block is not built off of the chain tip seen by this client. So it is important that the client be fully synchronized with the client that creates the block template. You can do this by explicitly connecting them via "addnode".

The block validation RPC uses the same call syntax as the "submitblock" RPC, and returns a JSONRPCException if the block validation fails. See "qa/rpc-tests/validateblocktemplate.py" for detailed python examples.

```sh
`bitcoin-cli validateblocktemplate <hex encoded block>`
```

1 change: 1 addition & 0 deletions qa/pull-tester/rpc-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def option_passed(option_without_dashes):
#Tests
testScripts = [ RpcTest(t) for t in [
'bip68-112-113-p2p',
'validateblocktemplate',
'parallel',
'wallet',
'excessive',
Expand Down
3 changes: 1 addition & 2 deletions qa/rpc-tests/bip68-sequence.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env python3
# Copyright (c) 2014-2015 The Bitcoin Core developers
# Copyright (c) 2015-2017 The Bitcoin Unlimited developers
#!/usr/bin/env python3
# Copyright (c) 2014-2016 The Bitcoin Core developers
#
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand Down
15 changes: 11 additions & 4 deletions qa/rpc-tests/test_framework/blocktools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .script import CScript, OP_TRUE, OP_CHECKSIG

# Create a block (with regtest difficulty)
def create_block(hashprev, coinbase, nTime=None):
def create_block(hashprev, coinbase, nTime=None, txns=None):
block = CBlock()
if nTime is None:
import time
Expand All @@ -17,7 +17,10 @@ def create_block(hashprev, coinbase, nTime=None):
block.nTime = nTime
block.hashPrevBlock = hashprev
block.nBits = 0x207fffff # Will break after a difficulty adjustment...
block.vtx.append(coinbase)
if coinbase:
block.vtx.append(coinbase)
if txns:
block.vtx += txns
block.hashMerkleRoot = block.calc_merkle_root()
block.calc_sha256()
return block
Expand Down Expand Up @@ -57,11 +60,15 @@ def create_coinbase(height, pubkey = None):
return coinbase

# Create a transaction with an anyone-can-spend output, that spends the
# nth output of prevtx.
# nth output of prevtx. pass a single integer value to make one output,
# or a list to create multiple outputs
def create_transaction(prevtx, n, sig, value):
if not type(value) is list:
value = [value]
tx = CTransaction()
assert(n < len(prevtx.vout))
tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
tx.vout.append(CTxOut(value, b""))
for v in value:
tx.vout.append(CTxOut(v, b""))
tx.calc_sha256()
return tx
6 changes: 4 additions & 2 deletions qa/rpc-tests/test_framework/mininode.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def deserialize(self, f):

def serialize(self):
r = b""
r += struct.pack("<q", self.nValue)
r += struct.pack("<q", int(self.nValue))
r += ser_string(self.scriptPubKey)
return r

Expand Down Expand Up @@ -535,7 +535,9 @@ def calc_merkle_root(self):
i2 = min(i+1, len(hashes)-1)
newhashes.append(hash256(hashes[i] + hashes[i2]))
hashes = newhashes
return uint256_from_str(hashes[0])
if hashes:
return uint256_from_str(hashes[0])
return 0

def is_valid(self):
self.calc_sha256()
Expand Down
Loading

0 comments on commit b22d70e

Please sign in to comment.