From bd3a844cc5af38dc0718285645b69509867e3880 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Wed, 16 Oct 2024 16:57:48 -0400 Subject: [PATCH] get_miner_data --- binaries/cuprated/src/rpc/json.rs | 41 ++++++++++++++----- .../src/rpc/request/blockchain_context.rs | 12 +++--- binaries/cuprated/src/rpc/request/txpool.rs | 20 ++++++++- storage/txpool/src/lib.rs | 2 +- storage/txpool/src/service/interface.rs | 13 +++++- storage/txpool/src/service/read.rs | 7 ++++ storage/txpool/src/tx.rs | 13 ++++++ 7 files changed, 88 insertions(+), 20 deletions(-) diff --git a/binaries/cuprated/src/rpc/json.rs b/binaries/cuprated/src/rpc/json.rs index 11ef85a3..204b36ff 100644 --- a/binaries/cuprated/src/rpc/json.rs +++ b/binaries/cuprated/src/rpc/json.rs @@ -41,8 +41,8 @@ use cuprate_rpc_types::{ SyncInfoResponse, }, misc::{ - AuxPow, BlockHeader, ChainInfo, GetBan, HardforkEntry, HistogramEntry, Status, - SyncInfoPeer, TxBacklogEntry, + AuxPow, BlockHeader, ChainInfo, GetBan, GetMinerDataTxBacklogEntry, HardforkEntry, + HistogramEntry, Status, SyncInfoPeer, TxBacklogEntry, }, CORE_RPC_VERSION, }; @@ -817,19 +817,38 @@ async fn get_transaction_pool_backlog( /// async fn get_miner_data( - state: CupratedRpcHandler, + mut state: CupratedRpcHandler, request: GetMinerDataRequest, ) -> Result { + let context = blockchain_context::context(&mut state.blockchain_context).await?; + let context = context.unchecked_blockchain_context(); + let major_version = context.current_hf.as_u8(); + let height = usize_to_u64(context.chain_height); + let prev_id = hex::encode(context.top_hash); + let seed_hash = todo!(); + let difficulty = format!("{:#x}", context.next_difficulty); + let median_weight = usize_to_u64(context.median_weight_for_block_reward); + let already_generated_coins = context.already_generated_coins; + let tx_backlog = txpool::block_template_backlog(&mut state.txpool_read) + .await? + .into_iter() + .map(|entry| GetMinerDataTxBacklogEntry { + id: hex::encode(entry.id), + weight: entry.weight, + fee: entry.fee, + }) + .collect(); + Ok(GetMinerDataResponse { base: ResponseBase::OK, - major_version: todo!(), - height: todo!(), - prev_id: todo!(), - seed_hash: todo!(), - difficulty: todo!(), - median_weight: todo!(), - already_generated_coins: todo!(), - tx_backlog: todo!(), + major_version, + height, + prev_id, + seed_hash, + difficulty, + median_weight, + already_generated_coins, + tx_backlog, }) } diff --git a/binaries/cuprated/src/rpc/request/blockchain_context.rs b/binaries/cuprated/src/rpc/request/blockchain_context.rs index 43f5fc59..9001b32d 100644 --- a/binaries/cuprated/src/rpc/request/blockchain_context.rs +++ b/binaries/cuprated/src/rpc/request/blockchain_context.rs @@ -15,9 +15,9 @@ use cuprate_types::{FeeEstimate, HardFork, HardForkInfo}; /// [`BlockChainContextRequest::Context`]. pub(crate) async fn context( - service: &mut BlockChainContextService, + blockchain_context: &mut BlockChainContextService, ) -> Result { - let BlockChainContextResponse::Context(context) = service + let BlockChainContextResponse::Context(context) = blockchain_context .ready() .await .map_err(|e| anyhow!(e))? @@ -33,10 +33,10 @@ pub(crate) async fn context( /// [`BlockChainContextRequest::HardForkInfo`]. pub(crate) async fn hard_fork_info( - service: &mut BlockChainContextService, + blockchain_context: &mut BlockChainContextService, hard_fork: HardFork, ) -> Result { - let BlockChainContextResponse::HardForkInfo(hf_info) = service + let BlockChainContextResponse::HardForkInfo(hf_info) = blockchain_context .ready() .await .map_err(|e| anyhow!(e))? @@ -52,10 +52,10 @@ pub(crate) async fn hard_fork_info( /// [`BlockChainContextRequest::FeeEstimate`]. pub(crate) async fn fee_estimate( - service: &mut BlockChainContextService, + blockchain_context: &mut BlockChainContextService, grace_blocks: u64, ) -> Result { - let BlockChainContextResponse::FeeEstimate(fee) = service + let BlockChainContextResponse::FeeEstimate(fee) = blockchain_context .ready() .await .map_err(|e| anyhow!(e))? diff --git a/binaries/cuprated/src/rpc/request/txpool.rs b/binaries/cuprated/src/rpc/request/txpool.rs index eadbb23d..3ef456cd 100644 --- a/binaries/cuprated/src/rpc/request/txpool.rs +++ b/binaries/cuprated/src/rpc/request/txpool.rs @@ -11,7 +11,7 @@ use cuprate_txpool::{ interface::{TxpoolReadRequest, TxpoolReadResponse}, TxpoolReadHandle, }, - TxEntry, + BlockTemplateTxEntry, TxEntry, }; // FIXME: use `anyhow::Error` over `tower::BoxError` in txpool. @@ -32,6 +32,24 @@ pub(crate) async fn backlog(txpool_read: &mut TxpoolReadHandle) -> Result Result, Error> { + let TxpoolReadResponse::BlockTemplateBacklog(tx_entries) = txpool_read + .ready() + .await + .map_err(|e| anyhow!(e))? + .call(TxpoolReadRequest::BlockTemplateBacklog) + .await + .map_err(|e| anyhow!(e))? + else { + unreachable!(); + }; + + Ok(tx_entries) +} + /// [`TxpoolReadRequest::Size`] pub(crate) async fn size( txpool_read: &mut TxpoolReadHandle, diff --git a/storage/txpool/src/lib.rs b/storage/txpool/src/lib.rs index 5fb3b143..20001360 100644 --- a/storage/txpool/src/lib.rs +++ b/storage/txpool/src/lib.rs @@ -15,7 +15,7 @@ pub mod types; pub use config::Config; pub use free::open; -pub use tx::TxEntry; +pub use tx::{BlockTemplateTxEntry, TxEntry}; //re-exports pub use cuprate_database; diff --git a/storage/txpool/src/service/interface.rs b/storage/txpool/src/service/interface.rs index d90b255d..394162a0 100644 --- a/storage/txpool/src/service/interface.rs +++ b/storage/txpool/src/service/interface.rs @@ -5,7 +5,10 @@ use std::sync::Arc; use cuprate_types::TransactionVerificationData; -use crate::{tx::TxEntry, types::TransactionHash}; +use crate::{ + tx::{BlockTemplateTxEntry, TxEntry}, + types::TransactionHash, +}; //---------------------------------------------------------------------------------------------------- TxpoolReadRequest /// The transaction pool [`tower::Service`] read request type. @@ -19,6 +22,9 @@ pub enum TxpoolReadRequest { /// Get information on all transactions in the pool. Backlog, + /// TODO + BlockTemplateBacklog, + /// Get the number of transactions in the pool. Size { /// TODO @@ -45,6 +51,11 @@ pub enum TxpoolReadResponse { /// the transactions currently in the pool. Backlog(Vec), + /// Response to [`TxpoolReadRequest::BlockTemplateBacklog`]. + /// + /// TODO + BlockTemplateBacklog(Vec), + /// Response to [`TxpoolReadRequest::Size`]. /// /// The inner value is the amount of diff --git a/storage/txpool/src/service/read.rs b/storage/txpool/src/service/read.rs index 0b75e8a6..229bf2be 100644 --- a/storage/txpool/src/service/read.rs +++ b/storage/txpool/src/service/read.rs @@ -66,6 +66,7 @@ fn map_request( TxpoolReadRequest::TxBlob(tx_hash) => tx_blob(env, &tx_hash), TxpoolReadRequest::TxVerificationData(tx_hash) => tx_verification_data(env, &tx_hash), TxpoolReadRequest::Backlog => backlog(env), + TxpoolReadRequest::BlockTemplateBacklog => block_template_backlog(env), TxpoolReadRequest::Size { include_sensitive_txs, } => size(env, include_sensitive_txs), @@ -119,6 +120,12 @@ fn backlog(env: &ConcreteEnv) -> ReadResponseResult { Ok(TxpoolReadResponse::Backlog(todo!())) } +/// [`TxpoolReadRequest::BlockTemplateBacklog`]. +#[inline] +fn block_template_backlog(env: &ConcreteEnv) -> ReadResponseResult { + Ok(TxpoolReadResponse::BlockTemplateBacklog(todo!())) +} + /// [`TxpoolReadRequest::Size`]. #[inline] fn size(env: &ConcreteEnv, include_sensitive_txs: bool) -> ReadResponseResult { diff --git a/storage/txpool/src/tx.rs b/storage/txpool/src/tx.rs index 6425326a..7daff156 100644 --- a/storage/txpool/src/tx.rs +++ b/storage/txpool/src/tx.rs @@ -12,3 +12,16 @@ pub struct TxEntry { /// How long the transaction has been in the pool. pub time_in_pool: std::time::Duration, } + +/// TODO +/// +/// Used in [`TxpoolReadResponse::BlockTemplateBacklog`](crate::service::interface::TxpoolReadResponse::BlockTemplateBacklog). +#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)] +pub struct BlockTemplateTxEntry { + /// TODO + pub id: [u8; 32], + /// TODO + pub weight: u64, + /// TODO + pub fee: u64, +}