-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathepicchain_system.cpp
More file actions
373 lines (326 loc) · 10.3 KB
/
epicchain_system.cpp
File metadata and controls
373 lines (326 loc) · 10.3 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <epicchain/node/neo_system.h>
#include <epicchain/persistence/leveldb_store.h>
#include <epicchain/smartcontract/native/gas_token.h>
#include <epicchain/smartcontract/native/neo_token.h>
#include <epicchain/smartcontract/native/policy_contract.h>
#include <epicchain/smartcontract/native/role_management.h>
#include <epicchain/smartcontract/native/contract_management.h>
#include <epicchain/logging/logger.h>
#include <chrono>
#include <stdexcept>
namespace epicchain::node
{
NeoSystem::NeoSystem(std::shared_ptr<ProtocolSettings> protocolSettings,
const std::string& storageEngine,
const std::string& storePath)
: protocolSettings_(protocolSettings)
, running_(false)
, storageEngine_(storageEngine)
, storePath_(storePath)
, nextCallbackId_(1)
{
if (!protocolSettings_)
{
throw std::invalid_argument("Protocol settings cannot be null");
}
}
NeoSystem::~NeoSystem()
{
Stop();
}
bool NeoSystem::Start()
{
if (running_)
{
return true;
}
try
{
// Initialize storage
if (!InitializeStorage())
{
return false;
}
// Initialize blockchain
if (!InitializeBlockchain())
{
CleanupStorage();
return false;
}
// Initialize memory pool
if (!InitializeMemoryPool())
{
CleanupStorage();
return false;
}
// Initialize native contracts
if (!InitializeNativeContracts())
{
CleanupStorage();
return false;
}
// Initialize networking
if (!InitializeNetworking())
{
CleanupNativeContracts();
CleanupStorage();
return false;
}
running_ = true;
return true;
}
catch (const std::exception& e)
{
// Cleanup on failure
CleanupNetworking();
CleanupNativeContracts();
CleanupStorage();
return false;
}
}
void NeoSystem::Stop()
{
if (!running_)
{
return;
}
running_ = false;
// Cleanup in reverse order of initialization
CleanupNetworking();
CleanupNativeContracts();
CleanupStorage();
// Clear callbacks
std::lock_guard<std::mutex> lock(callbackMutex_);
blockPersistCallbacks_.clear();
}
bool NeoSystem::IsRunning() const
{
return running_;
}
std::shared_ptr<ProtocolSettings> NeoSystem::GetProtocolSettings() const
{
return protocolSettings_;
}
std::shared_ptr<ledger::Blockchain> NeoSystem::GetBlockchain() const
{
return blockchain_;
}
std::shared_ptr<ledger::MemoryPool> NeoSystem::GetMemoryPool() const
{
return memoryPool_;
}
std::shared_ptr<network::P2PServer> NeoSystem::GetP2PServer() const
{
return p2pServer_;
}
std::shared_ptr<persistence::DataCache> NeoSystem::GetDataCache() const
{
return dataCache_;
}
std::unique_ptr<smartcontract::ApplicationEngine> NeoSystem::CreateApplicationEngine(
smartcontract::TriggerType trigger,
const io::ISerializable* container,
const ledger::Block* persistingBlock,
int64_t gas)
{
return std::make_unique<smartcontract::ApplicationEngine>(
trigger, container, dataCache_, persistingBlock, gas);
}
void NeoSystem::RegisterNativeContract(std::shared_ptr<smartcontract::native::NativeContract> contract)
{
if (!contract)
{
return;
}
nativeContracts_.push_back(contract);
// nativeContractMap_[contract->GetHash()] = contract.get();
}
smartcontract::native::NativeContract* NeoSystem::GetNativeContract(const io::UInt160& hash) const
{
auto it = nativeContractMap_.find(hash);
return (it != nativeContractMap_.end()) ? it->second : nullptr;
}
std::vector<std::shared_ptr<smartcontract::native::NativeContract>> NeoSystem::GetNativeContracts() const
{
return nativeContracts_;
}
uint32_t NeoSystem::GetCurrentBlockHeight() const
{
return blockchain_ ? blockchain_->GetHeight() : 0;
}
io::UInt256 NeoSystem::GetCurrentBlockHash() const
{
return blockchain_ ? blockchain_->GetCurrentBlockHash() : io::UInt256();
}
bool NeoSystem::RelayTransaction(std::shared_ptr<ledger::Transaction> transaction)
{
if (!transaction || !memoryPool_ || !p2pServer_)
{
return false;
}
// Add to memory pool
// auto result = memoryPool_->TryAdd(transaction);
// if (result != ledger::VerifyResult::Succeed)
// {
// return false;
// }
// Broadcast to network
// p2pServer_->BroadcastTransaction(transaction);
return true;
}
bool NeoSystem::RelayBlock(std::shared_ptr<ledger::Block> block)
{
if (!block || !blockchain_ || !p2pServer_)
{
return false;
}
// Add to blockchain
// auto result = blockchain_->OnNewBlock(*block);
// if (result != ledger::VerifyResult::Succeed)
// {
// return false;
// }
// Broadcast to network
// p2pServer_->BroadcastBlock(block);
return true;
}
int32_t NeoSystem::RegisterBlockPersistCallback(std::function<void(std::shared_ptr<ledger::Block>)> callback)
{
std::lock_guard<std::mutex> lock(callbackMutex_);
int32_t id = nextCallbackId_++;
blockPersistCallbacks_[id] = callback;
return id;
}
void NeoSystem::UnregisterBlockPersistCallback(int32_t callbackId)
{
std::lock_guard<std::mutex> lock(callbackMutex_);
blockPersistCallbacks_.erase(callbackId);
}
std::string NeoSystem::GetSystemStats() const
{
// Return JSON-formatted system statistics
return R"({
"blockchain": {
"height": )" + std::to_string(GetCurrentBlockHeight()) + R"(,
"hash": ")" + GetCurrentBlockHash().ToString() + R"("
},
"memoryPool": {
"count": )" + std::to_string(memoryPool_ ? memoryPool_->GetTransactionCount() : 0) + R"(
},
"network": {
"connectedPeers": )" + std::to_string(p2pServer_ ? p2pServer_->GetConnectedPeersCount() : 0) + R"(
},
"system": {
"running": )" + (running_ ? "true" : "false") + R"(,
"storageEngine": ")" + storageEngine_ + R"("
}
})";
}
bool NeoSystem::InitializeStorage()
{
try
{
if (storageEngine_ == "LevelDB")
{
auto store = std::make_shared<persistence::LevelDBStore>(storePath_);
if (!store->Start())
{
return false;
}
dataCache_ = std::static_pointer_cast<persistence::DataCache>(store);
}
else
{
throw std::runtime_error("Unsupported storage engine: " + storageEngine_);
}
return true;
}
catch (const std::exception& e)
{
return false;
}
}
bool NeoSystem::InitializeBlockchain()
{
try
{
blockchain_ = std::make_shared<ledger::Blockchain>(dataCache_);
return true;
}
catch (const std::exception& e)
{
return false;
}
}
bool NeoSystem::InitializeMemoryPool()
{
try
{
memoryPool_ = std::make_shared<ledger::MemoryPool>(protocolSettings_);
return true;
}
catch (const std::exception& e)
{
return false;
}
}
bool NeoSystem::InitializeNetworking()
{
try
{
// Create P2P server
network::IPEndPoint endpoint(network::IPAddress::Any(), protocolSettings_->GetP2PPort());
p2pServer_ = std::make_shared<network::P2PServer>(
// ioContext, endpoint, userAgent, startHeight
);
return true;
}
catch (const std::exception& e)
{
return false;
}
}
bool NeoSystem::InitializeNativeContracts()
{
try
{
// Initialize all native contracts
auto gasToken = smartcontract::native::GasToken::GetInstance();
auto EpicChain = smartcontract::native::EpicChain::GetInstance();
auto policyContract = smartcontract::native::PolicyContract::GetInstance();
auto roleManagement = smartcontract::native::RoleManagement::GetInstance();
auto contractManagement = smartcontract::native::ContractManagement::GetInstance();
RegisterNativeContract(gasToken);
RegisterNativeContract(EpicChain);
RegisterNativeContract(policyContract);
RegisterNativeContract(roleManagement);
RegisterNativeContract(contractManagement);
return true;
}
catch (const std::exception& e)
{
return false;
}
}
void NeoSystem::CleanupStorage()
{
if (dataCache_)
{
// Stop storage if it has a Stop method
dataCache_.reset();
}
}
void NeoSystem::CleanupNetworking()
{
if (p2pServer_)
{
// p2pServer_->Stop();
p2pServer_.reset();
}
}
void NeoSystem::CleanupNativeContracts()
{
nativeContracts_.clear();
nativeContractMap_.clear();
}
}