Skip to content

Commit

Permalink
snapshot to sys4 support
Browse files Browse the repository at this point in the history
Former-commit-id: c0ea807
  • Loading branch information
sidhujag committed Jun 2, 2019
1 parent aa5a74d commit 66a0262
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 1 deletion.
103 changes: 103 additions & 0 deletions src/assetallocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1681,3 +1681,106 @@ UniValue listassetallocations(const JSONRPCRequest& request) {
throw runtime_error("SYSCOIN_ASSET_ALLOCATION_RPC_ERROR: ERRCODE: 1510 - " + _("Scan failed"));
return oRes;
}
bool BuildAssetDumpJson(const CAsset& asset, UniValue& oAsset)
{
CAliasIndex dbAlias;
vector<unsigned char> vchAddress = asset.vchAliasOrAddress;
if(!paliasdb->ReadAlias(asset.vchAliasOrAddress, dbAlias)){
if(!paliasdb->ReadAddress(asset.vchAliasOrAddress, vchAddress)){
LogPrintf("Cannot find address from ReadAddress, trying to resolve %s as a Syscoin address...\n", stringFromVch(asset.vchAliasOrAddress).c_str());
vchAddress = asset.vchAliasOrAddress;
}
}
else{
vchAddress = vchFromString(EncodeBase58(dbAlias.vchAddress));
}
oAsset.push_back(Pair("symbol", stringFromVch(asset.vchSymbol)));
oAsset.push_back(Pair("publicvalue", stringFromVch(asset.vchPubData)));
oAsset.push_back(Pair("address", stringFromVch(vchAddress)));
oAsset.push_back(Pair("balance", ValueFromAssetAmount(asset.nBalance, asset.nPrecision, asset.bUseInputRanges)));
oAsset.push_back(Pair("total_supply", ValueFromAssetAmount(asset.nTotalSupply, asset.nPrecision, asset.bUseInputRanges)));
oAsset.push_back(Pair("max_supply", ValueFromAssetAmount(asset.nMaxSupply, asset.nPrecision, asset.bUseInputRanges)));
oAsset.push_back(Pair("precision", (int)asset.nPrecision));
return true;
}
bool BuildAssetAllocationDumpJson(CAssetAllocation& assetallocation, UniValue& oAssetAllocation)
{
CAliasIndex dbAlias;
vector<unsigned char> vchAddress = assetallocation.vchAliasOrAddress;
if(!paliasdb->ReadAlias(assetallocation.vchAliasOrAddress, dbAlias)){
if(!paliasdb->ReadAddress(assetallocation.vchAliasOrAddress, vchAddress)){
LogPrintf("Cannot find address from ReadAddress, trying to resolve %s as a Syscoin address...\n", stringFromVch(assetallocation.vchAliasOrAddress).c_str());
vchAddress = assetallocation.vchAliasOrAddress;
}
}
else{
vchAddress = vchFromString(EncodeBase58(dbAlias.vchAddress));
}
// prune values below 0.1 COINS
if(assetallocation.nBalance <= 0.1*COIN)
return false;
oAssetAllocation.push_back(Pair("address", stringFromVch(vchAddress)));
oAssetAllocation.push_back(Pair("balance", ValueFromAmount(assetallocation.nBalance)));

return true;
}
bool CAssetAllocationDB::DumpAssetAllocations(std::vector<unsigned char>vchAsset, UniValue& oRes) {
boost::scoped_ptr<CDBIterator> pcursor(NewIterator());
pcursor->SeekToFirst();
CAssetAllocation txPos;
pair<string, vector<unsigned char> > key;
while (pcursor->Valid()) {
boost::this_thread::interruption_point();
try {
if (pcursor->GetKey(key) && key.first == "assetallocationi") {
pcursor->GetValue(txPos);
if (vchAsset != txPos.vchAsset)
{
pcursor->Next();
continue;
}
UniValue oAssetAllocation(UniValue::VOBJ);
if (!BuildAssetAllocationDumpJson(txPos, oAssetAllocation))
{
pcursor->Next();
continue;
}
oRes.push_back(oAssetAllocation);
}
pcursor->Next();
}
catch (std::exception &e) {
return error("%s() : deserialize error", __PRETTY_FUNCTION__);
}
}
return true;
}
UniValue dumpassetallocations(const JSONRPCRequest& request) {
const UniValue &params = request.params;
if (request.fHelp || 3 < params.size())
throw runtime_error("dumpassetallocations [asset,...]\n"
"Dump all asset allocations of an array of asset guids.\n"
"\"assets\" (array) Array of Asset GUID to filter.\n"
" }\n"
+ HelpExampleCli("dumpassetallocations", "[\"31dc0be2d4ec9d5a\", \"e23e5b9150691d74\"]")
);
UniValue oAllRes(UniValue::VARR);
UniValue arrayGuids = params[0].get_array();
CAsset dbAsset;
for(unsigned i =0;i<arrayGuids.size();i++){
const std::string & guid = arrayGuids[i].get_str();
UniValue oRes(UniValue::VARR);
if(!GetAsset(vchFromString(guid), dbAsset))
continue;
UniValue oAsset(UniValue::VOBJ);
if(!BuildAssetDumpJson(dbAsset, oAsset))
continue;

if (!passetallocationdb->DumpAssetAllocations(vchFromString(guid), oRes))
throw runtime_error("SYSCOIN_ASSET_ALLOCATION_RPC_ERROR: ERRCODE: 1510 - " + _("Scan failed"));
LogPrintf("Dumped %s entries for asset %s\n", oRes.size(), guid.c_str());
oAsset.push_back(std::make_pair("allocations", oRes));
oAllRes.push_back(oAsset);
}
return oAllRes;
}
1 change: 1 addition & 0 deletions src/assetallocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class CAssetAllocationDB : public CDBWrapper {
}
void WriteAssetAllocationIndex(const CAssetAllocation& assetAllocationTuple, const CAsset& asset, const CAmount& nSenderBalance, const CAmount& nAmount, const std::string& strSender, const std::string& strReceiver);
bool ScanAssetAllocations(const int count, const int from, const UniValue& oOptions, UniValue& oRes);
bool DumpAssetAllocations(std::vector<unsigned char>vchGuid, UniValue& oRes);
};
class CAssetAllocationTransactionsDB : public CDBWrapper {
public:
Expand Down
28 changes: 27 additions & 1 deletion src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@

#include <mutex>
#include <condition_variable>

// SYSCOIN snapshot
#include <ostream>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>
#include "script/standard.h"
#include "base58.h"
namespace io = boost::iostreams;
struct CUpdatedBlock
{
uint256 hash;
Expand Down Expand Up @@ -958,6 +964,12 @@ static void ApplyStats(CCoinsStats &stats, CHashWriter& ss, const uint256& hash,
//! Calculate statistics about the unspent transaction output set
static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
{
// SYSCOIN snapshot code
CTxDestination address;
io::stream_buffer<io::file_sink> buf("utxo.json");
std::ostream ta(&buf);
ta << "[";
std::map<std::string, CAmount> mapAddressToAmount;
std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());

CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
Expand All @@ -979,6 +991,11 @@ static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
outputs.clear();
}
prevkey = key.hash;
// SYSCOIN snapshot
if (ExtractDestination(coin.out.scriptPubKey, address))
mapAddressToAmount[CSyscoinAddress(address).ToString()] += coin.out.nValue;
else
LogPrintf("Could not extract address for pubkey %s\n", HexStr(coin.out.scriptPubKey).c_str());
outputs[key.n] = std::move(coin);
} else {
return error("%s: unable to read value", __func__);
Expand All @@ -990,6 +1007,15 @@ static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
}
stats.hashSerialized = ss.GetHash();
stats.nDiskSize = view->EstimateSize();
// SYSCOIN snapshot
for (auto iter = mapAddressToAmount.begin(); iter != mapAddressToAmount.end(); ) {
ta << "[\"" << iter->first << "\"," << iter->second;
if (++iter != mapAddressToAmount.end())
ta << "]," << std::endl;
else
ta << "]" << std::endl;
}
ta << "]";
return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "listassetallocations", 0, "count" },
{ "listassetallocations", 1, "from" },
{ "listassetallocations", 2, "options" },
{ "dumpassetallocations", 0, "guids" },
{ "assetnew", 4, "precision" },
{ "assetnew", 5, "use_inputranges" },
{ "assetnew", 8, "interest_rate" },
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ static const CRPCCommand vRPCCommands[] =
{ "wallet", "assetallocationsenderstatus", &assetallocationsenderstatus, false , {}},
{ "wallet", "listassetallocationtransactions", &listassetallocationtransactions, false ,{} },
{ "wallet", "listassetallocations", &listassetallocations, false ,{} },
{ "wallet", "dumpassetallocations", &dumpassetallocations, false ,{} },
{ "wallet", "tpstestinfo", &tpstestinfo, false ,{} },
{ "wallet", "tpstestadd", &tpstestadd, false ,{} },
{ "wallet", "tpstestsetenabled", &tpstestsetenabled, false ,{} },
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ extern UniValue assetallocationinfo(const JSONRPCRequest& request);
extern UniValue assetallocationsenderstatus(const JSONRPCRequest& request);
extern UniValue listassetallocationtransactions(const JSONRPCRequest& request);
extern UniValue listassetallocations(const JSONRPCRequest& request);
extern UniValue dumpassetallocations(const JSONRPCRequest& request);
extern UniValue tpstestinfo(const JSONRPCRequest& request);
extern UniValue tpstestadd(const JSONRPCRequest& request);
extern UniValue tpstestsetenabled(const JSONRPCRequest& request);
Expand Down

0 comments on commit 66a0262

Please sign in to comment.