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 eth_getBlockTransactionCountByHash and eth_getBlockTransactionCountByNumber #117

Merged
merged 6 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
60 changes: 58 additions & 2 deletions SUPPORTED_APIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ The `status` options are:
| [`ETH`](#eth-namespace) | [`eth_getBalance`](#eth_getbalance) | `SUPPORTED` | Returns the balance of the account of given address |
| [`ETH`](#eth-namespace) | [`eth_getBlockByHash`](#eth_getblockbyhash) | `SUPPORTED` | Returns information about a block by block hash |
| [`ETH`](#eth-namespace) | [`eth_getBlockByNumber`](#eth_getblockbynumber) | `SUPPORTED` | Returns information about a block by block number |
| `ETH` | `eth_getBlockTransactionCountByHash` | `NOT IMPLEMENTED`<br />[GitHub Issue #44](https://github.com/matter-labs/era-test-node/issues/44) | Number of transactions in a block from a block matching the given block hash |
| `ETH` | `eth_getBlockTransactionCountByNumber` | `NOT IMPLEMENTED`<br />[GitHub Issue #43](https://github.com/matter-labs/era-test-node/issues/43) | Number of transactions in a block from a block matching the given block number |
| [`ETH`](#eth-namespace) | [`eth_getBlockTransactionCountByHash`](#eth_getblocktransactioncountbyhash) | `SUPPORTED` | Number of transactions in a block from a block matching the given block hash |
| [`ETH`](#eth-namespace) | [`eth_getBlockTransactionCountByNumber`](#eth_getblocktransactioncountbynumber) | `SUPPORTED` | Number of transactions in a block from a block matching the given block number |
| `ETH` | `eth_getCompilers` | `NOT IMPLEMENTED` | Returns a list of available compilers |
| [`ETH`](#eth-namespace) | [`eth_getTransactionByHash`](#eth_gettransactionbyhash) | `SUPPORTED` | Returns the information about a transaction requested by transaction hash |
| [`ETH`](#eth-namespace) | [`eth_getTransactionCount`](#eth_gettransactioncount) | `SUPPORTED` | Returns the number of transactions sent from an address |
Expand Down Expand Up @@ -512,6 +512,62 @@ curl --request POST \
}'
```

### `eth_getBlockTransactionCountByHash`

[source](src/node.rs)

Number of transactions in a block from a block matching the given block hash

#### Arguments

+ `block_hash: H256`

#### Status

`SUPPORTED`

#### Example

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

### `eth_getBlockTransactionCountByNumber`

[source](src/node.rs)

Number of transactions in a block from a block matching the given block number

#### Arguments

+ `block_number: BlockNumber`

#### Status

`SUPPORTED`

#### Example

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

### `eth_getCode`

[source](src/node.rs)
Expand Down
11 changes: 10 additions & 1 deletion src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,16 @@ pub trait ForkSource {
&self,
block_number: zksync_types::api::BlockNumber,
full_transactions: bool,
) -> eyre::Result<Option<zksync_types::api::Block<zksync_types::api::TransactionVariant>>>;
) -> eyre::Result<Option<Block<TransactionVariant>>>;

/// Returns the transaction count for a given block hash.
fn get_block_transaction_count_by_hash(&self, block_hash: H256) -> eyre::Result<Option<U256>>;

/// Returns the transaction count for a given block number.
fn get_block_transaction_count_by_number(
&self,
block_number: zksync_types::api::BlockNumber,
) -> eyre::Result<Option<U256>>;
}

/// Holds the information about the original chain.
Expand Down
22 changes: 22 additions & 0 deletions src/http_fork_source.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::RwLock;

use eyre::Context;
use zksync_basic_types::{H256, U256};
use zksync_web3_decl::{
jsonrpsee::http_client::{HttpClient, HttpClientBuilder},
namespaces::{EthNamespaceClient, ZksNamespaceClient},
Expand Down Expand Up @@ -198,6 +199,27 @@ impl ForkSource for HttpForkSource {
})
.wrap_err("fork http client failed")
}

/// Returns the transaction count for a given block hash.
fn get_block_transaction_count_by_hash(&self, block_hash: H256) -> eyre::Result<Option<U256>> {
let client = self.create_client();
block_on(async move { client.get_block_transaction_count_by_hash(block_hash).await })
.wrap_err("fork http client failed")
}

/// Returns the transaction count for a given block number.
fn get_block_transaction_count_by_number(
&self,
block_number: zksync_types::api::BlockNumber,
) -> eyre::Result<Option<U256>> {
let client = self.create_client();
block_on(async move {
client
.get_block_transaction_count_by_number(block_number)
.await
})
.wrap_err("fork http client failed")
}
}

#[cfg(test)]
Expand Down
Loading
Loading