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
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
use self::ExecutionEvent as EShim;
use crate::shim::{
    address::Address as ShimAddress, econ::TokenAmount as ShimTokenAmount,
    error::ExitCode as ShimExitCode, gas::GasCharge as ShimGasCharge,
    kernel::SyscallError as ShimSyscallError, state_tree::ActorID as ShimActorId,
    state_tree::ActorState as ShimActorState,
};
use cid::Cid;
use fvm2::trace::ExecutionEvent as E2;
use fvm3::trace::ExecutionEvent as E3;
use fvm4::trace::ExecutionEvent as E4;
use fvm_ipld_encoding::{ipld_block::IpldBlock, RawBytes};
use itertools::Either;

#[derive(Debug, Clone)]
pub enum ExecutionEvent {
    GasCharge(ShimGasCharge),
    Call(Call),
    CallReturn(CallReturn),
    CallAbort(ShimExitCode),
    CallError(ShimSyscallError),
    Log(String),
    InvokeActor(Either<Cid, InvokeActor>),
    Unknown(Either<E3, Either<E4, E2>>),
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct CallReturn {
    pub exit_code: Option<ShimExitCode>,
    pub data: Either<RawBytes, Option<IpldBlock>>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Call {
    /// `ActorID`
    pub from: u64,
    pub to: ShimAddress,
    pub method_num: u64,
    pub params: Either<RawBytes, Option<IpldBlock>>,
    pub value: ShimTokenAmount,
    pub gas_limit: Option<u64>,
    pub read_only: Option<bool>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct InvokeActor {
    pub id: ShimActorId,
    pub state: ShimActorState,
}

impl From<E2> for ExecutionEvent {
    fn from(value: E2) -> Self {
        match value {
            E2::GasCharge(gc) => EShim::GasCharge(gc.into()),
            E2::Call {
                from,
                to,
                method,
                params,
                value,
            } => EShim::Call(Call {
                from,
                to: to.into(),
                method_num: method,
                params: Either::Left(params),
                value: value.into(),
                gas_limit: None,
                read_only: None,
            }),
            E2::CallReturn(data) => EShim::CallReturn(CallReturn {
                exit_code: None,
                data: Either::Left(data),
            }),
            E2::CallAbort(ab) => EShim::CallAbort(ab.into()),
            E2::CallError(err) => EShim::CallError(err.into()),
            E2::Log(s) => EShim::Log(s),
            e => EShim::Unknown(Either::Right(Either::Right(e))),
        }
    }
}

impl From<E3> for ExecutionEvent {
    fn from(value: E3) -> Self {
        match value {
            E3::GasCharge(gc) => EShim::GasCharge(gc.into()),
            E3::Call {
                from,
                to,
                method,
                params,
                value,
                gas_limit,
                read_only,
            } => EShim::Call(Call {
                from,
                to: to.into(),
                method_num: method,
                params: Either::Right(params),
                value: value.into(),
                gas_limit: Some(gas_limit),
                read_only: Some(read_only),
            }),
            E3::CallReturn(exit_code, data) => EShim::CallReturn(CallReturn {
                exit_code: Some(exit_code.into()),
                data: Either::Right(data),
            }),
            E3::CallError(err) => EShim::CallError(err.into()),
            E3::InvokeActor { id, state } => EShim::InvokeActor(Either::Right(InvokeActor {
                id,
                state: state.into(),
            })),
            e => EShim::Unknown(Either::Left(e)),
        }
    }
}

impl From<E4> for ExecutionEvent {
    fn from(value: E4) -> Self {
        match value {
            E4::GasCharge(gc) => EShim::GasCharge(gc.into()),
            E4::Call {
                from,
                to,
                method,
                params,
                value,
                gas_limit,
                read_only,
            } => EShim::Call(Call {
                from,
                to: to.into(),
                method_num: method,
                params: Either::Right(params),
                value: value.into(),
                gas_limit: Some(gas_limit),
                read_only: Some(read_only),
            }),
            E4::CallReturn(exit_code, data) => EShim::CallReturn(CallReturn {
                exit_code: Some(exit_code.into()),
                data: Either::Right(data),
            }),
            E4::CallError(err) => EShim::CallError(err.into()),
            E4::InvokeActor { id, state } => EShim::InvokeActor(Either::Right(InvokeActor {
                id,
                state: state.into(),
            })),
            e => EShim::Unknown(Either::Right(Either::Left(e))),
        }
    }
}