Skip to content

Commit

Permalink
assets address index
Browse files Browse the repository at this point in the history
  • Loading branch information
nandofw committed Sep 11, 2023
1 parent d486544 commit e3ebc62
Show file tree
Hide file tree
Showing 11 changed files with 903 additions and 163 deletions.
35 changes: 30 additions & 5 deletions src/addressindex.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,42 @@ struct CMempoolAddressDelta {
struct CMempoolAddressDeltaKey {
int type;
uint160 addressBytes;
std::string asset;
uint256 txhash;
unsigned int index;
int spending;

CMempoolAddressDeltaKey(int addressType, uint160 addressHash, uint256 hash, unsigned int i, int s) {
type = addressType;
addressBytes = addressHash;
asset = "";
txhash = hash;
index = i;
spending = s;
}

CMempoolAddressDeltaKey(int addressType, uint160 addressHash, std::string assetId, uint256 hash, unsigned int i, int s) {
type = addressType;
addressBytes = addressHash;
assetId = assetId;
txhash = hash;
index = i;
spending = s;
}

CMempoolAddressDeltaKey(int addressType, uint160 addressHash, std::string assetId) {
type = addressType;
addressBytes = addressHash;
asset = assetId;
txhash.SetNull();
index = 0;
spending = 0;
}

CMempoolAddressDeltaKey(int addressType, uint160 addressHash) {
type = addressType;
addressBytes = addressHash;
asset = "";
txhash.SetNull();
index = 0;
spending = 0;
Expand All @@ -58,14 +79,18 @@ struct CMempoolAddressDeltaKeyCompare {
bool operator()(const CMempoolAddressDeltaKey &a, const CMempoolAddressDeltaKey &b) const {
if (a.type == b.type) {
if (a.addressBytes == b.addressBytes) {
if (a.txhash == b.txhash) {
if (a.index == b.index) {
return a.spending < b.spending;
if (a.asset == b.asset) {
if (a.txhash == b.txhash) {
if (a.index == b.index) {
return a.spending < b.spending;
} else {
return a.index < b.index;
}
} else {
return a.index < b.index;
return a.txhash < b.txhash;
}
} else {
return a.txhash < b.txhash;
return a.asset < b.asset;
}
} else {
return a.addressBytes < b.addressBytes;
Expand Down
43 changes: 41 additions & 2 deletions src/assets/assetsdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <consensus/params.h>
#include <script/ismine.h>
#include <tinyformat.h>
//#include <util.h>

#include <boost/thread.hpp>

Expand Down Expand Up @@ -123,4 +122,44 @@ bool CAssetsDB::LoadAssets() {
}

return true;
}
}

bool CAssetsDB::GetListAssets(std::vector<CDatabaseAssetData>& assets, const size_t count, const long start) {
::ChainstateActive().ForceFlushStateToDisk();

std::unique_ptr<CDBIterator> pcursor(NewIterator());
pcursor->Seek(std::make_pair(ASSET_FLAG, std::string()));

size_t skip = 0;
if (start >= 0) {
skip = start;
}

size_t loaded = 0;
size_t offset = 0;

// Load assets
while (pcursor->Valid() && loaded < count) {
boost::this_thread::interruption_point();

std::pair<char, std::string> key;
if (pcursor->GetKey(key) && key.first == ASSET_FLAG) {
if (offset < skip) {
offset += 1;
} else {
CDatabaseAssetData data;
if (pcursor->GetValue(data)) {
assets.push_back(data);
loaded += 1;
} else {
return error("%s: failed to read asset", __func__);
}
}
pcursor->Next();
} else {
break;
}
}

return true;
}
1 change: 1 addition & 0 deletions src/assets/assetsdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class CAssetsDB : public CDBWrapper {

// Helper functions
bool LoadAssets();
bool GetListAssets(std::vector<CDatabaseAssetData>& assets, const size_t count, const long start);
};


Expand Down
53 changes: 51 additions & 2 deletions src/indices/spent_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,19 @@ struct CTimestampIndexKey {
struct CAddressUnspentKey {
unsigned int type;
uint160 hashBytes;
std::string asset;
uint256 txhash;
size_t index;

size_t GetSerializeSize(int nType, int nVersion) const {
return 57;
return 57 + asset.size();
}

template<typename Stream>
void Serialize(Stream &s) const {
ser_writedata8(s, type);
hashBytes.Serialize(s);
::Serialize(s, asset);
txhash.Serialize(s);
ser_writedata32(s, index);
}
Expand All @@ -155,13 +157,23 @@ struct CAddressUnspentKey {
void Unserialize(Stream &s) {
type = ser_readdata8(s);
hashBytes.Unserialize(s);
::Unserialize(s, asset);
txhash.Unserialize(s);
index = ser_readdata32(s);
}

CAddressUnspentKey(unsigned int addressType, uint160 addressHash, uint256 txid, size_t indexValue) {
type = addressType;
hashBytes = addressHash;
asset = "RTM";
txhash = txid;
index = indexValue;
}

CAddressUnspentKey(unsigned int addressType, uint160 addressHash, std::string assetId, uint256 txid, size_t indexValue) {
type = addressType;
hashBytes = addressHash;
asset = assetId;
txhash = txid;
index = indexValue;
}
Expand All @@ -173,6 +185,7 @@ struct CAddressUnspentKey {
void SetNull() {
type = 0;
hashBytes.SetNull();
asset = "";
txhash.SetNull();
index = 0;
}
Expand All @@ -181,31 +194,50 @@ struct CAddressUnspentKey {
struct CAddressUnspentValue {
CAmount satoshis;
CScript script;
std::string asset;
bool isUnique;
uint64_t uniqueId;
int blockHeight;
int fSpendableHeight;
int64_t fSpendableTime;

SERIALIZE_METHODS(CAddressUnspentValue, obj
)
{
READWRITE(obj.satoshis, obj.script, obj.blockHeight, obj.fSpendableHeight, obj.fSpendableTime);
READWRITE(obj.satoshis, obj.script, obj.asset, obj.isUnique, obj.uniqueId, obj.blockHeight, obj.fSpendableHeight, obj.fSpendableTime);
}

CAddressUnspentValue(CAmount sats, CScript scriptPubKey, int height, int spendableHeight, int spendableTime) {
satoshis = sats;
script = scriptPubKey;
asset = "RTM";
isUnique = false;
uniqueId = 0;
blockHeight = height;
fSpendableHeight = spendableHeight;
fSpendableTime = spendableTime;
}

CAddressUnspentValue(CAmount sats, CScript scriptPubKey, std::string assetId, bool unique, uint64_t uniqueId, int height, int spendableHeight, int spendableTime) {
satoshis = sats;
script = scriptPubKey;
asset = assetId;
isUnique = unique;
uniqueId = uniqueId;
blockHeight = height;
fSpendableHeight = spendableHeight;
fSpendableTime = spendableTime;
}
CAddressUnspentValue() {
SetNull();
}

void SetNull() {
satoshis = -1;
script.clear();
asset = "";
isUnique = false;
uniqueId = 0;
blockHeight = 0;
fSpendableHeight = 0;
fSpendableTime = 0;
Expand All @@ -219,6 +251,7 @@ struct CAddressUnspentValue {
struct CAddressIndexKey {
unsigned int type;
uint160 hashBytes;
std::string asset;
int blockHeight;
unsigned int txindex;
uint256 txhash;
Expand All @@ -233,6 +266,7 @@ struct CAddressIndexKey {
void Serialize(Stream &s) const {
ser_writedata8(s, type);
hashBytes.Serialize(s);
::Serialize(s, asset);
// Heights are stored big-endian for key sorting in LevelDB
ser_writedata32be(s, blockHeight);
ser_writedata32be(s, txindex);
Expand All @@ -246,6 +280,7 @@ struct CAddressIndexKey {
void Unserialize(Stream &s) {
type = ser_readdata8(s);
hashBytes.Unserialize(s);
::Unserialize(s, asset);
blockHeight = ser_readdata32be(s);
txindex = ser_readdata32be(s);
txhash.Unserialize(s);
Expand All @@ -258,6 +293,19 @@ struct CAddressIndexKey {
size_t indexValue, bool isSpending) {
type = addressType;
hashBytes = addressHash;
asset = "RTM";
blockHeight = height;
txindex = blockindex;
txhash = txid;
index = indexValue;
spending = isSpending;
}

CAddressIndexKey(unsigned int addressType, uint160 addressHash, std::string assetId, int height, int blockindex, uint256 txid,
size_t indexValue, bool isSpending) {
type = addressType;
hashBytes = addressHash;
asset = assetId;
blockHeight = height;
txindex = blockindex;
txhash = txid;
Expand All @@ -272,6 +320,7 @@ struct CAddressIndexKey {
void SetNull() {
type = 0;
hashBytes.SetNull();
asset = "";
blockHeight = 0;
txindex = 0;
txhash.SetNull();
Expand Down
14 changes: 7 additions & 7 deletions src/qt/assetsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ void AssetsDialog::Asset_details_clicked() {
if (passetsCache->GetAssetMetaData(assetId, asset)) {
UniValue json(UniValue::VOBJ);
asset.ToJson(json);

// Title of popup window
QString strWindowtitle = tr("Details for asset: %1").arg(
QString::fromStdString(asset.name));
QString strText = QString::fromStdString(json.write(2));

QMessageBox::information(this, strWindowtitle, strText);
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Information);
msgBox.setWindowTitle(tr("Details for asset: %1").arg(QString::fromStdString(asset.name)));
msgBox.setText(QString::fromStdString(json.write(2)));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ static const CRPCConvertParam vRPCConvertParams[] =
{"listunspentassets", 2, "addresses"},
{"listunspentassets", 3, "include_unsafe"},
{"listunspentassets", 4, "query_options"},
{"listassets", 0, "verbose"},
{"listassets", 1, "count"},
{"listassets", 2, "start"},
};

class CRPCConvertTable {
Expand Down
Loading

0 comments on commit e3ebc62

Please sign in to comment.