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
use super::{
ingress::{Mailbox, Message},
Config,
};
use crate::{
types::{
block::BlockFormat,
inbound::{self, Inbound},
outbound::Outbound,
},
Scheme,
};
use commonware_codec::{DecodeExt, Encode};
use commonware_consensus::{simplex::types::Activity, types::Epoch, Viewable};
use commonware_cryptography::{
bls12381::primitives::variant::{MinSig, Variant},
Hasher,
};
use commonware_parallel::Sequential;
use commonware_runtime::{Sink, Spawner, Stream};
use commonware_stream::encrypted::{Receiver, Sender};
use commonware_utils::channel::mpsc;
use rand::Rng;
use rand_core::CryptoRngCore;
use tracing::{debug, info};
/// Genesis message to use during initialization.
const GENESIS: &[u8] = b"commonware is neat";
/// Application actor.
pub struct Application<R: CryptoRngCore + Spawner, H: Hasher, Si: Sink, St: Stream> {
context: R,
indexer: (Sender<Si>, Receiver<St>),
this_network: <MinSig as Variant>::Public,
other_network: Scheme,
hasher: H,
mailbox: mpsc::Receiver<Message<H::Digest>>,
}
impl<R: CryptoRngCore + Spawner, H: Hasher, Si: Sink, St: Stream> Application<R, H, Si, St> {
/// Create a new application actor.
pub fn new(context: R, config: Config<H, Si, St>) -> (Self, Scheme, Mailbox<H::Digest>) {
let (sender, mailbox) = mpsc::channel(config.mailbox_size);
let this_network = *config.this_network.identity();
(
Self {
context,
indexer: config.indexer,
this_network,
other_network: config.other_network,
hasher: config.hasher,
mailbox,
},
config.this_network,
Mailbox::new(sender),
)
}
/// Run the application actor.
pub async fn run(mut self) {
let (mut indexer_sender, mut indexer_receiver) = self.indexer;
while let Some(message) = self.mailbox.recv().await {
match message {
Message::Genesis { epoch, response } => {
// Sanity check. We don't support multiple epochs.
assert_eq!(epoch, Epoch::zero(), "epoch must be 0");
// Use the digest of the genesis message as the initial payload.
self.hasher.update(GENESIS);
let digest = self.hasher.finalize();
let _ = response.send(digest);
}
Message::Propose { round, response } => {
// Either propose a random message (prefix=0) or include a consensus certificate (prefix=1)
let block = match self.context.gen_bool(0.5) {
true => {
// Generate a random message
BlockFormat::<H::Digest>::Random(self.context.gen())
}
false => {
// Fetch a certificate from the indexer for the other network
let msg =
Inbound::GetFinalization::<H::Digest>(inbound::GetFinalization {
network: *self.other_network.identity(),
})
.encode();
indexer_sender
.send(msg)
.await
.expect("failed to send finalization to indexer");
let result = indexer_receiver
.recv()
.await
.expect("failed to receive from indexer");
let msg = Outbound::<H::Digest>::decode(result)
.expect("failed to decode result");
let finalization = match msg {
Outbound::Success(_) => {
debug!("no finalization found");
continue;
}
Outbound::Finalization(f) => f,
_ => panic!("unexpected response"),
};
// Verify certificate
assert!(
finalization.verify(
&mut self.context,
&self.other_network,
&Sequential
),
"indexer is corrupt"
);
// Use certificate as message
BlockFormat::Bridge(finalization)
}
};
// Hash the message
self.hasher.update(&block.encode());
let digest = self.hasher.finalize();
info!(?block, payload = ?digest, "proposed");
// Publish to indexer
let msg = Inbound::PutBlock::<H::Digest>(inbound::PutBlock {
network: self.this_network,
block,
})
.encode();
indexer_sender
.send(msg)
.await
.expect("failed to send block to indexer");
let result = indexer_receiver
.recv()
.await
.expect("failed to receive from indexer");
let msg =
Outbound::<H::Digest>::decode(result).expect("failed to decode result");
let Outbound::Success(success) = msg else {
panic!("unexpected response");
};
debug!(?round, success, "block published");
if !success {
continue;
}
// Send digest to consensus once we confirm indexer has underlying data
let _ = response.send(digest);
}
Message::Verify { payload, response } => {
// Fetch payload from indexer
let msg = Inbound::GetBlock(inbound::GetBlock {
network: self.this_network,
digest: payload,
})
.encode();
indexer_sender
.send(msg)
.await
.expect("failed to send block to indexer");
let result = indexer_receiver
.recv()
.await
.expect("failed to receive from indexer");
let msg =
Outbound::<H::Digest>::decode(result).expect("failed to decode result");
let block = match msg {
Outbound::Block(b) => b,
Outbound::Success(false) => {
let _ = response.send(false);
debug!("block not found");
continue;
}
_ => panic!("unexpected response"),
};
match block {
BlockFormat::Random(_) => {
let _ = response.send(true);
}
BlockFormat::Bridge(finalization) => {
let result = finalization.verify(
&mut self.context,
&self.other_network,
&Sequential,
);
let _ = response.send(result);
}
}
}
Message::Report { activity } => {
let view = activity.view();
match activity {
Activity::Notarization(notarization) => {
info!(%view, payload = ?notarization.proposal.payload, signature = ?notarization.certificate, "notarized");
}
Activity::Finalization(finalization) => {
info!(%view, payload = ?finalization.proposal.payload, signature = ?finalization.certificate, "finalized");
// Post finalization
let msg =
Inbound::PutFinalization::<H::Digest>(inbound::PutFinalization {
network: self.this_network,
finalization,
})
.encode();
indexer_sender
.send(msg)
.await
.expect("failed to send finalization to indexer");
let result = indexer_receiver
.recv()
.await
.expect("failed to receive from indexer");
let message = Outbound::<H::Digest>::decode(result)
.expect("failed to decode result");
let Outbound::Success(success) = message else {
panic!("unexpected response");
};
debug!(%view, success, "finalization posted");
}
Activity::Nullification(nullification) => {
info!(%view, signature = ?nullification.certificate, "nullified");
}
_ => {}
}
}
}
}
}
}