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
use std::ops::{Deref, DerefMut};
use std::result::Result as StdResult;
use anyhow::{Result, anyhow};
use cid::Cid;
use fvm_ipld_encoding::{DAG_CBOR, RawBytes};
use fvm_shared::ActorID;
use fvm_shared::address::Address;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::{ErrorNumber, ExitCode};
use fvm_shared::message::Message;
use fvm_shared::receipt::Receipt;
use num_traits::Zero;
use super::{ApplyFailure, ApplyKind, ApplyRet, Executor};
use crate::call_manager::{CallManager, InvocationResult, backtrace};
use crate::gas::{Gas, GasCharge, GasOutputs};
use crate::kernel::{Block, ClassifyResult, Context as _, ExecutionError, Kernel};
use crate::machine::{BURNT_FUNDS_ACTOR_ADDR, Machine, REWARD_ACTOR_ADDR};
/// The default [`Executor`].
///
/// # Warning
///
/// Message execution might run out of stack and crash (the entire process) if it doesn't have at
/// least 64MiB of stacks space. If you can't guarantee 64MiB of stack space, wrap this executor in
/// a [`ThreadedExecutor`][super::ThreadedExecutor].
// If the inner value is `None` it means the machine got poisoned and is unusable.
#[repr(transparent)]
pub struct DefaultExecutor<K: Kernel>(Option<<K::CallManager as CallManager>::Machine>);
impl<K: Kernel> Deref for DefaultExecutor<K> {
type Target = <K::CallManager as CallManager>::Machine;
fn deref(&self) -> &Self::Target {
self.0.as_ref().expect("machine poisoned")
}
}
impl<K: Kernel> DerefMut for DefaultExecutor<K> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut *self.0.as_mut().expect("machine poisoned")
}
}
impl<K> Executor for DefaultExecutor<K>
where
K: Kernel,
{
type Kernel = K;
/// This is the entrypoint to execute a message.
fn execute_message(
&mut self,
msg: Message,
apply_kind: ApplyKind,
raw_length: usize,
) -> anyhow::Result<ApplyRet> {
// Validate if the message was correct, charge for it, and extract some preliminary data.
let (sender_id, gas_cost, inclusion_cost) =
match self.preflight_message(&msg, apply_kind, raw_length)? {
Ok(res) => res,
Err(apply_ret) => return Ok(apply_ret),
};
// Apply the message.
let (res, gas_used, mut backtrace, exec_trace) = self.map_machine(|machine| {
let mut cm = K::CallManager::new(machine, msg.gas_limit, msg.from, msg.sequence);
// This error is fatal because it should have already been accounted for inside
// preflight_message.
if let Err(e) = cm.charge_gas(inclusion_cost) {
return (Err(e), cm.finish().1);
}
let params = if msg.params.is_empty() {
None
} else {
Some(Block::new(DAG_CBOR, msg.params.bytes()))
};
let result = cm.with_transaction(|cm| {
// Invoke the message.
let ret = cm.send::<K>(sender_id, msg.to, msg.method_num, params, &msg.value)?;
// Charge for including the result (before we end the transaction).
if let InvocationResult::Return(value) = &ret {
cm.charge_gas(cm.context().price_list.on_chain_return_value(
value.as_ref().map(|v| v.size() as usize).unwrap_or(0),
))?;
}
Ok(ret)
});
let (res, machine) = cm.finish();
(
Ok((result, res.gas_used, res.backtrace, res.exec_trace)),
machine,
)
})?;
// Extract the exit code and build the result of the message application.
let receipt = match res {
Ok(InvocationResult::Return(return_value)) => {
// Convert back into a top-level return "value". We throw away the codec here,
// unfortunately.
let return_data = return_value
.map(|blk| RawBytes::from(blk.data().to_vec()))
.unwrap_or_default();
backtrace.clear();
Receipt {
exit_code: ExitCode::OK,
return_data,
gas_used,
}
}
Ok(InvocationResult::Failure(exit_code)) => {
if exit_code.is_success() {
return Err(anyhow!("actor failed with status OK"));
}
Receipt {
exit_code,
return_data: Default::default(),
gas_used,
}
}
Err(ExecutionError::OutOfGas) => Receipt {
exit_code: ExitCode::SYS_OUT_OF_GAS,
return_data: Default::default(),
gas_used,
},
Err(ExecutionError::Syscall(err)) => {
// Errors indicate the message couldn't be dispatched at all
// (as opposed to failing during execution of the receiving actor).
// These errors are mapped to exit codes that persist on chain.
let exit_code = match err.1 {
ErrorNumber::InsufficientFunds => ExitCode::SYS_INSUFFICIENT_FUNDS,
ErrorNumber::NotFound => ExitCode::SYS_INVALID_RECEIVER,
_ => ExitCode::SYS_ASSERTION_FAILED,
};
backtrace.begin(backtrace::Cause::from_syscall("send", "send", err));
Receipt {
exit_code,
return_data: Default::default(),
gas_used,
}
}
Err(ExecutionError::Fatal(err)) => {
// We produce a receipt with SYS_ASSERTION_FAILED exit code, and
// we consume the full gas amount so that, in case of a network-
// wide fatal errors, all nodes behave deterministically.
//
// We set the backtrace from the fatal error to aid diagnosis.
// Note that we use backtrace#set_cause instead of backtrace#begin
// because we want to retain the propagation chain that we've
// accumulated on the way out.
let err = err.context(format!(
"[from={}, to={}, seq={}, m={}, h={}]",
msg.from,
msg.to,
msg.sequence,
msg.method_num,
self.context().epoch,
));
backtrace.set_cause(backtrace::Cause::from_fatal(err));
Receipt {
exit_code: ExitCode::SYS_ASSERTION_FAILED,
return_data: Default::default(),
gas_used: msg.gas_limit,
}
}
};
let failure_info = if backtrace.is_empty() || receipt.exit_code.is_success() {
None
} else {
Some(ApplyFailure::MessageBacktrace(backtrace))
};
match apply_kind {
ApplyKind::Explicit => self
.finish_message(msg, receipt, failure_info, gas_cost)
.map(|mut apply_ret| {
apply_ret.exec_trace = exec_trace;
apply_ret
}),
ApplyKind::Implicit => Ok(ApplyRet {
msg_receipt: receipt,
penalty: TokenAmount::zero(),
miner_tip: TokenAmount::zero(),
base_fee_burn: TokenAmount::zero(),
over_estimation_burn: TokenAmount::zero(),
refund: TokenAmount::zero(),
gas_refund: 0,
gas_burned: 0,
failure_info,
exec_trace,
}),
}
}
/// Flush the state-tree to the underlying blockstore.
fn flush(&mut self) -> anyhow::Result<Cid> {
let k = (**self).flush()?;
Ok(k)
}
}
impl<K> DefaultExecutor<K>
where
K: Kernel,
{
/// Create a new [`DefaultExecutor`] for executing messages on the [`Machine`].
pub fn new(m: <K::CallManager as CallManager>::Machine) -> Self {
Self(Some(m))
}
/// Consume consumes the executor and returns the Machine. If the Machine had
/// been poisoned during execution, the Option will be None.
pub fn into_machine(self) -> Option<<K::CallManager as CallManager>::Machine> {
self.0
}
// TODO: The return type here is very strange because we have three cases:
// 1. Continue (return actor ID & gas).
// 2. Short-circuit (return ApplyRet).
// 3. Fail (return an error).
// We could use custom types, but that would be even more annoying.
fn preflight_message(
&mut self,
msg: &Message,
apply_kind: ApplyKind,
raw_length: usize,
) -> Result<StdResult<(ActorID, TokenAmount, GasCharge), ApplyRet>> {
msg.check().or_fatal()?;
// TODO We don't like having price lists _inside_ the FVM, but passing
// these across the boundary is also a no-go.
let pl = &self.context().price_list;
let (inclusion_cost, miner_penalty_amount) = match apply_kind {
ApplyKind::Implicit => (
GasCharge::new("none", Gas::zero(), Gas::zero()),
Default::default(),
),
ApplyKind::Explicit => {
let inclusion_cost = pl.on_chain_message(raw_length);
let inclusion_total = inclusion_cost.total().round_up();
// Verify the cost of the message is not over the message gas limit.
if inclusion_total > msg.gas_limit {
return Ok(Err(ApplyRet::prevalidation_fail(
ExitCode::SYS_OUT_OF_GAS,
format!("Out of gas ({} > {})", inclusion_total, msg.gas_limit),
&self.context().base_fee * inclusion_total,
)));
}
let miner_penalty_amount = &self.context().base_fee * msg.gas_limit;
(inclusion_cost, miner_penalty_amount)
}
};
// Load sender actor state.
let sender_id = match self
.state_tree()
.lookup_id(&msg.from)
.with_context(|| format!("failed to lookup actor {}", &msg.from))?
{
Some(id) => id,
None => {
return Ok(Err(ApplyRet::prevalidation_fail(
ExitCode::SYS_SENDER_INVALID,
"Sender invalid",
miner_penalty_amount,
)));
}
};
if apply_kind == ApplyKind::Implicit {
return Ok(Ok((sender_id, TokenAmount::zero(), inclusion_cost)));
}
let sender = match self
.state_tree()
.get_actor(&Address::new_id(sender_id))
.with_context(|| format!("failed to lookup actor {}", &msg.from))?
{
Some(act) => act,
None => {
return Ok(Err(ApplyRet::prevalidation_fail(
ExitCode::SYS_SENDER_INVALID,
"Sender invalid",
miner_penalty_amount,
)));
}
};
// If sender is not an account actor, the message is invalid.
let sender_is_account = self.builtin_actors().is_account_actor(&sender.code);
if !sender_is_account {
return Ok(Err(ApplyRet::prevalidation_fail(
ExitCode::SYS_SENDER_INVALID,
"Send not from account actor",
miner_penalty_amount,
)));
};
// Check sequence is correct
if msg.sequence != sender.sequence {
return Ok(Err(ApplyRet::prevalidation_fail(
ExitCode::SYS_SENDER_STATE_INVALID,
format!(
"Actor sequence invalid: {} != {}",
msg.sequence, sender.sequence
),
miner_penalty_amount,
)));
};
// Ensure from actor has enough balance to cover the gas cost of the message.
let gas_cost: TokenAmount = msg.gas_fee_cap.clone() * msg.gas_limit;
if sender.balance < gas_cost {
return Ok(Err(ApplyRet::prevalidation_fail(
ExitCode::SYS_SENDER_STATE_INVALID,
format!(
"Actor balance less than needed: {} < {}",
sender.balance, gas_cost
),
miner_penalty_amount,
)));
}
// Deduct message inclusion gas cost and increment sequence.
self.state_tree_mut().mutate_actor_id(sender_id, |act| {
act.deduct_funds(&gas_cost)?;
act.sequence += 1;
Ok(())
})?;
Ok(Ok((sender_id, gas_cost, inclusion_cost)))
}
fn finish_message(
&mut self,
msg: Message,
receipt: Receipt,
failure_info: Option<ApplyFailure>,
gas_cost: TokenAmount,
) -> anyhow::Result<ApplyRet> {
// NOTE: we don't support old network versions in the FVM, so we always burn.
let GasOutputs {
base_fee_burn,
over_estimation_burn,
miner_penalty,
miner_tip,
refund,
gas_refund,
gas_burned,
} = GasOutputs::compute(
receipt.gas_used,
msg.gas_limit,
&self.context().base_fee,
&msg.gas_fee_cap,
&msg.gas_premium,
);
let mut transfer_to_actor = |addr: &Address, amt: &TokenAmount| -> anyhow::Result<()> {
if amt.is_negative() {
return Err(anyhow!("attempted to transfer negative value into actor"));
}
if amt.is_zero() {
return Ok(());
}
self.state_tree_mut()
.mutate_actor(addr, |act| {
act.deposit_funds(amt);
Ok(())
})
.context("failed to lookup actor for transfer")?;
Ok(())
};
transfer_to_actor(&BURNT_FUNDS_ACTOR_ADDR, &base_fee_burn)?;
transfer_to_actor(&REWARD_ACTOR_ADDR, &miner_tip)?;
transfer_to_actor(&BURNT_FUNDS_ACTOR_ADDR, &over_estimation_burn)?;
// refund unused gas
transfer_to_actor(&msg.from, &refund)?;
if (&base_fee_burn + &over_estimation_burn + &refund + &miner_tip) != gas_cost {
// Sanity check. This could be a fatal error.
return Err(anyhow!("Gas handling math is wrong"));
}
Ok(ApplyRet {
msg_receipt: receipt,
penalty: miner_penalty,
miner_tip,
base_fee_burn,
over_estimation_burn,
refund,
gas_refund,
gas_burned,
failure_info,
exec_trace: vec![],
})
}
fn map_machine<F, T>(&mut self, f: F) -> T
where
F: FnOnce(
<K::CallManager as CallManager>::Machine,
) -> (T, <K::CallManager as CallManager>::Machine),
{
replace_with::replace_with_and_return(
&mut self.0,
|| None,
|m| {
let (ret, machine) = f(m.unwrap());
(ret, Some(machine))
},
)
}
}