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
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::lotus_json::{lotus_json_with_self, LotusJson};
use crate::message::Message as _;
use crate::shim::executor::ApplyRet;
use crate::shim::{
    address::Address,
    clock::ChainEpoch,
    econ::TokenAmount,
    error::ExitCode,
    executor::Receipt,
    message::Message,
    state_tree::{ActorID, ActorState},
};
use cid::Cid;
use fvm_ipld_encoding::RawBytes;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct ApiInvocResult {
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<Message>")]
    pub msg: Message,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<Cid>")]
    pub msg_cid: Cid,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<Option<Receipt>>")]
    pub msg_rct: Option<Receipt>,
    pub error: String,
    pub duration: u64,
    pub gas_cost: MessageGasCost,
    pub execution_trace: Option<ExecutionTrace>,
}

lotus_json_with_self!(ApiInvocResult);

impl PartialEq for ApiInvocResult {
    /// Ignore [`Self::duration`] as it is implementation-dependent
    fn eq(&self, other: &Self) -> bool {
        self.msg == other.msg
            && self.msg_cid == other.msg_cid
            && self.msg_rct == other.msg_rct
            && self.error == other.error
            && self.gas_cost == other.gas_cost
            && self.execution_trace == other.execution_trace
    }
}

#[derive(Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct MessageGasCost {
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<Option<Cid>>")]
    pub message: Option<Cid>,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<TokenAmount>")]
    pub gas_used: TokenAmount,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<TokenAmount>")]
    pub base_fee_burn: TokenAmount,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<TokenAmount>")]
    pub over_estimation_burn: TokenAmount,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<TokenAmount>")]
    pub miner_penalty: TokenAmount,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<TokenAmount>")]
    pub miner_tip: TokenAmount,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<TokenAmount>")]
    pub refund: TokenAmount,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<TokenAmount>")]
    pub total_cost: TokenAmount,
}

lotus_json_with_self!(MessageGasCost);

impl MessageGasCost {
    pub fn new(message: &Message, apply_ret: &ApplyRet) -> anyhow::Result<Self> {
        Ok(Self {
            message: Some(message.cid()),
            gas_used: TokenAmount::from_atto(apply_ret.msg_receipt().gas_used()),
            base_fee_burn: apply_ret.base_fee_burn(),
            over_estimation_burn: apply_ret.over_estimation_burn(),
            miner_penalty: apply_ret.penalty(),
            miner_tip: apply_ret.miner_tip(),
            refund: apply_ret.refund(),
            total_cost: message.required_funds() - &apply_ret.refund(),
        })
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct ExecutionTrace {
    pub msg: MessageTrace,
    pub msg_rct: ReturnTrace,
    pub invoked_actor: Option<ActorTrace>,
    pub gas_charges: Vec<GasTrace>,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<Vec<ExecutionTrace>>")]
    pub subcalls: Vec<ExecutionTrace>,
}

lotus_json_with_self!(ExecutionTrace);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct MessageTrace {
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<Address>")]
    pub from: Address,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<Address>")]
    pub to: Address,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<TokenAmount>")]
    pub value: TokenAmount,
    pub method: u64,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<RawBytes>")]
    pub params: RawBytes,
    pub params_codec: u64,
    pub gas_limit: Option<u64>,
    pub read_only: Option<bool>,
}

lotus_json_with_self!(MessageTrace);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct ActorTrace {
    pub id: ActorID,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<ActorState>")]
    pub state: ActorState,
}

lotus_json_with_self!(ActorTrace);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct ReturnTrace {
    pub exit_code: ExitCode,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<RawBytes>")]
    pub r#return: RawBytes,
    pub return_codec: u64,
}

lotus_json_with_self!(ReturnTrace);

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct GasTrace {
    pub name: String,
    #[serde(rename = "tg")]
    pub total_gas: u64,
    #[serde(rename = "cg")]
    pub compute_gas: u64,
    #[serde(rename = "sg")]
    pub storage_gas: u64,
    #[serde(rename = "tt")]
    pub time_taken: u64,
}

lotus_json_with_self!(GasTrace);

impl PartialEq for GasTrace {
    /// Ignore [`Self::total_gas`] as it is implementation-dependent
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
            && self.total_gas == other.total_gas
            && self.compute_gas == other.compute_gas
            && self.storage_gas == other.storage_gas
    }
}

#[derive(PartialEq, Serialize, Deserialize, Clone, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct InvocResult {
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<Message>")]
    pub msg: Message,
    #[serde(with = "crate::lotus_json")]
    #[schemars(with = "LotusJson<Option<Receipt>>")]
    pub msg_rct: Option<Receipt>,
    pub error: Option<String>,
}
lotus_json_with_self!(InvocResult);

impl InvocResult {
    pub fn new(msg: Message, ret: &ApplyRet) -> Self {
        Self {
            msg,
            msg_rct: Some(ret.msg_receipt()),
            error: ret.failure_info(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct SectorExpiration {
    pub on_time: ChainEpoch,
    pub early: ChainEpoch,
}
lotus_json_with_self!(SectorExpiration);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct SectorLocation {
    pub deadline: u64,
    pub partition: u64,
}
lotus_json_with_self!(SectorLocation);