Merge pull request #848 from libscott/rpcScanNotarisationsDB
RPC methods to scan notarisations DB
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include "notarisationdb.h"
|
||||
#include "uint256.h"
|
||||
#include "cc/eval.h"
|
||||
#include "main.h"
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
@@ -82,3 +83,29 @@ void EraseBackNotarisations(const NotarisationsInBlock notarisations, CLevelDBBa
|
||||
batch.Erase(n.second.txHash);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Scan notarisationsdb backwards for blocks containing a notarisation
|
||||
* for given symbol. Return height of matched notarisation or 0.
|
||||
*/
|
||||
int ScanNotarisationsDB(int height, std::string symbol, int scanLimitBlocks, Notarisation& out)
|
||||
{
|
||||
if (height < 0 || height > chainActive.Height())
|
||||
return false;
|
||||
|
||||
for (int i=0; i<scanLimitBlocks; i++) {
|
||||
if (i > height) break;
|
||||
NotarisationsInBlock notarisations;
|
||||
uint256 blockHash = *chainActive[height-i]->phashBlock;
|
||||
if (!GetBlockNotarisations(blockHash, notarisations))
|
||||
continue;
|
||||
|
||||
BOOST_FOREACH(Notarisation& nota, notarisations) {
|
||||
if (strcmp(nota.second.symbol, symbol.data()) == 0) {
|
||||
out = nota;
|
||||
return height-i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -23,5 +23,6 @@ bool GetBlockNotarisations(uint256 blockHash, NotarisationsInBlock &nibs);
|
||||
bool GetBackNotarisation(uint256 notarisationHash, Notarisation &n);
|
||||
void WriteBackNotarisations(const NotarisationsInBlock notarisations, CLevelDBBatch &batch);
|
||||
void EraseBackNotarisations(const NotarisationsInBlock notarisations, CLevelDBBatch &batch);
|
||||
int ScanNotarisationsDB(int height, std::string symbol, int scanLimitBlocks, Notarisation& out);
|
||||
|
||||
#endif /* NOTARISATIONDB_H */
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "chainparams.h"
|
||||
#include "checkpoints.h"
|
||||
#include "crosschain.h"
|
||||
#include "notarisationdb.h"
|
||||
#include "importcoin.h"
|
||||
#include "base58.h"
|
||||
#include "consensus/validation.h"
|
||||
@@ -251,3 +252,55 @@ UniValue migrate_completeimporttransaction(const UniValue& params, bool fHelp)
|
||||
|
||||
return HexStr(E_MARSHAL(ss << importTx));
|
||||
}
|
||||
|
||||
|
||||
UniValue getNotarisationsForBlock(const UniValue& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() != 1)
|
||||
throw runtime_error("getNotarisationsForBlock blockHash\n\n"
|
||||
"Takes a block hash and returns notarisation transactions "
|
||||
"within the block");
|
||||
|
||||
uint256 blockHash = uint256S(params[0].get_str());
|
||||
|
||||
NotarisationsInBlock nibs;
|
||||
GetBlockNotarisations(blockHash, nibs);
|
||||
UniValue out(UniValue::VARR);
|
||||
BOOST_FOREACH(const Notarisation& n, nibs)
|
||||
{
|
||||
UniValue item(UniValue::VARR);
|
||||
item.push_back(n.first.GetHex());
|
||||
item.push_back(HexStr(E_MARSHAL(ss << n.second)));
|
||||
out.push_back(item);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
UniValue scanNotarisationsDB(const UniValue& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() < 2 || params.size() > 3)
|
||||
throw runtime_error("scanNotarisationsDB blockHeight symbol [blocksLimit=1440]\n\n"
|
||||
"Scans notarisationsdb backwards from height for a notarisation"
|
||||
" of given symbol");
|
||||
int height = atoi(params[0].get_str().c_str());
|
||||
std::string symbol = params[1].get_str().c_str();
|
||||
|
||||
int limit = 1440;
|
||||
if (params.size() > 2) {
|
||||
limit = atoi(params[2].get_str().c_str());
|
||||
}
|
||||
|
||||
if (height == 0) {
|
||||
height = chainActive.Height();
|
||||
}
|
||||
|
||||
Notarisation nota;
|
||||
int matchedHeight = ScanNotarisationsDB(height, symbol, limit, nota);
|
||||
if (!matchedHeight) return NullUniValue;
|
||||
UniValue out(UniValue::VOBJ);
|
||||
out.pushKV("height", matchedHeight);
|
||||
out.pushKV("hash", nota.first.GetHex());
|
||||
out.pushKV("opreturn", HexStr(E_MARSHAL(ss << nota.second)));
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -313,6 +313,8 @@ static const CRPCCommand vRPCCommands[] =
|
||||
{ "crosschain", "height_MoM", &height_MoM, true },
|
||||
{ "crosschain", "assetchainproof", &assetchainproof, true },
|
||||
{ "crosschain", "crosschainproof", &crosschainproof, true },
|
||||
{ "crosschain", "getNotarisationsForBlock", &getNotarisationsForBlock, true },
|
||||
{ "crosschain", "scanNotarisationsDB", &scanNotarisationsDB, true },
|
||||
{ "crosschain", "migrate_converttoexport", &migrate_converttoexport, true },
|
||||
{ "crosschain", "migrate_createimporttransaction", &migrate_createimporttransaction, true },
|
||||
{ "crosschain", "migrate_completeimporttransaction", &migrate_completeimporttransaction, true },
|
||||
|
||||
@@ -361,6 +361,8 @@ extern UniValue calc_MoM(const UniValue& params, bool fHelp);
|
||||
extern UniValue height_MoM(const UniValue& params, bool fHelp);
|
||||
extern UniValue assetchainproof(const UniValue& params, bool fHelp);
|
||||
extern UniValue crosschainproof(const UniValue& params, bool fHelp);
|
||||
extern UniValue getNotarisationsForBlock(const UniValue& params, bool fHelp);
|
||||
extern UniValue scanNotarisationsDB(const UniValue& params, bool fHelp);
|
||||
extern UniValue migrate_converttoexport(const UniValue& params, bool fHelp);
|
||||
extern UniValue migrate_createimporttransaction(const UniValue& params, bool fHelp);
|
||||
extern UniValue migrate_completeimporttransaction(const UniValue& params, bool fHelp);
|
||||
|
||||
Reference in New Issue
Block a user