11 #include "validation.h" 13 #include <boost/thread.hpp> 15 static const char ASSET_FLAG =
'A';
16 static const char ASSET_ADDRESS_QUANTITY_FLAG =
'B';
17 static const char ADDRESS_ASSET_QUANTITY_FLAG =
'C';
18 static const char MY_ASSET_FLAG =
'M';
19 static const char BLOCK_ASSET_UNDO_DATA =
'U';
20 static const char MEMPOOL_REISSUED_TX =
'Z';
22 static size_t MAX_DATABASE_RESULTS = 50000;
30 return Write(std::make_pair(ASSET_FLAG, asset.
strName), data);
35 return Write(std::make_pair(ASSET_ADDRESS_QUANTITY_FLAG, std::make_pair(assetName, address)), quantity);
39 return Write(std::make_pair(ADDRESS_ASSET_QUANTITY_FLAG, std::make_pair(address, assetName)), quantity);
46 bool ret =
Read(std::make_pair(ASSET_FLAG, strName), data);
59 return Read(std::make_pair(ASSET_ADDRESS_QUANTITY_FLAG, std::make_pair(assetName, address)), quantity);
63 return Read(std::make_pair(ADDRESS_ASSET_QUANTITY_FLAG, std::make_pair(address, assetName)), quantity);
68 return Erase(std::make_pair(ASSET_FLAG, assetName));
73 return Erase(std::make_pair(MY_ASSET_FLAG, assetName));
77 return Erase(std::make_pair(ASSET_ADDRESS_QUANTITY_FLAG, std::make_pair(assetName, address)));
81 return Erase(std::make_pair(ADDRESS_ASSET_QUANTITY_FLAG, std::make_pair(address, assetName)));
88 return Write(std::make_pair(BLOCK_ASSET_UNDO_DATA, blockhash), assetUndoData);
94 if (
Exists(std::make_pair(BLOCK_ASSET_UNDO_DATA, blockhash)))
95 return Read(std::make_pair(BLOCK_ASSET_UNDO_DATA, blockhash), assetUndoData);
114 mapReissuedTx.insert(std::make_pair(pair.second, pair.first));
121 std::unique_ptr<CDBIterator> pcursor(
NewIterator());
123 pcursor->Seek(std::make_pair(ASSET_FLAG, std::string()));
126 while (pcursor->Valid()) {
127 boost::this_thread::interruption_point();
128 std::pair<char, std::string> key;
129 if (pcursor->GetKey(key) && key.first == ASSET_FLAG) {
131 if (pcursor->GetValue(data)) {
140 return error(
"%s: failed to read asset", __func__);
149 std::unique_ptr<CDBIterator> pcursor3(
NewIterator());
150 pcursor3->Seek(std::make_pair(ASSET_ADDRESS_QUANTITY_FLAG, std::make_pair(std::string(), std::string())));
153 while (pcursor3->Valid()) {
154 boost::this_thread::interruption_point();
155 std::pair<char, std::pair<std::string, std::string> > key;
156 if (pcursor3->GetKey(key) && key.first == ASSET_ADDRESS_QUANTITY_FLAG) {
158 if (pcursor3->GetValue(value)) {
160 std::make_pair(std::make_pair(key.second.first, key.second.second), value));
165 return error(
"%s: failed to read my address quantity from database", __func__);
176 bool CAssetsDB::AssetDir(std::vector<CDatabasedAssetData>& assets,
const std::string filter,
const size_t count,
const long start)
180 std::unique_ptr<CDBIterator> pcursor(
NewIterator());
181 pcursor->Seek(std::make_pair(ASSET_FLAG, std::string()));
184 bool wildcard =
prefix.back() ==
'*';
195 while (pcursor->Valid()) {
196 boost::this_thread::interruption_point();
198 std::pair<char, std::string> key;
199 if (pcursor->GetKey(key) && key.first == ASSET_FLAG) {
201 (wildcard && key.second.find(
prefix) == 0) ||
202 (!wildcard && key.second ==
prefix)) {
208 skip = table_size + start;
209 pcursor->SeekToFirst();
217 while (pcursor->Valid() && loaded < count) {
218 boost::this_thread::interruption_point();
220 std::pair<char, std::string> key;
221 if (pcursor->GetKey(key) && key.first == ASSET_FLAG) {
223 (wildcard && key.second.find(
prefix) == 0) ||
224 (!wildcard && key.second ==
prefix)) {
230 if (pcursor->GetValue(data)) {
231 assets.push_back(data);
234 return error(
"%s: failed to read asset", __func__);
247 bool CAssetsDB::AddressDir(std::vector<std::pair<std::string, CAmount> >& vecAssetAmount,
int& totalEntries,
const bool& fGetTotal,
const std::string& address,
const size_t count,
const long start)
251 std::unique_ptr<CDBIterator> pcursor(
NewIterator());
252 pcursor->Seek(std::make_pair(ADDRESS_ASSET_QUANTITY_FLAG, std::make_pair(address, std::string())));
256 while (pcursor->Valid()) {
257 boost::this_thread::interruption_point();
259 std::pair<char, std::pair<std::string, std::string> > key;
260 if (pcursor->GetKey(key) && key.first == ADDRESS_ASSET_QUANTITY_FLAG && key.second.first == address) {
275 while (pcursor->Valid()) {
276 boost::this_thread::interruption_point();
278 std::pair<char, std::pair<std::string, std::string> > key;
279 if (pcursor->GetKey(key) && key.first == ADDRESS_ASSET_QUANTITY_FLAG && key.second.first == address) {
284 skip = table_size + start;
285 pcursor->SeekToFirst();
293 while (pcursor->Valid() && loaded < count && loaded < MAX_DATABASE_RESULTS) {
294 boost::this_thread::interruption_point();
296 std::pair<char, std::pair<std::string, std::string> > key;
297 if (pcursor->GetKey(key) && key.first == ADDRESS_ASSET_QUANTITY_FLAG && key.second.first == address) {
303 if (pcursor->GetValue(amount)) {
304 vecAssetAmount.emplace_back(std::make_pair(key.second.second, amount));
307 return error(
"%s: failed to Address Asset Quanity", __func__);
320 bool CAssetsDB::AssetAddressDir(std::vector<std::pair<std::string, CAmount> >& vecAddressAmount,
int& totalEntries,
const bool& fGetTotal,
const std::string& assetName,
const size_t count,
const long start)
324 std::unique_ptr<CDBIterator> pcursor(
NewIterator());
325 pcursor->Seek(std::make_pair(ASSET_ADDRESS_QUANTITY_FLAG, std::make_pair(assetName, std::string())));
329 while (pcursor->Valid()) {
330 boost::this_thread::interruption_point();
332 std::pair<char, std::pair<std::string, std::string> > key;
333 if (pcursor->GetKey(key) && key.first == ASSET_ADDRESS_QUANTITY_FLAG && key.second.first == assetName) {
348 while (pcursor->Valid()) {
349 boost::this_thread::interruption_point();
351 std::pair<char, std::pair<std::string, std::string> > key;
352 if (pcursor->GetKey(key) && key.first == ASSET_ADDRESS_QUANTITY_FLAG && key.second.first == assetName) {
357 skip = table_size + start;
358 pcursor->SeekToFirst();
365 while (pcursor->Valid() && loaded < count && loaded < MAX_DATABASE_RESULTS) {
366 boost::this_thread::interruption_point();
368 std::pair<char, std::pair<std::string, std::string> > key;
369 if (pcursor->GetKey(key) && key.first == ASSET_ADDRESS_QUANTITY_FLAG && key.second.first == assetName) {
375 if (pcursor->GetValue(amount)) {
376 vecAddressAmount.emplace_back(std::make_pair(key.second.second, amount));
379 return error(
"%s: failed to Asset Address Quanity", __func__);
bool Exists(const K &key) const
bool WriteAssetAddressQuantity(const std::string &assetName, const std::string &address, const CAmount &quantity)
bool WriteReissuedMempoolState()
bool WriteBlockUndoAssetData(const uint256 &blockhash, const std::vector< std::pair< std::string, CBlockAssetUndo > > &assetUndoData)
bool ReadBlockUndoAssetData(const uint256 &blockhash, std::vector< std::pair< std::string, CBlockAssetUndo > > &assetUndoData)
bool EraseMyAssetData(const std::string &assetName)
bool ReadAssetAddressQuantity(const std::string &assetName, const std::string &address, CAmount &quantity)
bool ReadAddressAssetQuantity(const std::string &address, const std::string &assetName, CAmount &quantity)
CDBIterator * NewIterator()
bool EraseAddressAssetQuantity(const std::string &address, const std::string &assetName)
int64_t CAmount
Amount in corbies (Can be negative)
bool AssetDir(std::vector< CDatabasedAssetData > &assets, const std::string filter, const size_t count, const long start)
bool WriteAddressAssetQuantity(const std::string &address, const std::string &assetName, const CAmount &quantity)
bool Erase(const K &key, bool fSync=false)
bool EraseAssetAddressQuantity(const std::string &assetName, const std::string &address)
std::map< std::string, uint256 > mapReissuedAssets
bool ReadAssetData(const std::string &strName, CNewAsset &asset, int &nHeight, uint256 &blockHash)
bool Read(const K &key, V &value) const
bool EraseAssetData(const std::string &assetName)
void FlushStateToDisk()
Flush all state, indexes and buffers to disk.
std::map< std::pair< std::string, std::string >, CAmount > mapAssetsAddressAmount
bool WriteAssetData(const CNewAsset &asset, const int nHeight, const uint256 &blockHash)
#define MAX_CACHE_ASSETS_SIZE
CAssetsCache * passets
Global variable that point to the active assets (protected by cs_main)
bool Write(const K &key, const V &value, bool fSync=false)
std::map< uint256, std::string > mapReissuedTx
bool ReadReissuedMempoolState()
bool AddressDir(std::vector< std::pair< std::string, CAmount > > &vecAssetAmount, int &totalEntries, const bool &fGetTotal, const std::string &address, const size_t count, const long start)
bool error(const char *fmt, const Args &... args)
const fs::path & GetDataDir(bool fNetSpecific)
CLRUCache< std::string, CDatabasedAssetData > * passetsCache
Global variable that point to the assets metadata LRU Cache (protected by cs_main) ...
bool AssetAddressDir(std::vector< std::pair< std::string, CAmount > > &vecAddressAmount, int &totalEntries, const bool &fGetTotal, const std::string &assetName, const size_t count, const long start)
CAssetsDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)