Skip to content

Conversation

maflcko
Copy link
Member

@maflcko maflcko 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 commented Jun 5, 2018

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.

@laanwj
Copy link
Member

laanwj commented Jun 5, 2018

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

@TheBlueMatt
Copy link
Contributor

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
Contributor

@promag promag left a 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.
*
*
*
Copy link
Contributor

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Contributor

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.

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

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.

Copy link
Member Author

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

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.

Copy link
Member Author

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.

Copy link
Contributor

@jnewbery jnewbery left a 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.
*
Copy link
Contributor

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.

bytes_to_hex_str,
)

b2x = bytes_to_hex_str
Copy link
Contributor

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,
+)

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
Contributor

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?

Copy link
Member Author

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

Copy link
Contributor

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

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

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.

Copy link
Contributor

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

Copy link
Contributor

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)

Copy link
Member Author

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

@jnewbery
Copy link
Contributor

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

@maflcko maflcko force-pushed the Mf1806-rpcBlockHeader branch from 2595028 to 03ac2b4 Compare June 26, 2018 09:23
@maflcko maflcko force-pushed the Mf1806-rpcBlockHeader branch from 03ac2b4 to 3ea4750 Compare July 11, 2018 15:03
@maflcko maflcko force-pushed the Mf1806-rpcBlockHeader branch 3 times, most recently from dfc8cf6 to fa7d7dd Compare July 11, 2018 15:57
@sipa
Copy link
Member

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

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"...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 commented Jul 14, 2018

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

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

promag commented Jul 14, 2018

utACK fa7d7dd.

@maflcko maflcko force-pushed the Mf1806-rpcBlockHeader branch from fa7d7dd to 5ce7446 Compare August 13, 2018 18:29
@maflcko maflcko force-pushed the Mf1806-rpcBlockHeader branch from 5ce7446 to fa091b0 Compare August 13, 2018 18:30
@maflcko
Copy link
Member Author

maflcko commented Aug 13, 2018

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

@laanwj
Copy link
Member

laanwj commented Aug 15, 2018

utACK fa091b0

@laanwj laanwj merged commit fa091b0 into bitcoin:master Aug 15, 2018
@maflcko maflcko deleted the Mf1806-rpcBlockHeader branch August 15, 2018 16:16
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request 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 pushed a commit to Munkybooty/dash that referenced this pull request 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 pull request 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 pull request 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 pull request 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 pull request 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
Development

Successfully merging this pull request may close these issues.

8 participants