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

feat: add missing Tx receipt info #1931

Merged
merged 12 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions crates/rpc/rpc-types/src/eth/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ impl From<Index> for U256 {
}
}

impl From<usize> for Index {
fn from(idx: usize) -> Self {
Index(idx)
}
}

impl Serialize for Index {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down
3 changes: 2 additions & 1 deletion crates/rpc/rpc/src/eth/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ where

/// Handler for: `eth_sendTransaction`
async fn send_transaction(&self, _request: TransactionRequest) -> Result<H256> {
Err(internal_rpc_err("unimplemented"))
trace!(target: "rpc::eth", "Serving eth_sendTransaction");
Ok(EthApi::send_transaction(self, _request).await?)
}

/// Handler for: `eth_sendRawTransaction`
Expand Down
67 changes: 55 additions & 12 deletions crates/rpc/rpc/src/eth/api/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ where
None => return Ok(None),
};

Self::build_transaction_receipt(tx, meta, receipt).map(Some)
self.build_transaction_receipt(tx, meta, receipt).await.map(Some)
}
}

Expand Down Expand Up @@ -269,7 +269,8 @@ where
/// Helper function for `eth_getTransactionReceipt`
///
/// Returns the receipt
pub(crate) fn build_transaction_receipt(
pub(crate) async fn build_transaction_receipt(
&self,
tx: TransactionSigned,
meta: TransactionMeta,
mut receipt: Receipt,
Expand All @@ -285,7 +286,7 @@ where
from: transaction.signer(),
to: None,
cumulative_gas_used: U256::from(receipt.cumulative_gas_used),
gas_used: Some(U256::from(0)),
gas_used: None,
chirag-bgh marked this conversation as resolved.
Show resolved Hide resolved
contract_address: None,
logs: std::mem::take(&mut receipt.logs).into_iter().map(Log::from_primitive).collect(),
effective_gas_price: U128::from(0),
Expand All @@ -296,6 +297,36 @@ where
status_code: if receipt.success { Some(U64::from(1)) } else { Some(U64::from(0)) },
};

// TODO maybe add a helper function for this
let prev_tx_hash = match self
.transaction_by_block_and_tx_index(
meta.block_number,
((meta.index - 1) as usize).into(),
chirag-bgh marked this conversation as resolved.
Show resolved Hide resolved
)
.await?
{
Some(tx) => tx.hash,
None => return Err(EthApiError::Unsupported("Invalid Block or Transaction index")),
};
chirag-bgh marked this conversation as resolved.
Show resolved Hide resolved

let prev_receipt = match self.client().receipt_by_hash(prev_tx_hash)? {
Some(recpt) => recpt,
None => return Err(EthApiError::Unsupported("Previous receipt not found")),
};
chirag-bgh marked this conversation as resolved.
Show resolved Hide resolved

let len = res_receipt.logs.len() as u64;
res_receipt.logs.iter_mut().for_each(|log| {
let mut _index = 0;
chirag-bgh marked this conversation as resolved.
Show resolved Hide resolved
log.transaction_hash = Some(meta.tx_hash);
log.transaction_index = Some(U256::from(meta.index));
log.block_hash = Some(meta.block_hash);
log.block_number = Some(U256::from(meta.block_number));
log.log_index = Some(U256::from(meta.index * (len - 1) + _index));
log.transaction_log_index = Some(U256::from(_index));
log.removed = false;
_index += 1;
});

match tx.transaction.kind() {
Create => {
// set contract address if creation was successful
Expand All @@ -310,26 +341,38 @@ where
}

match tx.transaction {
PrimitiveTransaction::Legacy(TxLegacy { gas_limit, gas_price, .. }) => {
// TODO: set actual gas used
res_receipt.gas_used = Some(U256::from(gas_limit));
PrimitiveTransaction::Legacy(TxLegacy { gas_price, .. }) => {
// gas_used = CumulativeGasUsed(this_tx) - CumulativeGasUsed(tx_index - 1)
res_receipt.gas_used = Some(
U256::from(receipt.cumulative_gas_used)
.checked_sub(U256::from(prev_receipt.cumulative_gas_used))
.ok_or(EthApiError::Unsupported("Overflow occured"))?,
);

res_receipt.transaction_type = U256::from(0);
res_receipt.effective_gas_price = U128::from(gas_price);
}
PrimitiveTransaction::Eip2930(TxEip2930 { gas_limit, gas_price, .. }) => {
// TODO: set actual gas used
res_receipt.gas_used = Some(U256::from(gas_limit));
PrimitiveTransaction::Eip2930(TxEip2930 { gas_price, .. }) => {
// gas_used = CumulativeGasUsed(this_tx) - CumulativeGasUsed(tx_index - 1)
res_receipt.gas_used = Some(
U256::from(receipt.cumulative_gas_used)
.checked_sub(U256::from(prev_receipt.cumulative_gas_used))
.ok_or(EthApiError::Unsupported("Overflow occured"))?,
);
res_receipt.transaction_type = U256::from(1);
res_receipt.effective_gas_price = U128::from(gas_price);
}
PrimitiveTransaction::Eip1559(TxEip1559 {
gas_limit,
max_fee_per_gas,
max_priority_fee_per_gas,
..
}) => {
// TODO: set actual gas used
res_receipt.gas_used = Some(U256::from(gas_limit));
// gas_used = CumulativeGasUsed(this_tx) - CumulativeGasUsed(tx_index - 1)
res_receipt.gas_used = Some(
U256::from(receipt.cumulative_gas_used)
.checked_sub(U256::from(prev_receipt.cumulative_gas_used))
.ok_or(EthApiError::Unsupported("Overflow occured"))?,
);
res_receipt.transaction_type = U256::from(2);
res_receipt.effective_gas_price =
U128::from(max_fee_per_gas + max_priority_fee_per_gas)
Expand Down