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

Commit 5f24765

Browse files
authored
feat(providers): add mining related apis (#2008)
* feat(providers): add miner_start endpoint * add other mining related apis * add eth_mining * add miner_stop * expose Geth miner rpc api
1 parent 578b1c4 commit 5f24765

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

Diff for: ethers-core/src/utils/geth.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const GETH_STARTUP_TIMEOUT_MILLIS: u64 = 10_000;
1616
const GETH_DIAL_LOOP_TIMEOUT: Duration = Duration::new(20, 0);
1717

1818
/// The exposed APIs
19-
const API: &str = "eth,net,web3,txpool,admin";
19+
const API: &str = "eth,net,web3,txpool,admin,miner";
2020

2121
/// The geth command
2222
const GETH: &str = "geth";

Diff for: ethers-providers/src/lib.rs

+21
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,11 @@ pub trait Middleware: Sync + Send + Debug {
502502
self.inner().get_proof(from, locations, block).await.map_err(FromErr::from)
503503
}
504504

505+
/// Returns an indication if this node is currently mining.
506+
async fn mining(&self) -> Result<bool, Self::Error> {
507+
self.inner().mining().await.map_err(FromErr::from)
508+
}
509+
505510
// Admin namespace
506511

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

536+
// Miner namespace
537+
538+
/// Starts the miner with the given number of threads. If threads is nil, the number of workers
539+
/// started is equal to the number of logical CPUs that are usable by this process. If mining
540+
/// is already running, this method adjust the number of threads allowed to use and updates the
541+
/// minimum price required by the transaction pool.
542+
async fn start_mining(&self, threads: Option<usize>) -> Result<(), Self::Error> {
543+
self.inner().start_mining(threads).await.map_err(FromErr::from)
544+
}
545+
546+
/// Stop terminates the miner, both at the consensus engine level as well as at
547+
/// the block creation level.
548+
async fn stop_mining(&self) -> Result<(), Self::Error> {
549+
self.inner().stop_mining().await.map_err(FromErr::from)
550+
}
551+
531552
// Mempool inspection for Geth's API
532553

533554
async fn txpool_content(&self) -> Result<TxpoolContent, Self::Error> {

Diff for: ethers-providers/src/provider.rs

+22
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,11 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
813813
self.request("eth_getProof", [from, locations, block]).await
814814
}
815815

816+
/// Returns an indication if this node is currently mining.
817+
async fn mining(&self) -> Result<bool, Self::Error> {
818+
self.request("eth_mining", ()).await
819+
}
820+
816821
// Admin namespace
817822

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

863+
// Miner namespace
864+
865+
/// Starts the miner with the given number of threads. If threads is nil, the number of workers
866+
/// started is equal to the number of logical CPUs that are usable by this process. If mining
867+
/// is already running, this method adjust the number of threads allowed to use and updates the
868+
/// minimum price required by the transaction pool.
869+
async fn start_mining(&self, threads: Option<usize>) -> Result<(), Self::Error> {
870+
let threads = utils::serialize(&threads);
871+
self.request("miner_start", [threads]).await
872+
}
873+
874+
/// Stop terminates the miner, both at the consensus engine level as well as at the block
875+
/// creation level.
876+
async fn stop_mining(&self) -> Result<(), Self::Error> {
877+
self.request("miner_stop", ()).await
878+
}
879+
858880
////// Ethereum Naming Service
859881
// The Ethereum Naming Service (ENS) allows easy to remember and use names to
860882
// be assigned to Ethereum addresses. Any provider operation which takes an address

0 commit comments

Comments
 (0)