From 7ccdbefc53fe32e6e34d783a1b814406014590f3 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Mon, 15 May 2023 14:44:48 +0800 Subject: [PATCH 1/8] Fix receipts result output --- lib/ain-grpc/src/receipt.rs | 38 ++++++++++++++++++++++++++++++++++--- lib/ain-grpc/src/utils.rs | 4 ++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/ain-grpc/src/receipt.rs b/lib/ain-grpc/src/receipt.rs index 4416325dc2..80e41030f7 100644 --- a/lib/ain-grpc/src/receipt.rs +++ b/lib/ain-grpc/src/receipt.rs @@ -1,6 +1,21 @@ use ain_evm::receipt::Receipt; -use ethereum::{EIP658ReceiptData, Log}; +use ethereum::EIP658ReceiptData; use primitive_types::{H160, H256, U256}; +use crate::utils::format_bytes; + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub struct LogResult { + pub address: H160, + pub topics: Vec, + pub data: String, + pub block_number: U256, + pub block_hash: H256, + pub transaction_hash: H256, + pub transaction_index: String, + pub log_index: usize, + pub removed: bool +} #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(rename_all = "camelCase")] @@ -12,7 +27,7 @@ pub struct ReceiptResult { pub effective_gas_price: U256, pub from: H160, pub gas_used: U256, - pub logs: Vec, + pub logs: Vec, pub logs_bloom: String, pub status: String, pub to: Option, @@ -32,7 +47,24 @@ impl From for ReceiptResult { effective_gas_price: Default::default(), from: b.from, gas_used: data.used_gas, - logs: data.logs, + logs: { + let mut log_index = 0; + + data.logs.iter().map(|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: { + log_index += 1; + log_index - 1 + }, + removed: false, + }).collect::>() + }, logs_bloom: format!("{:#x}", data.logs_bloom), status: format!("{:#x}", data.status_code), to: b.to, diff --git a/lib/ain-grpc/src/utils.rs b/lib/ain-grpc/src/utils.rs index c89e2ca3f8..861814661b 100644 --- a/lib/ain-grpc/src/utils.rs +++ b/lib/ain-grpc/src/utils.rs @@ -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) -> String { + format!("0x{}", String::from_utf8(bytes).unwrap()) +} \ No newline at end of file From e18c94c0cd77ea53f134e1667ebfefd5d56b1a8a Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Mon, 15 May 2023 14:48:39 +0800 Subject: [PATCH 2/8] Formatting --- lib/ain-grpc/src/receipt.rs | 35 +++++++++++++++++++---------------- lib/ain-grpc/src/utils.rs | 2 +- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/ain-grpc/src/receipt.rs b/lib/ain-grpc/src/receipt.rs index 80e41030f7..b2d06a7d99 100644 --- a/lib/ain-grpc/src/receipt.rs +++ b/lib/ain-grpc/src/receipt.rs @@ -1,7 +1,7 @@ +use crate::utils::format_bytes; use ain_evm::receipt::Receipt; use ethereum::EIP658ReceiptData; use primitive_types::{H160, H256, U256}; -use crate::utils::format_bytes; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(rename_all = "camelCase")] @@ -14,7 +14,7 @@ pub struct LogResult { pub transaction_hash: H256, pub transaction_index: String, pub log_index: usize, - pub removed: bool + pub removed: bool, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] @@ -50,20 +50,23 @@ impl From for ReceiptResult { logs: { let mut log_index = 0; - data.logs.iter().map(|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: { - log_index += 1; - log_index - 1 - }, - removed: false, - }).collect::>() + data.logs + .iter() + .map(|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: { + log_index += 1; + log_index - 1 + }, + removed: false, + }) + .collect::>() }, logs_bloom: format!("{:#x}", data.logs_bloom), status: format!("{:#x}", data.status_code), diff --git a/lib/ain-grpc/src/utils.rs b/lib/ain-grpc/src/utils.rs index 861814661b..e41669aad9 100644 --- a/lib/ain-grpc/src/utils.rs +++ b/lib/ain-grpc/src/utils.rs @@ -14,4 +14,4 @@ pub fn format_u256(number: U256) -> String { pub fn format_bytes(bytes: Vec) -> String { format!("0x{}", String::from_utf8(bytes).unwrap()) -} \ No newline at end of file +} From 5bb9822deac62011740d741dbb02085c799d7283 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Mon, 15 May 2023 14:58:35 +0800 Subject: [PATCH 3/8] Return hex encoded index --- lib/ain-grpc/src/receipt.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ain-grpc/src/receipt.rs b/lib/ain-grpc/src/receipt.rs index b2d06a7d99..644b7d60f6 100644 --- a/lib/ain-grpc/src/receipt.rs +++ b/lib/ain-grpc/src/receipt.rs @@ -13,7 +13,7 @@ pub struct LogResult { pub block_hash: H256, pub transaction_hash: H256, pub transaction_index: String, - pub log_index: usize, + pub log_index: String, pub removed: bool, } @@ -62,7 +62,7 @@ impl From for ReceiptResult { transaction_index: format!("{:#x}", b.tx_index), log_index: { log_index += 1; - log_index - 1 + format!("{:#x}", log_index - 1) }, removed: false, }) From a420074e4c87f47fafba891a527bdc33d701cca0 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Mon, 15 May 2023 15:17:36 +0800 Subject: [PATCH 4/8] Make logIndex index across block instead of transaction --- lib/ain-evm/src/receipt.rs | 12 ++++++++++-- lib/ain-grpc/src/receipt.rs | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/ain-evm/src/receipt.rs b/lib/ain-evm/src/receipt.rs index 339291568b..aa542a8e19 100644 --- a/lib/ain-evm/src/receipt.rs +++ b/lib/ain-evm/src/receipt.rs @@ -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; @@ -20,6 +20,7 @@ pub struct Receipt { pub tx_index: usize, pub tx_type: u8, pub contract_address: Option, + pub logs_index: usize, } pub struct ReceiptHandler { @@ -54,12 +55,13 @@ impl ReceiptHandler { block_hash: H256, block_number: U256, ) -> Vec { + let mut logs_size = 0; 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(), @@ -71,6 +73,12 @@ impl ReceiptHandler { .to() .is_none() .then(|| get_contract_address(&signed_tx.sender, &signed_tx.nonce())), + logs_index: { + let logs_len = EIP658ReceiptData::from(receipt).logs.len(); + logs_size += logs_len; + + logs_size - logs_len + } }) .collect() } diff --git a/lib/ain-grpc/src/receipt.rs b/lib/ain-grpc/src/receipt.rs index 644b7d60f6..d0ae367280 100644 --- a/lib/ain-grpc/src/receipt.rs +++ b/lib/ain-grpc/src/receipt.rs @@ -62,7 +62,7 @@ impl From for ReceiptResult { transaction_index: format!("{:#x}", b.tx_index), log_index: { log_index += 1; - format!("{:#x}", log_index - 1) + format!("{:#x}", b.logs_index + log_index - 1) }, removed: false, }) From 82e0cfeab43cdcfa8341e457eab311af11cea7a3 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Mon, 15 May 2023 15:26:37 +0800 Subject: [PATCH 5/8] Formatting --- lib/ain-evm/src/receipt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ain-evm/src/receipt.rs b/lib/ain-evm/src/receipt.rs index aa542a8e19..52f6363b4b 100644 --- a/lib/ain-evm/src/receipt.rs +++ b/lib/ain-evm/src/receipt.rs @@ -78,7 +78,7 @@ impl ReceiptHandler { logs_size += logs_len; logs_size - logs_len - } + }, }) .collect() } From fa86c5c44cdd2cc983f84ad6559178b85b2b1760 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Mon, 15 May 2023 15:38:11 +0800 Subject: [PATCH 6/8] Track cumulative gas usage --- lib/ain-evm/src/receipt.rs | 10 +++++++++- lib/ain-grpc/src/receipt.rs | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/ain-evm/src/receipt.rs b/lib/ain-evm/src/receipt.rs index 52f6363b4b..178f276a56 100644 --- a/lib/ain-evm/src/receipt.rs +++ b/lib/ain-evm/src/receipt.rs @@ -21,6 +21,7 @@ pub struct Receipt { pub tx_type: u8, pub contract_address: Option, pub logs_index: usize, + pub cumulative_gas: U256, } pub struct ReceiptHandler { @@ -56,6 +57,8 @@ impl ReceiptHandler { block_number: U256, ) -> Vec { let mut logs_size = 0; + let mut cumulative_gas = U256::zero(); + transactions .iter() .enumerate() @@ -74,11 +77,16 @@ impl ReceiptHandler { .is_none() .then(|| get_contract_address(&signed_tx.sender, &signed_tx.nonce())), logs_index: { - let logs_len = EIP658ReceiptData::from(receipt).logs.len(); + let logs_len = EIP658ReceiptData::from(receipt.clone()).logs.len(); logs_size += logs_len; logs_size - logs_len }, + cumulative_gas: { + cumulative_gas = cumulative_gas + EIP658ReceiptData::from(receipt).used_gas; + + cumulative_gas + }, }) .collect() } diff --git a/lib/ain-grpc/src/receipt.rs b/lib/ain-grpc/src/receipt.rs index d0ae367280..3eb81eb01e 100644 --- a/lib/ain-grpc/src/receipt.rs +++ b/lib/ain-grpc/src/receipt.rs @@ -43,7 +43,7 @@ impl From 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, From 97cd815c6a202df52eaef9fddba0daf74fd95a9d Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Mon, 15 May 2023 15:55:06 +0800 Subject: [PATCH 7/8] Use enumerate --- lib/ain-grpc/src/receipt.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/ain-grpc/src/receipt.rs b/lib/ain-grpc/src/receipt.rs index 3eb81eb01e..7c8630eb22 100644 --- a/lib/ain-grpc/src/receipt.rs +++ b/lib/ain-grpc/src/receipt.rs @@ -48,11 +48,10 @@ impl From for ReceiptResult { from: b.from, gas_used: data.used_gas, logs: { - let mut log_index = 0; - data.logs .iter() - .map(|x| LogResult { + .enumerate() + .map(|(index, x)| LogResult { address: x.clone().address, topics: x.clone().topics, data: format_bytes(x.data.to_ascii_lowercase()), @@ -60,10 +59,7 @@ impl From for ReceiptResult { block_hash: b.block_hash, transaction_hash: b.tx_hash, transaction_index: format!("{:#x}", b.tx_index), - log_index: { - log_index += 1; - format!("{:#x}", b.logs_index + log_index - 1) - }, + log_index: { format!("{:#x}", b.logs_index + log_index) }, removed: false, }) .collect::>() From a7e29d3fe868135db58b7b0d176a30c775891ea4 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Mon, 15 May 2023 17:47:27 +0800 Subject: [PATCH 8/8] Fix build and warnings --- lib/ain-evm/src/receipt.rs | 2 +- lib/ain-grpc/src/receipt.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ain-evm/src/receipt.rs b/lib/ain-evm/src/receipt.rs index 178f276a56..114907e2e0 100644 --- a/lib/ain-evm/src/receipt.rs +++ b/lib/ain-evm/src/receipt.rs @@ -83,7 +83,7 @@ impl ReceiptHandler { logs_size - logs_len }, cumulative_gas: { - cumulative_gas = cumulative_gas + EIP658ReceiptData::from(receipt).used_gas; + cumulative_gas += EIP658ReceiptData::from(receipt).used_gas; cumulative_gas }, diff --git a/lib/ain-grpc/src/receipt.rs b/lib/ain-grpc/src/receipt.rs index 7c8630eb22..2598c0817b 100644 --- a/lib/ain-grpc/src/receipt.rs +++ b/lib/ain-grpc/src/receipt.rs @@ -51,7 +51,7 @@ impl From for ReceiptResult { data.logs .iter() .enumerate() - .map(|(index, x)| LogResult { + .map(|(log_index, x)| LogResult { address: x.clone().address, topics: x.clone().topics, data: format_bytes(x.data.to_ascii_lowercase()),