Skip to content

Commit

Permalink
feat: add eth_getTransactionByBlockHashAndIndex and eth_getTransactio…
Browse files Browse the repository at this point in the history
…nByBlockNumberAndIndex (#159)
  • Loading branch information
nbaztec authored Oct 5, 2023
1 parent 4f627b5 commit 79f3dae
Show file tree
Hide file tree
Showing 6 changed files with 600 additions and 22 deletions.
66 changes: 62 additions & 4 deletions SUPPORTED_APIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ The `status` options are:
| [`ETH`](#eth-namespace) | [`eth_getFilterLogs`](#eth_getfilterlogs) | `SUPPORTED` | Returns an array of all logs matching filter with given id |
| [`ETH`](#eth-namespace) | [`eth_getLogs`](#eth_getlogs) | `SUPPORTED` | Returns an array of all logs matching a given filter object |
| `ETH` | `eth_getProof` | `NOT IMPLEMENTED` | Returns the details for the account at the specified address and block number, the account's Merkle proof, and the storage values for the specified storage keys with their Merkle-proofs |
| [`ETH`](#eth-namespace) | `eth_getStorageAt`(#`eth_getstorageat) | `SUPPORTED` | Returns the value from a storage position at a given address |
| `ETH` | `eth_getTransactionByBlockHashAndIndex` | `NOT IMPLEMENTED`<br />[GitHub Issue #46](https://github.com/matter-labs/era-test-node/issues/46) | Returns information about a transaction by block hash and transaction index position |
| `ETH` | `eth_getTransactionByBlockNumberAndIndex` | `NOT IMPLEMENTED`<br />[GitHub Issue #47](https://github.com/matter-labs/era-test-node/issues/47) | Returns information about a transaction by block number and transaction index position |
| [`ETH`](#eth-namespace) | [`eth_getStorageAt`](#eth_getstorageat) | `SUPPORTED` | Returns the value from a storage position at a given address |
| [`ETH`](#eth-namespace) | [`eth_getTransactionByBlockHashAndIndex`](#eth_gettransactionbyblockhashandindex) | `SUPPORTED` | Returns information about a transaction by block hash and transaction index position |
| [`ETH`](#eth-namespace) | [`eth_getTransactionByBlockNumberAndIndex`](#eth_gettransactionbyblocknumberandindex) | `SUPPORTED` | Returns information about a transaction by block number and transaction index position |
| [`ETH`](#eth-namespace) | [`eth_getTransactionReceipt`](#eth_gettransactionreceipt) | `SUPPORTED` | Returns the receipt of a transaction by transaction hash |
| `ETH` | `eth_getUncleByBlockHashAndIndex` | `NOT IMPLEMENTED` | Returns information about a uncle of a block by hash and uncle index position |
| `ETH` | `eth_getUncleByBlockNumberAndIndex` | `NOT IMPLEMENTED` | Returns information about a uncle of a block by hash and uncle index position |
Expand Down Expand Up @@ -784,7 +784,6 @@ curl --request POST \
}'
```


### `eth_getFilterLogs`

[source](src/node.rs)
Expand Down Expand Up @@ -1101,6 +1100,65 @@ curl --request POST \
}'
```

### `eth_getTransactionByBlockHashAndIndex`

[source](src/node.rs)

Returns information about a transaction by block hash and transaction index position

#### Arguments

+ `block_hash: H256`
+ `index: U64`

#### Status

`SUPPORTED`

#### Example

```bash
curl --request POST \
--url http://localhost:8011/ \
--header 'content-type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": "1",
"method": "eth_getTransactionByBlockHashAndIndex",
"params": ["0x0000000000000000000000000000000000000000000000000000000000000008", "0x1"]
}'
```

### `eth_getTransactionByBlockNumberAndIndex`

[source](src/node.rs)

Returns information about a transaction by block number and transaction index position

#### Arguments

+ `block_number: BlockNumber`
+ `index: U64`

#### Status

`SUPPORTED`

#### Example

```bash
curl --request POST \
--url http://localhost:8011/ \
--header 'content-type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": "1",
"method": "eth_getTransactionByBlockNumberAndIndex",
"params": ["latest", "0x1"]
}'
```


## `HARDHAT NAMESPACE`

### `hardhat_setBalance`
Expand Down
18 changes: 17 additions & 1 deletion src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ use zksync_types::{
use zksync_state::ReadStorage;
use zksync_utils::{bytecode::hash_bytecode, h256_to_u256};

use zksync_web3_decl::{jsonrpsee::http_client::HttpClient, namespaces::EthNamespaceClient};
use zksync_web3_decl::{
jsonrpsee::http_client::HttpClient, namespaces::EthNamespaceClient, types::Index,
};
use zksync_web3_decl::{jsonrpsee::http_client::HttpClientBuilder, namespaces::ZksNamespaceClient};

use crate::{cache::CacheConfig, node::TEST_NODE_NETWORK_ID};
Expand Down Expand Up @@ -231,6 +233,20 @@ pub trait ForkSource {
&self,
block_number: zksync_types::api::BlockNumber,
) -> eyre::Result<Option<U256>>;

/// Returns information about a transaction by block hash and transaction index position.
fn get_transaction_by_block_hash_and_index(
&self,
block_hash: H256,
index: Index,
) -> eyre::Result<Option<Transaction>>;

/// Returns information about a transaction by block number and transaction index position.
fn get_transaction_by_block_number_and_index(
&self,
block_number: BlockNumber,
index: Index,
) -> eyre::Result<Option<Transaction>>;
}

/// Holds the information about the original chain.
Expand Down
32 changes: 32 additions & 0 deletions src/http_fork_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use std::sync::RwLock;

use eyre::Context;
use zksync_basic_types::{H256, U256};
use zksync_types::api::Transaction;
use zksync_web3_decl::{
jsonrpsee::http_client::{HttpClient, HttpClientBuilder},
namespaces::{EthNamespaceClient, ZksNamespaceClient},
types::Index,
};

use crate::{
Expand Down Expand Up @@ -220,6 +222,36 @@ impl ForkSource for HttpForkSource {
})
.wrap_err("fork http client failed")
}

/// Returns information about a transaction by block hash and transaction index position.
fn get_transaction_by_block_hash_and_index(
&self,
block_hash: H256,
index: Index,
) -> eyre::Result<Option<Transaction>> {
let client = self.create_client();
block_on(async move {
client
.get_transaction_by_block_hash_and_index(block_hash, index)
.await
})
.wrap_err("fork http client failed")
}

/// Returns information about a transaction by block number and transaction index position.
fn get_transaction_by_block_number_and_index(
&self,
block_number: zksync_types::api::BlockNumber,
index: Index,
) -> eyre::Result<Option<Transaction>> {
let client = self.create_client();
block_on(async move {
client
.get_transaction_by_block_number_and_index(block_number, index)
.await
})
.wrap_err("fork http client failed")
}
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 79f3dae

Please sign in to comment.