-
Notifications
You must be signed in to change notification settings - Fork 37.7k
rpc: Add submitheader #13399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rpc: Add submitheader #13399
Conversation
Note to reviewers: This pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
Your OP is empty - can you provide rationale what this is to be used for? |
I asked for this as a part of my BetterHash mining protocol work, however I've also wanted this at several points in order to be able to implement chain-sync logic outside of bitcoind over RPC. You'd need this or something like it to do headers-first sync in such a system. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to include #13395?
src/validation.h
Outdated
@@ -234,7 +234,6 @@ static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024; | |||
* (and possibly also) BlockChecked will have been called. | |||
* | |||
* | |||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commit "rpc: Expose ProcessNewBlockHeaders"
This should be in commit "doc: Rewrite some validation doc to be machine-readable:".
src/validation.h
Outdated
@@ -245,8 +244,6 @@ bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<cons | |||
/** | |||
* Process incoming block headers. | |||
* | |||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commit "rpc: Expose ProcessNewBlockHeaders"
This should be in commit "doc: Rewrite some validation doc to be machine-readable:".
src/validation.h
Outdated
@@ -232,28 +232,25 @@ static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024; | |||
* | |||
* Note that we guarantee that either the proof-of-work is valid on pblock, or | |||
* (and possibly also) BlockChecked will have been called. | |||
* | |||
* Call without cs_main held. | |||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be removed?
9e2b936
to
2595028
Compare
self.log.info('submitheader tests') | ||
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='xx' * 80)) | ||
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='ff' * 78)) | ||
assert_raises_rpc_error(-25, 'Must submit previous header', lambda: node.submitheader(hexdata='ff' * 80)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, could include the previous hash.
src/rpc/mining.cpp
Outdated
} | ||
{ | ||
LOCK(cs_main); | ||
if (LookupBlockIndex(h.GetHash())) return NullUniValue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These verifications are done in ProcessNewBlockHeaders
-> CChainState::AcceptBlockHeader
. Could use ProcessNewBlockHeaders
return value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the docstring above: The result is always None
and not the return value of PNBH
} | ||
|
||
CValidationState state; | ||
ProcessNewBlockHeaders({h}, state, Params(), /* ppindex */ nullptr, /* first_invalid */ nullptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the lock is released above, there is a point where the block can be processed in between.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That race shouldn't affect the return value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. utACK 2595028
A few nits inline. Feel free to ignore.
src/validation.h
Outdated
@@ -232,28 +232,24 @@ static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024; | |||
* | |||
* Note that we guarantee that either the proof-of-work is valid on pblock, or | |||
* (and possibly also) BlockChecked will have been called. | |||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
supernit: you remove this text in commit doc: Rewrite some validation doc to be machine-readable, and then remove the lines in commit rpc: Expose ProcessNewBlockHeaders. Just remove the lines in the first commit.
Same for ProcessNewBlockHeaders
comment below.
test/functional/mining_basic.py
Outdated
bytes_to_hex_str, | ||
) | ||
|
||
b2x = bytes_to_hex_str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can just use:
from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
bytes_to_hex_str as b2x,
+)
test/functional/mining_basic.py
Outdated
bad_block2 = copy.deepcopy(block) | ||
bad_block2.hashPrevBlock = bad_block.sha256 | ||
bad_block2.solve() | ||
assert_raises_rpc_error(-25, 'bad-prevblk', lambda: node.submitheader(hexdata=b2x(CBlockHeader(bad_block2).serialize()))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normal way we do this is:
assert_raises_rpc_error(-25, 'bad-prevblk', node.submitheader, b2x(CBlockHeader(bad_block2).serialize()))
Any reason you've chosen to use a lambda?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to use named arguments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should work:
assert_raises_rpc_error(-25, 'bad-prevblk', node.submitheader, hexdata=b2x(CBlockHeader(bad_block2).serialize()))
src/rpc/mining.cpp
Outdated
} | ||
{ | ||
LOCK(cs_main); | ||
if (LookupBlockIndex(h.GetHash())) return NullUniValue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be slightly nicer to have a return value passed to the user, if only to differentiate between whether the header was already in the block index or if it newly added to the block index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jnewbery do you see a use case for that?
assert chain_tip(block.hash) in node.getchaintips() | ||
node.submitheader(hexdata=b2x(CBlockHeader(block).serialize())) # Noop | ||
assert chain_tip(block.hash) in node.getchaintips() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps test submitting a block header that isn't the most-work tip (ie a fork from some earlier block)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done at the end of this function
#13439 has been merged. Is this PR ready for rebase and rereview? |
2595028
to
03ac2b4
Compare
03ac2b4
to
3ea4750
Compare
dfc8cf6
to
fa7d7dd
Compare
utACK fa7d7dd34cbd180ec9587c640078dfb7bf2ead04. Having this functionality can't hurt, as it available with identical semantics via P2P anyway. I have no opinion about the test code style. |
bad_block_lock.vtx[0].rehash() | ||
bad_block_lock.hashMerkleRoot = bad_block_lock.calc_merkle_root() | ||
bad_block_lock.solve() | ||
assert_equal(node.submitblock(hexdata=b2x(bad_block_lock.serialize())), 'invalid') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does master have a bug? This should be returning "bad-txns-nonfinal", not "invalid"...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the TODO
Line 756 in 8803c91
// TODO Maybe pass down fNewBlock to AcceptBlockHeader, so it is properly set to true in this case? |
Please rename either the PR or the RPC method to match the other... |
utACK fa7d7dd. |
fa7d7dd
to
5ce7446
Compare
5ce7446
to
fa091b0
Compare
Rebased. Only conflict was in tests, no other changes. |
utACK fa091b0 |
fa091b0 qa: Add tests for submitheader (MarcoFalke) 36b1b63 rpc: Expose ProcessNewBlockHeaders (MarcoFalke) Pull request description: This exposes `ProcessNewBlockHeaders` as an rpc called `submitheader`. This can be used to check for invalid block headers and submission of valid block headers via the rpc. Tree-SHA512: a61e850470f15465f88e450609116df0a98d5d9afadf36b2033d820933d8b6a4012f9f2b3246319c08a0e511bef517f5d808cd0f44ffca91d10895a938004f0b
fa091b0 qa: Add tests for submitheader (MarcoFalke) 36b1b63 rpc: Expose ProcessNewBlockHeaders (MarcoFalke) Pull request description: This exposes `ProcessNewBlockHeaders` as an rpc called `submitheader`. This can be used to check for invalid block headers and submission of valid block headers via the rpc. Tree-SHA512: a61e850470f15465f88e450609116df0a98d5d9afadf36b2033d820933d8b6a4012f9f2b3246319c08a0e511bef517f5d808cd0f44ffca91d10895a938004f0b # Conflicts: # src/core_io.h # src/rpc/mining.cpp # test/functional/mining_basic.py
fa091b0 qa: Add tests for submitheader (MarcoFalke) 36b1b63 rpc: Expose ProcessNewBlockHeaders (MarcoFalke) Pull request description: This exposes `ProcessNewBlockHeaders` as an rpc called `submitheader`. This can be used to check for invalid block headers and submission of valid block headers via the rpc. Tree-SHA512: a61e850470f15465f88e450609116df0a98d5d9afadf36b2033d820933d8b6a4012f9f2b3246319c08a0e511bef517f5d808cd0f44ffca91d10895a938004f0b
fa091b0 qa: Add tests for submitheader (MarcoFalke) 36b1b63 rpc: Expose ProcessNewBlockHeaders (MarcoFalke) Pull request description: This exposes `ProcessNewBlockHeaders` as an rpc called `submitheader`. This can be used to check for invalid block headers and submission of valid block headers via the rpc. Tree-SHA512: a61e850470f15465f88e450609116df0a98d5d9afadf36b2033d820933d8b6a4012f9f2b3246319c08a0e511bef517f5d808cd0f44ffca91d10895a938004f0b
* Merge bitcoin#13399: rpc: Add submitheader fa091b0 qa: Add tests for submitheader (MarcoFalke) 36b1b63 rpc: Expose ProcessNewBlockHeaders (MarcoFalke) Pull request description: This exposes `ProcessNewBlockHeaders` as an rpc called `submitheader`. This can be used to check for invalid block headers and submission of valid block headers via the rpc. Tree-SHA512: a61e850470f15465f88e450609116df0a98d5d9afadf36b2033d820933d8b6a4012f9f2b3246319c08a0e511bef517f5d808cd0f44ffca91d10895a938004f0b * Update test/functional/mining_basic.py Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com> Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
* Merge bitcoin#13399: rpc: Add submitheader fa091b0 qa: Add tests for submitheader (MarcoFalke) 36b1b63 rpc: Expose ProcessNewBlockHeaders (MarcoFalke) Pull request description: This exposes `ProcessNewBlockHeaders` as an rpc called `submitheader`. This can be used to check for invalid block headers and submission of valid block headers via the rpc. Tree-SHA512: a61e850470f15465f88e450609116df0a98d5d9afadf36b2033d820933d8b6a4012f9f2b3246319c08a0e511bef517f5d808cd0f44ffca91d10895a938004f0b * Update test/functional/mining_basic.py Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com> Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
This exposes
ProcessNewBlockHeaders
as an rpc calledsubmitheader
. This can be used to check for invalid block headers and submission of valid block headers via the rpc.