Skip to content

Commit

Permalink
feat: adds hardhat_getAutomine for evm emulator and hardhat ignition …
Browse files Browse the repository at this point in the history
…usage (#357)

* feat: adds hardhat_getAutomine for evm emulator and hardhat ignition usage

* chore: lint

* chore: pull latest

* chore: update comments

* chore: update supported apis

* chore: update supported apis
  • Loading branch information
dutterbutter authored Oct 29, 2024
1 parent 3cd918b commit 28cad46
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
35 changes: 34 additions & 1 deletion SUPPORTED_APIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ The `status` options are:
| `HARDHAT` | `hardhat_addCompilationResult` | `NOT IMPLEMENTED` | Add information about compiled contracts |
| `HARDHAT` | `hardhat_dropTransaction` | `NOT IMPLEMENTED` | Remove a transaction from the mempool |
| [`HARDHAT`](#hardhat-namespace) | [`hardhat_impersonateAccount`](#hardhat_impersonateaccount) | `SUPPORTED` | Impersonate an account |
| `HARDHAT` | `hardhat_getAutomine` | `NOT IMPLEMENTED` | Returns `true` if automatic mining is enabled, and `false` otherwise |
| [`HARDHAT`](#hardhat-namespace) | [`hardhat_getAutomine`](#hardhat_getautomine) | `PARTIAL` | Currently always returns `true` as era-test-node by default mines new blocks with each new transaction. |
| `HARDHAT` | `hardhat_metadata` | `NOT IMPLEMENTED` | Returns the metadata of the current network |
| [`HARDHAT`](#hardhat-namespace) | [`hardhat_mine`](#hardhat_mine) | Mine any number of blocks at once, in constant time |
| [`HARDHAT`](#hardhat-namespace) | [`hardhat_reset`](#hardhat_reset) | `PARTIALLY` | Resets the state of the network; cannot revert to past block numbers, unless they're in a fork |
Expand Down Expand Up @@ -1571,6 +1571,39 @@ curl --request POST \

```

### `hardhat_getAutomine`

[source](src/node/hardhat.rs)

This method retrieves the current automine status of the network. Automine mode mines a new block automatically with each transaction, ensuring that transactions are processed immediately. By default, automine is enabled, returning `true` for each call.

Disabling automine to allow for pending transactions in the mempool and manual or interval mining is not currently supported. Thus, `hardhat_getAutomine` will always return true.

#### Arguments

This RPC method does not accept any arguments.

#### Example

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

#### Response

A boolean value indicating the automine status:

- `true`: Automine is enabled (the default state).
- `false`: Automine is disabled (not currently supported).

### `hardhat_reset`

[source](src/node/hardhat.rs)
Expand Down
18 changes: 18 additions & 0 deletions src/namespaces/hardhat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ pub trait HardhatNamespaceT {
#[rpc(name = "hardhat_mine")]
fn hardhat_mine(&self, num_blocks: Option<U64>, interval: Option<U64>) -> RpcResult<bool>;

/// Retrieves the current automine status of the network.
///
/// This method always returns `true` as automining is enabled by default, meaning a new block is
/// mined immediately with each transaction. Disabling automining to allow pending transactions in the
/// mempool and manual or interval mining is currently not supported. To implement this, modifications
/// would be needed to support pending transaction handling and refactor `run_l2_tx` and `run_l2_tx_raw`
/// for modularity and maintainability.
///
/// # Arguments
///
/// This RPC method does not accept any arguments.
///
/// # Returns
///
/// A `BoxFuture` containing a `Result` with a `bool` value indicating the automine status (`true` for enabled).
#[rpc(name = "hardhat_getAutomine")]
fn hardhat_get_automine(&self) -> RpcResult<bool>;

/// Reset the state of the network back to a fresh forked state, or disable forking.
///
/// # Arguments
Expand Down
9 changes: 9 additions & 0 deletions src/node/hardhat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> HardhatNam
.into_boxed_future()
}

fn hardhat_get_automine(&self) -> RpcResult<bool> {
self.get_automine()
.map_err(|err| {
tracing::error!("failed getting automine: {:?}", err);
into_jsrpc_error(Web3Error::InternalError(err))
})
.into_boxed_future()
}

fn reset_network(&self, reset_spec: Option<ResetRequest>) -> RpcResult<bool> {
self.reset_network(reset_spec)
.map_err(|err| {
Expand Down
9 changes: 9 additions & 0 deletions src/node/in_memory_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> InMemoryNo
})
}

// @dev This function is necessary for Hardhat Ignite compatibility with `evm_emulator`.
// It always returns `true`, as each new transaction automatically mines a new block by default.
// Disabling auto mining would require adding functionality to mine blocks with pending transactions.
// This feature is not yet implemented and should be deferred until `run_l2_tx` and `run_l2_tx_raw` are
// refactored to handle pending transactions and modularized into smaller functions for maintainability.
pub fn get_automine(&self) -> Result<bool> {
Ok(true)
}

pub fn reset_network(&self, reset_spec: Option<ResetRequest>) -> Result<bool> {
let (opt_url, block_number) = if let Some(spec) = reset_spec {
if let Some(to) = spec.to {
Expand Down

0 comments on commit 28cad46

Please sign in to comment.