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
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn main() -> Result<(), configure_me_codegen::Error> {
.src_prefix("capnp")
.file("capnp/server.capnp")
.file("capnp/wallet.capnp")
.file("capnp/chain.capnp")
.run()
.unwrap();
configure_me_codegen::build_script_auto()
Expand Down
5 changes: 5 additions & 0 deletions capnp/chain.capnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@0x93074320567c5aeb;

interface Chain {
getTip @0 () -> (height :UInt32, hash :Text);
}
2 changes: 2 additions & 0 deletions capnp/server.capnp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
@0xc2e20cc9503cf68f;

using Wallet = import "wallet.capnp";
using Chain = import "chain.capnp";

interface Server {
echo @0 (msg :Text) -> (reply :Text);
shutdown @1 () -> ();
makeWallet @2 () -> (wallet :Wallet.Wallet);
makeChain @3 () -> (chain :Chain.Chain);
}
23 changes: 23 additions & 0 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,20 @@ enum Commands {
Echo(Echo),
/// Terminate the server.
Stop,
/// Chain commands.
#[command(subcommand)]
Chain(ChainCmd),
/// Wallet commands.
#[command(subcommand)]
Wallet(WalletCmd),
}

#[derive(Debug, Clone, clap::Subcommand)]
enum ChainCmd {
/// Show the current chain tip.
Tip,
}

#[derive(Debug, Clone, clap::Args)]
struct Echo {
/// The message to echo.
Expand Down Expand Up @@ -195,6 +204,20 @@ fn main() {
shutdown_req.send().promise.await.unwrap();
println!("Kernel node stopping...");
}
Commands::Chain(cmd) => {
let chain_response = client.make_chain_request().send().promise.await.unwrap();
let client = chain_response.get().unwrap().get_chain().unwrap();
match cmd {
ChainCmd::Tip => {
let req = client.get_tip_request();
let result = req.send().promise.await.unwrap();
let r = result.get().unwrap();
let height = r.get_height();
let hash = r.get_hash().unwrap().to_string().unwrap();
println!("height={height} hash={hash}");
}
}
}
Commands::Wallet(cmd) => {
let wallet_response = client.make_wallet_request().send().promise.await.unwrap();
let client = wallet_response.get().unwrap().get_wallet().unwrap();
Expand Down
3 changes: 3 additions & 0 deletions src/bin/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ fn main() {
}

let wallet_for_ipc = Arc::clone(&wallet);
let chainman_for_ipc = Arc::clone(&node_state.chainman);
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
Expand All @@ -685,6 +686,7 @@ fn main() {
};
debug!(target: Category::IPC, "Handling inbound IPC call");
let state = Arc::clone(&wallet_for_ipc);
let chainman = Arc::clone(&chainman_for_ipc);
let (reader, writer) = stream.into_split();
let buf_reader = futures::io::BufReader::new(reader.compat());
let buf_writer = futures::io::BufWriter::new(writer.compat_write());
Expand All @@ -698,6 +700,7 @@ fn main() {
ipc_shutdown.clone(),
broadcast_tx.clone(),
state,
chainman,
));
let rpc_system =
capnp_rpc::RpcSystem::new(Box::new(network), Some(client.client));
Expand Down
48 changes: 45 additions & 3 deletions src/ipc.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
use std::sync::{mpsc, Arc, Mutex};

use bitcoin::consensus::Decodable;
use bitcoin::hashes::Hash;
use bitcoin::secp256k1::{SecretKey, XOnlyPublicKey};
use bitcoin::{Amount, FeeRate, Transaction};
use bitcoin::{Amount, BlockHash, FeeRate, Transaction};
use bitcoinkernel::{core::BlockHashExt, ChainstateManager};
use wallet::silentpayments::{Recipient, Wallet};

use crate::{server_capnp, wallet_capnp};
use crate::{chain_capnp, server_capnp, wallet_capnp};

#[derive(Debug)]
pub struct IpcInterface {
tx: mpsc::Sender<()>,
broadcast_tx: mpsc::SyncSender<Transaction>,
state: Arc<Mutex<Wallet>>,
chainman: Arc<ChainstateManager>,
}

impl IpcInterface {
pub fn new(
tx: mpsc::Sender<()>,
broadcast_tx: mpsc::SyncSender<Transaction>,
state: Arc<Mutex<Wallet>>,
chainman: Arc<ChainstateManager>,
) -> Self {
Self {
tx,
broadcast_tx,
state,
chainman,
}
}
}
Expand Down Expand Up @@ -63,6 +67,44 @@ impl server_capnp::server::Server for IpcInterface {
results.get().set_wallet(client);
Ok(())
}

async fn make_chain(
self: capnp::capability::Rc<Self>,
_: server_capnp::server::MakeChainParams,
mut results: server_capnp::server::MakeChainResults,
) -> Result<(), capnp::Error> {
let client: chain_capnp::chain::Client =
capnp_rpc::new_client(ChainIpcInterface::new(self.chainman.clone()));
results.get().set_chain(client);
Ok(())
}
}

pub struct ChainIpcInterface {
chainman: Arc<ChainstateManager>,
}

impl ChainIpcInterface {
pub fn new(chainman: Arc<ChainstateManager>) -> Self {
Self { chainman }
}
}

impl chain_capnp::chain::Server for ChainIpcInterface {
async fn get_tip(
self: capnp::capability::Rc<Self>,
_: chain_capnp::chain::GetTipParams,
mut results: chain_capnp::chain::GetTipResults,
) -> Result<(), capnp::Error> {
let tip = self.chainman.active_chain().tip();
let height = tip.height();
let hash = BlockHash::from_byte_array(tip.block_hash().to_bytes());

let mut r = results.get();
r.set_height(height as u32);
r.set_hash(hash.to_string());
Ok(())
}
}

pub struct WalletIpcInterface {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ pub fn resolve_seeds(network: Network) -> Vec<IpAddr> {

capnp::generated_code!(pub mod server_capnp);
capnp::generated_code!(pub mod wallet_capnp);
capnp::generated_code!(pub mod chain_capnp);
Loading