Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
feat(providers): add mining related apis (#2008)
Browse files Browse the repository at this point in the history
* feat(providers): add miner_start endpoint

* add other mining related apis

 * add eth_mining
 * add miner_stop

* expose Geth miner rpc api
  • Loading branch information
Rjected authored Jan 4, 2023
1 parent 578b1c4 commit 5f24765
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ethers-core/src/utils/geth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const GETH_STARTUP_TIMEOUT_MILLIS: u64 = 10_000;
const GETH_DIAL_LOOP_TIMEOUT: Duration = Duration::new(20, 0);

/// The exposed APIs
const API: &str = "eth,net,web3,txpool,admin";
const API: &str = "eth,net,web3,txpool,admin,miner";

/// The geth command
const GETH: &str = "geth";
Expand Down
21 changes: 21 additions & 0 deletions ethers-providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,11 @@ pub trait Middleware: Sync + Send + Debug {
self.inner().get_proof(from, locations, block).await.map_err(FromErr::from)
}

/// Returns an indication if this node is currently mining.
async fn mining(&self) -> Result<bool, Self::Error> {
self.inner().mining().await.map_err(FromErr::from)
}

// Admin namespace

async fn add_peer(&self, enode_url: String) -> Result<bool, Self::Error> {
Expand All @@ -528,6 +533,22 @@ pub trait Middleware: Sync + Send + Debug {
self.inner().remove_trusted_peer(enode_url).await.map_err(FromErr::from)
}

// Miner namespace

/// Starts the miner with the given number of threads. If threads is nil, the number of workers
/// started is equal to the number of logical CPUs that are usable by this process. If mining
/// is already running, this method adjust the number of threads allowed to use and updates the
/// minimum price required by the transaction pool.
async fn start_mining(&self, threads: Option<usize>) -> Result<(), Self::Error> {
self.inner().start_mining(threads).await.map_err(FromErr::from)
}

/// Stop terminates the miner, both at the consensus engine level as well as at
/// the block creation level.
async fn stop_mining(&self) -> Result<(), Self::Error> {
self.inner().stop_mining().await.map_err(FromErr::from)
}

// Mempool inspection for Geth's API

async fn txpool_content(&self) -> Result<TxpoolContent, Self::Error> {
Expand Down
22 changes: 22 additions & 0 deletions ethers-providers/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,11 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
self.request("eth_getProof", [from, locations, block]).await
}

/// Returns an indication if this node is currently mining.
async fn mining(&self) -> Result<bool, Self::Error> {
self.request("eth_mining", ()).await
}

// Admin namespace

/// Requests adding the given peer, returning a boolean representing whether or not the peer
Expand Down Expand Up @@ -855,6 +860,23 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
self.request("admin_removeTrustedPeer", [enode_url]).await
}

// Miner namespace

/// Starts the miner with the given number of threads. If threads is nil, the number of workers
/// started is equal to the number of logical CPUs that are usable by this process. If mining
/// is already running, this method adjust the number of threads allowed to use and updates the
/// minimum price required by the transaction pool.
async fn start_mining(&self, threads: Option<usize>) -> Result<(), Self::Error> {
let threads = utils::serialize(&threads);
self.request("miner_start", [threads]).await
}

/// Stop terminates the miner, both at the consensus engine level as well as at the block
/// creation level.
async fn stop_mining(&self) -> Result<(), Self::Error> {
self.request("miner_stop", ()).await
}

////// Ethereum Naming Service
// The Ethereum Naming Service (ENS) allows easy to remember and use names to
// be assigned to Ethereum addresses. Any provider operation which takes an address
Expand Down

0 comments on commit 5f24765

Please sign in to comment.