Skip to content

Commit

Permalink
Merge branch 'master' into evm/create-block
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo committed Aug 4, 2023
2 parents 71e61e5 + da8a7d5 commit 59e5da5
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 41 deletions.
1 change: 1 addition & 0 deletions lib/.rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
group_imports = "StdExternalCrate"
4 changes: 2 additions & 2 deletions lib/ain-contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ pub fn get_dst20_codehash() -> Result<H256, Box<dyn Error>> {

pub fn dst20_address_from_token_id(token_id: &str) -> Result<H160, Box<dyn Error>> {
let number_str = format!("{:x}", token_id.parse::<u64>()?);
let padded_number_str = format!("{:0>38}", number_str);
let final_str = format!("ff{}", padded_number_str);
let padded_number_str = format!("{number_str:0>38}");
let final_str = format!("ff{padded_number_str}");

Ok(H160::from_str(&final_str)?)
}
Expand Down
4 changes: 2 additions & 2 deletions lib/ain-cpp-imports/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub mod ffi {
fn getAccounts() -> Vec<String>;
fn getDatadir() -> String;
fn getNetwork() -> String;
fn getDifficulty(_block_hash: [u8; 32]) -> u32;
fn getChainWork(_block_hash: [u8; 32]) -> [u8; 32];
fn getDifficulty(block_hash: [u8; 32]) -> u32;
fn getChainWork(block_hash: [u8; 32]) -> [u8; 32];
fn getPoolTransactions() -> Vec<String>;
fn getNativeTxSize(data: Vec<u8>) -> u64;
fn getMinRelayTxFee() -> u64;
Expand Down
11 changes: 3 additions & 8 deletions lib/ain-evm/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,8 @@ impl EVMBackend {
}

pub fn get_contract_storage(&self, contract: H160, storage_index: &[u8]) -> Result<U256> {
let account = match self.get_account(&contract) {
Some(account) => account,
None => return Ok(U256::zero()),
let Some(account) = self.get_account(&contract) else {
return Ok(U256::zero());
};

let state = self
Expand Down Expand Up @@ -438,11 +437,7 @@ impl fmt::Display for EVMBackendError {
}
EVMBackendError::TrieError(e) => write!(f, "EVMBackendError: Trie error {e}"),
EVMBackendError::NoSuchAccount(address) => {
write!(
f,
"EVMBackendError: No such acccount for address {}",
address
)
write!(f, "EVMBackendError: No such acccount for address {address}")
}
EVMBackendError::InsufficientBalance(InsufficientBalance {
address,
Expand Down
8 changes: 4 additions & 4 deletions lib/ain-evm/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ impl BlockService {
.unwrap_or_default()
}

pub fn connect_block(&self, block: BlockAny) {
self.storage.put_latest_block(Some(&block));
self.storage.put_block(&block);
pub fn connect_block(&self, block: &BlockAny) {
self.storage.put_latest_block(Some(block));
self.storage.put_block(block);
}

pub fn base_fee_calculation(
Expand Down Expand Up @@ -233,7 +233,7 @@ impl BlockService {
.collect::<Vec<f64>>();
let mut data = Data::new(priority_fees);

for pct in priority_fee_percentile.iter() {
for pct in &priority_fee_percentile {
block_rewards.push(U256::from(data.percentile(*pct).ceil() as u64));
}

Expand Down
11 changes: 5 additions & 6 deletions lib/ain-evm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ impl EVMCoreService {
PartialHeader {
state_root,
number: U256::zero(),
beneficiary: Default::default(),
beneficiary: H160::default(),
receipts_root: ReceiptService::get_receipts_root(&Vec::new()),
logs_bloom: Default::default(),
gas_used: Default::default(),
logs_bloom: Bloom::default(),
gas_used: U256::default(),
gas_limit: genesis.gas_limit.unwrap_or(MAX_GAS_PER_BLOCK),
extra_data: genesis.extra_data.unwrap_or_default().into(),
parent_hash: genesis.parent_hash.unwrap_or_default(),
Expand Down Expand Up @@ -387,8 +387,7 @@ impl EVMCoreService {
let latest_block = self
.storage
.get_latest_block()
.map(|b| b.header.number)
.unwrap_or_else(U256::zero);
.map_or_else(U256::zero, |b| b.header.number);

self.get_nonce(address, latest_block)
.unwrap_or_else(|_| U256::zero())
Expand Down Expand Up @@ -518,7 +517,7 @@ impl EVMCoreService {
state_root,
Arc::clone(&self.trie_store),
Arc::clone(&self.storage),
Default::default(),
Vicinity::default(),
)
}
}
Expand Down
11 changes: 6 additions & 5 deletions lib/ain-evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ impl EVMServices {
block.header.number, block.header.state_root
);

self.block.connect_block(block.clone());
self.block.connect_block(&block);
self.logs
.generate_logs_from_receipts(&receipts, block.header.number);
self.receipt.put_receipts(receipts);
Expand Down Expand Up @@ -402,7 +402,7 @@ impl EVMServices {
.queue_tx(queue_id, tx.clone(), hash, gas_used, base_fee)?;

if let QueueTx::SignedTx(signed_tx) = tx {
self.filters.add_tx_to_filters(signed_tx.transaction.hash())
self.filters.add_tx_to_filters(signed_tx.transaction.hash());
}

Ok(())
Expand Down Expand Up @@ -493,9 +493,10 @@ impl EVMServices {
.backend
.get_contract_storage(contract, storage_index.as_bytes())?;

let new_balance = match out {
true => balance.checked_sub(amount),
false => balance.checked_add(amount),
let new_balance = if out {
balance.checked_sub(amount)
} else {
balance.checked_add(amount)
}
.ok_or_else(|| format_err!("Balance overflow/underflow"))?;

Expand Down
4 changes: 2 additions & 2 deletions lib/ain-evm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<'backend> Executor for AinExecutor<'backend> {
fn call(&mut self, ctx: ExecutorContext) -> TxResponse {
let metadata = StackSubstateMetadata::new(ctx.gas_limit, &Self::CONFIG);
let state = MemoryStackState::new(metadata, self.backend);
let precompiles = MetachainPrecompiles::new();
let precompiles = MetachainPrecompiles;
let mut executor = StackExecutor::new_with_precompiles(state, &Self::CONFIG, &precompiles);
let access_list = ctx
.access_list
Expand Down Expand Up @@ -127,7 +127,7 @@ impl<'backend> Executor for AinExecutor<'backend> {

let metadata = StackSubstateMetadata::new(ctx.gas_limit, &Self::CONFIG);
let state = MemoryStackState::new(metadata, self.backend);
let precompiles = MetachainPrecompiles::new();
let precompiles = MetachainPrecompiles;
let mut executor = StackExecutor::new_with_precompiles(state, &Self::CONFIG, &precompiles);
let access_list = ctx
.access_list
Expand Down
4 changes: 1 addition & 3 deletions lib/ain-evm/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ impl LogService {
let mut log_index = 0; // log index is a block level index
for receipt in receipts {
let logs = match &receipt.receipt {
ReceiptV3::Legacy(r) => &r.logs,
ReceiptV3::EIP2930(r) => &r.logs,
ReceiptV3::EIP1559(r) => &r.logs,
ReceiptV3::Legacy(r) | ReceiptV3::EIP2930(r) | ReceiptV3::EIP1559(r) => &r.logs,
};

for log in logs {
Expand Down
4 changes: 0 additions & 4 deletions lib/ain-evm/src/precompiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ pub struct MetachainPrecompiles;
// Ethereum precompiles available as of shangai fork :
// Ref: Ethereum Yellow Paper (https://ethereum.github.io/yellowpaper/paper.pdf) Page 12
impl MetachainPrecompiles {
pub fn new() -> Self {
Self::default()
}

pub fn used_addresses() -> [H160; 9] {
[
hash(1),
Expand Down
2 changes: 1 addition & 1 deletion lib/ain-evm/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl ReceiptService {
transactions
.iter()
.enumerate()
.zip(receipts.into_iter())
.zip(receipts)
.map(|((index, signed_tx), receipt)| {
let receipt_data = match &receipt {
ReceiptV3::Legacy(data)
Expand Down
8 changes: 4 additions & 4 deletions lib/ain-grpc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn should_get_block_by_hash() {
Vec::new(),
Vec::new(),
);
handler.block.connect_block(block.clone());
handler.block.connect_block(&block);
handler.storage.put_block(block.clone());

let block = handler
Expand Down Expand Up @@ -122,7 +122,7 @@ fn should_get_transaction_by_hash() {
Vec::new(),
);

handler.block.connect_block(block.clone());
handler.block.connect_block(&block);
handler.storage.put_block(block.clone());

let input = EthGetTransactionByHashInput {
Expand Down Expand Up @@ -174,7 +174,7 @@ fn should_get_transaction_by_block_hash_and_index() {
Vec::new(),
);

handler.block.connect_block(block.clone());
handler.block.connect_block(&block);
handler.storage.put_block(block.clone());

let input = EthGetTransactionByBlockHashAndIndexInput {
Expand Down Expand Up @@ -229,7 +229,7 @@ fn should_get_transaction_by_block_number_and_index() {
Vec::new(),
);

handler.block.connect_block(block.clone());
handler.block.connect_block(&block);
handler.storage.put_block(block.clone());

let input = EthGetTransactionByBlockNumberAndIndexInput {
Expand Down

0 comments on commit 59e5da5

Please sign in to comment.