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
#![allow(dead_code)]

use std::collections::HashMap;

use massa_hash::Hash;
use massa_models::async_msg_id::AsyncMessageId;
use massa_models::block_id::BlockId;
use massa_models::config::{GENESIS_TIMESTAMP, T0, THREAD_COUNT};
use massa_models::deferred_calls::DeferredCallId;
use massa_models::denunciation::DenunciationIndex;
use massa_models::operation::OperationId;
use massa_time::MassaTime;

use massa_deferred_calls::DeferredCall;
use massa_models::address::Address;
use massa_models::amount::Amount;
use massa_models::slot::Slot;
use serde::Serialize;

use crate::types_trace_info::ExecutionResult;

/// Struct to store Roll related operation
#[derive(Debug, Clone)]
pub enum RollOperationInfo {
    /// Roll buy amount
    RollBuy(Address, u64),
    /// Roll sell amount
    RollSell(Address, u64),
}

#[derive(Debug, Clone, Serialize)]
/// Transfer value struct
pub enum TransferValue {
    /// rolls
    Rolls(u64),
    /// amount of deferred credits
    DeferredCredits(Amount),
    /// amount of coins
    Coins(Amount),
}

/// representation of a transfer
#[derive(Clone, Debug, Serialize)]
pub struct TransferInfo {
    /// sender address
    pub from: Option<Address>,
    /// destination address
    pub to: Option<Address>,
    /// Transfer context
    pub context: TransferContext,
    /// Transfer id
    pub id: Option<String>,
    /// Transfer value
    pub value: TransferValue,
}

impl TransferInfo {
    /// Create a new TransferHistory structure
    pub fn new(
        value: TransferValue,
        context: TransferContext,
        from: Option<Address>,
        to: Option<Address>,
    ) -> Self {
        Self {
            from,
            to,
            context,
            value,
            id: None,
        }
    }
}

#[derive(Debug, Default, Clone, Serialize)]
/// context for the transfer
pub struct OriginTransferContext {
    /// original operation id
    pub operation_id: Option<OperationId>,
    /// deferred call id
    pub deferred_call_id: Option<DeferredCallId>,
    /// async message id
    pub async_message_id: Option<AsyncMessageId>,
    /// async message id as string
    pub async_message_id_str: Option<String>,
    /// denunciation index
    pub denunciation_index: Option<DenunciationIndex>,
}

#[allow(missing_docs)]
#[derive(Debug, Clone, Serialize)]
pub enum TransferContext {
    TransactionCoins(OriginTransferContext),
    AyncMsgCancel(OriginTransferContext),
    DeferredCredits(OriginTransferContext),
    DeferredCallFail(OriginTransferContext),
    DeferredCallCancel(OriginTransferContext),
    DeferredCallCoins(OriginTransferContext),
    DeferredCallRegister(OriginTransferContext),
    DeferredCallStorageRefund(OriginTransferContext),
    OperationFee(OriginTransferContext),
    RollBuy(OriginTransferContext),
    RollSell(OriginTransferContext),
    RollSlash,
    CreateSCStorage,
    DatastoreStorage,
    CallSCCoins(OriginTransferContext),
    AsyncMsgCoins(OriginTransferContext),
    EndorsementCreatorReward,
    EndorsementTargetReward,
    BlockCreatorReward,
    ReadOnlyBytecodeExecutionFee,
    ReadOnlyFunctionCallFee,
    ReadOnlyFunctionCallCoins,
    SetBytecodeStorage,
    AbiCallCoins,
    AbiTransferCoins,
    AbiTransferForCoins,
    AbiSendMsgCoins,
    AbiSendMsgFee,
}

/// Struct for execution info
#[derive(Debug, Clone)]
pub struct ExecutionInfoForSlot {
    /// Slot
    pub slot: Slot,
    /// Timestamp
    pub timestamp: MassaTime,
    /// Reward for block producer
    pub block_producer_reward: Option<(Address, Amount)>,
    /// Rewards for endorsmement creators
    pub endorsement_creator_rewards: HashMap<Address, Amount>,
    /// Reward for endorsement target
    pub endorsement_target_reward: Option<(Address, Amount)>,
    /// Executed denunciation
    pub denunciations: Vec<Result<DenunciationResult, String>>,
    /// Executed Roll buy / sell
    pub roll_operations: Vec<RollOperationInfo>,
    /// Executed Async message
    pub async_messages: Vec<Result<AsyncMessageExecutionResult, String>>,
    /// Executed Deferred calls
    pub deferred_calls_messages: Vec<Result<DeferredCallExecutionResult, String>>,
    /// Deferred credits execution (empty if execution-info feature is NOT enabled)
    pub deferred_credits_execution: Vec<(Address, Result<Amount, String>)>,
    /// Cancel async message execution (empty if execution-info feature is NOT enabled)
    pub cancel_async_message_execution: Vec<(Address, Result<Amount, String>)>,
    /// Auto sell roll execution (empty if execution-info feature is NOT enabled)
    pub auto_sell_execution: Vec<(Address, Amount)>,
    /// execution trail hash
    pub execution_trail_hash: Hash,
    /// block id (empty if no block)
    pub opt_block_id: Option<String>,
    /// Transfers (empty if execution-info feature is NOT enabled)
    pub transfers: Vec<TransferInfo>,
}

impl ExecutionInfoForSlot {
    /// Create a new ExecutionInfoForSlot structure
    pub fn new(slot: Slot, execution_trail_hash: Hash, block_id: Option<BlockId>) -> Self {
        let timestamp = massa_models::timeslots::get_block_slot_timestamp(
            THREAD_COUNT,
            T0,
            *GENESIS_TIMESTAMP,
            slot,
        )
        .expect("Error getting timestamp for slot in ExecutionInfoForSlot");
        Self {
            slot,
            timestamp,
            block_producer_reward: None,
            endorsement_creator_rewards: Default::default(),
            endorsement_target_reward: None,
            denunciations: Default::default(),
            roll_operations: Default::default(),
            async_messages: Default::default(),
            deferred_calls_messages: Default::default(),
            deferred_credits_execution: vec![],
            cancel_async_message_execution: vec![],
            auto_sell_execution: vec![],
            opt_block_id: block_id.map(|b| b.to_string()),
            transfers: vec![],
            execution_trail_hash,
        }
    }

    /// When the slot is finalized, we can build the transfer ids
    pub fn build_transfer_list(&mut self) {
        #[cfg(feature = "execution-info")]
        {
            use massa_models::async_msg_id::AsyncMessageIdSerializer;
            use massa_serialization::Serializer;
            use tracing::error;
            let msg_id_serializer = AsyncMessageIdSerializer::new();
            self.transfers
                .iter_mut()
                .enumerate()
                .for_each(|(index, transfer)| {
                    let id = format!("{}:{}", self.execution_trail_hash, index);
                    transfer.id = Some(id);

                    // serialize the msg_id if it exists
                    match &transfer.context {
                        TransferContext::AsyncMsgCoins(ctx)
                        | TransferContext::AyncMsgCancel(ctx) => {
                            if let Some(id) = ctx.async_message_id {
                                let mut buf = Vec::new();
                                let str_opt: Option<String> =
                                    match msg_id_serializer.serialize(&id, &mut buf) {
                                        Ok(_) => String::from_utf8(buf).ok(),
                                        Err(er) => {
                                            error!("Error serializing async_msg_id: {:?}", er);
                                            None
                                        }
                                    };
                                transfer.context = match transfer.context {
                                    TransferContext::AsyncMsgCoins(_) => {
                                        TransferContext::AsyncMsgCoins(OriginTransferContext {
                                            async_message_id_str: str_opt,
                                            ..Default::default()
                                        })
                                    }
                                    TransferContext::AyncMsgCancel(_) => {
                                        TransferContext::AyncMsgCancel(OriginTransferContext {
                                            async_message_id_str: str_opt,
                                            ..Default::default()
                                        })
                                    }
                                    // not reachable
                                    _ => transfer.context.clone(),
                                };
                            }
                        }
                        _ => {}
                    }
                });
        }
    }
}

/// structure describing the output of a denunciation execution
#[derive(Debug, Clone)]
pub struct DenunciationResult {
    /// Target address of the denunciation
    pub address_denounced: Address,
    /// Denunciation slot
    pub slot: Slot,
    /// Amount slashed if successfully executed
    pub slashed: Amount,
}

/// An async message execution result
#[derive(Debug, Clone)]
pub struct AsyncMessageExecutionResult {
    /// Execution success or not
    pub success: bool,
    /// Sender address
    pub sender: Option<Address>,
    /// Destination address
    pub destination: Option<Address>,
    /// Amount
    pub coins: Option<Amount>,
    /// Traces
    pub traces: Option<ExecutionResult>,
}

impl AsyncMessageExecutionResult {
    /// Create a new AsyncMessageExecutionResult structure
    pub fn new() -> Self {
        Self {
            success: false,
            sender: None,
            destination: None,
            coins: None,
            traces: None,
        }
    }
}

impl Default for AsyncMessageExecutionResult {
    fn default() -> Self {
        Self::new()
    }
}

/// Deferred call execution result
#[derive(Debug, Clone)]
pub struct DeferredCallExecutionResult {
    /// Execution success or not
    pub success: bool,
    /// sender address
    pub sender: Address,
    /// target address
    pub target_address: Address,
    pub(crate) target_function: String,
    /// coins
    pub coins: Amount,
    /// fee
    pub fee: Amount,
    /// traces
    pub traces: Option<ExecutionResult>,
}

impl DeferredCallExecutionResult {
    /// Create a new DeferredCallExecutionResult structure
    pub fn new(call: &DeferredCall) -> Self {
        Self {
            success: false,
            sender: call.sender_address,
            target_address: call.target_address,
            target_function: call.target_function.clone(),
            coins: call.coins,
            fee: call.fee,
            traces: None,
        }
    }
}