8000 addnode json-rpc command compliant with bitcoin-core: by qlrd · Pull Request #465 · vinteumorg/Floresta · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

addnode json-rpc command compliant with bitcoin-core: #465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 29, 2025
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
37 changes: 33 additions & 4 deletions crates/floresta-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use clap::Parser;
use clap::Subcommand;
use floresta_cli::jsonrpc_client::Client;
use floresta_cli::rpc::FlorestaRPC;
use floresta_cli::rpc_types::AddNodeCommand;
use floresta_cli::rpc_types::GetBlockRes;

// Main function that runs the CLI application
Expand Down Expand Up @@ -85,7 +86,15 @@ fn do_request(cmd: &Cli, client: Client) -> anyhow::Result<String> {
}
Methods::GetPeerInfo => serde_json::to_string_pretty(&client.get_peer_info()?)?,
8000 Methods::Stop => serde_json::to_string_pretty(&client.stop()?)?,
Methods::AddNode { node } => serde_json::to_string_pretty(&client.add_node(node)?)?,
Methods::AddNode {
node,
command,
v2transport,
} => {
let transport = v2transport.unwrap_or(false);
serde_json::to_string_pretty(&client.add_node(node, command, transport)?)?
}

Methods::FindTxOut {
txid,
vout,
Expand Down Expand Up @@ -189,10 +198,30 @@ pub enum Methods {
/// Stops the node
#[command(name = "stop")]
Stop,
/// Connects with a peer, given its address and port
/// Usage: addnode <ip:[port]>
/// Attempts to add or remove a node from the addnode list.
/// Or try a connection to a node once.
///
/// Arguments:
/// 1. node (string, required):
/// - The address of the peer to connect to;
///
/// 2. command (string, required):
/// - 'add' to add a node to the list;
/// - 'remove' to remove a node from the list;
/// - 'onetry' to try a connection to the node once.
///
/// 3. v2transport (boolean, optional, default=false):
/// - Attempt to connect using BIP324 v2 transport protocol (ignored for 'remove' command)
///
/// Result: json null
///
/// Usage: addnode <ip:[port]> <add|remove|onetry> [true|false]
#[command(name = "addnode")]
AddNode { node: String },
AddNode {
node: String,
command: AddNodeCommand,
v2transport: Option<bool>,
},
#[command(name = "findtxout")]
FindTxOut {
txid: Txid,
Expand Down
13 changes: 10 additions & 3 deletions crates/floresta-cli/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub trait FlorestaRPC {
/// Tells florestad to connect with a peer
///
/// You can use this to connect with a given node, providing it's IP address and port.
fn add_node(&self, node: String) -> Result<bool>;
fn add_node(&self, node: String, command: AddNodeCommand, v2transport: bool) -> Result<Value>;
/// Finds an specific utxo in the chain
///
/// You can use this to look for a utxo. If it exists, it will return the amount and
Expand Down Expand Up @@ -170,8 +170,15 @@ impl<T: JsonRPCClient> FlorestaRPC for T {
self.call("getrpcinfo", &[])
}

fn add_node(&self, node: String) -> Result<bool> {
self.call("addnode", &[Value::String(node)])
fn add_node(&self, node: String, command: AddNodeCommand, v2transport: bool) -> Result<Value> {
self.call(
"addnode",
&[
Value::String(node),
Value::String(command.to_string()),
Value::Bool(v2transport),
],
)
}

fn stop(&self) -> Result<String> {
Expand Down
29 changes: 29 additions & 0 deletions crates/floresta-cli/src/rpc_types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::Display;

use clap::ValueEnum;
use serde::Deserialize;
use serde::Serialize;

Expand Down Expand Up @@ -357,4 +358,32 @@ pub struct GetRpcInfoRes {
logpath: String,
}

#[derive(Debug, Clone, ValueEnum, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
/// Enum to represent the different subcommands for the addnode command
pub enum AddNodeCommand {
/// Add a node to the addnode list (but not connect to it)
Add,

/// Remove a node from the addnode list (but not necessarily disconnect from it)
Remove,

/// Connect to a node once, but don't add it to the addnode list
Onetry,
}

/// A simple implementation to convert the enum to a string.
/// Useful for get the subcommand name of addnode with
/// command.to_string()
impl Display for AddNodeCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let cmd = match self {
AddNodeCommand::Add => "add",
AddNodeCommand::Remove => "remove",
AddNodeCommand:: "onetry",
};
write!(f, "{cmd}")
}
}

impl std::error::Error for Error {}
5 changes: 5 additions & 0 deletions crates/floresta-wire/src/p2p_wire/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::{self};
use std::io;
use std::net::IpAddr;

use floresta_chain::BlockchainError;
use floresta_common::impl_error_from;
Expand Down Expand Up @@ -30,6 +31,10 @@ pub enum WireError {
PeerMisbehaving,
#[error("Failed to init Utreexo peers: anchors.json does not exist yet")]
AnchorFileNotFound,
#[error("Peer {0}:{1} already exists")]
PeerAlreadyExists(IpAddr, u16),
#[error("Peer {0}:{1} not found")]
PeerNotFoundAtAddress(IpAddr, u16),
#[error("Generic io error: {0}")]
Io(std::io::Error),
#[error("{0}")]
Expand Down
Loading
Loading
0