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

feat(providers): add mining related apis #2008

Merged
merged 3 commits into from
Jan 4, 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
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