From eb42f0c5d95eeb94899507ef6dfaea8db45553fc Mon Sep 17 00:00:00 2001 From: Dusan Stanivukovic Date: Mon, 12 Jul 2021 15:22:27 +0200 Subject: [PATCH] gasPrice is required field for Transaction object (#481) --- crates/rpc/src/v1/impls/debug.rs | 20 +++++++++------ crates/rpc/src/v1/impls/eth.rs | 35 ++++++++++++++++++-------- crates/rpc/src/v1/types/block.rs | 2 +- crates/rpc/src/v1/types/transaction.rs | 35 ++++++++------------------ 4 files changed, 49 insertions(+), 43 deletions(-) diff --git a/crates/rpc/src/v1/impls/debug.rs b/crates/rpc/src/v1/impls/debug.rs index 6c22fa780..d2998d624 100644 --- a/crates/rpc/src/v1/impls/debug.rs +++ b/crates/rpc/src/v1/impls/debug.rs @@ -52,6 +52,7 @@ impl Debug for DebugClient { .map(|(block, reason)| { let number = block.header.number(); let hash = block.header.hash(); + let base_fee = block.header.base_fee(); RichBlock { inner: Block { hash: Some(hash), @@ -76,7 +77,7 @@ impl Debug for DebugClient { .cloned() .map(Into::into) .collect(), - base_fee_per_gas: block.header.base_fee(), + base_fee_per_gas: base_fee, uncles: block.uncles.iter().map(Header::hash).collect(), transactions: BlockTransactions::Full( block @@ -84,13 +85,16 @@ impl Debug for DebugClient { .into_iter() .enumerate() .map(|(transaction_index, signed)| { - Transaction::from_localized(LocalizedTransaction { - block_number: number, - block_hash: hash, - transaction_index, - signed, - cached_sender: None, - }) + Transaction::from_localized( + LocalizedTransaction { + block_number: number, + block_hash: hash, + transaction_index, + signed, + cached_sender: None, + }, + base_fee, + ) }) .collect(), ), diff --git a/crates/rpc/src/v1/impls/eth.rs b/crates/rpc/src/v1/impls/eth.rs index ba3f0b107..c86df8b76 100644 --- a/crates/rpc/src/v1/impls/eth.rs +++ b/crates/rpc/src/v1/impls/eth.rs @@ -260,6 +260,11 @@ where (Some(block), Some(total_difficulty)) => { let view = block.header_view(); let eip1559_enabled = client.engine().schedule(view.number()).eip1559; + let base_fee = if eip1559_enabled { + Some(view.base_fee()) + } else { + None + }; Ok(Some(RichBlock { inner: Block { hash: match is_pending { @@ -292,13 +297,7 @@ where .into_iter() .map(Into::into) .collect(), - base_fee_per_gas: { - if eip1559_enabled { - Some(view.base_fee()) - } else { - None - } - }, + base_fee_per_gas: base_fee, uncles: block.uncle_hashes(), transactions: match include_txs { true => BlockTransactions::Full( @@ -306,7 +305,7 @@ where .view() .localized_transactions() .into_iter() - .map(Transaction::from_localized) + .map(|t| Transaction::from_localized(t, base_fee)) .collect(), ), false => BlockTransactions::Hashes(block.transaction_hashes()), @@ -322,7 +321,23 @@ where fn transaction(&self, id: PendingTransactionId) -> Result> { let client_transaction = |id| match self.client.block_transaction(id) { - Some(t) => Ok(Some(Transaction::from_localized(t))), + Some(t) => { + let block = self + .rich_block(BlockNumber::Num(t.block_number).into(), false) + .and_then(errors::check_block_number_existence( + &*self.client, + BlockNumber::Num(t.block_number).into(), + self.options, + )); + let base_fee = match block { + Ok(block) => match block { + Some(block) => block.base_fee_per_gas, + None => return Ok(None), + }, + Err(_) => return Ok(None), + }; + Ok(Some(Transaction::from_localized(t, base_fee))) + } None => Ok(None), }; @@ -361,7 +376,7 @@ where cached_sender, } }) - .map(Transaction::from_localized); + .map(|t| Transaction::from_localized(t, pending_block.header.base_fee())); Ok(transaction) } diff --git a/crates/rpc/src/v1/types/block.rs b/crates/rpc/src/v1/types/block.rs index 5eb989e59..0286e5272 100644 --- a/crates/rpc/src/v1/types/block.rs +++ b/crates/rpc/src/v1/types/block.rs @@ -229,7 +229,7 @@ mod tests { let serialized = serde_json::to_string(&t).unwrap(); assert_eq!( serialized, - r#"[{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x0","gas":"0x0","input":"0x","creates":null,"raw":"0x","publicKey":null,"chainId":null,"v":"0x0","r":"0x0","s":"0x0","condition":null}]"# + r#"[{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":"0x","creates":null,"raw":"0x","publicKey":null,"chainId":null,"v":"0x0","r":"0x0","s":"0x0","condition":null}]"# ); let t = BlockTransactions::Hashes(vec![H256::default().into()]); diff --git a/crates/rpc/src/v1/types/transaction.rs b/crates/rpc/src/v1/types/transaction.rs index c731ffe92..220a701a9 100644 --- a/crates/rpc/src/v1/types/transaction.rs +++ b/crates/rpc/src/v1/types/transaction.rs @@ -50,8 +50,7 @@ pub struct Transaction { /// Transfered value pub value: U256, /// Gas Price - #[serde(skip_serializing_if = "Option::is_none")] - pub gas_price: Option, + pub gas_price: U256, /// Max fee per gas #[serde(skip_serializing_if = "Option::is_none")] pub max_fee_per_gas: Option, @@ -190,7 +189,7 @@ impl RichRawTransaction { impl Transaction { /// Convert `LocalizedTransaction` into RPC Transaction. - pub fn from_localized(mut t: LocalizedTransaction) -> Transaction { + pub fn from_localized(mut t: LocalizedTransaction, base_fee: Option) -> Transaction { let signature = t.signature(); let scheme = CreateContractAddress::FromSenderAndNonce; @@ -209,17 +208,11 @@ impl Transaction { TypedTransaction::Legacy(_) => None, }; - let (gas_price, max_fee_per_gas) = match t.as_unsigned() { - TypedTransaction::Legacy(_) => (Some(t.tx().gas_price), None), - TypedTransaction::AccessList(_) => (Some(t.tx().gas_price), None), - TypedTransaction::EIP1559Transaction(_) => (None, Some(t.tx().gas_price)), - }; - - let max_priority_fee_per_gas = + let (max_fee_per_gas, max_priority_fee_per_gas) = if let TypedTransaction::EIP1559Transaction(tx) = t.as_unsigned() { - Some(tx.max_priority_fee_per_gas) + (Some(tx.tx().gas_price), Some(tx.max_priority_fee_per_gas)) } else { - None + (None, None) }; let standard_v = if t.tx_type() == TypedTxId::Legacy { @@ -240,7 +233,7 @@ impl Transaction { Action::Call(ref address) => Some(*address), }, value: t.tx().value, - gas_price, + gas_price: t.effective_gas_price(base_fee), max_fee_per_gas, gas: t.tx().gas, input: Bytes::new(t.tx().data.clone()), @@ -284,17 +277,11 @@ impl Transaction { TypedTransaction::Legacy(_) => None, }; - let (gas_price, max_fee_per_gas) = match t.as_unsigned() { - TypedTransaction::Legacy(_) => (Some(t.tx().gas_price), None), - TypedTransaction::AccessList(_) => (Some(t.tx().gas_price), None), - TypedTransaction::EIP1559Transaction(_) => (None, Some(t.tx().gas_price)), - }; - - let max_priority_fee_per_gas = + let (max_fee_per_gas, max_priority_fee_per_gas) = if let TypedTransaction::EIP1559Transaction(tx) = t.as_unsigned() { - Some(tx.max_priority_fee_per_gas) + (Some(tx.tx().gas_price), Some(tx.max_priority_fee_per_gas)) } else { - None + (None, None) }; let standard_v = if t.tx_type() == TypedTxId::Legacy { @@ -315,7 +302,7 @@ impl Transaction { Action::Call(ref address) => Some(*address), }, value: t.tx().value, - gas_price, + gas_price: t.tx().gas_price, max_fee_per_gas, gas: t.tx().gas, input: Bytes::new(t.tx().data.clone()), @@ -387,7 +374,7 @@ mod tests { let serialized = serde_json::to_string(&t).unwrap(); assert_eq!( serialized, - r#"{"type":"0x1","hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x0","gas":"0x0","input":"0x","creates":null,"raw":"0x","publicKey":null,"chainId":null,"v":"0x0","r":"0x0","s":"0x0","condition":null,"accessList":[{"address":"0x0000000000000000000000000000000000000000","storageKeys":[]}]}"# + r#"{"type":"0x1","hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":"0x","creates":null,"raw":"0x","publicKey":null,"chainId":null,"v":"0x0","r":"0x0","s":"0x0","condition":null,"accessList":[{"address":"0x0000000000000000000000000000000000000000","storageKeys":[]}]}"# ); }