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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// 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::rpc::{
    errors::{invalid_params, invalid_params_check},
    types::{
        address::RpcAddress,
        cfx::{
            check_rpc_address_network, check_two_rpc_address_network_match,
            to_primitive_access_list, CfxAccessList,
        },
        Bytes,
    },
    CoreResult,
};
use cfx_addr::Network;
use cfx_parameters::{
    block::{
        CIP1559_CORE_TRANSACTION_GAS_RATIO, DEFAULT_TARGET_BLOCK_GAS_LIMIT,
    },
    RATIO_BASE_TEN,
};
use cfx_types::{Address, AddressSpaceUtil, U256, U64};
use cfx_util_macros::bail;
use cfxcore_accounts::AccountProvider;
use cfxkey::Password;
use primitives::{
    transaction::{
        Action, Cip1559Transaction, Cip2930Transaction, NativeTransaction,
        TypedNativeTransaction::*, CIP1559_TYPE, CIP2930_TYPE, LEGACY_TX_TYPE,
    },
    SignedTransaction, Transaction, TransactionWithSignature,
};
use serde::{Deserialize, Serialize};
use std::{convert::Into, sync::Arc};

/// The maximum gas limit accepted by most tx pools.
pub const DEFAULT_CFX_GAS_CALL_REQUEST: u64 = DEFAULT_TARGET_BLOCK_GAS_LIMIT
    * CIP1559_CORE_TRANSACTION_GAS_RATIO
    / RATIO_BASE_TEN;

#[derive(Debug, Default, Deserialize, PartialEq, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct TransactionRequest {
    /// From
    pub from: Option<RpcAddress>,
    /// To
    pub to: Option<RpcAddress>,
    /// Gas Price
    pub gas_price: Option<U256>,
    /// Gas
    pub gas: Option<U256>,
    /// Value
    pub value: Option<U256>,
    /// Data
    pub data: Option<Bytes>,
    /// Nonce
    pub nonce: Option<U256>,
    /// StorageLimit
    pub storage_limit: Option<U64>,
    /// Access list in EIP-2930
    pub access_list: Option<CfxAccessList>,
    pub max_fee_per_gas: Option<U256>,
    pub max_priority_fee_per_gas: Option<U256>,
    #[serde(rename = "type")]
    pub transaction_type: Option<U64>,
    ///
    pub chain_id: Option<U256>,
    ///
    pub epoch_height: Option<U256>,
}

#[derive(Debug, Default, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EstimateGasAndCollateralResponse {
    /// The recommended gas_limit.
    pub gas_limit: U256,
    /// The amount of gas used in the execution.
    pub gas_used: U256,
    /// The number of bytes collateralized in the execution.
    pub storage_collateralized: U64,
}

#[derive(Debug, Default, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CheckBalanceAgainstTransactionResponse {
    /// Whether the account should pay transaction fee by self.
    pub will_pay_tx_fee: bool,
    /// Whether the account should pay collateral by self.
    pub will_pay_collateral: bool,
    /// Whether the account balance is enough for this transaction.
    pub is_balance_enough: bool,
}

impl TransactionRequest {
    pub fn check_rpc_address_network(
        &self, param_name: &str, expected: &Network,
    ) -> CoreResult<()> {
        let rpc_request_network = invalid_params_check(
            param_name,
            check_two_rpc_address_network_match(
                self.from.as_ref(),
                self.to.as_ref(),
            ),
        )?;
        invalid_params_check(
            param_name,
            check_rpc_address_network(rpc_request_network, expected),
        )
        .map_err(|e| e.into())
    }

    pub fn transaction_type(&self) -> u8 {
        if let Some(tx_type) = self.transaction_type {
            tx_type.as_usize() as u8
        } else {
            if self.max_fee_per_gas.is_some()
                || self.max_priority_fee_per_gas.is_some()
            {
                CIP1559_TYPE
            } else if self.access_list.is_some() {
                CIP2930_TYPE
            } else {
                LEGACY_TX_TYPE
            }
        }
    }

    pub fn has_gas_price(&self) -> bool {
        self.gas_price.is_some()
            || self.max_fee_per_gas.is_some()
            || self.max_priority_fee_per_gas.is_some()
    }

    pub fn sign_with(
        self, epoch_height: u64, chain_id: u32, password: Option<String>,
        accounts: Arc<AccountProvider>,
    ) -> CoreResult<TransactionWithSignature> {
        let gas = self.gas.ok_or("should have gas")?;
        let nonce = self.nonce.ok_or("should have nonce")?;
        let transaction_type = self.transaction_type();
        let action = self.to.map_or(Action::Create, |rpc_addr| {
            Action::Call(rpc_addr.hex_address)
        });

        let value = self.value.unwrap_or_default();
        let storage_limit = self
            .storage_limit
            .map(|v| v.as_u64())
            .ok_or("should have storage_limit")?;
        let data = self.data.unwrap_or_default().into_vec();

        let access_list = self.access_list.unwrap_or(vec![]);

        let typed_native_tx = match transaction_type {
            LEGACY_TX_TYPE => {
                let gas_price =
                    self.gas_price.ok_or("should have gas_price")?;
                Cip155(NativeTransaction {
                    nonce,
                    action,
                    gas,
                    gas_price,
                    value,
                    storage_limit,
                    epoch_height,
                    chain_id,
                    data,
                })
            }
            CIP2930_TYPE => {
                let gas_price =
                    self.gas_price.ok_or("should have gas_price")?;
                Cip2930(Cip2930Transaction {
                    nonce,
                    gas_price,
                    gas,
                    action,
                    value,
                    storage_limit,
                    epoch_height,
                    chain_id,
                    data,
                    access_list: to_primitive_access_list(access_list),
                })
            }
            CIP1559_TYPE => {
                let max_fee_per_gas = self
                    .max_fee_per_gas
                    .ok_or("should have max_fee_per_gas")?;
                let max_priority_fee_per_gas = self
                    .max_priority_fee_per_gas
                    .ok_or("should have max_priority_fee_per_gas")?;
                Cip1559(Cip1559Transaction {
                    nonce,
                    action,
                    gas,
                    value,
                    max_fee_per_gas,
                    max_priority_fee_per_gas,
                    storage_limit,
                    epoch_height,
                    chain_id,
                    data,
                    access_list: to_primitive_access_list(access_list),
                })
            }
            x => {
                return Err(
                    invalid_params("Unrecognized transaction type", x).into()
                );
            }
        };

        let tx = Transaction::Native(typed_native_tx);
        let password = password.map(Password::from);
        let sig = accounts
            .sign(self.from.unwrap().into(), password, tx.signature_hash())
            // TODO: sign error into secret store error codes.
            .map_err(|e| format!("failed to sign transaction: {:?}", e))?;

        Ok(tx.with_signature(sig))
    }

    pub fn sign_call(
        self, epoch_height: u64, chain_id: u32, max_gas: Option<U256>,
    ) -> CoreResult<SignedTransaction> {
        let max_gas = max_gas.unwrap_or(DEFAULT_CFX_GAS_CALL_REQUEST.into());
        let gas = self.gas.unwrap_or(max_gas);
        if gas > max_gas {
            bail!(invalid_params(
                "gas",
                format!("specified gas is larger than max gas {:?}", max_gas)
            ))
        }
        let transaction_type = self.transaction_type();
        let nonce = self.nonce.unwrap_or_default();
        let action = self.to.map_or(Action::Create, |rpc_addr| {
            Action::Call(rpc_addr.hex_address)
        });

        let value = self.value.unwrap_or_default();
        let storage_limit = self
            .storage_limit
            .map(|v| v.as_u64())
            .unwrap_or(std::u64::MAX);
        let data = self.data.unwrap_or_default().into_vec();

        let gas_price = self.gas_price.unwrap_or(1.into());
        let max_fee_per_gas = self
            .max_fee_per_gas
            .or(self.max_priority_fee_per_gas)
            .unwrap_or(gas_price);
        let max_priority_fee_per_gas =
            self.max_priority_fee_per_gas.unwrap_or(U256::zero());
        let access_list = self.access_list.unwrap_or(vec![]);

        let transaction = match transaction_type {
            LEGACY_TX_TYPE => Cip155(NativeTransaction {
                nonce,
                action,
                gas,
                gas_price,
                value,
                storage_limit,
                epoch_height,
                chain_id,
                data,
            }),
            CIP2930_TYPE => Cip2930(Cip2930Transaction {
                nonce,
                gas_price,
                gas,
                action,
                value,
                storage_limit,
                epoch_height,
                chain_id,
                data,
                access_list: to_primitive_access_list(access_list),
            }),
            CIP1559_TYPE => Cip1559(Cip1559Transaction {
                nonce,
                action,
                gas,
                value,
                max_fee_per_gas,
                max_priority_fee_per_gas,
                storage_limit,
                epoch_height,
                chain_id,
                data,
                access_list: to_primitive_access_list(access_list),
            }),
            x => {
                return Err(
                    invalid_params("Unrecognized transaction type", x).into()
                );
            }
        };

        let from = self
            .from
            .map_or_else(|| Address::zero(), |rpc_addr| rpc_addr.hex_address);

        Ok(transaction.fake_sign_rpc(from.with_native_space()))
    }
}

#[cfg(test)]
mod tests {
    use super::TransactionRequest;

    use crate::rpc::types::address::RpcAddress;
    use cfx_addr::Network;
    use cfx_types::{H160, U256, U64};
    use rustc_hex::FromHex;
    use serde_json;
    use std::str::FromStr;

    #[test]
    fn call_request_deserialize() {
        let expected = TransactionRequest {
            from: Some(
                RpcAddress::try_from_h160(
                    H160::from_low_u64_be(1),
                    Network::Main,
                )
                .unwrap(),
            ),
            to: Some(
                RpcAddress::try_from_h160(
                    H160::from_low_u64_be(2),
                    Network::Main,
                )
                .unwrap(),
            ),
            gas_price: Some(U256::from(1)),
            gas: Some(U256::from(2)),
            value: Some(U256::from(3)),
            data: Some(vec![0x12, 0x34, 0x56].into()),
            storage_limit: Some(U64::from_str("7b").unwrap()),
            nonce: Some(U256::from(4)),
            ..Default::default()
        };

        let s = r#"{
            "from":"CFX:TYPE.BUILTIN:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEJC4EYEY6",
            "to":"CFX:TYPE.BUILTIN:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJD0WN6U9U",
            "gasPrice":"0x1",
            "gas":"0x2",
            "value":"0x3",
            "data":"0x123456",
            "storageLimit":"0x7b",
            "nonce":"0x4"
        }"#;
        let deserialized_result = serde_json::from_str::<TransactionRequest>(s);
        assert!(
            deserialized_result.is_ok(),
            "serialized str should look like {}",
            serde_json::to_string(&expected).unwrap()
        );
        assert_eq!(deserialized_result.unwrap(), expected);
    }

    #[test]
    fn call_request_deserialize2() {
        let expected = TransactionRequest {
            from: Some(RpcAddress::try_from_h160(H160::from_str("160e8dd61c5d32be8058bb8eb970870f07233155").unwrap(),  Network::Main ).unwrap()),
            to: Some(RpcAddress::try_from_h160(H160::from_str("846e8dd67c5d32be8058bb8eb970870f07244567").unwrap(), Network::Main).unwrap()),
            gas_price: Some(U256::from_str("9184e72a000").unwrap()),
            gas: Some(U256::from_str("76c0").unwrap()),
            value: Some(U256::from_str("9184e72a").unwrap()),
            storage_limit: Some(U64::from_str("3344adf").unwrap()),
            data: Some("d46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675".from_hex::<Vec<u8>>().unwrap().into()),
            nonce: None,
            ..Default::default()
        };

        let s = r#"{
            "from": "CFX:TYPE.USER:AANA7DS0DVSXFTYANC727SNUU6HUSJ3VMYC3F1AY93",
            "to": "CFX:TYPE.CONTRACT:ACCG7DS0TVSXFTYANC727SNUU6HUSKCFP6KB3NFJ02",
            "gas": "0x76c0",
            "gasPrice": "0x9184e72a000",
            "value": "0x9184e72a",
            "storageLimit":"0x3344adf",
            "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
        }"#;
        let deserialized_result = serde_json::from_str::<TransactionRequest>(s);
        assert!(
            deserialized_result.is_ok(),
            "serialized str should look like {}",
            serde_json::to_string(&expected).unwrap()
        );
        assert_eq!(deserialized_result.unwrap(), expected);
    }

    #[test]
    fn call_request_deserialize_empty() {
        let expected = TransactionRequest {
            from: Some(
                RpcAddress::try_from_h160(
                    H160::from_low_u64_be(1),
                    Network::Main,
                )
                .unwrap(),
            ),
            to: None,
            gas_price: None,
            gas: None,
            value: None,
            data: None,
            storage_limit: None,
            nonce: None,
            ..Default::default()
        };

        let s = r#"{"from":"CFX:TYPE.BUILTIN:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEJC4EYEY6"}"#;
        let deserialized_result = serde_json::from_str::<TransactionRequest>(s);
        assert!(
            deserialized_result.is_ok(),
            "serialized str should look like {}",
            serde_json::to_string(&expected).unwrap()
        );
        assert_eq!(deserialized_result.unwrap(), expected);
    }
}