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

use crate::{executive_observer::ExecutiveObserver, substate::Substate};
use cfx_bytes::Bytes;
use cfx_types::{AddressWithSpace, U256};
use cfx_vm_types::Spec;
use primitives::{
    receipt::{SortedStorageChanges, StorageChange},
    LogEntry, TransactionWithSignature,
};
use typemap::ShareDebugMap;

use super::{
    fresh_executive::CostInfo,
    pre_checked_executive::{ExecutiveReturn, RefundInfo},
};

#[derive(Debug)]
pub struct Executed {
    /// Transaction base gas: 21000 (for tx) or 53000 (for contract creation) +
    /// calldata gas
    pub base_gas: u64,

    /// Gas used during execution of transaction.
    pub gas_used: U256,

    /// Fee that need to be paid by execution of this transaction.
    pub fee: U256,

    /// Fee burnt by CIP-1559
    pub burnt_fee: Option<U256>,

    /// Gas charged during execution of transaction.
    pub gas_charged: U256,

    /// If the gas fee is born by designated sponsor.
    pub gas_sponsor_paid: bool,

    /// Vector of logs generated by transaction.
    pub logs: Vec<LogEntry>,

    /// If the storage cost is born by designated sponsor.
    pub storage_sponsor_paid: bool,

    /// Any accounts that occupy some storage.
    pub storage_collateralized: Vec<StorageChange>,

    /// Any accounts that release some storage.
    pub storage_released: Vec<StorageChange>,

    /// Addresses of contracts created during execution of transaction.
    /// Ordered from earliest creation.
    ///
    /// eg. sender creates contract A and A in constructor creates contract B
    ///
    /// B creation ends first, and it will be the first element of the vector.
    ///
    /// Note: if the contract init code return with empty output, the contract
    /// address is still included here, even if it is not considered as a
    /// contract. This is a strange behaviour from Parity's code and not become
    /// a part of the protocol.
    pub contracts_created: Vec<AddressWithSpace>,

    /// Transaction output.
    pub output: Bytes,

    /// Extension output of executed
    pub ext_result: ExecutedExt,
}

pub type ExecutedExt = ShareDebugMap;

impl Executed {
    pub(super) fn not_enough_balance_fee_charged(
        tx: &TransactionWithSignature, actual_gas_cost: &U256, cost: CostInfo,
        ext_result: ExecutedExt, spec: &Spec,
    ) -> Self {
        let gas_charged = if cost.gas_price == U256::zero() {
            U256::zero()
        } else {
            actual_gas_cost / cost.gas_price
        };
        let mut gas_sponsor_paid = cost.gas_sponsored;
        let mut storage_sponsor_paid = cost.storage_sponsored;
        if !spec.cip78b {
            gas_sponsor_paid = false;
            storage_sponsor_paid = false;
        }

        let burnt_fee = spec.cip1559.then(|| {
            let target_burnt = tx.gas().saturating_mul(cost.burnt_gas_price);
            U256::min(*actual_gas_cost, target_burnt)
        });

        Self {
            gas_used: *tx.gas(),
            gas_charged,
            fee: *actual_gas_cost,
            burnt_fee,
            gas_sponsor_paid,
            logs: vec![],
            contracts_created: vec![],
            storage_sponsor_paid,
            storage_collateralized: Vec::new(),
            storage_released: Vec::new(),
            output: Default::default(),
            base_gas: cost.base_gas,
            ext_result,
        }
    }

    pub(super) fn execution_error_fully_charged(
        tx: &TransactionWithSignature, cost: CostInfo, ext_result: ExecutedExt,
        spec: &Spec,
    ) -> Self {
        let mut storage_sponsor_paid = cost.storage_sponsored;
        let mut gas_sponsor_paid = cost.gas_sponsored;

        if !spec.cip78b {
            gas_sponsor_paid = false;
            storage_sponsor_paid = false;
        }
        if spec.cip145 {
            gas_sponsor_paid = false;
        }

        let fee = tx.gas().saturating_mul(cost.gas_price);

        let burnt_fee = spec
            .cip1559
            .then(|| tx.gas().saturating_mul(cost.burnt_gas_price));

        Self {
            gas_used: *tx.gas(),
            gas_charged: *tx.gas(),
            fee,
            burnt_fee,
            gas_sponsor_paid,
            logs: vec![],
            contracts_created: vec![],
            storage_sponsor_paid,
            storage_collateralized: Vec::new(),
            storage_released: Vec::new(),
            output: Default::default(),
            base_gas: cost.base_gas,
            ext_result,
        }
    }

    pub(super) fn from_executive_return(
        r: &ExecutiveReturn, refund_info: RefundInfo, cost: CostInfo,
        substate: Substate, ext_result: ExecutedExt, spec: &Spec,
    ) -> Self {
        let output = r.return_data.to_vec();

        let SortedStorageChanges {
            storage_collateralized,
            storage_released,
        } = if r.apply_state {
            substate.compute_storage_changes()
        } else {
            Default::default()
        };

        let RefundInfo {
            gas_used,
            gas_charged,
            fees_value: fee,
            burnt_fees_value: burnt_fee,
            ..
        } = refund_info;
        let mut storage_sponsor_paid = if spec.cip78a {
            cost.storage_sponsored
        } else {
            cost.storage_sponsor_eligible
        };

        let mut gas_sponsor_paid = cost.gas_sponsored;
        if !r.apply_state && !spec.cip78b {
            gas_sponsor_paid = false;
            storage_sponsor_paid = false;
        }

        Executed {
            gas_used,
            gas_charged,
            fee,
            burnt_fee,
            gas_sponsor_paid,
            logs: substate.logs.to_vec(),
            contracts_created: substate.contracts_created.to_vec(),
            storage_sponsor_paid,
            storage_collateralized,
            storage_released,
            output,
            base_gas: cost.base_gas,
            ext_result,
        }
    }
}

pub fn make_ext_result<O: ExecutiveObserver>(observer: O) -> ShareDebugMap {
    let mut ext_result = ShareDebugMap::custom();
    observer.drain_trace(&mut ext_result);
    ext_result
}