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

RPCs for spark names #1519

Open
wants to merge 1 commit into
base: spark-names
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "evo/providertx.h"
#include "evo/deterministicmns.h"
#include "evo/cbtx.h"
#include "../sparkname.h"

#include "llmq/quorums_chainlocks.h"
#include "llmq/quorums_instantsend.h"
Expand Down Expand Up @@ -176,6 +177,91 @@ UniValue getblockcount(const JSONRPCRequest& request)
return chainActive.Height();
}

UniValue getsparknames(const JSONRPCRequest &request)
{
if (request.fHelp || request.params.size() > 1) {
throw std::runtime_error(
"getsparknames ( height )\n"
"\nReturns a list of all Spark names.\n"
"\nArguments:\n"
"1. height (numeric, optional) The block height to filter Spark names (default is the spark names start block height).\n"
"\nResult:\n"
"[\n"
" \"name1\", (string) The Spark name and address\n"
" \"name2\", (string) Another Spark name and address\n"
" ...\n"
"]\n"
"\nExamples:\n"
+ HelpExampleCli("getsparknames", "1000")
+ HelpExampleRpc("getsparknames", "1000")
);
}

LOCK(cs_main);

const Consensus::Params &consensusParams = Params().GetConsensus();
int nHeight = consensusParams.nSparkNamesStartBlock;
if (request.params.size() == 1) {
nHeight = request.params[0].get_int();
}
CSparkNameManager *sparkNameManager = CSparkNameManager::GetInstance();
std::set<std::string> sparkNames = sparkNameManager->GetSparkNames(nHeight);
UniValue result(UniValue::VARR);
for (const auto &name : sparkNames) {
result.push_back(name);
unsigned char network = spark::GetNetworkType();
spark::Address SparkAddr;
sparkNameManager->GetSparkAddress(name, chainActive.Tip()->nHeight, SparkAddr);
std::string strAddress = SparkAddr.encode(network);
result.push_back(strAddress);
}
return result;
}

UniValue getsparknamedata(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() > 1) {
throw std::runtime_error(
"getsparknamedata ( sparkname )\n"
"\nReturns info about spark name.\n"
"\nArguments:\n"
"Spark name (string)\n"
"\nResult:\n"
"[\n"
"1. Address (string)\n"
"2. Block Height (string)\n"
"3. TxId (string)\n"
"]\n"
"\nExamples:\n"
+ HelpExampleCli("getsparknamedata", "sparkname")
+ HelpExampleRpc("getsparknamedata", "sparkname")
);
}

LOCK(cs_main);

std::string sparkName = request.params[0].get_str();
CSparkNameManager *sparkNameManager = CSparkNameManager::GetInstance();

spark::Address SparkAddr;
sparkNameManager->GetSparkAddress(sparkName, chainActive.Tip()->nHeight, SparkAddr);

UniValue result(UniValue::VARR);
unsigned char network = spark::GetNetworkType();

std::string strAddress = SparkAddr.encode(network);
result.push_back(strAddress);

uint64_t nameBlockHeight = sparkNameManager->GetSparkNameBlockHeight(sparkName);
result.push_back(nameBlockHeight);

std::string sparkNameTxId = sparkNameManager->GetSparkNameTxID(sparkName);
result.push_back(sparkNameTxId);

return result;

}

UniValue getbestblockhash(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 0)
Expand Down Expand Up @@ -1658,6 +1744,8 @@ static const CRPCCommand commands[] =
{ "blockchain", "getblockchaininfo", &getblockchaininfo, true, {} },
{ "blockchain", "getbestblockhash", &getbestblockhash, true, {} },
{ "blockchain", "getblockcount", &getblockcount, true, {} },
{ "blockchain", "getsparknames", &getsparknames, true, {} },
{ "blockchain", "getsparknamedata", &getsparknamedata, true, {} },
{ "blockchain", "getblock", &getblock, true, {"blockhash","verbose"} },
{ "blockchain", "getblockhash", &getblockhash, true, {"height"} },
{ "blockchain", "getblockhashes", &getblockhashes, true, {"high", "low"} },
Expand Down
42 changes: 42 additions & 0 deletions src/sparkname.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "script/standard.h"
#include "base58.h"
#include "sparkname.h"
#include "validation.h"

CSparkNameManager *CSparkNameManager::sharedSparkNameManager = new CSparkNameManager();

Expand Down Expand Up @@ -53,6 +54,47 @@ bool CSparkNameManager::GetSparkAddress(const std::string &name, int nHeight, sp
}
}

uint64_t CSparkNameManager::GetSparkNameBlockHeight(const std::string &name) const
{
auto it = sparkNames.find(name);
if (it == sparkNames.end())
throw std::runtime_error("Spark name not found: " + name);

size_t height = it->second.second;
return height;
}

std::string CSparkNameManager::GetSparkNameTxID(const std::string &name) const
{
auto it = sparkNames.find(name);
if (it == sparkNames.end())
throw std::runtime_error("Spark name not found: " + name);

uint32_t blockHeight = it->second.second;

CBlockIndex* pBlockIndex = chainActive[blockHeight];
if (!pBlockIndex)
throw std::runtime_error("Block not found at height: " + std::to_string(blockHeight));

CBlock block;
if (!ReadBlockFromDisk(block, pBlockIndex, Params().GetConsensus()))
throw std::runtime_error("Failed to read block from disk.");

CSparkNameManager *sparkNameManager = CSparkNameManager::GetInstance();
for (const CTransactionRef& tx : block.vtx)
{
CSparkNameTxData sparkNameData;
CValidationState state;

if (sparkNameManager->CheckSparkNameTx(*tx, blockHeight, state, &sparkNameData))
{
return (*tx).GetHash().ToString();
}
}

throw std::runtime_error("Spark name transaction not found for: " + name);
}

bool CSparkNameManager::ParseSparkNameTxData(const CTransaction &tx, spark::SpendTransaction &sparkTx, CSparkNameTxData &sparkNameData, size_t &sparkNameDataPos)
{
CDataStream serializedSpark(SER_NETWORK, PROTOCOL_VERSION);
Expand Down
4 changes: 4 additions & 0 deletions src/sparkname.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class CSparkNameManager
}

static CSparkNameManager *GetInstance() { return sharedSparkNameManager; };

uint64_t GetSparkNameBlockHeight(const std::string &name) const;

std::string GetSparkNameTxID(const std::string &name) const;
};

#endif // FIRO_SPARKNAME_H