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
This commit is contained in:
Dan Cline 2023-01-04 16:30:03 -05:00 committed by GitHub
parent 578b1c4d2d
commit 5f2476510b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 1 deletions

View File

@ -16,7 +16,7 @@ const GETH_STARTUP_TIMEOUT_MILLIS: u64 = 10_000;
const GETH_DIAL_LOOP_TIMEOUT: Duration = Duration::new(20, 0); const GETH_DIAL_LOOP_TIMEOUT: Duration = Duration::new(20, 0);
/// The exposed APIs /// The exposed APIs
const API: &str = "eth,net,web3,txpool,admin"; const API: &str = "eth,net,web3,txpool,admin,miner";
/// The geth command /// The geth command
const GETH: &str = "geth"; const GETH: &str = "geth";

View File

@ -502,6 +502,11 @@ pub trait Middleware: Sync + Send + Debug {
self.inner().get_proof(from, locations, block).await.map_err(FromErr::from) 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 // Admin namespace
async fn add_peer(&self, enode_url: String) -> Result<bool, Self::Error> { async fn add_peer(&self, enode_url: String) -> Result<bool, Self::Error> {
@ -528,6 +533,22 @@ pub trait Middleware: Sync + Send + Debug {
self.inner().remove_trusted_peer(enode_url).await.map_err(FromErr::from) 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 // Mempool inspection for Geth's API
async fn txpool_content(&self) -> Result<TxpoolContent, Self::Error> { async fn txpool_content(&self) -> Result<TxpoolContent, Self::Error> {

View File

@ -813,6 +813,11 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
self.request("eth_getProof", [from, locations, block]).await 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 // Admin namespace
/// Requests adding the given peer, returning a boolean representing whether or not the peer /// 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> {
self.request("admin_removeTrustedPeer", [enode_url]).await 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 ////// Ethereum Naming Service
// The Ethereum Naming Service (ENS) allows easy to remember and use names to // 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 // be assigned to Ethereum addresses. Any provider operation which takes an address