Skip to content

Commit

Permalink
implement eth_transactionByBlockNumberAndIndex, merge after pull (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-sumner committed Feb 3, 2023
1 parent fa66403 commit 34d2583
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 18 deletions.
10 changes: 6 additions & 4 deletions core/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use eyre::{ErrReport, Error, Result};
use eyre::Result;
use reth_primitives::{
rpc::{BlockId as EthBlockId, BlockNumber},
Bloom, Bytes, H160, H256, H64, U256,
Expand Down Expand Up @@ -447,7 +447,9 @@ pub fn decode_execute_at_address_return(
Ok(segmented_result)
}

fn starknet_tx_into_eth_tx(tx: StarknetTransaction) -> Result<EtherTransaction, Error> {
pub fn starknet_tx_into_eth_tx(
tx: StarknetTransaction,
) -> Result<EtherTransaction, LightClientError> {
let mut ether_tx = EtherTransaction::default();
println!("2.1 Inside Getting transactions");

Expand Down Expand Up @@ -606,13 +608,13 @@ fn starknet_tx_into_eth_tx(tx: StarknetTransaction) -> Result<EtherTransaction,
Ok(ether_tx)
}

fn felt_option_to_u256(element: Option<&FieldElement>) -> Result<U256, Error> {
fn felt_option_to_u256(element: Option<&FieldElement>) -> Result<U256, LightClientError> {
match element {
Some(x) => {
let inner = x.to_bytes_be();
Ok(U256::from_be_bytes(inner))
}
None => Err(ErrReport::new(InvalidFieldElementError)),
None => Ok(U256::from(0)),
}
}

Expand Down
25 changes: 24 additions & 1 deletion core/src/lightclient/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ extern crate hex;

use crate::helpers::{
decode_execute_at_address_return, ethers_block_number_to_starknet_block_id,
starknet_block_to_eth_block, FeltOrFeltArray, MaybePendingStarknetBlock,
starknet_block_to_eth_block, starknet_tx_into_eth_tx, FeltOrFeltArray,
MaybePendingStarknetBlock,
};

use crate::lightclient::types::Transaction as EtherTransaction;
use async_trait::async_trait;
use mockall::predicate::*;
use mockall::*;
use reth_rpc_types::Index;
pub mod constants;
use constants::{
selectors::{BYTECODE, GET_STARKNET_CONTRACT_ADDRESS},
Expand Down Expand Up @@ -63,6 +66,11 @@ pub trait StarknetClient: Send + Sync {
calldata: Bytes,
starknet_block_id: StarknetBlockId,
) -> Result<Bytes, LightClientError>;
async fn transaction_by_block_number_and_index(
&self,
block_id: StarknetBlockId,
tx_index: Index,
) -> Result<EtherTransaction, LightClientError>;
async fn syncing(&self) -> Result<SyncStatus, LightClientError>;
async fn block_transaction_count_by_number(
&self,
Expand Down Expand Up @@ -331,4 +339,19 @@ impl StarknetClient for StarknetClientImpl {
MaybePendingBlockWithTxHashes::PendingBlock(_) => Ok(None),
}
}

async fn transaction_by_block_number_and_index(
&self,
block_id: StarknetBlockId,
tx_index: Index,
) -> Result<EtherTransaction, LightClientError> {
let usize_index: usize = tx_index.into();
let index: u64 = usize_index as u64;
let starknet_tx = self
.client
.get_transaction_by_block_id_and_index(&block_id, index)
.await?;
let eth_tx = starknet_tx_into_eth_tx(starknet_tx)?;
Ok(eth_tx)
}
}
29 changes: 29 additions & 0 deletions docs/methods/eth_getTransactionByBlockNumberAndindex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# eth_getTransactionByBlockNumberAndIndex

## Metadata

- name: eth_getTransactionByBlockNumberAndIndex
- prefix: eth
- state:
- [specification]
- [issue]

## Specification Description

Initial implementation of transaction_by_block_number_and_index

### Parameters

- None

### Returns

- Ethereum Transaction information

## Kakarot Logic

Does not interact with Kakarot

### Starknet methods

Implements and calls transaction_by_block_number_and_index in Starknet light client
8 changes: 8 additions & 0 deletions rpc-call-examples/transactionByBlockNumberAndIndex.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
POST http://127.0.0.1:3030
Content-Type: application/json
{
"jsonrpc":"2.0","method":"eth_getTransactionByBlockNumberAndIndex","params":[
"123",
2
],"id":3
}
31 changes: 18 additions & 13 deletions rpc/src/eth_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use reth_primitives::{
Address, Bytes, H64, U256, U64,
};
use reth_rpc_types::{
CallRequest, EIP1186AccountProofResponse, FeeHistory, Index, SyncStatus, Transaction,
TransactionReceipt, TransactionRequest, Work,
CallRequest, EIP1186AccountProofResponse, FeeHistory, Index, SyncStatus, TransactionReceipt,
TransactionRequest, Work,
};
use serde_json::Value;

use kakarot_rpc_core::lightclient::types::Transaction as EtherTransaction;

/// The RPC module for the Ethereum protocol required by Kakarot.
///
///
Expand Down Expand Up @@ -88,23 +90,23 @@ trait EthApi {

/// Returns the information about a transaction requested by transaction hash.
#[method(name = "eth_getTransactionByHash")]
async fn transaction_by_hash(&self, hash: H256) -> Result<Option<Transaction>>;
async fn transaction_by_hash(&self, hash: H256) -> Result<Option<EtherTransaction>>;

/// Returns information about a transaction by block hash and transaction index position.
#[method(name = "eth_getTransactionByBlockHashAndIndex")]
async fn transaction_by_block_hash_and_index(
&self,
hash: H256,
index: Index,
) -> Result<Option<Transaction>>;
) -> Result<Option<EtherTransaction>>;

/// Returns information about a transaction by block number and transaction index position.
#[method(name = "eth_getTransactionByBlockNumberAndIndex")]
async fn transaction_by_block_number_and_index(
&self,
number: BlockNumber,
index: Index,
) -> Result<Option<Transaction>>;
) -> Result<Option<EtherTransaction>>;

/// Returns the receipt of a transaction by transaction hash.
#[method(name = "eth_getTransactionReceipt")]
Expand Down Expand Up @@ -350,11 +352,8 @@ impl EthApiServer for KakarotEthRpc {
todo!()
}

async fn transaction_by_hash(
&self,
_hash: H256,
) -> Result<Option<reth_rpc_types::Transaction>> {
let ether_tx = Transaction::default();
async fn transaction_by_hash(&self, _hash: H256) -> Result<Option<EtherTransaction>> {
let ether_tx = EtherTransaction::default();

Ok(Some(ether_tx))
}
Expand All @@ -363,16 +362,22 @@ impl EthApiServer for KakarotEthRpc {
&self,
_hash: H256,
_index: Index,
) -> Result<Option<reth_rpc_types::Transaction>> {
) -> Result<Option<EtherTransaction>> {
todo!()
}

async fn transaction_by_block_number_and_index(
&self,
_number: BlockNumber,
_index: Index,
) -> Result<Option<reth_rpc_types::Transaction>> {
todo!()
) -> Result<Option<EtherTransaction>> {
let block_id = BlockId::Number(_number);
let starknet_block_id = ethers_block_id_to_starknet_block_id(block_id)?;
let tx = self
.starknet_client
.transaction_by_block_number_and_index(starknet_block_id, _index)
.await?;
Ok(Some(tx))
}

async fn transaction_receipt(&self, _hash: H256) -> Result<Option<TransactionReceipt>> {
Expand Down

0 comments on commit 34d2583

Please sign in to comment.