-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathenclave.h
More file actions
473 lines (407 loc) · 15.6 KB
/
Copy pathenclave.h
File metadata and controls
473 lines (407 loc) · 15.6 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache 2.0 License.
#pragma once
#include "ccf/app_interface.h"
#include "ccf/js/core/context.h"
#include "ccf/node_context.h"
#include "ccf/node_subsystem_interface.h"
#include "ccf/pal/mem.h"
#include "crypto/openssl/hash.h"
#include "ds/internal_logger.h"
#include "ds/oversized.h"
#include "ds/work_beacon.h"
#include "host/ledger.h"
#include "indexing/enclave_lfs_access.h"
#include "indexing/historical_transaction_fetcher.h"
#include "interface.h"
#include "js/interpreter_cache.h"
#include "kv/ledger_chunker.h"
#include "node/commit_callback_subsystem.h"
#include "node/historical_queries.h"
#include "node/network_state.h"
#include "node/node_state.h"
#include "node/node_types.h"
#include "node/rpc/cosesigconfig_subsystem.h"
#include "node/rpc/custom_protocol_subsystem.h"
#include "node/rpc/forwarder.h"
#include "node/rpc/gov_effects.h"
#include "node/rpc/ledger_subsystem.h"
#include "node/rpc/member_frontend.h"
#include "node/rpc/network_identity_accessors_impl.h"
#include "node/rpc/network_identity_subsystem.h"
#include "node/rpc/node_frontend.h"
#include "node/rpc/node_operation.h"
#include "node/rpc/user_frontend.h"
#include "node/signature_cache_subsystem.h"
#include "rpc_map.h"
#include "rpc_sessions.h"
#include "tasks/worker.h"
#include <openssl/engine.h>
namespace ccf
{
class Enclave
{
private:
std::unique_ptr<ringbuffer::Circuit> circuit;
std::unique_ptr<ringbuffer::WriterFactory> basic_writer_factory;
std::unique_ptr<oversized::WriterFactory> writer_factory;
ccf::ds::WorkBeaconPtr work_beacon;
ccf::NetworkState network;
std::shared_ptr<RPCMap> rpc_map;
std::shared_ptr<RPCSessions> rpcsessions;
std::unique_ptr<ccf::NodeState> node;
ringbuffer::WriterPtr to_host = nullptr;
std::chrono::high_resolution_clock::time_point last_tick_time;
std::atomic<bool> worker_stop_signal = false;
StartType start_type{};
struct NodeContext : public ccf::AbstractNodeContext
{
const ccf::NodeId this_node;
NodeContext(ccf::NodeId id) : this_node(std::move(id)) {}
[[nodiscard]] ccf::NodeId get_node_id() const override
{
return this_node;
}
};
std::unique_ptr<NodeContext> context = nullptr;
std::shared_ptr<ccf::historical::StateCache> historical_state_cache =
nullptr;
std::shared_ptr<ccf::indexing::Indexer> indexer = nullptr;
std::shared_ptr<ccf::indexing::EnclaveLFSAccess> lfs_access = nullptr;
public:
Enclave(
std::unique_ptr<ringbuffer::Circuit> circuit_,
std::unique_ptr<ringbuffer::WriterFactory> basic_writer_factory_,
std::unique_ptr<oversized::WriterFactory> writer_factory_,
size_t sig_tx_interval,
size_t sig_ms_interval,
size_t chunk_threshold,
const ccf::consensus::Configuration& consensus_config,
const ccf::crypto::CurveID& curve_id,
ccf::ds::WorkBeaconPtr work_beacon_,
asynchost::Ledger& ledger_) :
circuit(std::move(circuit_)),
basic_writer_factory(std::move(basic_writer_factory_)),
writer_factory(std::move(writer_factory_)),
work_beacon(std::move(work_beacon_)),
rpc_map(std::make_shared<RPCMap>()),
rpcsessions(std::make_shared<RPCSessions>(*writer_factory, rpc_map))
{
to_host = writer_factory->create_writer_to_outside();
LOG_TRACE_FMT("Creating ledger secrets");
network.ledger_secrets = std::make_shared<ccf::LedgerSecrets>();
network.tables->set_chunker(
std::make_shared<ccf::kv::LedgerChunker>(chunk_threshold));
LOG_TRACE_FMT("Creating node");
node = std::make_unique<ccf::NodeState>(
*writer_factory, network, rpcsessions, curve_id);
LOG_TRACE_FMT("Creating context");
context = std::make_unique<NodeContext>(node->get_node_id());
LOG_TRACE_FMT("Creating context subsystems");
historical_state_cache = std::make_shared<ccf::historical::StateCache>(
*network.tables,
network.ledger_secrets,
writer_factory->create_writer_to_outside());
context->install_subsystem(historical_state_cache);
indexer = std::make_shared<ccf::indexing::Indexer>(
std::make_shared<ccf::indexing::HistoricalTransactionFetcher>(
historical_state_cache));
context->install_subsystem(indexer);
lfs_access = std::make_shared<ccf::indexing::EnclaveLFSAccess>(
ccf::tasks::get_main_job_board());
context->install_subsystem(lfs_access);
context->install_subsystem(std::make_shared<ccf::NodeOperation>(*node));
context->install_subsystem(
std::make_shared<ccf::GovernanceEffects>(*node));
context->install_subsystem(
std::make_shared<ccf::NetworkIdentitySubsystem>(
std::make_shared<ccf::NodeStateAccessor>(*node),
std::make_shared<ccf::HistoricalStateAccessor>(
historical_state_cache),
network.identity,
std::make_shared<ccf::TaskSchedulerImpl>()));
context->install_subsystem(
std::make_shared<ccf::NodeConfigurationSubsystem>(*node));
auto cpss = std::make_shared<ccf::CustomProtocolSubsystem>(*node);
context->install_subsystem(cpss);
rpcsessions->set_custom_protocol_subsystem(cpss);
auto ledger_subsystem =
std::make_shared<ccf::ReadLedgerSubsystem>(ledger_);
context->install_subsystem(ledger_subsystem);
static constexpr size_t max_interpreter_cache_size = 10;
auto interpreter_cache =
std::make_shared<ccf::js::InterpreterCache>(max_interpreter_cache_size);
context->install_subsystem(interpreter_cache);
context->install_subsystem(
std::make_shared<ccf::AbstractCOSESignaturesConfigSubsystem>(*node));
auto commit_callbacks = std::make_shared<ccf::CommitCallbackSubsystem>();
context->install_subsystem(commit_callbacks);
rpcsessions->set_commit_callbacks_subsystem(commit_callbacks);
auto signature_cache = std::make_shared<ccf::SignatureCacheSubsystem>();
context->install_subsystem(signature_cache);
LOG_TRACE_FMT("Creating RPC actors / ffi");
rpc_map->register_frontend<ccf::ActorsType::members>(
std::make_unique<ccf::MemberRpcFrontend>(network, *context));
rpc_map->register_frontend<ccf::ActorsType::users>(
std::make_unique<ccf::UserRpcFrontend>(
network, ccf::make_user_endpoints(*context), *context));
rpc_map->register_frontend<ccf::ActorsType::nodes>(
std::make_unique<ccf::NodeRpcFrontend>(network, *context));
LOG_TRACE_FMT("Initialize node");
node->initialize(
consensus_config,
rpc_map,
rpcsessions,
indexer,
commit_callbacks,
signature_cache,
sig_tx_interval,
sig_ms_interval);
}
~Enclave()
{
LOG_TRACE_FMT("Shutting down enclave");
}
CreateNodeStatus create_new_node(
StartType start_type_,
const ccf::StartupConfig& ccf_config_,
std::vector<uint8_t>& node_cert,
std::vector<uint8_t>& service_cert)
{
start_type = start_type_;
rpcsessions->update_listening_interface_options(ccf_config_.network);
node->set_n2n_message_limit(ccf_config_.node_to_node_message_limit);
historical_state_cache->set_soft_cache_limit(
ccf_config_.historical_cache_soft_limit);
auto network_identity_subsystem =
context->get_subsystem<ccf::NetworkIdentitySubsystem>();
if (network_identity_subsystem == nullptr)
{
LOG_FAIL_FMT(
"NetworkIdentitySubsystem is not installed; cannot start "
"network identity fetching");
return CreateNodeStatus::InternalError;
}
try
{
network_identity_subsystem->start_with_config(
ccf_config_.identity_history_fetch);
}
catch (const std::exception& e)
{
LOG_FAIL_FMT("Failed to start network identity fetching: {}", e.what());
return CreateNodeStatus::InternalError;
}
// If we haven't heard from a node for multiple elections, then cleanup
// their node-to-node channel
const auto idle_timeout =
std::chrono::milliseconds(ccf_config_.consensus.election_timeout) * 4;
node->set_n2n_idle_timeout(idle_timeout);
ccf::NodeCreateInfo create_info;
try
{
LOG_TRACE_FMT(
"Creating node with start_type {}", start_type_to_str(start_type));
create_info = node->create(start_type, ccf_config_);
}
catch (const std::exception& e)
{
LOG_FAIL_FMT("Error starting node: {}", e.what());
return CreateNodeStatus::InternalError;
}
// Copy node and service certs out
node_cert = create_info.self_signed_node_cert.raw();
if (start_type == StartType::Start || start_type == StartType::Recover)
{
// When starting a node in start or recover modes, fresh network secrets
// are created and the associated certificate can be passed to the host
service_cert = create_info.service_cert.raw();
}
return CreateNodeStatus::OK;
}
bool run_main()
{
LOG_DEBUG_FMT("Running main thread");
{
messaging::BufferProcessor bp("Enclave");
// reconstruct oversized messages sent to the enclave
oversized::FragmentReconstructor fr(bp.get_dispatcher());
DISPATCHER_SET_MESSAGE_HANDLER(
bp, AdminMessage::stop, [this, &bp](const uint8_t*, size_t) {
bp.set_finished();
this->worker_stop_signal.store(true);
});
DISPATCHER_SET_MESSAGE_HANDLER(
bp, AdminMessage::stop_notice, [this](const uint8_t*, size_t) {
node->stop_notice();
});
last_tick_time = decltype(last_tick_time)::clock::now();
DISPATCHER_SET_MESSAGE_HANDLER(
bp,
AdminMessage::tick,
[this, &disp = bp.get_dispatcher()](const uint8_t*, size_t) {
const auto time_now = decltype(last_tick_time)::clock::now();
const auto elapsed_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(
time_now - last_tick_time);
if (elapsed_ms.count() > 0)
{
last_tick_time += elapsed_ms;
node->tick(elapsed_ms);
historical_state_cache->tick(elapsed_ms);
ccf::tasks::tick(elapsed_ms);
// When recovering, no signature should be emitted while the
// public ledger is being read
if (!node->is_reading_public_ledger())
{
for (auto& [actor, frontend] : rpc_map->frontends())
{
frontend->tick(elapsed_ms);
}
}
node->tick_end();
}
});
DISPATCHER_SET_MESSAGE_HANDLER(
bp, ccf::node_inbound, [this](const uint8_t* data, size_t size) {
try
{
node->recv_node_inbound(data, size);
}
catch (const std::exception& e)
{
LOG_DEBUG_FMT(
"Ignoring node_inbound message due to exception: {}", e.what());
}
});
DISPATCHER_SET_MESSAGE_HANDLER(
bp,
::consensus::ledger_entry_range,
[this](const uint8_t* data, size_t size) {
const auto [from_seqno, to_seqno, purpose, body] =
ringbuffer::read_message<::consensus::ledger_entry_range>(
data, size);
switch (purpose)
{
case ::consensus::LedgerRequestPurpose::Recovery:
{
if (node->is_reading_public_ledger())
{
node->recover_public_ledger_entries(body);
}
else if (node->is_reading_private_ledger())
{
node->recover_private_ledger_entries(body);
}
else
{
auto [s, _, __] = node->state();
LOG_FAIL_FMT(
"Cannot recover ledger entry: Unexpected node state {}", s);
}
break;
}
case ::consensus::LedgerRequestPurpose::HistoricalQuery:
{
historical_state_cache->handle_ledger_entries(
from_seqno, to_seqno, body);
break;
}
default:
{
LOG_FAIL_FMT("Unhandled purpose: {}", purpose);
}
}
});
DISPATCHER_SET_MESSAGE_HANDLER(
bp,
::consensus::ledger_no_entry_range,
[this](const uint8_t* data, size_t size) {
const auto [from_seqno, to_seqno, purpose] =
ringbuffer::read_message<::consensus::ledger_no_entry_range>(
data, size);
switch (purpose)
{
case ::consensus::LedgerRequestPurpose::Recovery:
{
node->recover_ledger_end();
break;
}
case ::consensus::LedgerRequestPurpose::HistoricalQuery:
{
historical_state_cache->handle_no_entry_range(
from_seqno, to_seqno);
break;
}
default:
{
LOG_FAIL_FMT("Unhandled purpose: {}", purpose);
}
}
});
DISPATCHER_SET_MESSAGE_HANDLER(
bp,
::consensus::snapshot_allocated,
[this](const uint8_t* data, size_t size) {
const auto [snapshot_span, generation_count] =
ringbuffer::read_message<::consensus::snapshot_allocated>(
data, size);
node->write_snapshot(snapshot_span, generation_count);
});
rpcsessions->register_message_handlers(bp.get_dispatcher());
// Maximum number of inbound ringbuffer messages which will be
// processed in a single iteration
static constexpr size_t max_messages = 256;
while (!bp.get_finished())
{
// Wait until the host indicates that some ringbuffer messages are
// available, but wake at least every 100ms to check thread messages
work_beacon->wait_for_work_with_timeout(
std::chrono::milliseconds(100));
// First, read some messages from the ringbuffer
auto read = bp.read_n(max_messages, circuit->read_from_outside());
// Then, execute some tasks
auto& job_board = ccf::tasks::get_main_job_board();
ccf::tasks::Task task = job_board.get_task();
size_t tasks_done = 0;
while (task != nullptr)
{
ccf::tasks::try_do_task(*task);
++tasks_done;
if (tasks_done >= max_messages)
{
break;
}
task = job_board.get_task();
}
// If no messages were read from the ringbuffer and tasks were
// executed, idle
if (read == 0 && tasks_done == 0)
{
std::this_thread::yield();
}
}
LOG_INFO_FMT("Enclave stopped successfully. Stopping host...");
RINGBUFFER_WRITE_MESSAGE(AdminMessage::stopped, to_host);
return true;
}
}
bool run_worker()
{
LOG_DEBUG_FMT("Running worker thread");
{
auto& job_board = ccf::tasks::get_main_job_board();
const auto timeout = std::chrono::milliseconds(100);
while (!worker_stop_signal.load())
{
auto task = job_board.wait_for_task(timeout);
if (task != nullptr)
{
ccf::tasks::try_do_task(*task);
}
}
}
return true;
}
};
}