gasPrice is required field for Transaction object (#481)

This commit is contained in:
Dusan Stanivukovic 2021-07-12 15:22:27 +02:00 committed by GitHub
parent 33908e8361
commit eb42f0c5d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 43 deletions

View File

@ -52,6 +52,7 @@ impl<C: BlockChainClient + 'static> Debug for DebugClient<C> {
.map(|(block, reason)| { .map(|(block, reason)| {
let number = block.header.number(); let number = block.header.number();
let hash = block.header.hash(); let hash = block.header.hash();
let base_fee = block.header.base_fee();
RichBlock { RichBlock {
inner: Block { inner: Block {
hash: Some(hash), hash: Some(hash),
@ -76,7 +77,7 @@ impl<C: BlockChainClient + 'static> Debug for DebugClient<C> {
.cloned() .cloned()
.map(Into::into) .map(Into::into)
.collect(), .collect(),
base_fee_per_gas: block.header.base_fee(), base_fee_per_gas: base_fee,
uncles: block.uncles.iter().map(Header::hash).collect(), uncles: block.uncles.iter().map(Header::hash).collect(),
transactions: BlockTransactions::Full( transactions: BlockTransactions::Full(
block block
@ -84,13 +85,16 @@ impl<C: BlockChainClient + 'static> Debug for DebugClient<C> {
.into_iter() .into_iter()
.enumerate() .enumerate()
.map(|(transaction_index, signed)| { .map(|(transaction_index, signed)| {
Transaction::from_localized(LocalizedTransaction { Transaction::from_localized(
block_number: number, LocalizedTransaction {
block_hash: hash, block_number: number,
transaction_index, block_hash: hash,
signed, transaction_index,
cached_sender: None, signed,
}) cached_sender: None,
},
base_fee,
)
}) })
.collect(), .collect(),
), ),

View File

@ -260,6 +260,11 @@ where
(Some(block), Some(total_difficulty)) => { (Some(block), Some(total_difficulty)) => {
let view = block.header_view(); let view = block.header_view();
let eip1559_enabled = client.engine().schedule(view.number()).eip1559; let eip1559_enabled = client.engine().schedule(view.number()).eip1559;
let base_fee = if eip1559_enabled {
Some(view.base_fee())
} else {
None
};
Ok(Some(RichBlock { Ok(Some(RichBlock {
inner: Block { inner: Block {
hash: match is_pending { hash: match is_pending {
@ -292,13 +297,7 @@ where
.into_iter() .into_iter()
.map(Into::into) .map(Into::into)
.collect(), .collect(),
base_fee_per_gas: { base_fee_per_gas: base_fee,
if eip1559_enabled {
Some(view.base_fee())
} else {
None
}
},
uncles: block.uncle_hashes(), uncles: block.uncle_hashes(),
transactions: match include_txs { transactions: match include_txs {
true => BlockTransactions::Full( true => BlockTransactions::Full(
@ -306,7 +305,7 @@ where
.view() .view()
.localized_transactions() .localized_transactions()
.into_iter() .into_iter()
.map(Transaction::from_localized) .map(|t| Transaction::from_localized(t, base_fee))
.collect(), .collect(),
), ),
false => BlockTransactions::Hashes(block.transaction_hashes()), false => BlockTransactions::Hashes(block.transaction_hashes()),
@ -322,7 +321,23 @@ where
fn transaction(&self, id: PendingTransactionId) -> Result<Option<Transaction>> { fn transaction(&self, id: PendingTransactionId) -> Result<Option<Transaction>> {
let client_transaction = |id| match self.client.block_transaction(id) { 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), None => Ok(None),
}; };
@ -361,7 +376,7 @@ where
cached_sender, cached_sender,
} }
}) })
.map(Transaction::from_localized); .map(|t| Transaction::from_localized(t, pending_block.header.base_fee()));
Ok(transaction) Ok(transaction)
} }

View File

@ -229,7 +229,7 @@ mod tests {
let serialized = serde_json::to_string(&t).unwrap(); let serialized = serde_json::to_string(&t).unwrap();
assert_eq!( assert_eq!(
serialized, 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()]); let t = BlockTransactions::Hashes(vec![H256::default().into()]);

View File

@ -50,8 +50,7 @@ pub struct Transaction {
/// Transfered value /// Transfered value
pub value: U256, pub value: U256,
/// Gas Price /// Gas Price
#[serde(skip_serializing_if = "Option::is_none")] pub gas_price: U256,
pub gas_price: Option<U256>,
/// Max fee per gas /// Max fee per gas
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub max_fee_per_gas: Option<U256>, pub max_fee_per_gas: Option<U256>,
@ -190,7 +189,7 @@ impl RichRawTransaction {
impl Transaction { impl Transaction {
/// Convert `LocalizedTransaction` into RPC Transaction. /// Convert `LocalizedTransaction` into RPC Transaction.
pub fn from_localized(mut t: LocalizedTransaction) -> Transaction { pub fn from_localized(mut t: LocalizedTransaction, base_fee: Option<U256>) -> Transaction {
let signature = t.signature(); let signature = t.signature();
let scheme = CreateContractAddress::FromSenderAndNonce; let scheme = CreateContractAddress::FromSenderAndNonce;
@ -209,17 +208,11 @@ impl Transaction {
TypedTransaction::Legacy(_) => None, TypedTransaction::Legacy(_) => None,
}; };
let (gas_price, max_fee_per_gas) = match t.as_unsigned() { let (max_fee_per_gas, max_priority_fee_per_gas) =
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 =
if let TypedTransaction::EIP1559Transaction(tx) = t.as_unsigned() { 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 { } else {
None (None, None)
}; };
let standard_v = if t.tx_type() == TypedTxId::Legacy { let standard_v = if t.tx_type() == TypedTxId::Legacy {
@ -240,7 +233,7 @@ impl Transaction {
Action::Call(ref address) => Some(*address), Action::Call(ref address) => Some(*address),
}, },
value: t.tx().value, value: t.tx().value,
gas_price, gas_price: t.effective_gas_price(base_fee),
max_fee_per_gas, max_fee_per_gas,
gas: t.tx().gas, gas: t.tx().gas,
input: Bytes::new(t.tx().data.clone()), input: Bytes::new(t.tx().data.clone()),
@ -284,17 +277,11 @@ impl Transaction {
TypedTransaction::Legacy(_) => None, TypedTransaction::Legacy(_) => None,
}; };
let (gas_price, max_fee_per_gas) = match t.as_unsigned() { let (max_fee_per_gas, max_priority_fee_per_gas) =
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 =
if let TypedTransaction::EIP1559Transaction(tx) = t.as_unsigned() { 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 { } else {
None (None, None)
}; };
let standard_v = if t.tx_type() == TypedTxId::Legacy { let standard_v = if t.tx_type() == TypedTxId::Legacy {
@ -315,7 +302,7 @@ impl Transaction {
Action::Call(ref address) => Some(*address), Action::Call(ref address) => Some(*address),
}, },
value: t.tx().value, value: t.tx().value,
gas_price, gas_price: t.tx().gas_price,
max_fee_per_gas, max_fee_per_gas,
gas: t.tx().gas, gas: t.tx().gas,
input: Bytes::new(t.tx().data.clone()), input: Bytes::new(t.tx().data.clone()),
@ -387,7 +374,7 @@ mod tests {
let serialized = serde_json::to_string(&t).unwrap(); let serialized = serde_json::to_string(&t).unwrap();
assert_eq!( assert_eq!(
serialized, 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":[]}]}"#
); );
} }