Skip to content
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

Merged
merged 2 commits into from Aug 15, 2018
Merged

rpc: Add submitheader #13399

merged 2 commits into from Aug 15, 2018

Conversation

@MarcoFalke
Copy link
Member

@MarcoFalke MarcoFalke commented Jun 5, 2018

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.

@DrahtBot
Copy link
Contributor

@DrahtBot DrahtBot commented Jun 5, 2018

Note to reviewers: This pull request conflicts with the following ones:
  • #13945 (Refactoring CRPCCommand with enum category by isghe)
  • #13815 (util: Add [[nodiscard]] to all {Decode,Parse}... functions returning bool by practicalswift)

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.

@laanwj
Copy link
Member

@laanwj laanwj commented Jun 5, 2018

Your OP is empty - can you provide rationale what this is to be used for?

@TheBlueMatt
Copy link
Contributor

@TheBlueMatt TheBlueMatt commented Jun 5, 2018

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.

Copy link
Member

@promag promag left a comment

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.
*
*
*
Copy link
Member

@promag promag Jun 5, 2018

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.
*
*
Copy link
Member

@promag promag Jun 5, 2018

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.
*
Copy link
Member

@promag promag Jun 5, 2018

Could be removed?

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))
Copy link
Member

@promag promag Jun 11, 2018

nit, could include the previous hash.

}
{
LOCK(cs_main);
if (LookupBlockIndex(h.GetHash())) return NullUniValue;
Copy link
Member

@promag promag Jun 11, 2018

These verifications are done in ProcessNewBlockHeaders -> CChainState::AcceptBlockHeader. Could use ProcessNewBlockHeaders return value.

Copy link
Member Author

@MarcoFalke MarcoFalke Jul 11, 2018

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);
Copy link
Member

@promag promag Jun 11, 2018

Since the lock is released above, there is a point where the block can be processed in between.

Copy link
Member Author

@MarcoFalke MarcoFalke Jul 11, 2018

That race shouldn't affect the return value.

Copy link
Member

@jnewbery jnewbery left a comment

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.
*
Copy link
Member

@jnewbery jnewbery Jun 11, 2018

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.

bytes_to_hex_str,
)

b2x = bytes_to_hex_str
Copy link
Member

@jnewbery jnewbery Jun 11, 2018

Can just use:

from test_framework.util import (
    assert_equal,
    assert_raises_rpc_error,
    bytes_to_hex_str as b2x,
+)

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())))
Copy link
Member

@jnewbery jnewbery Jun 11, 2018

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?

Copy link
Member Author

@MarcoFalke MarcoFalke Jun 11, 2018

I prefer to use named arguments

Copy link
Member

@promag promag Jun 11, 2018

This should work:

assert_raises_rpc_error(-25, 'bad-prevblk', node.submitheader, hexdata=b2x(CBlockHeader(bad_block2).serialize()))

}
{
LOCK(cs_main);
if (LookupBlockIndex(h.GetHash())) return NullUniValue;
Copy link
Member

@jnewbery jnewbery Jun 11, 2018

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.

Copy link
Member

@promag promag Jun 11, 2018

@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()

Copy link
Member

@jnewbery jnewbery Jun 11, 2018

perhaps test submitting a block header that isn't the most-work tip (ie a fork from some earlier block)

Copy link
Member Author

@MarcoFalke MarcoFalke Jul 11, 2018

Done at the end of this function

@jnewbery
Copy link
Member

@jnewbery jnewbery commented Jun 20, 2018

#13439 has been merged. Is this PR ready for rebase and rereview?

@MarcoFalke MarcoFalke force-pushed the Mf1806-rpcBlockHeader branch from 2595028 to 03ac2b4 Jun 26, 2018
@MarcoFalke MarcoFalke force-pushed the Mf1806-rpcBlockHeader branch from 03ac2b4 to 3ea4750 Jul 11, 2018
@MarcoFalke MarcoFalke force-pushed the Mf1806-rpcBlockHeader branch 3 times, most recently from dfc8cf6 to fa7d7dd Jul 11, 2018
@sipa
Copy link
Member

@sipa sipa commented Jul 13, 2018

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')
Copy link
Member

@luke-jr luke-jr Jul 14, 2018

Does master have a bug? This should be returning "bad-txns-nonfinal", not "invalid"...

Copy link
Member Author

@MarcoFalke MarcoFalke Jul 14, 2018

See the TODO

// TODO Maybe pass down fNewBlock to AcceptBlockHeader, so it is properly set to true in this case?

@luke-jr
Copy link
Member

@luke-jr luke-jr commented Jul 14, 2018

Please rename either the PR or the RPC method to match the other...

@MarcoFalke MarcoFalke changed the title rpc: Add submitblockheader rpc: Add submitheader Jul 14, 2018
@promag
Copy link
Member

@promag promag commented Jul 14, 2018

utACK fa7d7dd.

@MarcoFalke MarcoFalke force-pushed the Mf1806-rpcBlockHeader branch from fa7d7dd to 5ce7446 Aug 13, 2018
@MarcoFalke MarcoFalke force-pushed the Mf1806-rpcBlockHeader branch from 5ce7446 to fa091b0 Aug 13, 2018
@MarcoFalke
Copy link
Member Author

@MarcoFalke MarcoFalke commented Aug 13, 2018

Rebased. Only conflict was in tests, no other changes.

@laanwj
Copy link
Member

@laanwj laanwj commented Aug 15, 2018

utACK fa091b0

ken2812221 pushed a commit to ken2812221/bitcoin that referenced this issue Aug 15, 2018
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
@laanwj laanwj merged commit fa091b0 into bitcoin:master Aug 15, 2018
1 check passed
@MarcoFalke MarcoFalke deleted the Mf1806-rpcBlockHeader branch Aug 15, 2018
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this issue May 26, 2021
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
Munkybooty added a commit to Munkybooty/dash that referenced this issue Jun 29, 2021
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
5tefan added a commit to 5tefan/dash that referenced this issue Aug 9, 2021
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
5tefan added a commit to 5tefan/dash that referenced this issue Aug 9, 2021
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
UdjinM6 added a commit to dashpay/dash that referenced this issue Aug 10, 2021
* 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>
5tefan added a commit to 5tefan/dash that referenced this issue Aug 12, 2021
* 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>
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

8 participants