Skip to content
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
12 changes: 8 additions & 4 deletions app/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ fn set_tracing_subscriber(log_level: tracing::Level) -> anyhow::Result<()> {
})
}

async fn spawn_rpc_server(
server: server::Server<DefaultEnforcer>,
async fn spawn_rpc_server<RpcClient>(
server: server::Server<DefaultEnforcer, RpcClient>,
serve_rpc_addr: SocketAddr,
) -> anyhow::Result<ServerHandle> {
) -> anyhow::Result<ServerHandle>
where
RpcClient: bitcoin_jsonrpsee::MainClient + Send + Sync + 'static,
{
tracing::info!("serving RPC on {}", serve_rpc_addr);

use server::RpcServer;
Expand Down Expand Up @@ -109,7 +112,7 @@ async fn main() -> anyhow::Result<()> {
enforcer,
mempool,
tx_cache,
rpc_client,
rpc_client.clone(),
sequence_stream,
|err| async {
let err = anyhow::Error::from(err);
Expand All @@ -121,6 +124,7 @@ async fn main() -> anyhow::Result<()> {
mempool,
network,
network_info,
rpc_client,
sample_block_template,
)?;
let rpc_server_handle =
Expand Down
31 changes: 28 additions & 3 deletions lib/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ pub trait Rpc {
&self,
request: BlockTemplateRequest,
) -> RpcResult<BlockTemplate>;

/// Returns None if the block is invalid, otherwise the error code
/// describing why the block was rejected.
#[method(name = "submitblock")]
async fn submit_block(
&self,
block_hex: String,
) -> RpcResult<Option<String>>;
}

#[derive(Debug, Error)]
Expand All @@ -36,20 +44,22 @@ pub enum CreateServerError {
SampleBlockTemplate,
}

pub struct Server<Enforcer> {
pub struct Server<Enforcer, RpcClient> {
coinbase_spk: ScriptBuf,
mempool: MempoolSync<Enforcer>,
network: Network,
network_info: NetworkInfo,
rpc_client: RpcClient,
sample_block_template: BlockTemplate,
}

impl<Enforcer> Server<Enforcer> {
impl<Enforcer, RpcClient> Server<Enforcer, RpcClient> {
pub fn new(
coinbase_spk: ScriptBuf,
mempool: MempoolSync<Enforcer>,
network: Network,
network_info: NetworkInfo,
rpc_client: RpcClient,
sample_block_template: BlockTemplate,
) -> Result<Self, CreateServerError> {
if matches!(
Expand All @@ -63,6 +73,7 @@ impl<Enforcer> Server<Enforcer> {
mempool,
network,
network_info,
rpc_client,
sample_block_template,
})
}
Expand Down Expand Up @@ -435,9 +446,10 @@ async fn block_txs<const COINBASE_TXN: bool, BP>(
}

#[async_trait]
impl<BP> RpcServer for Server<BP>
impl<BP, RpcClient> RpcServer for Server<BP, RpcClient>
where
BP: CusfBlockProducer + Send + Sync + 'static,
RpcClient: bitcoin_jsonrpsee::client::MainClient + Send + Sync + 'static,
{
async fn get_block_template(
&self,
Expand Down Expand Up @@ -596,4 +608,17 @@ where
};
Ok(res)
}

async fn submit_block(
&self,
block_hex: String,
) -> RpcResult<Option<String>> {
self.rpc_client
.submit_block(block_hex)
.await
.map_err(|err| match err {
jsonrpsee::core::ClientError::Call(err) => err,
err => internal_error(err),
})
}
}
Loading