Skip to content

Commit

Permalink
Tx ids with lTags
Browse files Browse the repository at this point in the history
  • Loading branch information
levonpetrosyan93 committed Jun 9, 2024
1 parent ef93908 commit 40d9bf0
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ class CBlockIndex
sigma::spend_info_container sigmaSpentSerials;
std::unordered_map<Scalar, int> lelantusSpentSerials;
std::unordered_map<GroupElement, int> spentLTags;
// linking tag hash mapped to tx hash
std::unordered_map<uint256, uint256> ltagTxhash;

//! list of disabling sporks active at this block height
//! std::map {feature name} -> {block number when feature is re-enabled again, parameter}
Expand Down Expand Up @@ -303,6 +305,7 @@ class CBlockIndex
sparkMintedCoins.clear();
sparkSetHash.clear();
spentLTags.clear();
ltagTxhash.clear();
sparkTxHashContext.clear();
sigmaSpentSerials.clear();
lelantusSpentSerials.clear();
Expand Down Expand Up @@ -563,6 +566,7 @@ class CDiskBlockIndex : public CBlockIndex

if (GetBoolArg("-mobile", false)) {
READWRITE(sparkTxHashContext);
READWRITE(ltagTxhash);
}
}

Expand Down
58 changes: 58 additions & 0 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,63 @@ UniValue getusedcoinstags(const JSONRPCRequest& request)
return ret;
}

UniValue getusedcoinstagstxhashes(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 1)
throw std::runtime_error(
"getusedcoinstagstxhashes\n"
"\nReturns the set of used coin tags.\n"
"\nArguments:\n"
"{\n"
" \"startNumber \" (int) Number of elements already existing on user side\n"
"}\n"
"\nResult:\n"
"{\n"
" \"tags\" (std::string[]) array of Serialized GroupElements paired with unit256 (tx ids) \n"
"}\n"
);

int startNumber;
try {
startNumber = std::stol(request.params[0].get_str());
} catch (std::logic_error const & e) {
throw std::runtime_error(std::string("An exception occurred while parsing parameters: ") + e.what());
}

spark::CSparkState* sparkState = spark::CSparkState::GetState();
std::unordered_map<GroupElement, int, spark::CLTagHash> tags;
std::unordered_map<uint256, uint256> ltagTxhash;
{
LOCK(cs_main);
tags = sparkState->GetSpends();
ltagTxhash = sparkState->GetSpendTxIds();
}
UniValue serializedTagsTxIds(UniValue::VARR);
int i = 0;
for ( auto it = tags.begin(); it != tags.end(); ++it, ++i) {
if ((tags.size() - i - 1) < startNumber)
continue;
std::vector<unsigned char> serialized;
serialized.resize(34);
it->first.serialize(serialized.data());
std::vector<UniValue> data;
data.push_back(EncodeBase64(serialized.data(), 34));
uint256 txid;
uint256 ltagHash = primitives::GetLTagHash(it->first);
if (ltagTxhash.count(ltagHash) > 0)
txid = ltagTxhash[ltagHash];
data.push_back(EncodeBase64(txid.begin(), txid.size()));
UniValue entity(UniValue::VARR);
entity.push_backV(data);
serializedTagsTxIds.push_back(entity);
}

UniValue ret(UniValue::VOBJ);
ret.push_back(Pair("tagsandtxids", serializedTagsTxIds));

return ret;
}

UniValue getsparklatestcoinid(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 0)
Expand Down Expand Up @@ -1854,6 +1911,7 @@ static const CRPCCommand commands[] =
{ "mobile", "getsparkanonymityset", &getsparkanonymityset, false },
{ "mobile", "getsparkmintmetadata", &getsparkmintmetadata, true },
{ "mobile", "getusedcoinstags", &getusedcoinstags, false },
{ "mobile", "getusedcoinstagstxhashes", &getusedcoinstagstxhashes, false },
{ "mobile", "getsparklatestcoinid", &getsparklatestcoinid, true },
{ "mobile", "getmempooltxids", &getmempooltxids, true },
{ "mobile", "getmempooltxs", &getmempooltxs, true },
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ static const CRPCCommand vRPCCommands[] =
{ "mobile", "getsparkanonymityset", &getsparkanonymityset, false },
{ "mobile", "getsparkmintmetadata", &getsparkmintmetadata, true },
{ "mobile", "getusedcoinstags", &getusedcoinstags, false },
{ "mobile", "getusedcoinstagstxhashes", &getusedcoinstagstxhashes, false },
{ "mobile", "getsparklatestcoinid", &getsparklatestcoinid, true },
{ "mobile", "getmempooltxids", &getmempooltxids, true },
{ "mobile", "getmempooltxs", &getmempooltxs, true },
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ extern UniValue getlatestcoinid(const JSONRPCRequest& params);
extern UniValue getsparkanonymityset(const JSONRPCRequest& params);
extern UniValue getsparkmintmetadata(const JSONRPCRequest& params);
extern UniValue getusedcoinstags(const JSONRPCRequest& params);
extern UniValue getusedcoinstagstxhashes(const JSONRPCRequest& params);
extern UniValue getsparklatestcoinid(const JSONRPCRequest& params);
extern UniValue getmempooltxids(const JSONRPCRequest& params);
extern UniValue getmempooltxs(const JSONRPCRequest& params);
Expand Down
24 changes: 23 additions & 1 deletion src/spark/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,16 @@ bool ConnectBlockSpark(
}

if (!fJustCheck) {
BOOST_FOREACH(auto& lTag, pblock->sparkTxInfo->spentLTags) {
BOOST_FOREACH (auto& lTag, pblock->sparkTxInfo->spentLTags) {
pindexNew->spentLTags.insert(lTag);
sparkState.AddSpend(lTag.first, lTag.second);
}
if (GetBoolArg("-mobile", false)) {
BOOST_FOREACH (auto& lTag, pblock->sparkTxInfo->ltagTxhash) {
pindexNew->ltagTxhash.insert(lTag);
sparkState.AddLTagTxHash(lTag.first, lTag.second);
}
}
}
else {
return true;
Expand Down Expand Up @@ -693,6 +699,9 @@ bool CheckSparkSpendTransaction(
if (sparkTxInfo && !sparkTxInfo->fInfoIsComplete) {
for (size_t i = 0; i < lTags.size(); i++) {
sparkTxInfo->spentLTags.insert(std::make_pair(lTags[i], ids[i]));
if (GetBoolArg("-mobile", false)) {
sparkTxInfo->ltagTxhash.insert(std::make_pair(primitives::GetLTagHash(lTags[i]), hashTx));
}
}
}
}
Expand Down Expand Up @@ -1038,6 +1047,10 @@ void CSparkState::AddSpend(const GroupElement& lTag, int coinGroupId) {
}
}

void CSparkState::AddLTagTxHash(const uint256& lTagHash, const uint256& txHash) {
ltagTxhash[lTagHash] = txHash;
}

void CSparkState::RemoveSpend(const GroupElement& lTag) {
auto iter = usedLTags.find(lTag);
if (iter != usedLTags.end()) {
Expand Down Expand Up @@ -1074,6 +1087,11 @@ void CSparkState::AddBlock(CBlockIndex *index) {
for (auto const &lTags : index->spentLTags) {
AddSpend(lTags.first, lTags.second);
}
if (GetBoolArg("-mobile", false)) {
for (auto const &elem : index->ltagTxhash) {
AddLTagTxHash(elem.first, elem.second);
}
}
}

void CSparkState::RemoveBlock(CBlockIndex *index) {
Expand Down Expand Up @@ -1320,6 +1338,10 @@ std::unordered_map<GroupElement, int, spark::CLTagHash> const & CSparkState::Get
return usedLTags;
}

std::unordered_map<uint256, uint256> const& CSparkState::GetSpendTxIds() const {
return ltagTxhash;
}

std::unordered_map<int, CSparkState::SparkCoinGroupInfo> const& CSparkState::GetCoinGroups() const {
return coinGroups;
}
Expand Down
5 changes: 5 additions & 0 deletions src/spark/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class CSparkTxInfo {

// linking tag for every spend (map from lTag to coin group id)
std::unordered_map<GroupElement, int> spentLTags;
std::unordered_map<uint256, uint256> ltagTxhash;

// information about transactions in the block is complete
bool fInfoIsComplete;
Expand Down Expand Up @@ -168,6 +169,7 @@ class CSparkState {
void AddMintsToStateAndBlockIndex(CBlockIndex *index, const CBlock* pblock);

void AddSpend(const GroupElement& lTag, int coinGroupId);
void AddLTagTxHash(const uint256& lTagHash, const uint256& txHash);
void RemoveSpend(const GroupElement& lTag);
// Add everything from the block to the state
void AddBlock(CBlockIndex *index);
Expand Down Expand Up @@ -213,6 +215,7 @@ class CSparkState {

std::unordered_map<spark::Coin, CMintedCoinInfo, spark::CoinHash> const & GetMints() const;
std::unordered_map<GroupElement, int, spark::CLTagHash> const & GetSpends() const;
std::unordered_map<uint256, uint256> const& GetSpendTxIds() const;
std::unordered_map<int, SparkCoinGroupInfo> const & GetCoinGroups() const;
std::unordered_map<GroupElement, uint256, spark::CLTagHash> const & GetMempoolLTags() const;

Expand All @@ -238,6 +241,8 @@ class CSparkState {
std::unordered_map<spark::Coin, CMintedCoinInfo, spark::CoinHash> mintedCoins;
// Set of all used coin linking tags.
std::unordered_map<GroupElement, int, spark::CLTagHash> usedLTags;
// linking tag hash mapped to tx hash
std::unordered_map<uint256, uint256> ltagTxhash;

typedef std::map<int, size_t> metainfo_container_t;
metainfo_container_t extendedMintMetaInfo, mintMetaInfo, spendMetaInfo;
Expand Down
1 change: 1 addition & 0 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256
pindexNew->sparkSetHash = diskindex.sparkSetHash;
pindexNew->spentLTags = diskindex.spentLTags;
pindexNew->sparkTxHashContext = diskindex.sparkTxHashContext;
pindexNew->ltagTxhash = diskindex.ltagTxhash;

pindexNew->activeDisablingSporks = diskindex.activeDisablingSporks;

Expand Down

0 comments on commit 40d9bf0

Please sign in to comment.