Skip to content

Commit e2c44ef

Browse files
authored
Merge pull request #2290 from RoboticMind/rpc-block-by-time
rpc: Create getblockbymintime
2 parents d57ca01 + 893c54f commit e2c44ef

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/rpc/blockchain.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "blockchain.h"
88
#include "node/blockstorage.h"
99
#include <util/string.h>
10+
#include "gridcoin/support/block_finder.h"
1011

1112
#include <univalue.h>
1213

@@ -594,6 +595,32 @@ UniValue getblockbynumber(const UniValue& params, bool fHelp)
594595
return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);
595596
}
596597

598+
UniValue getblockbymintime(const UniValue& params, bool fHelp)
599+
{
600+
if (fHelp || params.size() < 1 || params.size() > 2)
601+
throw runtime_error(
602+
"getblockbymintime <timestamp> [bool:txinfo]\n"
603+
"\n"
604+
"[bool:txinfo] optional to print more detailed tx info\n"
605+
"\n"
606+
"Returns details of the block at or just after the given timestamp\n");
607+
608+
int64_t nTimestamp = params[0].get_int64();
609+
610+
if (nTimestamp < pindexGenesisBlock->nTime || nTimestamp > pindexBest->nTime)
611+
throw runtime_error("Timestamp out of range. Cannot be below the time of the genesis block or above the time of the latest block");
612+
613+
LOCK(cs_main);
614+
615+
CBlock block;
616+
static GRC::BlockFinder block_finder;
617+
618+
CBlockIndex* pblockindex = block_finder.FindByMinTime(nTimestamp);
619+
ReadBlockFromDisk(block, pblockindex, Params().GetConsensus());
620+
621+
return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);
622+
}
623+
597624
UniValue getblocksbatch(const UniValue& params, bool fHelp)
598625
{
599626
g_timer.InitTimer(__func__, LogInstance().WillLogCategory(BCLog::LogFlags::RPC));

src/rpc/client.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ static const CRPCConvertParam vRPCConvertParams[] =
198198
{ "getblock" , 1 },
199199
{ "getblockbynumber" , 0 },
200200
{ "getblockbynumber" , 1 },
201+
{ "getblockbymintime" , 0 },
202+
{ "getblockbymintime" , 1 },
201203
{ "getblocksbatch" , 1 },
202204
{ "getblocksbatch" , 2 },
203205
{ "getblockhash" , 0 },

src/rpc/server.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ static const CRPCCommand vRPCCommands[] =
412412
{ "getbestblockhash", &getbestblockhash, cat_network },
413413
{ "getblock", &getblock, cat_network },
414414
{ "getblockbynumber", &getblockbynumber, cat_network },
415+
{ "getblockbymintime", &getblockbymintime, cat_network },
415416
{ "getblocksbatch", &getblocksbatch, cat_network },
416417
{ "getblockcount", &getblockcount, cat_network },
417418
{ "getblockhash", &getblockhash, cat_network },

src/rpc/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ extern UniValue getaddednodeinfo(const UniValue& params, bool fHelp);
227227
extern UniValue getbestblockhash(const UniValue& params, bool fHelp);
228228
extern UniValue getblock(const UniValue& params, bool fHelp);
229229
extern UniValue getblockbynumber(const UniValue& params, bool fHelp);
230+
extern UniValue getblockbymintime(const UniValue& params, bool fHelp);
230231
extern UniValue getblocksbatch(const UniValue& params, bool fHelp);
231232
extern UniValue getblockchaininfo(const UniValue& params, bool fHelp);
232233
extern UniValue getblockcount(const UniValue& params, bool fHelp);

0 commit comments

Comments
 (0)