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
// Copyright 2020 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

use cfx_bytes::Bytes;

use crate::AddressPocket;
use cfx_types::{Address, Bloom, BloomInput, Space, U256};
use cfx_vm_types::{ActionParams, CallType, CreateType};
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
use rlp_derive::{RlpDecodable, RlpEncodable};
use serde::{ser::SerializeStruct, Serialize, Serializer};
use strum_macros::EnumDiscriminants;

/// Description of a _call_ action, either a `CALL` operation or a message
/// transaction.
#[derive(Debug, Clone, PartialEq, RlpEncodable, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Call {
    /// The space
    pub space: Space,
    /// The sending account.
    pub from: Address,
    /// The destination account.
    pub to: Address,
    /// The value transferred to the destination account.
    pub value: U256,
    /// The gas available for executing the call.
    pub gas: U256,
    /// The input data provided to the call.
    pub input: Bytes,
    /// The type of the call.
    pub call_type: CallType,
}

impl Decodable for Call {
    fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
        match rlp.item_count()? {
            6 => Ok(Call {
                space: Space::Native,
                from: rlp.val_at(0)?,
                to: rlp.val_at(1)?,
                value: rlp.val_at(2)?,
                gas: rlp.val_at(3)?,
                input: rlp.val_at(4)?,
                call_type: rlp.val_at(5)?,
            }),
            7 => Ok(Call {
                space: rlp.val_at(0)?,
                from: rlp.val_at(1)?,
                to: rlp.val_at(2)?,
                value: rlp.val_at(3)?,
                gas: rlp.val_at(4)?,
                input: rlp.val_at(5)?,
                call_type: rlp.val_at(6)?,
            }),
            _ => Err(DecoderError::RlpInvalidLength),
        }
    }
}

impl From<ActionParams> for Call {
    fn from(p: ActionParams) -> Self {
        match p.call_type {
            CallType::DelegateCall | CallType::CallCode => Call {
                space: p.space,
                from: p.address,
                to: p.code_address,
                value: p.value.value(),
                gas: p.gas,
                input: p.data.unwrap_or_else(Vec::new),
                call_type: p.call_type,
            },
            _ => Call {
                space: p.space,
                from: p.sender,
                to: p.address,
                value: p.value.value(),
                gas: p.gas,
                input: p.data.unwrap_or_else(Vec::new),
                call_type: p.call_type,
            },
        }
    }
}

impl Call {
    /// Returns call action bloom.
    /// The bloom contains from and to addresses.
    pub fn bloom(&self) -> Bloom {
        let mut bloom = Bloom::default();
        bloom.accrue(BloomInput::Raw(self.from.as_bytes()));
        bloom.accrue(BloomInput::Raw(self.to.as_bytes()));
        bloom
    }
}

/// The outcome of the action result.
#[derive(Debug, PartialEq, Clone, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Outcome {
    Success,
    Reverted,
    Fail,
}

impl Encodable for Outcome {
    fn rlp_append(&self, s: &mut RlpStream) {
        let v = match *self {
            Outcome::Success => 0u32,
            Outcome::Reverted => 1,
            Outcome::Fail => 2,
        };
        Encodable::rlp_append(&v, s);
    }
}

impl Decodable for Outcome {
    fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
        rlp.as_val().and_then(|v| {
            Ok(match v {
                0u32 => Outcome::Success,
                1 => Outcome::Reverted,
                2 => Outcome::Fail,
                _ => {
                    return Err(DecoderError::Custom(
                        "Invalid value of CallType item",
                    ));
                }
            })
        })
    }
}

/// Description of the result of a _call_ action.
#[derive(Debug, Clone, PartialEq, RlpEncodable, RlpDecodable, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CallResult {
    /// The outcome of the result
    pub outcome: Outcome,
    /// The amount of gas left
    pub gas_left: U256,
    /// Output data
    pub return_data: Bytes,
}

/// Description of a _create_ action, either a `CREATE` operation or a create
/// transaction.
#[derive(Debug, Clone, PartialEq, RlpEncodable, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Create {
    /// Space
    pub space: Space,
    /// The address of the creator.
    pub from: Address,
    /// The value with which the new account is endowed.
    pub value: U256,
    /// The gas available for the creation init code.
    pub gas: U256,
    /// The init code.
    pub init: Bytes,
    /// The create type `CREATE` or `CREATE2`
    pub create_type: CreateType,
}

impl Decodable for Create {
    fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
        match rlp.item_count()? {
            5 => Ok(Create {
                space: Space::Native,
                from: rlp.val_at(0)?,
                value: rlp.val_at(1)?,
                gas: rlp.val_at(2)?,
                init: rlp.val_at(3)?,
                create_type: rlp.val_at(4)?,
            }),
            6 => Ok(Create {
                space: rlp.val_at(0)?,
                from: rlp.val_at(1)?,
                value: rlp.val_at(2)?,
                gas: rlp.val_at(3)?,
                init: rlp.val_at(4)?,
                create_type: rlp.val_at(5)?,
            }),
            _ => Err(DecoderError::RlpInvalidLength),
        }
    }
}

impl From<ActionParams> for Create {
    fn from(p: ActionParams) -> Self {
        Create {
            space: p.space,
            from: p.sender,
            value: p.value.value(),
            gas: p.gas,
            init: p.code.map_or_else(Vec::new, |c| (*c).clone()),
            create_type: p.create_type,
        }
    }
}

impl Create {
    /// Returns bloom create action bloom.
    /// The bloom contains only from address.
    pub fn bloom(&self) -> Bloom {
        BloomInput::Raw(self.from.as_bytes()).into()
    }
}

/// Description of the result of a _create_ action.
#[derive(Debug, Clone, PartialEq, RlpEncodable, RlpDecodable, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateResult {
    /// The outcome of the create
    pub outcome: Outcome,
    /// The created contract address
    pub addr: Address,
    /// The amount of gas left
    pub gas_left: U256,
    /// Output data
    pub return_data: Bytes,
}

impl CreateResult {
    /// Returns create result bloom.
    /// The bloom contains only created contract address.
    pub fn bloom(&self) -> Bloom {
        if self.outcome == Outcome::Success {
            BloomInput::Raw(self.addr.as_bytes()).into()
        } else {
            Bloom::default()
        }
    }
}

/// Description of the result of an internal transfer action regarding about
/// CFX.
#[derive(Debug, Clone, PartialEq, RlpEncodable, RlpDecodable)]
pub struct InternalTransferAction {
    /// The source address. If it is zero, then it is an interest mint action.
    pub from: AddressPocket,
    /// The destination address. If it is zero, then it is a burnt action.
    pub to: AddressPocket,
    /// The amount of CFX
    pub value: U256,
}

impl Serialize for InternalTransferAction {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where S: Serializer {
        let mut s = serializer.serialize_struct("InternalTransferAction", 5)?;
        s.serialize_field("from", &self.from.inner_address_or_default())?;
        s.serialize_field("fromPocket", &*self.from.pocket())?;
        s.serialize_field("fromSpace", &*self.from.space())?;
        s.serialize_field("to", &self.to.inner_address_or_default())?;
        s.serialize_field("toPocket", &*self.to.pocket())?;
        s.serialize_field("toSpace", &*self.to.space())?;
        s.serialize_field("value", &self.value)?;
        s.end()
    }
}

impl InternalTransferAction {
    pub fn bloom(&self) -> Bloom {
        let mut bloom = Bloom::default();
        bloom.accrue(BloomInput::Raw(
            self.from.inner_address_or_default().as_ref(),
        ));
        bloom.accrue(BloomInput::Raw(
            self.to.inner_address_or_default().as_ref(),
        ));
        bloom
    }
}

/// Description of an action that we trace; will be either a call or a create.
#[derive(Debug, Clone, PartialEq, EnumDiscriminants)]
#[strum_discriminants(name(ActionType))]
pub enum Action {
    /// It's a call action.
    Call(Call),
    /// It's a create action.
    Create(Create),
    /// It's the result of a call action
    CallResult(CallResult),
    /// It's the result of a create action
    CreateResult(CreateResult),
    /// It's an internal transfer action
    InternalTransferAction(InternalTransferAction),
}

impl Encodable for Action {
    fn rlp_append(&self, s: &mut RlpStream) {
        s.begin_list(2);
        match *self {
            Action::Call(ref call) => {
                s.append(&0u8);
                s.append(call);
            }
            Action::Create(ref create) => {
                s.append(&1u8);
                s.append(create);
            }
            Action::CallResult(ref call_result) => {
                s.append(&2u8);
                s.append(call_result);
            }
            Action::CreateResult(ref create_result) => {
                s.append(&3u8);
                s.append(create_result);
            }
            Action::InternalTransferAction(ref internal_action) => {
                s.append(&4u8);
                s.append(internal_action);
            }
        }
    }
}

impl Decodable for Action {
    fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
        let action_type: u8 = rlp.val_at(0)?;
        match action_type {
            0 => rlp.val_at(1).map(Action::Call),
            1 => rlp.val_at(1).map(Action::Create),
            2 => rlp.val_at(1).map(Action::CallResult),
            3 => rlp.val_at(1).map(Action::CreateResult),
            4 => rlp.val_at(1).map(Action::InternalTransferAction),
            _ => Err(DecoderError::Custom("Invalid action type.")),
        }
    }
}

impl Action {
    /// Returns action bloom.
    pub fn bloom(&self) -> Bloom {
        match *self {
            Action::Call(ref call) => call.bloom(),
            Action::Create(ref create) => create.bloom(),
            Action::CallResult(_) => Bloom::default(),
            Action::CreateResult(ref create_result) => create_result.bloom(),
            Action::InternalTransferAction(ref internal_action) => {
                internal_action.bloom()
            }
        }
    }
}