-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathchain.rs
More file actions
539 lines (478 loc) · 20.4 KB
/
Copy pathchain.rs
File metadata and controls
539 lines (478 loc) · 20.4 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// Copyright 2022-2024 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT
use crate::fvm::state::ipc::GatewayCaller;
use crate::fvm::{topdown, FvmApplyRet, PowerUpdates};
use crate::{
fvm::state::FvmExecState,
fvm::FvmMessage,
signed::{SignedMessageApplyRes, SignedMessageCheckRes, SyntheticMessage, VerifiableMessage},
CheckInterpreter, ExecInterpreter, GenesisInterpreter, ProposalInterpreter, QueryInterpreter,
};
use anyhow::{bail, Context};
use async_stm::atomically;
use async_trait::async_trait;
use fendermint_tracing::emit;
use fendermint_vm_actor_interface::ipc;
use fendermint_vm_event::ParentFinalityMissingQuorum;
use fendermint_vm_message::ipc::ParentFinality;
use fendermint_vm_message::{
chain::ChainMessage,
ipc::{BottomUpCheckpoint, CertifiedMessage, IpcMessage, SignedRelayedMessage},
};
use fendermint_vm_resolver::pool::{ResolveKey, ResolvePool};
use fendermint_vm_topdown::proxy::IPCProviderProxy;
use fendermint_vm_topdown::voting::{ValidatorKey, VoteTally};
use fendermint_vm_topdown::{
CachedFinalityProvider, IPCParentFinality, ParentFinalityProvider, ParentViewProvider, Toggle,
};
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::RawBytes;
use fvm_shared::clock::ChainEpoch;
use fvm_shared::econ::TokenAmount;
use num_traits::Zero;
use std::sync::Arc;
/// A resolution pool for bottom-up and top-down checkpoints.
pub type CheckpointPool = ResolvePool<CheckpointPoolItem>;
pub type TopDownFinalityProvider = Arc<Toggle<CachedFinalityProvider<IPCProviderProxy>>>;
/// These are the extra state items that the chain interpreter needs,
/// a sort of "environment" supporting IPC.
#[derive(Clone)]
pub struct ChainEnv {
/// CID resolution pool.
pub checkpoint_pool: CheckpointPool,
/// The parent finality provider for top down checkpoint
pub parent_finality_provider: TopDownFinalityProvider,
pub parent_finality_votes: VoteTally,
}
#[derive(Clone, Hash, PartialEq, Eq)]
pub enum CheckpointPoolItem {
/// BottomUp checkpoints to be resolved from the originating subnet or the current one.
BottomUp(CertifiedMessage<BottomUpCheckpoint>),
// We can extend this to include top-down checkpoints as well, with slightly
// different resolution semantics (resolving it from a trusted parent, and
// awaiting finality before declaring it available).
}
impl From<&CheckpointPoolItem> for ResolveKey {
fn from(value: &CheckpointPoolItem) -> Self {
match value {
CheckpointPoolItem::BottomUp(cp) => {
(cp.message.subnet_id.clone(), cp.message.bottom_up_messages)
}
}
}
}
/// A user sent a transaction which they are not allowed to do.
pub struct IllegalMessage;
// For now this is the only option, later we can expand.
pub enum ChainMessageApplyRet {
Signed(SignedMessageApplyRes),
/// The IPC chain message execution result
Ipc(FvmApplyRet),
}
/// We only allow signed messages into the mempool.
pub type ChainMessageCheckRes = Result<SignedMessageCheckRes, IllegalMessage>;
/// Interpreter working on chain messages; in the future it will schedule
/// CID lookups to turn references into self-contained user or cross messages.
#[derive(Clone)]
pub struct ChainMessageInterpreter<I, DB> {
inner: I,
gateway_caller: GatewayCaller<DB>,
}
impl<I, DB> ChainMessageInterpreter<I, DB> {
pub fn new(inner: I) -> Self {
Self {
inner,
gateway_caller: GatewayCaller::default(),
}
}
}
#[async_trait]
impl<I, DB> ProposalInterpreter for ChainMessageInterpreter<I, DB>
where
DB: Blockstore + Clone + 'static + Send + Sync,
I: Sync + Send,
{
type State = ChainEnv;
type Message = ChainMessage;
/// Check whether there are any "ready" messages in the IPLD resolution mempool which can be appended to the proposal.
///
/// We could also use this to select the most profitable user transactions, within the gas limit. We can also take into
/// account the transactions which are part of top-down or bottom-up checkpoints, to stay within gas limits.
async fn prepare(
&self,
state: Self::State,
mut msgs: Vec<Self::Message>,
) -> anyhow::Result<Vec<Self::Message>> {
// Collect resolved CIDs ready to be proposed from the pool.
let ckpts = atomically(|| state.checkpoint_pool.collect_resolved()).await;
// Create transactions ready to be included on the chain.
let ckpts = ckpts.into_iter().map(|ckpt| match ckpt {
CheckpointPoolItem::BottomUp(ckpt) => ChainMessage::Ipc(IpcMessage::BottomUpExec(ckpt)),
});
// Prepare top down proposals.
// Before we try to find a quorum, pause incoming votes. This is optional but if there are lots of votes coming in it might hold up proposals.
atomically(|| state.parent_finality_votes.pause_votes_until_find_quorum()).await;
// The pre-requisite for proposal is that there is a quorum of gossiped votes at that height.
// The final proposal can be at most as high as the quorum, but can be less if we have already,
// hit some limits such as how many blocks we can propose in a single step.
let finalities = atomically(|| {
let parent = state.parent_finality_provider.next_proposal()?;
let quorum = state
.parent_finality_votes
.find_quorum()?
.map(|(height, block_hash)| IPCParentFinality { height, block_hash });
Ok((parent, quorum))
})
.await;
let maybe_finality = match finalities {
(Some(parent), Some(quorum)) => Some(if parent.height <= quorum.height {
parent
} else {
quorum
}),
(Some(parent), None) => {
emit!(
DEBUG,
ParentFinalityMissingQuorum {
block_height: parent.height,
block_hash: &hex::encode(&parent.block_hash),
}
);
None
}
(None, _) => {
// This is normal, the parent probably hasn't produced a block yet.
None
}
};
if let Some(finality) = maybe_finality {
msgs.push(ChainMessage::Ipc(IpcMessage::TopDownExec(ParentFinality {
height: finality.height as ChainEpoch,
block_hash: finality.block_hash,
})))
}
// Append at the end - if we run out of block space, these are going to be reproposed in the next block.
msgs.extend(ckpts);
Ok(msgs)
}
/// Perform finality checks on top-down transactions and availability checks on bottom-up transactions.
async fn process(&self, env: Self::State, msgs: Vec<Self::Message>) -> anyhow::Result<bool> {
for msg in msgs {
match msg {
ChainMessage::Ipc(IpcMessage::BottomUpExec(msg)) => {
let item = CheckpointPoolItem::BottomUp(msg);
// We can just look in memory because when we start the application, we should retrieve any
// pending checkpoints (relayed but not executed) from the ledger, so they should be there.
// We don't have to validate the checkpoint here, because
// 1) we validated it when it was relayed, and
// 2) if a validator proposes something invalid, we can make them pay during execution.
let is_resolved =
atomically(|| match env.checkpoint_pool.get_status(&item)? {
None => Ok(false),
Some(status) => status.is_resolved(),
})
.await;
if !is_resolved {
return Ok(false);
}
}
ChainMessage::Ipc(IpcMessage::TopDownExec(ParentFinality {
height,
block_hash,
})) => {
let prop = IPCParentFinality {
height: height as u64,
block_hash,
};
let is_final =
atomically(|| env.parent_finality_provider.check_proposal(&prop)).await;
if !is_final {
return Ok(false);
}
}
_ => {}
};
}
Ok(true)
}
}
#[async_trait]
impl<I, DB> ExecInterpreter for ChainMessageInterpreter<I, DB>
where
DB: Blockstore + Clone + 'static + Send + Sync + Clone,
I: ExecInterpreter<
Message = VerifiableMessage,
DeliverOutput = SignedMessageApplyRes,
State = FvmExecState<DB>,
EndOutput = PowerUpdates,
>,
{
// The state consists of the resolver pool, which this interpreter needs, and the rest of the
// state which the inner interpreter uses. This is a technical solution because the pool doesn't
// fit with the state we use for execution messages further down the stack, which depend on block
// height and are used in queries as well.
type State = (ChainEnv, I::State);
type Message = ChainMessage;
type BeginOutput = I::BeginOutput;
type DeliverOutput = ChainMessageApplyRet;
type EndOutput = I::EndOutput;
async fn deliver(
&self,
(env, mut state): Self::State,
msg: Self::Message,
) -> anyhow::Result<(Self::State, Self::DeliverOutput)> {
match msg {
ChainMessage::Signed(msg) => {
let (state, ret) = self
.inner
.deliver(state, VerifiableMessage::Signed(msg))
.await?;
Ok(((env, state), ChainMessageApplyRet::Signed(ret)))
}
ChainMessage::Ipc(msg) => match msg {
IpcMessage::BottomUpResolve(msg) => {
let smsg = relayed_bottom_up_ckpt_to_fvm(&msg)
.context("failed to syntesize FVM message")?;
// Let the FVM validate the checkpoint quorum certificate and take note of the relayer for rewards.
let (state, ret) = self
.inner
.deliver(state, VerifiableMessage::Synthetic(smsg))
.await
.context("failed to deliver bottom up checkpoint")?;
// If successful, add the CID to the background resolution pool.
let is_success = match ret {
Ok(ref ret) => ret.fvm.apply_ret.msg_receipt.exit_code.is_success(),
Err(_) => false,
};
if is_success {
// For now try to get it from the child subnet. If the same comes up for execution, include own.
atomically(|| {
env.checkpoint_pool.add(
CheckpointPoolItem::BottomUp(msg.message.message.clone()),
false,
)
})
.await;
}
// We can use the same result type for now, it's isomorphic.
Ok(((env, state), ChainMessageApplyRet::Signed(ret)))
}
IpcMessage::BottomUpExec(_) => {
todo!("#197: implement BottomUp checkpoint execution")
}
IpcMessage::TopDownExec(p) => {
if !env.parent_finality_provider.is_enabled() {
bail!("cannot execute IPC top-down message: parent provider disabled");
}
// commit parent finality first
let finality = IPCParentFinality::new(p.height, p.block_hash);
tracing::debug!(
finality = finality.to_string(),
"chain interpreter received topdown exec proposal",
);
let (prev_height, prev_finality) = topdown::commit_finality(
&self.gateway_caller,
&mut state,
finality.clone(),
&env.parent_finality_provider,
)
.await
.context("failed to commit finality")?;
tracing::debug!(
previous_committed_height = prev_height,
previous_committed_finality = prev_finality
.as_ref()
.map(|f| format!("{f}"))
.unwrap_or_else(|| String::from("None")),
"chain interpreter committed topdown finality",
);
// The commitment of the finality for block `N` triggers
// the execution of all side-effects up till `N-1`, as for
// deferred execution chains, this is the latest state that
// we know for sure that we have available.
let execution_fr = prev_height;
let execution_to = finality.height - 1;
// error happens if we cannot get the validator set from ipc agent after retries
let validator_changes = env
.parent_finality_provider
.validator_changes_from(execution_fr, execution_to)
.await
.context("failed to fetch validator changes")?;
tracing::debug!(
from = execution_fr,
to = execution_to,
msgs = validator_changes.len(),
"chain interpreter received total validator changes"
);
self.gateway_caller
.store_validator_changes(&mut state, validator_changes)
.context("failed to store validator changes")?;
// error happens if we cannot get the cross messages from ipc agent after retries
let msgs = env
.parent_finality_provider
.top_down_msgs_from(execution_fr, execution_to)
.await
.context("failed to fetch top down messages")?;
tracing::debug!(
number_of_messages = msgs.len(),
start = execution_fr,
end = execution_to,
"chain interpreter received topdown msgs",
);
let ret = topdown::execute_topdown_msgs(&self.gateway_caller, &mut state, msgs)
.await
.context("failed to execute top down messages")?;
tracing::debug!("chain interpreter applied topdown msgs");
atomically(|| {
env.parent_finality_provider
.set_new_finality(finality.clone(), prev_finality.clone())?;
env.parent_finality_votes
.set_finalized(finality.height, finality.block_hash.clone())?;
Ok(())
})
.await;
tracing::debug!(
finality = finality.to_string(),
"chain interpreter has set new"
);
Ok(((env, state), ChainMessageApplyRet::Ipc(ret)))
}
},
}
}
async fn begin(
&self,
(env, state): Self::State,
) -> anyhow::Result<(Self::State, Self::BeginOutput)> {
let (state, out) = self.inner.begin(state).await?;
Ok(((env, state), out))
}
async fn end(
&self,
(env, state): Self::State,
) -> anyhow::Result<(Self::State, Self::EndOutput)> {
let (state, out) = self.inner.end(state).await?;
// Update any component that needs to know about changes in the power table.
if !out.0.is_empty() {
let power_updates = out
.0
.iter()
.map(|v| {
let vk = ValidatorKey::from(v.public_key.0);
let w = v.power.0;
(vk, w)
})
.collect::<Vec<_>>();
atomically(|| {
env.parent_finality_votes
.update_power_table(power_updates.clone())
})
.await;
}
Ok(((env, state), out))
}
}
#[async_trait]
impl<I, DB> CheckInterpreter for ChainMessageInterpreter<I, DB>
where
DB: Blockstore + Clone + 'static + Send + Sync,
I: CheckInterpreter<Message = VerifiableMessage, Output = SignedMessageCheckRes>,
{
type State = I::State;
type Message = ChainMessage;
type Output = ChainMessageCheckRes;
async fn check(
&self,
state: Self::State,
msg: Self::Message,
is_recheck: bool,
) -> anyhow::Result<(Self::State, Self::Output)> {
match msg {
ChainMessage::Signed(msg) => {
let (state, ret) = self
.inner
.check(state, VerifiableMessage::Signed(msg), is_recheck)
.await?;
Ok((state, Ok(ret)))
}
ChainMessage::Ipc(msg) => {
match msg {
IpcMessage::BottomUpResolve(msg) => {
let msg = relayed_bottom_up_ckpt_to_fvm(&msg)
.context("failed to syntesize FVM message")?;
let (state, ret) = self
.inner
.check(state, VerifiableMessage::Synthetic(msg), is_recheck)
.await
.context("failed to check bottom up resolve")?;
Ok((state, Ok(ret)))
}
IpcMessage::TopDownExec(_) | IpcMessage::BottomUpExec(_) => {
// Users cannot send these messages, only validators can propose them in blocks.
Ok((state, Err(IllegalMessage)))
}
}
}
}
}
}
#[async_trait]
impl<I, DB> QueryInterpreter for ChainMessageInterpreter<I, DB>
where
DB: Blockstore + Clone + 'static + Send + Sync,
I: QueryInterpreter,
{
type State = I::State;
type Query = I::Query;
type Output = I::Output;
async fn query(
&self,
state: Self::State,
qry: Self::Query,
) -> anyhow::Result<(Self::State, Self::Output)> {
self.inner.query(state, qry).await
}
}
#[async_trait]
impl<I, DB> GenesisInterpreter for ChainMessageInterpreter<I, DB>
where
DB: Blockstore + Clone + 'static + Send + Sync,
I: GenesisInterpreter,
{
type State = I::State;
type Genesis = I::Genesis;
type Output = I::Output;
async fn init(
&self,
state: Self::State,
genesis: Self::Genesis,
) -> anyhow::Result<(Self::State, Self::Output)> {
self.inner.init(state, genesis).await
}
}
/// Convert a signed relayed bottom-up checkpoint to a syntetic message we can send to the FVM.
///
/// By mapping to an FVM message we invoke the right contract to validate the checkpoint,
/// and automatically charge the relayer gas for the execution of the check, but not the
/// execution of the cross-messages, which aren't part of the payload.
fn relayed_bottom_up_ckpt_to_fvm(
relayed: &SignedRelayedMessage<CertifiedMessage<BottomUpCheckpoint>>,
) -> anyhow::Result<SyntheticMessage> {
// TODO #192: Convert the checkpoint to what the actor expects.
let params = RawBytes::default();
let msg = FvmMessage {
version: 0,
from: relayed.message.relayer,
to: ipc::GATEWAY_ACTOR_ADDR,
sequence: relayed.message.sequence,
value: TokenAmount::zero(),
method_num: ipc::gateway::METHOD_INVOKE_CONTRACT,
params,
gas_limit: relayed.message.gas_limit,
gas_fee_cap: relayed.message.gas_fee_cap.clone(),
gas_premium: relayed.message.gas_premium.clone(),
};
let msg = SyntheticMessage::new(msg, &relayed.message, relayed.signature.clone())
.context("failed to create syntetic message")?;
Ok(msg)
}