Repository navigation
Expand file tree
/
Copy pathMicroCore.cpp
More file actions
executable file
·242 lines (198 loc) · 5.06 KB
/
Copy pathMicroCore.cpp
File metadata and controls
executable file
·242 lines (198 loc) · 5.06 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// Created by mwo on 5/11/15.
//
#include "MicroCore.h"
namespace xmreg
{
/**
* The constructor is interesting, as
* m_mempool and m_blockchain_storage depend
* on each other.
*
* So basically m_mempool is initialized with
* reference to Blockchain (i.e., Blockchain&)
* and m_blockchain_storage is initialized with
* reference to m_mempool (i.e., tx_memory_pool&)
*
* The same is done in cryptonode::core.
*/
MicroCore::MicroCore():
m_mempool(core_storage),
core_storage(m_mempool, m_service_node_list),
m_service_node_list(core_storage),
m_device {&hw::get_device("default")}
{
}
/**
* Initialized the MicroCore object.
*
* Create BlockchainLMDB on the heap.
* Open database files located in blockchain_path.
* Initialize m_blockchain_storage with the BlockchainLMDB object.
*/
bool
MicroCore::init(const string& _blockchain_path, network_type nt)
{
blockchain_path = _blockchain_path;
nettype = nt;
std::unique_ptr<BlockchainDB> db = std::make_unique<BlockchainLMDB>();
try
{
// try opening lmdb database files
db->open(blockchain_path, DBF_RDONLY);
}
catch (const std::exception& e)
{
cerr << "Error opening database: " << e.what();
return false;
}
// check if the blockchain database
// is successful opened
if(!db->is_open())
return false;
// initialize Blockchain object to manage
// the database.
if (!core_storage.init(db.release(), nettype))
{
cerr << "Error opening database: core_storage->init(db, nettype)\n" ;
return false;
}
if (!m_mempool.init())
{
cerr << "Error opening database: m_mempool.init()\n" ;
return false;
}
initialization_succeeded = true;
return true;
}
/**
* Get m_blockchain_storage.
* Initialize m_blockchain_storage with the BlockchainLMDB object.
*/
Blockchain const&
MicroCore::get_core() const
{
return core_storage;
}
tx_memory_pool const&
MicroCore::get_mempool() const
{
return m_mempool;
}
bool
MicroCore::get_block_from_height(uint64_t height, block& blk) const
{
try
{
blk = core_storage.get_db().get_block_from_height(height);
}
catch (const exception& e)
{
cerr << e.what() << endl;
return false;
}
return true;
}
bool
MicroCore::get_block_complete_entry(block const& b, block_complete_entry& bce)
{
bce.block = cryptonote::block_to_blob(b);
for (const auto &tx_hash: b.tx_hashes)
{
transaction tx;
if (!get_tx(tx_hash, tx))
return false;
cryptonote::blobdata txblob = tx_to_blob(tx);
bce.txs.push_back(txblob);
}
return true;
}
bool
MicroCore::get_tx(crypto::hash const& tx_hash, transaction& tx) const
{
if (core_storage.have_tx(tx_hash))
{
// get transaction with given hash
tx = core_storage.get_db().get_tx(tx_hash);
return true;
}
return true;
}
bool
MicroCore::get_output_histogram(
vector<uint64_t> const& amounts,
uint64_t min_count,
histogram_map& histogram,
bool unlocked,
uint64_t recent_cutoff) const
{
try
{
histogram = core_storage.get_output_histogram(
amounts,
unlocked,
recent_cutoff,
min_count);
}
catch (std::exception const& e)
{
cerr << e.what() << endl;
return false;
}
return true;
}
bool
MicroCore::get_output_histogram(
COMMAND_RPC_GET_OUTPUT_HISTOGRAM::request const& req,
COMMAND_RPC_GET_OUTPUT_HISTOGRAM::response& res) const
{
// based on bool core_rpc_server::on_get_output_histogram(const ...
MicroCore::histogram_map histogram;
if (!get_output_histogram(req.amounts,
req.min_count,
histogram,
req.unlocked,
req.recent_cutoff))
{
return false;
}
res.histogram.clear();
res.histogram.reserve(histogram.size());
for (auto const& i: histogram)
{
if (std::get<0>(i.second)
>= req.min_count
&& (std::get<0>(i.second) <= req.max_count
|| req.max_count == 0))
res.histogram.push_back(
COMMAND_RPC_GET_OUTPUT_HISTOGRAM::entry(
i.first,
std::get<0>(i.second),
std::get<1>(i.second),
std::get<2>(i.second)));
}
res.status = CORE_RPC_STATUS_OK;
return true;
}
hw::device* const
MicroCore::get_device() const
{
return m_device;
}
bool
MicroCore::init_success() const
{
return initialization_succeeded;
}
MicroCore::~MicroCore()
{
//cout << "\n\nMicroCore::~MicroCore()\n\n";
if (initialization_succeeded)
{
//core_storage.get_db().safesyncmode(true);
if (core_storage.get_db().is_open())
core_storage.get_db().close();
//cout << "\n\n core_storage.get_db().close();;\n\n";
}
}
}