Skip to content

Commit

Permalink
Merge branch 'master' into fix/getfilterlogtest
Browse files Browse the repository at this point in the history
  • Loading branch information
DrPing authored Jul 26, 2023
2 parents 5c437d4 + d9d26e9 commit 90bb9c0
Show file tree
Hide file tree
Showing 39 changed files with 607 additions and 466 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
container: defi/ain-builder:latest
env:
GITHUB_PULL_REQUEST: ${{ github.event.number }}
TESTS_FAILFAST: 1
TESTS_COMBINED_LOGS: 10000
steps:
- name: Checkout base branch and/or merge
if: github.event_name != 'pull_request'
Expand Down
10 changes: 9 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1377,14 +1377,21 @@ AC_SUBST(PROTOC_INCLUDE_DIR)
dnl If the host and build triplets are the same, we keep this empty
if test x$host = x$build; then
RUST_TARGET=
RUST_HOST=
else
RUST_TARGET=`TARGET="$host" $ac_abs_confdir/make.sh get_rust_target`
RUST_HOST=`$ac_abs_confdir/make.sh get_rust_triplet "$build"`
if test $? != 0; then
AC_MSG_ERROR("unsupported build system")
fi
RUST_TARGET=`$ac_abs_confdir/make.sh get_rust_triplet "$host"`
if test $? != 0; then
AC_MSG_ERROR("unsupported host target")
fi
fi
AC_SUBST(RUST_TARGET)
AC_SUBST(RUST_HOST)
AM_CONDITIONAL([HAVE_RUST_TARGET], [test x$RUST_TARGET != x])
AM_CONDITIONAL([HAVE_RUST_HOST], [test x$RUST_HOST != x])

AC_CONFIG_FILES([Makefile src/Makefile lib/Makefile test/config.ini])
AC_CONFIG_FILES([contrib/devtools/split-debug.sh],[chmod +x contrib/devtools/split-debug.sh])
Expand Down Expand Up @@ -1473,6 +1480,7 @@ echo " CXXFLAGS = $DEBUG_CXXFLAGS $HARDENED_CXXFLAGS $WARN_CXXFLAGS
echo " LDFLAGS = $PTHREAD_CFLAGS $HARDENED_LDFLAGS $GPROF_LDFLAGS $LDFLAGS"
echo " ARFLAGS = $ARFLAGS"
echo " CARGO = $CARGO"
echo " RUST_HOST = $RUST_HOST"
echo " RUST_TARGET = $RUST_TARGET"
echo " ENABLE_DEBUG = $ENABLE_DEBUG"
echo
3 changes: 1 addition & 2 deletions lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ lto = true
[patch.crates-io]
vsdb = { git = 'https://github.com/defich/vsdb.git' }
vsdbsled = { git = 'https://github.com/defich/vsdbsled.git' }
ethereum = { git = 'https://github.com/defich/ethereum.git' }
10 changes: 10 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ CARGO_EXTRA_ARGS ?= $(if $(subst $(build),,$(host)),,--all-targets)

# Export compiler flags
export CC CXX CFLAGS CXXFLAGS CPPFLAGS LDFLAGS AR NM RANLIB
# Set the native compilers
if HAVE_RUST_HOST
export CC_$(RUST_HOST)=gcc
export CXX_$(RUST_HOST)=g++
export CFLAGS_$(RUST_HOST)=
export CXXFLAGS_$(RUST_HOST)=
endif

# Export compiler support
export PKG_CONFIG_PATH PKGCONFIG_LIBDIR PYTHONPATH
# Export protoc vars
Expand All @@ -39,6 +47,8 @@ export PROTOC PROTOC_INCLUDE_DIR
# Ensure nested autotools calls by cargo don't end up in unexpected places
unexport DESTDIR

export RUST_BACKTRACE=1

.PHONY:
all: build

Expand Down
18 changes: 5 additions & 13 deletions lib/ain-evm/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ impl BlockService {
.unwrap_or_default()
}

pub fn connect_block(&self, block: BlockAny, base_fee: U256) {
pub fn connect_block(&self, block: BlockAny) {
self.storage.put_latest_block(Some(&block));
self.storage.put_block(&block);
self.storage.set_base_fee(block.header.hash(), base_fee);
}

fn pre_changi_intermediate_2_base_fee_calculation(
Expand Down Expand Up @@ -169,10 +168,7 @@ impl BlockService {
.storage
.get_block_by_hash(&parent_hash)
.expect("Parent block not found");
let parent_base_fee = self
.storage
.get_base_fee(&parent_block.header.hash())
.expect("Parent base fee not found");
let parent_base_fee = parent_block.header.base_fee;
let parent_gas_used = parent_block.header.gas_used.as_u64();
let parent_gas_target =
parent_block.header.gas_limit.as_u64() / elasticity_multiplier.as_u64();
Expand Down Expand Up @@ -221,10 +217,7 @@ impl BlockService {
.iter()
.map(|block| {
debug!("Processing block {}", block.header.number);
let base_fee = self
.storage
.get_base_fee(&block.header.hash())
.unwrap_or_else(|| panic!("No base fee for block {}", block.header.number));
let base_fee = block.header.base_fee;

let gas_ratio = if block.header.gas_limit == U256::zero() {
f64::default() // empty block
Expand Down Expand Up @@ -352,13 +345,12 @@ impl BlockService {

pub fn get_legacy_fee(&self) -> U256 {
let priority_fee = self.suggested_priority_fee();
let latest_block_hash = self
let base_fee = self
.storage
.get_latest_block()
.expect("Unable to get latest block")
.header
.hash();
let base_fee = self.storage.get_base_fee(&latest_block_hash).unwrap();
.base_fee;

base_fee + priority_fee
}
Expand Down
65 changes: 33 additions & 32 deletions lib/ain-evm/src/core.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::backend::{EVMBackend, EVMBackendError, InsufficientBalance, Vicinity};
use crate::block::INITIAL_BASE_FEE;
use crate::executor::TxResponse;
use crate::fee::calculate_prepay_gas;
use crate::fee::calculate_prepay_gas_fee;
use crate::gas::{check_tx_intrinsic_gas, MIN_GAS_PER_TX};
use crate::receipt::ReceiptService;
use crate::storage::traits::{BlockStorage, PersistentStateError};
use crate::storage::Storage;
Expand All @@ -27,7 +28,6 @@ use vsdb_core::vsdb_set_base_dir;

pub type NativeTxHash = [u8; 32];

pub const MIN_GAS_PER_TX: U256 = U256([21_000, 0, 0, 0]);
pub const MAX_GAS_PER_BLOCK: U256 = U256([30_000_000, 0, 0, 0]);

pub struct EVMCoreService {
Expand Down Expand Up @@ -102,15 +102,13 @@ impl EVMCoreService {
nonce: genesis.nonce.unwrap_or_default(),
timestamp: genesis.timestamp.unwrap_or_default().as_u64(),
difficulty: genesis.difficulty.unwrap_or_default(),
base_fee: genesis.base_fee.unwrap_or(INITIAL_BASE_FEE),
},
Vec::new(),
Vec::new(),
);
storage.put_latest_block(Some(&block));
storage.put_block(&block);
// NOTE(canonbrother): set an initial base fee for genesis block
// https://github.com/ethereum/go-ethereum/blob/46ec972c9c56a4e0d97d812f2eaf9e3657c66276/params/protocol_params.go#LL125C2-L125C16
storage.set_base_fee(block.header.hash(), INITIAL_BASE_FEE);

handler
}
Expand Down Expand Up @@ -167,7 +165,7 @@ impl EVMCoreService {
pub fn validate_raw_tx(
&self,
tx: &str,
context: u64,
queue_id: u64,
use_context: bool,
) -> Result<ValidateTxInfo, Box<dyn Error>> {
debug!("[validate_raw_tx] raw transaction : {:#?}", tx);
Expand Down Expand Up @@ -199,6 +197,7 @@ impl EVMCoreService {
);
debug!("[validate_raw_tx] nonce : {:#?}", nonce);

// Validate tx nonce
if nonce > signed_tx.nonce() {
return Err(anyhow!(
"Invalid nonce. Account nonce {}, signed_tx nonce {}",
Expand All @@ -214,7 +213,7 @@ impl EVMCoreService {

debug!("[validate_raw_tx] Account balance : {:x?}", balance);

let prepay_fee = calculate_prepay_gas(&signed_tx)?;
let prepay_fee = calculate_prepay_gas_fee(&signed_tx)?;
debug!("[validate_raw_tx] prepay_fee : {:x?}", prepay_fee);

let gas_limit = signed_tx.gas_limit();
Expand All @@ -224,11 +223,9 @@ impl EVMCoreService {
return Err(anyhow!("insufficient balance to pay fees").into());
}

if gas_limit < MIN_GAS_PER_TX {
debug!("[validate_raw_tx] gas limit is below the minimum gas per tx");
return Err(anyhow!("gas limit is below the minimum gas per tx").into());
}
} else if balance < MIN_GAS_PER_TX || balance < prepay_fee {
// Validate tx gas limit with intrinsic gas
check_tx_intrinsic_gas(&signed_tx)?;
} else if balance < MIN_GAS_PER_TX.into() || balance < prepay_fee {
debug!("[validate_raw_tx] insufficient balance to pay fees");
return Err(anyhow!("insufficient balance to pay fees").into());
}
Expand All @@ -238,6 +235,7 @@ impl EVMCoreService {
return Err(anyhow!("Gas limit higher than MAX_GAS_PER_BLOCK").into());
}

// Get tx gas usage
let used_gas = if use_context {
let TxResponse { used_gas, .. } = self.call(EthCallArgs {
caller: Some(signed_tx.sender),
Expand All @@ -253,11 +251,12 @@ impl EVMCoreService {
u64::default()
};

// Validate total gas usage in queued txs exceeds block size
if use_context {
debug!("[validate_raw_tx] used_gas: {:#?}", used_gas);
let total_current_gas_used = self
.tx_queues
.get_total_gas_used(context)
.get_total_gas_used(queue_id)
.unwrap_or_default();

if U256::from(total_current_gas_used + used_gas) > MAX_GAS_PER_BLOCK {
Expand Down Expand Up @@ -286,19 +285,20 @@ impl EVMCoreService {
impl EVMCoreService {
pub fn add_balance(
&self,
context: u64,
queue_id: u64,
address: H160,
amount: U256,
hash: NativeTxHash,
) -> Result<(), EVMError> {
let queue_tx = QueueTx::BridgeTx(BridgeTx::EvmIn(BalanceUpdate { address, amount }));
self.tx_queues.queue_tx(context, queue_tx, hash, 0u64)?;
self.tx_queues
.queue_tx(queue_id, queue_tx, hash, 0u64, U256::zero())?;
Ok(())
}

pub fn sub_balance(
&self,
context: u64,
queue_id: u64,
address: H160,
amount: U256,
hash: NativeTxHash,
Expand All @@ -317,34 +317,35 @@ impl EVMCoreService {
.into())
} else {
let queue_tx = QueueTx::BridgeTx(BridgeTx::EvmOut(BalanceUpdate { address, amount }));
self.tx_queues.queue_tx(context, queue_tx, hash, 0u64)?;
self.tx_queues
.queue_tx(queue_id, queue_tx, hash, 0u64, U256::zero())?;
Ok(())
}
}

pub fn get_context(&self) -> u64 {
self.tx_queues.get_context()
pub fn get_queue_id(&self) -> u64 {
self.tx_queues.get_queue_id()
}

pub fn clear(&self, context: u64) -> Result<(), EVMError> {
self.tx_queues.clear(context)?;
pub fn clear(&self, queue_id: u64) -> Result<(), EVMError> {
self.tx_queues.clear(queue_id)?;
Ok(())
}

pub fn remove(&self, context: u64) {
self.tx_queues.remove(context);
pub fn remove(&self, queue_id: u64) {
self.tx_queues.remove(queue_id);
}

pub fn remove_txs_by_sender(&self, context: u64, address: H160) -> Result<(), EVMError> {
self.tx_queues.remove_txs_by_sender(context, address)?;
pub fn remove_txs_by_sender(&self, queue_id: u64, address: H160) -> Result<(), EVMError> {
self.tx_queues.remove_txs_by_sender(queue_id, address)?;
Ok(())
}

/// Retrieves the next valid nonce for the specified account within a particular context.
/// Retrieves the next valid nonce for the specified account within a particular queue.
///
/// The method first attempts to retrieve the next valid nonce from the transaction queue associated with the
/// provided context. If no nonce is found in the transaction queue, that means that no transactions have been
/// queued for this account in this context. It falls back to retrieving the nonce from the storage at the latest
/// provided queue_id. If no nonce is found in the transaction queue, that means that no transactions have been
/// queued for this account in this queue_id. It falls back to retrieving the nonce from the storage at the latest
/// block. If no nonce is found in the storage (i.e., no transactions for this account have been committed yet),
/// the nonce is defaulted to zero.
///
Expand All @@ -353,16 +354,16 @@ impl EVMCoreService {
///
/// # Arguments
///
/// * `context` - The context queue number.
/// * `queue_id` - The queue_id queue number.
/// * `address` - The EVM address of the account whose nonce we want to retrieve.
///
/// # Returns
///
/// Returns the next valid nonce as a `U256`. Defaults to U256::zero()
pub fn get_next_valid_nonce_in_context(&self, context: u64, address: H160) -> U256 {
pub fn get_next_valid_nonce_in_queue(&self, queue_id: u64, address: H160) -> U256 {
let nonce = self
.tx_queues
.get_next_valid_nonce(context, address)
.get_next_valid_nonce(queue_id, address)
.unwrap_or_else(|| {
let latest_block = self
.storage
Expand All @@ -375,7 +376,7 @@ impl EVMCoreService {
});

debug!(
"Account {:x?} nonce {:x?} in context {context}",
"Account {:x?} nonce {:x?} in queue_id {queue_id}",
address, nonce
);
nonce
Expand Down
Loading

0 comments on commit 90bb9c0

Please sign in to comment.