-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathassetsnapshotdb.cpp
151 lines (121 loc) · 5.5 KB
/
assetsnapshotdb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2019 The Raven Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "assetsnapshotdb.h"
#include "validation.h"
#include "base58.h"
#include <boost/algorithm/string.hpp>
#include <boost/thread.hpp>
static const char SNAPSHOTCHECK_FLAG = 'C'; // Snapshot Check
CAssetSnapshotDBEntry::CAssetSnapshotDBEntry()
{
SetNull();
}
CAssetSnapshotDBEntry::CAssetSnapshotDBEntry(
const std::string & p_assetName, int p_snapshotHeight,
const std::set<std::pair<std::string, CAmount>> & p_ownersAndAmounts
)
{
SetNull();
height = p_snapshotHeight;
assetName = p_assetName;
for (auto const & currPair : p_ownersAndAmounts) {
ownersAndAmounts.insert(currPair);
}
heightAndName = std::to_string(height) + assetName;
}
CAssetSnapshotDB::CAssetSnapshotDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(GetDataDir() / "rewards" / "assetsnapshot", nCacheSize, fMemory, fWipe) {
}
bool CAssetSnapshotDB::AddAssetOwnershipSnapshot(
const std::string & p_assetName, int p_height)
{
LogPrint(BCLog::REWARDS, "AddAssetOwnershipSnapshot: Adding snapshot for '%s' at height %d\n",
p_assetName.c_str(), p_height);
// Retrieve ownership interest for the asset at this height
if (passetsdb == nullptr) {
LogPrint(BCLog::REWARDS, "AddAssetOwnershipSnapshot: Invalid assets DB!\n");
return false;
}
std::set<std::pair<std::string, CAmount>> ownersAndAmounts;
std::vector<std::pair<std::string, CAmount>> tempOwnersAndAmounts;
int totalEntryCount;
if (!passetsdb->AssetAddressDir(tempOwnersAndAmounts, totalEntryCount, true, p_assetName, INT_MAX, 0)) {
LogPrint(BCLog::REWARDS, "AddAssetOwnershipSnapshot: Failed to retrieve assets directory for '%s'\n", p_assetName.c_str());
return false;
}
// Retrieve all of the addresses/amounts in batches
const int MAX_RETRIEVAL_COUNT = 100;
bool errorsOccurred = false;
for (int retrievalOffset = 0; retrievalOffset < totalEntryCount; retrievalOffset += MAX_RETRIEVAL_COUNT) {
// Retrieve the specified segment of addresses
if (!passetsdb->AssetAddressDir(tempOwnersAndAmounts, totalEntryCount, false, p_assetName, MAX_RETRIEVAL_COUNT, retrievalOffset)) {
LogPrint(BCLog::REWARDS, "AddAssetOwnershipSnapshot: Failed to retrieve assets directory for '%s'\n", p_assetName.c_str());
errorsOccurred = true;
break;
}
// Verify that some addresses were returned
if (tempOwnersAndAmounts.size() == 0) {
LogPrint(BCLog::REWARDS, "AddAssetOwnershipSnapshot: No addresses were retrieved.\n");
continue;
}
// Move these into the main set
for (auto const & currPair : tempOwnersAndAmounts) {
// Verify that the address is valid
CTxDestination dest = DecodeDestination(currPair.first);
if (IsValidDestination(dest)) {
ownersAndAmounts.insert(currPair);
}
else {
LogPrint(BCLog::REWARDS, "AddAssetOwnershipSnapshot: Address '%s' is invalid.\n", currPair.first.c_str());
}
}
tempOwnersAndAmounts.clear();
}
if (errorsOccurred) {
LogPrint(BCLog::REWARDS, "AddAssetOwnershipSnapshot: Errors occurred while acquiring ownership info for asset '%s'.\n", p_assetName.c_str());
return false;
}
if (ownersAndAmounts.size() == 0) {
LogPrint(BCLog::REWARDS, "AddAssetOwnershipSnapshot: No owners exist for asset '%s'.\n", p_assetName.c_str());
return false;
}
// Write the snapshot to the database. We don't care if we overwrite, because it should be identical.
CAssetSnapshotDBEntry snapshotEntry(p_assetName, p_height, ownersAndAmounts);
if (Write(std::make_pair(SNAPSHOTCHECK_FLAG, snapshotEntry.heightAndName), snapshotEntry)) {
LogPrint(BCLog::REWARDS, "AddAssetOwnershipSnapshot: Successfully added snapshot for '%s' at height %d (ownerCount = %d).\n",
p_assetName.c_str(), p_height, ownersAndAmounts.size());
return true;
}
return false;
}
bool CAssetSnapshotDB::RetrieveOwnershipSnapshot(
const std::string & p_assetName, int p_height,
CAssetSnapshotDBEntry & p_snapshotEntry)
{
// Load up the snapshot entries at this height
std::string heightAndName = std::to_string(p_height) + p_assetName;
LogPrint(BCLog::REWARDS, "%s : Attempting to retrieve snapshot: heightAndName='%s'\n",
__func__,
heightAndName.c_str());
bool succeeded = Read(std::make_pair(SNAPSHOTCHECK_FLAG, heightAndName), p_snapshotEntry);
LogPrint(BCLog::REWARDS, "%s : Retrieval of snapshot for '%s' %s!\n",
__func__,
heightAndName.c_str(),
succeeded ? "succeeded" : "failed");
return succeeded;
}
bool CAssetSnapshotDB::RemoveOwnershipSnapshot(
const std::string & p_assetName, int p_height)
{
// Load up the snapshot entries at this height
std::string heightAndName = std::to_string(p_height) + p_assetName;
LogPrint(BCLog::REWARDS, "%s : Attempting to remove snapshot: heightAndName='%s'\n",
__func__,
heightAndName.c_str());
bool succeeded = Erase(std::make_pair(SNAPSHOTCHECK_FLAG, heightAndName), true);
LogPrint(BCLog::REWARDS, "%s : Removal of snapshot for '%s' %s!\n",
__func__,
heightAndName.c_str(),
succeeded ? "succeeded" : "failed");
return succeeded;
}