Skip to content

Commit

Permalink
Merge pull request #192 from bitfinity-network/EPROD-1000_legacy_tx_type
Browse files Browse the repository at this point in the history
[Eprod-1000] legacy tx type
  • Loading branch information
ufoscout authored Sep 16, 2024
2 parents 0ae5df4 + d05b1f0 commit 6e01bee
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 93 deletions.
85 changes: 0 additions & 85 deletions src/did/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,6 @@ mod test {

use super::*;
use crate::test_utils::{read_all_files_to_json, test_candid_roundtrip, test_json_roundtrip};
use crate::transaction::StorableExecutionResult;
use crate::BlockId;

#[test]
Expand Down Expand Up @@ -634,90 +633,6 @@ mod test {
tx.into()
}

#[test]
fn test_storable_storable_exe_result() {
let exe_result = StorableExecutionResult {
exe_result: ExeResult::Success {
gas_used: Default::default(),
logs: Default::default(),
logs_bloom: Default::default(),
output: Default::default(),
},
transaction_hash: H256::from(ethereum_types::H256::random()),
transaction_index: rand::random::<u64>().into(),
block_hash: H256::from(ethereum_types::H256::random()),
block_number: rand::random::<u64>().into(),
from: H160::from(ethereum_types::H160::random()),
to: Some(H160::from(ethereum_types::H160::random())),
transaction_type: Default::default(),
cumulative_gas_used: rand::random::<u64>().into(),
gas_price: Default::default(),
max_fee_per_gas: Default::default(),
max_priority_fee_per_gas: Default::default(),
timestamp: 0,
};

let serialized = exe_result.to_bytes();
let deserialized = StorableExecutionResult::from_bytes(serialized);

assert_eq!(exe_result, deserialized);
}

#[test]
fn test_candid_storable_exe_result() {
let exe_result = StorableExecutionResult {
exe_result: ExeResult::Halt {
error: HaltError::CallTooDeep,
gas_used: Default::default(),
},
transaction_hash: H256::from(ethereum_types::H256::random()),
transaction_index: rand::random::<u64>().into(),
block_hash: H256::from(ethereum_types::H256::random()),
block_number: rand::random::<u64>().into(),
from: H160::from(ethereum_types::H160::random()),
to: Some(H160::from(ethereum_types::H160::random())),
transaction_type: Default::default(),
cumulative_gas_used: rand::random::<u64>().into(),
gas_price: Default::default(),
max_fee_per_gas: Default::default(),
max_priority_fee_per_gas: Default::default(),
timestamp: 0,
};

let res0 = Encode!(&exe_result).unwrap();
let res = Decode!(res0.as_slice(), StorableExecutionResult).unwrap();

assert_eq!(exe_result, res);
}

#[test]
fn test_serde_storable_exe_result() {
let exe_result = StorableExecutionResult {
exe_result: ExeResult::Revert {
revert_message: Default::default(),
gas_used: Default::default(),
output: Default::default(),
},
transaction_hash: H256::from(ethereum_types::H256::random()),
transaction_index: rand::random::<u64>().into(),
block_hash: H256::from(ethereum_types::H256::random()),
block_number: rand::random::<u64>().into(),
from: H160::from(ethereum_types::H160::random()),
to: Some(H160::from(ethereum_types::H160::random())),
transaction_type: Default::default(),
cumulative_gas_used: rand::random::<u64>().into(),
gas_price: Default::default(),
max_fee_per_gas: Default::default(),
max_priority_fee_per_gas: Default::default(),
timestamp: 0,
};

let encoded_value = serde_json::json!(&exe_result);
let decoded_value: StorableExecutionResult = serde_json::from_value(encoded_value).unwrap();

assert_eq!(exe_result, decoded_value);
}

#[test]
fn test_block_result() {
let block = Block::<Transaction> {
Expand Down
2 changes: 2 additions & 0 deletions src/did/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub const EIP1559_INITIAL_BASE_FEE: u128 = 1000000000;
pub const EIP1559_ELASTICITY_MULTIPLIER: u128 = 2;
pub const EIP1559_BASE_FEE_MAX_CHANGE_DENOMINATOR: u128 = 8;

/// Identifier for Legacy Transaction
pub const TRANSACTION_TYPE_LEGACY: u64 = 0;
/// Identifier for Eip2930 Transaction
pub const TRANSACTION_TYPE_EIP2930: u64 = 1;
/// Identifier for Eip1559 Transaction
Expand Down
12 changes: 9 additions & 3 deletions src/did/src/fees.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};

use crate::constant::{TRANSACTION_TYPE_EIP1559, TRANSACTION_TYPE_EIP2930};
use crate::constant::{
TRANSACTION_TYPE_EIP1559, TRANSACTION_TYPE_EIP2930, TRANSACTION_TYPE_LEGACY,
};
use crate::transaction::StorableExecutionResult;
use crate::{Transaction, U256, U64};

Expand Down Expand Up @@ -58,7 +60,9 @@ pub trait FeeCalculation {
fn gas_cost(&self) -> U256 {
match self.transaction_type().map(u64::from) {
Some(TRANSACTION_TYPE_EIP1559) => self.max_fee_per_gas().unwrap_or_default(),
Some(TRANSACTION_TYPE_EIP2930) | None => self.gas_price().unwrap_or_default(),
Some(TRANSACTION_TYPE_EIP2930) | Some(TRANSACTION_TYPE_LEGACY) | None => {
self.gas_price().unwrap_or_default()
}
_ => panic!("invalid transaction type"),
}
}
Expand All @@ -67,7 +71,9 @@ pub trait FeeCalculation {
fn max_priority_fee_or_gas_price(&self) -> U256 {
match self.transaction_type().map(u64::from) {
Some(TRANSACTION_TYPE_EIP1559) => self.max_priority_fee_per_gas().unwrap_or_default(),
Some(TRANSACTION_TYPE_EIP2930) | None => self.gas_price().unwrap_or_default(),
Some(TRANSACTION_TYPE_EIP2930) | Some(TRANSACTION_TYPE_LEGACY) | None => {
self.gas_price().unwrap_or_default()
}
_ => panic!("invalid transaction type"),
}
}
Expand Down
Loading

0 comments on commit 6e01bee

Please sign in to comment.