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

Implement "getchains" RPC command to monitor blockchain forks. #4536

Merged
merged 1 commit into from Aug 3, 2014

Conversation

domob1812
Copy link
Contributor

Port over chronokings/huntercoin#19 from
Huntercoin: This implements a new RPC command "getchains" that can be
used to find all currently active chain heads. This is similar to the
-printblocktree startup option, but it can be used without restarting
just via the RPC interface on a running daemon.

@domob1812
Copy link
Contributor Author

Of course, the name of the RPC call is up for discussion - maybe "getchaintips" is better, or "getblockchainheads" or something like that. For now, I just used the original name. This command has been included (and tested) so far in Huntercoin, Namecoin and Motocoin.

@petertodd
Copy link
Contributor

"getchaintips" seems more intuitive to me; I've wanted this feature before myself.

@sipa
Copy link
Member

sipa commented Jul 15, 2014

Just iterate over the mapBlockIndex entries, removing the ones that are the pprev of another mapBlockIndex entry.

@jgarzik
Copy link
Contributor

jgarzik commented Jul 15, 2014

ACK concept + @sipa suggestion

if (fHelp || params.size () != 0)
throw runtime_error(
"getchains\n"
"Return status of all known chains.\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

"Returns status" is not very helpful. More detail on what information is returned, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was just meant as a "place-holder" for now. I will extend the help text, also including example output (similar to other help texts).

@domob1812
Copy link
Contributor Author

Thanks for the input - I'll try to incorporate everything and update the pull request. I wanted to know if there's interest in this feature at all first, and I'm glad that there is. :)

@sipa
Copy link
Member

sipa commented Jul 15, 2014

Yes, no problem with such an RPC.

break;
}

obj.push_back (Pair ("branch_len", len));
Copy link
Contributor

Choose a reason for hiding this comment

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

branch_len should be branchlength to be consistent with other RPC results (e.g. see getblockchaininfo or getblocktemplate).

@gavinandresen
Copy link
Contributor

Also: a regression test that exercises this new RPC call would be spiffy (see qa/rpc-tests/ for examples).

@domob1812
Copy link
Contributor Author

I've now updated the code and help text. What do you think about it? @sipa, is this the algorithm you had in mind? I'll also build a new unit test for the call, possibly tomorrow.

@domob1812
Copy link
Contributor Author

@TheBlueMatt: Not sure why the test failed - the log shows no conclusive error message (but I haven't got any experience with your script so far), and it builds perfectly fine for me on GNU/Linux (after a make distclean). Also make check works fine. Any hints?

@jgarzik
Copy link
Contributor

jgarzik commented Jul 17, 2014

Currently pulltester is broken.

@domob1812
Copy link
Contributor Author

Basic regtest added. Since the test framework chain doesn't contain orphans, it only checks that the "isbest:true" result is as expected. Ok?


/* Lock everything. Not sure if this is needed for the whole duration
of the call, but better be safe than sorry. */
LOCK(cs_main);
Copy link
Member

Choose a reason for hiding this comment

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

As the command is not marked as threadSafe in the RPC command table, acquiring any locks is redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. So just removing the LOCK in the call is fine?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. The calling code acquires the cs_main lock and cs_wallet locks if that bit is not set.

@laanwj
Copy link
Member

laanwj commented Jul 18, 2014

Useful information, ACK on concept.

{
std::map<uint256, CBlockIndex*>::const_iterator ia, ib;

ia = mapBlockIndex.find (a);
Copy link
Member

Choose a reason for hiding this comment

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

Coding style nit: no space before ()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. I'm used to GNU projects and tried to adapt my style to the surrounding code, but it seemed not to have worked 100%. I can, of course, change this if it matters. Should I?

@domob1812
Copy link
Contributor Author

I've now updated the whitespace and replaced the "branch" output in the JSON result with "branchlen", according to the last feedback. Should I provide a quashed commit, or is the series of commits also fine?

@laanwj
Copy link
Member

laanwj commented Jul 21, 2014

A series of commits is fine if the commits are distinct changes, or a logical sequence of changes.

In this case I'd suggest squashing as all of the changes apply to the same code.

@domob1812
Copy link
Contributor Author

Squashed commit of the last version.

@sipa
Copy link
Member

sipa commented Jul 27, 2014

I think the implementation can be made more efficient by using sets/maps of CBlockIndex* entries rather than uint256's. (it means you don't need a find in the comparator, and the data structures are 4 or 8 times smaller).

}
while (!chainActive.Contains(pcur));
}
obj.push_back(Pair("branchlen", branchLen));
Copy link
Member

Choose a reason for hiding this comment

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

You can use pcur->nHeight - chainActive.FindFork(pcur)->nHeight; instead of this loop (which uses the recently added CBlockIndex* skiplist).

@domob1812
Copy link
Contributor Author

Indeed, very good observations! I've updated the patch now. Let me know if it is ok like this, then I will again provide a squashed commit instead.

setTips.insert(i->second);
for (i = mapBlockIndex.begin(); i != mapBlockIndex.end(); ++i)
{
const CBlockIndex* pprev = i->second->pprev;
Copy link
Member

Choose a reason for hiding this comment

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

No need for the find/erase. Just setTips.erase(pprev).

@laanwj laanwj added the RPC label Jul 31, 2014
@sipa
Copy link
Member

sipa commented Aug 3, 2014

Tested ACK. Works nicely.

@sipa
Copy link
Member

sipa commented Aug 3, 2014

Please squash you commits, though.

@laanwj
Copy link
Member

laanwj commented Aug 3, 2014

Another tested ACK (after squashing commits)

Port over chronokings/huntercoin#19 from
Huntercoin:  This implements a new RPC command "getchaintips" that can be
used to find all currently active chain heads.  This is similar to the
-printblocktree startup option, but it can be used without restarting
just via the RPC interface on a running daemon.
@domob1812
Copy link
Contributor Author

Thanks for the review - I've updated the branch to a single squashed commit.

@BitcoinPullTester
Copy link

Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/p4536_b33bd7a3be1cbcc8d255178307976b7762125b18/ for binaries and test log.
This test script verifies pulls every time they are updated. It, however, dies sometimes and fails to test properly. If you are waiting on a test, please check timestamps to verify that the test.log is moving at http://jenkins.bluematt.me/pull-tester/current/
Contact BlueMatt on freenode if something looks broken.

@sipa sipa merged commit b33bd7a into bitcoin:master Aug 3, 2014
sipa added a commit that referenced this pull request Aug 3, 2014
b33bd7a Implement "getchaintips" RPC command to monitor blockchain forks. (Daniel Kraft)
@domob1812 domob1812 deleted the btc-getchains branch August 4, 2014 05:00
@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.

None yet

8 participants