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

evm: Fix receipts log output #1985

Merged
merged 8 commits into from
May 15, 2023
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
20 changes: 18 additions & 2 deletions lib/ain-evm/src/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::storage::{traits::ReceiptStorage, Storage};
use crate::transaction::SignedTx;
use ethereum::{EnvelopedEncodable, ReceiptV3};
use ethereum::{EIP658ReceiptData, EnvelopedEncodable, ReceiptV3};
use primitive_types::{H160, H256, U256};

use ethereum::util::ordered_trie_root;
Expand All @@ -20,6 +20,8 @@ pub struct Receipt {
pub tx_index: usize,
pub tx_type: u8,
pub contract_address: Option<H160>,
pub logs_index: usize,
pub cumulative_gas: U256,
}

pub struct ReceiptHandler {
Expand Down Expand Up @@ -54,12 +56,15 @@ impl ReceiptHandler {
block_hash: H256,
block_number: U256,
) -> Vec<Receipt> {
let mut logs_size = 0;
let mut cumulative_gas = U256::zero();

transactions
.iter()
.enumerate()
.zip(receipts.into_iter())
.map(|((index, signed_tx), receipt)| Receipt {
receipt,
receipt: receipt.clone(),
block_hash,
block_number,
tx_hash: signed_tx.transaction.hash(),
Expand All @@ -71,6 +76,17 @@ impl ReceiptHandler {
.to()
.is_none()
.then(|| get_contract_address(&signed_tx.sender, &signed_tx.nonce())),
logs_index: {
let logs_len = EIP658ReceiptData::from(receipt.clone()).logs.len();
logs_size += logs_len;

logs_size - logs_len
},
cumulative_gas: {
cumulative_gas += EIP658ReceiptData::from(receipt).used_gas;

cumulative_gas
},
})
.collect()
}
Expand Down
39 changes: 35 additions & 4 deletions lib/ain-grpc/src/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
use crate::utils::format_bytes;
use ain_evm::receipt::Receipt;
use ethereum::{EIP658ReceiptData, Log};
use ethereum::EIP658ReceiptData;
use primitive_types::{H160, H256, U256};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct LogResult {
pub address: H160,
pub topics: Vec<H256>,
pub data: String,
pub block_number: U256,
pub block_hash: H256,
pub transaction_hash: H256,
pub transaction_index: String,
pub log_index: String,
pub removed: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ReceiptResult {
Expand All @@ -12,7 +27,7 @@ pub struct ReceiptResult {
pub effective_gas_price: U256,
pub from: H160,
pub gas_used: U256,
pub logs: Vec<Log>,
pub logs: Vec<LogResult>,
pub logs_bloom: String,
pub status: String,
pub to: Option<H160>,
Expand All @@ -28,11 +43,27 @@ impl From<Receipt> for ReceiptResult {
block_hash: b.block_hash,
block_number: b.block_number,
contract_address: b.contract_address,
cumulative_gas_used: Default::default(),
cumulative_gas_used: b.cumulative_gas,
effective_gas_price: Default::default(),
from: b.from,
gas_used: data.used_gas,
logs: data.logs,
logs: {
data.logs
.iter()
.enumerate()
.map(|(log_index, x)| LogResult {
address: x.clone().address,
topics: x.clone().topics,
data: format_bytes(x.data.to_ascii_lowercase()),
block_number: b.block_number,
block_hash: b.block_hash,
transaction_hash: b.tx_hash,
transaction_index: format!("{:#x}", b.tx_index),
log_index: { format!("{:#x}", b.logs_index + log_index) },
removed: false,
})
.collect::<Vec<LogResult>>()
},
logs_bloom: format!("{:#x}", data.logs_bloom),
status: format!("{:#x}", data.status_code),
to: b.to,
Expand Down
4 changes: 4 additions & 0 deletions lib/ain-grpc/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ pub fn format_address(hash: H160) -> String {
pub fn format_u256(number: U256) -> String {
format!("{number:#x}")
}

pub fn format_bytes(bytes: Vec<u8>) -> String {
format!("0x{}", String::from_utf8(bytes).unwrap())
}