Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic GAS_LIMIT calculation #55

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
46 changes: 30 additions & 16 deletions translator/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::transaction::TelosEVMTransaction;
use crate::types::env::{ANTELOPE_EPOCH_MS, ANTELOPE_INTERVAL_MS};
use crate::types::env::{ANTELOPE_EPOCH_MS, ANTELOPE_INTERVAL_MS, DEFAULT_GAS_LIMIT};
use crate::types::evm_types::{
AccountRow, AccountStateRow, CreateAction, EvmContractConfigRow, OpenWalletAction,
PrintedReceipt, RawAction, SetRevisionAction, TransferAction, WithdrawAction,
Expand All @@ -11,7 +11,7 @@ use crate::types::ship_types::{
use crate::types::translator_types::NameToAddressCache;
use alloy::primitives::{Bloom, Bytes, FixedBytes, B256, U256};
use alloy_consensus::constants::{EMPTY_OMMER_ROOT_HASH, EMPTY_ROOT_HASH};
use alloy_consensus::{Header, TxEnvelope};
use alloy_consensus::{Header, Transaction, TxEnvelope};
use alloy_eips::eip2718::Encodable2718;
use alloy_rlp::Encodable;
use antelope::chain::checksum::Checksum256;
Expand All @@ -21,8 +21,8 @@ use reth_primitives::ReceiptWithBloom;
use reth_rpc_types::ExecutionPayloadV1;
use reth_telos_rpc_engine_api::structs::TelosEngineAPIExtraFields;
use reth_trie_common::root::ordered_trie_root_with_encoder;
use std::cmp::Ordering;
use tracing::warn;
use std::cmp::{max, Ordering};
use tracing::{debug, warn};

const MINIMUM_FEE_PER_GAS: u128 = 7;

Expand Down Expand Up @@ -94,6 +94,7 @@ pub struct ProcessingEVMBlock {
block_traces: Option<Vec<TransactionTrace>>,
contract_rows: Option<Vec<(bool, ContractRow)>>,
cumulative_gas_used: u64,
dyn_gas_limit: Option<u128>,
pub decoded_rows: Vec<DecodedRow>,
pub transactions: Vec<(TelosEVMTransaction, ReceiptWithBloom)>,
pub new_gas_price: Option<(u64, U256)>,
Expand Down Expand Up @@ -141,6 +142,7 @@ impl ProcessingEVMBlock {
block_traces: None,
contract_rows: None,
cumulative_gas_used: 0,
dyn_gas_limit: None,
decoded_rows: vec![],
transactions: vec![],

Expand Down Expand Up @@ -198,6 +200,19 @@ impl ProcessingEVMBlock {
});
}

fn add_transaction(&mut self, transaction: TelosEVMTransaction) {
let full_receipt = transaction.receipt(self.cumulative_gas_used);
let gas_limit = transaction.envelope.gas_limit() + self.cumulative_gas_used as u128;
self.cumulative_gas_used = full_receipt.receipt.cumulative_gas_used;
self.transactions.push((transaction, full_receipt));

if self.dyn_gas_limit.is_none() {
self.dyn_gas_limit = Some(gas_limit);
} else if gas_limit > self.dyn_gas_limit.unwrap() {
self.dyn_gas_limit = Some(gas_limit)
}
}

async fn handle_action(
&mut self,
action: Box<dyn BasicTrace + Send>,
Expand Down Expand Up @@ -235,11 +250,7 @@ impl ProcessingEVMBlock {
.await;

match transaction_result {
Ok(transaction) => {
let full_receipt = transaction.receipt(self.cumulative_gas_used);
self.cumulative_gas_used = full_receipt.receipt.cumulative_gas_used;
self.transactions.push((transaction, full_receipt));
}
Ok(transaction) => self.add_transaction(transaction),
Err(e) => {
panic!("Error handling action. Error: {}", e);
}
Expand All @@ -255,9 +266,7 @@ impl ProcessingEVMBlock {
native_to_evm_cache,
)
.await;
let full_receipt = transaction.receipt(self.cumulative_gas_used);
self.cumulative_gas_used = full_receipt.receipt.cumulative_gas_used;
self.transactions.push((transaction, full_receipt));
self.add_transaction(transaction);
} else if action_account == EOSIO_TOKEN
&& action_name == TRANSFER
&& action_receiver == EOSIO_EVM
Expand All @@ -278,9 +287,7 @@ impl ProcessingEVMBlock {
native_to_evm_cache,
)
.await;
let full_receipt = transaction.receipt(self.cumulative_gas_used);
self.cumulative_gas_used = full_receipt.receipt.cumulative_gas_used;
self.transactions.push((transaction, full_receipt));
self.add_transaction(transaction);
} else if action_account == EOSIO_EVM && action_name == DORESOURCES {
let config_delta_row = self
.find_config_row()
Expand Down Expand Up @@ -400,6 +407,13 @@ impl ProcessingEVMBlock {
logs_bloom.accrue_bloom(&receipt.bloom);
}

let gas_limit = if let Some(dyn_gas) = self.dyn_gas_limit {
debug!("Dynamic gas limit: {}", dyn_gas);
max(DEFAULT_GAS_LIMIT, dyn_gas)
} else {
DEFAULT_GAS_LIMIT
};

let header = Header {
parent_hash,
ommers_hash: EMPTY_OMMER_ROOT_HASH,
Expand All @@ -411,7 +425,7 @@ impl ProcessingEVMBlock {
logs_bloom,
difficulty: Default::default(),
number: (self.block_num - block_delta) as u64,
gas_limit: 0x7fffffff,
gas_limit,
gas_used: self.cumulative_gas_used as u128,
timestamp: (((self.signed_block.clone().unwrap().header.header.timestamp as u64)
* ANTELOPE_INTERVAL_MS)
Expand Down
2 changes: 2 additions & 0 deletions translator/src/types/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub const ANTELOPE_INTERVAL_MS: u64 = 500;

pub const ZERO_HASH_HEX: &str = "0000000000000000000000000000000000000000000000000000000000000000";

pub const DEFAULT_GAS_LIMIT: u128 = 0x7fffffff;

lazy_static! {
pub static ref ZERO_HASH: FixedBytes<32> = FixedBytes::from_str(ZERO_HASH_HEX).unwrap();
pub static ref MAINNET_GENESIS_CONFIG: TranslatorConfig = TranslatorConfig {
Expand Down
Loading