1use super::trace::ExecutionEvent;
5use crate::shim::{
6 econ::TokenAmount, fvm_shared_latest::ActorID, fvm_shared_latest::error::ExitCode,
7};
8use crate::utils::get_size::{GetSize, vec_heap_size_with_fn_helper};
9use cid::Cid;
10use fil_actors_shared::fvm_ipld_amt::{Amt, Amtv0};
11use fvm_ipld_blockstore::Blockstore;
12use fvm_ipld_encoding::RawBytes;
13use fvm_shared2::receipt::Receipt as Receipt_v2;
14use fvm_shared3::event::ActorEvent as ActorEvent_v3;
15use fvm_shared3::event::Entry as Entry_v3;
16use fvm_shared3::event::StampedEvent as StampedEvent_v3;
17pub use fvm_shared3::receipt::Receipt as Receipt_v3;
18use fvm_shared4::event::ActorEvent as ActorEvent_v4;
19use fvm_shared4::event::Entry as Entry_v4;
20use fvm_shared4::event::StampedEvent as StampedEvent_v4;
21use fvm_shared4::receipt::Receipt as Receipt_v4;
22use fvm2::executor::ApplyRet as ApplyRet_v2;
23use fvm3::executor::ApplyRet as ApplyRet_v3;
24use fvm4::executor::ApplyRet as ApplyRet_v4;
25use serde::Serialize;
26use spire_enum::prelude::delegated_enum;
27use std::borrow::Borrow as _;
28
29#[delegated_enum(impl_conversions)]
30#[derive(Clone, Debug)]
31pub enum ApplyRet {
32 V2(ApplyRet_v2),
33 V3(ApplyRet_v3),
34 V4(ApplyRet_v4),
35}
36
37impl ApplyRet {
38 pub fn failure_info(&self) -> Option<String> {
42 let exit_code = self.exit_code();
43 if exit_code.is_success() {
44 return None;
45 }
46 let info =
47 delegate_apply_ret!(self => |r| r.failure_info.as_ref().map(ToString::to_string));
48 Some(format!(
49 "{} (RetCode={exit_code})",
50 info.unwrap_or_else(|| "unknown error".into())
51 ))
52 }
53
54 pub fn miner_tip(&self) -> TokenAmount {
55 delegate_apply_ret!(self.miner_tip.borrow().into())
56 }
57
58 pub fn penalty(&self) -> TokenAmount {
59 delegate_apply_ret!(self.penalty.borrow().into())
60 }
61
62 pub fn msg_receipt(&self) -> Receipt {
65 delegate_apply_ret!(self.msg_receipt.clone().into())
66 }
67
68 pub fn return_data(&self) -> RawBytes {
70 delegate_apply_ret!(self => |r| r.msg_receipt.return_data.clone())
71 }
72
73 pub fn exit_code(&self) -> ExitCode {
74 ExitCode::new(delegate_apply_ret!(self => |r| r.msg_receipt.exit_code.value()))
75 }
76
77 pub fn gas_used(&self) -> u64 {
78 match self {
79 ApplyRet::V2(v2) => v2.msg_receipt.gas_used as u64,
80 ApplyRet::V3(v3) => v3.msg_receipt.gas_used,
81 ApplyRet::V4(v4) => v4.msg_receipt.gas_used,
82 }
83 }
84
85 pub fn refund(&self) -> TokenAmount {
86 delegate_apply_ret!(self.refund.borrow().into())
87 }
88
89 pub fn base_fee_burn(&self) -> TokenAmount {
90 delegate_apply_ret!(self.base_fee_burn.borrow().into())
91 }
92
93 pub fn over_estimation_burn(&self) -> TokenAmount {
94 delegate_apply_ret!(self.over_estimation_burn.borrow().into())
95 }
96
97 pub fn into_exec_trace(self) -> Vec<ExecutionEvent> {
99 delegate_apply_ret!(self => |r| r.exec_trace.into_iter().map(Into::into).collect())
100 }
101
102 pub fn exec_trace(&self) -> Vec<ExecutionEvent> {
103 delegate_apply_ret!(self => |r| r.exec_trace.iter().cloned().map(Into::into).collect())
104 }
105
106 pub fn into_receipt_and_events(self) -> (Receipt, Option<Vec<StampedEvent>>) {
107 match self {
108 ApplyRet::V2(v2) => (v2.msg_receipt.into(), None),
109 ApplyRet::V3(v3) => {
110 let events = v3
111 .msg_receipt
112 .events_root
113 .is_some()
114 .then(|| v3.events.into_iter().map(Into::into).collect());
115 (v3.msg_receipt.into(), events)
116 }
117 ApplyRet::V4(v4) => {
118 let events = v4
119 .msg_receipt
120 .events_root
121 .is_some()
122 .then(|| v4.events.into_iter().map(Into::into).collect());
123 (v4.msg_receipt.into(), events)
124 }
125 }
126 }
127}
128
129#[delegated_enum(impl_conversions)]
132#[derive(Clone, Debug, Serialize)]
133#[serde(untagged)]
134pub enum Receipt {
135 V2(Receipt_v2),
136 V3(Receipt_v3),
137 V4(Receipt_v4),
138}
139
140impl GetSize for Receipt {
141 fn get_heap_size_with_tracker<T: get_size2::GetSizeTracker>(&self, tracker: T) -> (usize, T) {
142 delegate_receipt!(self.return_data.bytes().get_heap_size_with_tracker(tracker))
143 }
144}
145
146impl PartialEq for Receipt {
147 fn eq(&self, other: &Self) -> bool {
148 self.exit_code() == other.exit_code()
149 && self.return_data() == other.return_data()
150 && self.gas_used() == other.gas_used()
151 && self.events_root() == other.events_root()
152 }
153}
154
155impl Receipt {
156 pub fn exit_code(&self) -> ExitCode {
157 match self {
158 Receipt::V2(v2) => ExitCode::new(v2.exit_code.value()),
159 Receipt::V3(v3) => ExitCode::new(v3.exit_code.value()),
160 Receipt::V4(v4) => v4.exit_code,
161 }
162 }
163
164 pub fn return_data(&self) -> RawBytes {
165 delegate_receipt!(self.return_data.clone())
166 }
167
168 pub fn gas_used(&self) -> u64 {
169 match self {
170 Receipt::V2(v2) => v2.gas_used as u64,
171 Receipt::V3(v3) => v3.gas_used,
172 Receipt::V4(v4) => v4.gas_used,
173 }
174 }
175 pub fn events_root(&self) -> Option<Cid> {
176 match self {
177 Receipt::V2(_) => None,
178 Receipt::V3(v3) => v3.events_root,
179 Receipt::V4(v4) => v4.events_root,
180 }
181 }
182
183 pub fn get_receipt(
184 db: &impl Blockstore,
185 receipts: &Cid,
186 i: u64,
187 ) -> anyhow::Result<Option<Self>> {
188 if let Ok(amt) = Amtv0::load(receipts, db)
190 && let Ok(receipts) = amt.get(i)
191 {
192 return Ok(receipts.cloned().map(Receipt::V4));
193 }
194
195 let amt = Amtv0::load(receipts, db)?;
197 let receipts = amt.get(i)?;
198 Ok(receipts.cloned().map(Receipt::V2))
199 }
200
201 pub fn get_receipts(db: &impl Blockstore, receipts_cid: Cid) -> anyhow::Result<Vec<Receipt>> {
202 let mut receipts = Vec::new();
203
204 if let Ok(amt) = Amtv0::<fvm_shared4::receipt::Receipt, _>::load(&receipts_cid, db) {
206 amt.for_each_cacheless(|_, receipt| {
207 receipts.push(Receipt::V4(receipt.clone()));
208 Ok(())
209 })?;
210 } else {
211 let amt = Amtv0::<fvm_shared2::receipt::Receipt, _>::load(&receipts_cid, db)?;
213 amt.for_each_cacheless(|_, receipt| {
214 receipts.push(Receipt::V2(receipt.clone()));
215 Ok(())
216 })?;
217 }
218
219 Ok(receipts)
220 }
221}
222
223#[delegated_enum(impl_conversions)]
224#[derive(Clone, Debug)]
225pub enum Entry {
226 V3(Entry_v3),
227 V4(Entry_v4),
228}
229
230impl Entry {
231 pub fn into_parts(self) -> (u64, String, u64, Vec<u8>) {
232 delegate_entry!(self => |e| (e.flags.bits(), e.key, e.codec, e.value))
233 }
234
235 pub fn value(&self) -> &Vec<u8> {
236 delegate_entry!(self.value.borrow())
237 }
238
239 pub fn codec(&self) -> u64 {
240 delegate_entry!(self.codec)
241 }
242
243 pub fn key(&self) -> &String {
244 delegate_entry!(self.key.borrow())
245 }
246}
247
248#[delegated_enum(impl_conversions)]
249#[derive(Clone, Debug)]
250pub enum ActorEvent {
251 V3(ActorEvent_v3),
252 V4(ActorEvent_v4),
253}
254
255#[delegated_enum(impl_conversions)]
257#[derive(Clone, Debug, Serialize)]
258#[serde(untagged)]
259pub enum StampedEvent {
260 V3(StampedEvent_v3),
261 V4(StampedEvent_v4),
262}
263
264impl GetSize for StampedEvent {
265 fn get_heap_size_with_tracker<T: get_size2::GetSizeTracker>(&self, tracker: T) -> (usize, T) {
266 delegate_stamped_event!(self => |e| vec_heap_size_with_fn_helper(&e.event.entries, tracker, |e, mut tr| {
267 (e.key.get_heap_size_with_tracker(&mut tr).0 + e.value.get_heap_size_with_tracker(&mut tr).0, tr)
268 }))
269 }
270}
271
272impl StampedEvent {
273 pub fn emitter(&self) -> ActorID {
275 delegate_stamped_event!(self.emitter)
276 }
277
278 pub fn entries(&self) -> Vec<Entry> {
279 delegate_stamped_event!(self => |e| e.event.entries.iter().cloned().map(Into::into).collect())
280 }
281
282 pub fn into_entries(self) -> Vec<Entry> {
283 match self {
284 StampedEvent::V3(e) => e.event.entries.into_iter().map(Into::into).collect(),
285 StampedEvent::V4(e) => e.event.entries.into_iter().map(Into::into).collect(),
286 }
287 }
288
289 pub fn get_events<DB: Blockstore>(
292 db: &DB,
293 events_root: &Cid,
294 ) -> anyhow::Result<Vec<StampedEvent>> {
295 let mut events = Vec::new();
296
297 if let Ok(amt) = Amt::<StampedEvent_v4, _>::load(events_root, db) {
299 amt.for_each_cacheless(|_, event| {
300 events.push(StampedEvent::V4(event.clone()));
301 Ok(())
302 })?;
303 } else {
304 let amt = Amt::<StampedEvent_v3, _>::load(events_root, db)?;
306 amt.for_each_cacheless(|_, event| {
307 events.push(StampedEvent::V3(event.clone()));
308 Ok(())
309 })?;
310 }
311
312 Ok(events)
313 }
314}
315
316#[cfg(test)]
317mod test_utils;
318#[cfg(test)]
319pub(crate) use test_utils::{create_raw_event_v3, create_raw_event_v4};
320
321#[cfg(test)]
322mod tests {
323 use super::*;
324 use quickcheck_macros::quickcheck;
325
326 #[quickcheck]
327 fn receipt_cbor_serde_serialize(receipt: Receipt) {
328 let encoded = fvm_ipld_encoding::to_vec(&receipt).unwrap();
329 let encoded2 = match &receipt {
330 Receipt::V2(v) => fvm_ipld_encoding::to_vec(v),
331 Receipt::V3(v) => fvm_ipld_encoding::to_vec(v),
332 Receipt::V4(v) => fvm_ipld_encoding::to_vec(v),
333 }
334 .unwrap();
335 assert_eq!(encoded, encoded2);
336 }
337}