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
use std::fmt::{self, Display, Formatter};
use datasize::DataSize;
use derive_more::From;
use prometheus::Registry;
use thiserror::Error;
use crate::{
components::{
chainspec_loader::{self, ChainspecLoader},
contract_runtime::{self, ContractRuntime},
small_network::NodeId,
storage::{self, Storage, StorageType},
Component,
},
effect::{
requests::{ContractRuntimeRequest, NetworkRequest, StorageRequest},
EffectBuilder, Effects,
},
protocol::Message,
reactor::{self, validator, EventQueueHandle},
types::CryptoRngCore,
utils::WithDir,
};
#[derive(Debug, From)]
#[must_use]
pub enum Event {
#[from]
Chainspec(chainspec_loader::Event),
#[from]
Storage(storage::Event<Storage>),
#[from]
ContractRuntime(contract_runtime::Event),
}
impl From<StorageRequest<Storage>> for Event {
fn from(request: StorageRequest<Storage>) -> Self {
Event::Storage(storage::Event::Request(request))
}
}
impl From<ContractRuntimeRequest> for Event {
fn from(request: ContractRuntimeRequest) -> Self {
Event::ContractRuntime(contract_runtime::Event::Request(request))
}
}
impl From<NetworkRequest<NodeId, Message>> for Event {
fn from(_request: NetworkRequest<NodeId, Message>) -> Self {
unreachable!("no network traffic happens during initialization")
}
}
impl Display for Event {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Event::Chainspec(event) => write!(formatter, "chainspec: {}", event),
Event::Storage(event) => write!(formatter, "storage: {}", event),
Event::ContractRuntime(event) => write!(formatter, "contract runtime: {}", event),
}
}
}
#[derive(Debug, Error)]
pub enum Error {
#[error("config error: {0}")]
ConfigError(String),
#[error("prometheus (metrics) error: {0}")]
Metrics(#[from] prometheus::Error),
#[error("chainspec error: {0}")]
Chainspec(#[from] chainspec_loader::Error),
#[error("storage error: {0}")]
Storage(#[from] storage::Error),
#[error("contract runtime config error: {0}")]
ContractRuntime(#[from] contract_runtime::ConfigError),
}
#[derive(DataSize, Debug)]
pub struct Reactor {
pub(super) config: WithDir<validator::Config>,
pub(super) chainspec_loader: ChainspecLoader,
pub(super) storage: Storage,
pub(super) contract_runtime: ContractRuntime,
}
impl Reactor {
pub fn stopped_successfully(&self) -> bool {
self.chainspec_loader.stopped_successfully()
}
}
impl reactor::Reactor for Reactor {
type Event = Event;
type Config = WithDir<validator::Config>;
type Error = Error;
fn new(
config: Self::Config,
registry: &Registry,
event_queue: EventQueueHandle<Self::Event>,
_rng: &mut dyn CryptoRngCore,
) -> Result<(Self, Effects<Self::Event>), Error> {
let chainspec = config
.value()
.node
.chainspec_config_path
.clone()
.load(config.dir())
.map_err(|err| Error::ConfigError(err.to_string()))?;
chainspec.validate_config();
let effect_builder = EffectBuilder::new(event_queue);
let storage_config = config.map_ref(|cfg| cfg.storage.clone());
let storage = Storage::new(storage_config.clone())?;
let contract_runtime =
ContractRuntime::new(storage_config, &config.value().contract_runtime, registry)?;
let (chainspec_loader, chainspec_effects) =
ChainspecLoader::new(chainspec, effect_builder)?;
let effects = reactor::wrap_effects(Event::Chainspec, chainspec_effects);
Ok((
Reactor {
config,
chainspec_loader,
storage,
contract_runtime,
},
effects,
))
}
fn dispatch_event(
&mut self,
effect_builder: EffectBuilder<Self::Event>,
rng: &mut dyn CryptoRngCore,
event: Event,
) -> Effects<Self::Event> {
match event {
Event::Chainspec(event) => reactor::wrap_effects(
Event::Chainspec,
self.chainspec_loader
.handle_event(effect_builder, rng, event),
),
Event::Storage(event) => reactor::wrap_effects(
Event::Storage,
self.storage.handle_event(effect_builder, rng, event),
),
Event::ContractRuntime(event) => reactor::wrap_effects(
Event::ContractRuntime,
self.contract_runtime
.handle_event(effect_builder, rng, event),
),
}
}
fn is_stopped(&mut self) -> bool {
self.chainspec_loader.is_stopped()
}
}