Browse Source

Merge pull request #151 from MyHush/getchaintips

Add optional minBranchLen argument to getchaintips RPC
size_on_disk
Duke Leto 6 years ago
committed by GitHub
parent
commit
3ff1205367
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      qa/rpc-tests/getchaintips.py
  2. 18
      src/rpcblockchain.cpp
  3. 1
      src/rpcclient.cpp

18
qa/rpc-tests/getchaintips.py

@ -13,13 +13,15 @@ from test_framework.util import assert_equal
class GetChainTipsTest (BitcoinTestFramework):
def run_test (self):
BitcoinTestFramework.run_test (self)
rpc = self.nodes[0]
#for node in self.nodes:
# node.generate(200)
tips = self.nodes[0].getchaintips ()
tips = self.nodes[0].getchaintips()
assert_equal (len (tips), 1)
assert_equal (tips[0]['branchlen'], 0)
assert_equal (tips[0]['height'], 200)
assert_equal (tips[0]['status'], 'active')
assert_equal (tips[0]['branchlen'], 0)
assert_equal (tips[0]['height'], 0)
# Split the network and build two chains of different lengths.
self.split_network ()
@ -31,14 +33,14 @@ class GetChainTipsTest (BitcoinTestFramework):
assert_equal (len (tips), 1)
shortTip = tips[0]
assert_equal (shortTip['branchlen'], 0)
assert_equal (shortTip['height'], 210)
assert_equal (shortTip['height'], 10)
assert_equal (tips[0]['status'], 'active')
tips = self.nodes[3].getchaintips ()
assert_equal (len (tips), 1)
longTip = tips[0]
assert_equal (longTip['branchlen'], 0)
assert_equal (longTip['height'], 220)
assert_equal (longTip['height'], 20)
assert_equal (tips[0]['status'], 'active')
# Join the network halves and check that we now have two tips
@ -55,5 +57,9 @@ class GetChainTipsTest (BitcoinTestFramework):
tips[1]['status'] = 'active'
assert_equal (tips[1], shortTip)
# test optional minBranchLen
tips = self.nodes[0].getchaintips(42)
assert_equal (len (tips), 0)
if __name__ == '__main__':
GetChainTipsTest ().main ()

18
src/rpcblockchain.cpp

@ -965,7 +965,7 @@ struct CompareBlocksByHeight
UniValue getchaintips(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 0)
if (fHelp || params.size() > 1)
throw runtime_error(
"getchaintips\n"
"Return information about all known tips in the block tree,"
@ -998,6 +998,10 @@ UniValue getchaintips(const UniValue& params, bool fHelp)
LOCK(cs_main);
int minBranchLen = 0;
if (params.size() > 0)
minBranchLen = params[0].get_int();
/* Build up a list of chain tips. We start with the list of all
known blocks, and successively remove blocks that appear as pprev
of another block. */
@ -1011,18 +1015,22 @@ UniValue getchaintips(const UniValue& params, bool fHelp)
setTips.erase(pprev);
}
// Always report the currently active tip.
setTips.insert(chainActive.Tip());
if (minBranchLen == 0) {
// insert currently active tip
setTips.insert(chainActive.Tip());
}
/* Construct the output array. */
UniValue res(UniValue::VARR);
BOOST_FOREACH(const CBlockIndex* block, setTips)
{
const int branchLen = block->nHeight - chainActive.FindFork(block)->nHeight;
if (branchLen < minBranchLen)
continue;
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("height", block->nHeight));
obj.push_back(Pair("hash", block->phashBlock->GetHex()));
const int branchLen = block->nHeight - chainActive.FindFork(block)->nHeight;
obj.push_back(Pair("branchlen", branchLen));
string status;

1
src/rpcclient.cpp

@ -46,6 +46,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "getbalance", 1 },
{ "getbalance", 2 },
{ "getblockhash", 0 },
{ "getchaintips", 0 },
{ "move", 2 },
{ "move", 3 },
{ "sendfrom", 2 },

Loading…
Cancel
Save