Skip to content

Commit

Permalink
Create the getblockbymintime RPC Command
Browse files Browse the repository at this point in the history
  • Loading branch information
RoboticMind committed Aug 19, 2021
1 parent d57ca01 commit 441759c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "blockchain.h"
#include "node/blockstorage.h"
#include <util/string.h>
#include "gridcoin/support/block_finder.h"

#include <univalue.h>

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

UniValue getblockbymintime(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 2)
throw runtime_error(
"getblockbymintime <timestamp> [bool:txinfo]\n"
"\n"
"[bool:txinfo] optional to print more detailed tx info\n"
"\n"
"Returns details of the block at or just after the given timestamp\n");

int64_t nTimestamp = params[0].get_int64();

if (nTimestamp < pindexGenesisBlock->nTime || nTimestamp > pindexBest->nTime)
throw runtime_error("Timestamp out of range. Cannot be below the time of the gensis block or above the time of the latest block");

LOCK(cs_main);

CBlock block;
static GRC::BlockFinder block_finder;

CBlockIndex* pblockindex = block_finder.FindByMinTime(nTimestamp);
ReadBlockFromDisk(block, pblockindex, Params().GetConsensus());

return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);
}

UniValue getblocksbatch(const UniValue& params, bool fHelp)
{
g_timer.InitTimer(__func__, LogInstance().WillLogCategory(BCLog::LogFlags::RPC));
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "getblock" , 1 },
{ "getblockbynumber" , 0 },
{ "getblockbynumber" , 1 },
{ "getblockbymintime" , 0 },
{ "getblockbymintime" , 1 },
{ "getblocksbatch" , 1 },
{ "getblocksbatch" , 2 },
{ "getblockhash" , 0 },
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ static const CRPCCommand vRPCCommands[] =
{ "getbestblockhash", &getbestblockhash, cat_network },
{ "getblock", &getblock, cat_network },
{ "getblockbynumber", &getblockbynumber, cat_network },
{ "getblockbymintime", &getblockbymintime, cat_network },
{ "getblocksbatch", &getblocksbatch, cat_network },
{ "getblockcount", &getblockcount, cat_network },
{ "getblockhash", &getblockhash, cat_network },
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ extern UniValue getaddednodeinfo(const UniValue& params, bool fHelp);
extern UniValue getbestblockhash(const UniValue& params, bool fHelp);
extern UniValue getblock(const UniValue& params, bool fHelp);
extern UniValue getblockbynumber(const UniValue& params, bool fHelp);
extern UniValue getblockbymintime(const UniValue& params, bool fHelp);
extern UniValue getblocksbatch(const UniValue& params, bool fHelp);
extern UniValue getblockchaininfo(const UniValue& params, bool fHelp);
extern UniValue getblockcount(const UniValue& params, bool fHelp);
Expand Down

0 comments on commit 441759c

Please sign in to comment.