Skip to content
Open
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 13 additions & 9 deletions bin/floresta-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ use bitcoin::Txid;
use clap::Parser;
use clap::Subcommand;
use floresta_rpc::jsonrpc_client::Client;
use floresta_rpc::rpc::FlorestaRPC;
use floresta_rpc::rpc_interfaces::BlockchainRpc;
use floresta_rpc::rpc_interfaces::ControlRpc;
use floresta_rpc::rpc_interfaces::NetworkRpc;
use floresta_rpc::rpc_interfaces::RawTransactionRpc;
use floresta_rpc::rpc_interfaces::WalletRpc;
use floresta_rpc::rpc_types::AddNodeCommand;
use floresta_rpc::rpc_types::RescanConfidence;

Expand Down Expand Up @@ -63,24 +67,24 @@ fn do_request(cmd: &Cli, client: Client) -> anyhow::Result<String> {
Methods::GetBestBlockHash => serde_json::to_string_pretty(&client.get_best_block_hash()?)?,
Methods::GetBlockCount => serde_json::to_string_pretty(&client.get_block_count()?)?,
Methods::GetTxOut { txid, vout } => {
serde_json::to_string_pretty(&client.get_tx_out(txid, vout)?)?
serde_json::to_string_pretty(&client.get_tx_out(txid, vout, false)?)?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this false for?

}
Methods::GetTxOutProof { txids, blockhash } => {
serde_json::to_string_pretty(&client.get_txout_proof(txids, blockhash))?
serde_json::to_string_pretty(&client.get_txout_proof(&txids, blockhash)?)?
}
Methods::GetTransaction { txid, .. } => {
serde_json::to_string_pretty(&client.get_transaction(txid, Some(true))?)?
Methods::GetTransaction { txid, verbose } => {
serde_json::to_string_pretty(&client.get_raw_transaction(txid, verbose)?)?
}
Methods::RescanBlockchain {
start_block,
stop_block,
use_timestamp,
confidence,
} => serde_json::to_string_pretty(&client.rescanblockchain(
} => serde_json::to_string_pretty(&client.rescan_blockchain(
Some(start_block),
Some(stop_block),
use_timestamp,
confidence,
Some(confidence),
)?)?,
Methods::SendRawTransaction { tx } => {
serde_json::to_string_pretty(&client.send_raw_transaction(tx)?)?
Expand Down Expand Up @@ -215,7 +219,7 @@ pub enum Methods {

/// Returns the transaction, assuming it is cached by our watch only wallet
#[command(name = "gettransaction")]
GetTransaction { txid: Txid, verbose: Option<bool> },
GetTransaction { txid: Txid, verbose: Option<u32> },
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verbosity then


#[doc = include_str!("../../../doc/rpc/rescanblockchain.md")]
#[command(
Expand Down Expand Up @@ -362,7 +366,7 @@ pub enum Methods {
)]
DisconnectNode {
node_address: String,
node_id: Option<usize>,
node_id: Option<u32>,
},

#[command(name = "findtxout")]
Expand Down
1 change: 1 addition & 0 deletions crates/floresta-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ floresta-electrum = { workspace = true }
floresta-mempool = { workspace = true }
floresta-watch-only = { workspace = true }
floresta-wire = { workspace = true }
floresta-rpc = { workspace = true}
metrics = { workspace = true, optional = true }

[dev-dependencies]
Expand Down
63 changes: 31 additions & 32 deletions crates/floresta-node/src/json_rpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use core::net::SocketAddr;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Instant;

Expand Down Expand Up @@ -30,6 +31,7 @@ use bitcoin::Txid;
use floresta_chain::ThreadSafeChain;
use floresta_compact_filters::flat_filters_store::FlatFiltersStore;
use floresta_compact_filters::network_filters::NetworkFilters;
use floresta_rpc::rpc_interfaces::RpcMethods;
use floresta_watch_only::kv_database::KvDatabase;
use floresta_watch_only::AddressCache;
use floresta_watch_only::CachedTransaction;
Expand Down Expand Up @@ -213,14 +215,16 @@ async fn handle_json_rpc_request(
},
);

match method.as_str() {
let method = RpcMethods::from_str(&method).map_err(|_| JsonRpcError::MethodNotFound)?;

match method {
// blockchain
"getbestblockhash" => {
RpcMethods::GetBestBlockHash => {
let hash = state.get_best_block_hash()?;
Ok(serde_json::to_value(hash).unwrap())
}

"getblock" => {
RpcMethods::GetBlock => {
let hash = get_hash(&params, 0, "block_hash")?;
// Default value in case of missing parameter is 1
let verbosity: u8 =
Expand All @@ -232,30 +236,30 @@ async fn handle_json_rpc_request(
.map(|v| serde_json::to_value(v).expect("GetBlockRes implements serde"))
}

"getblockchaininfo" => state
RpcMethods::GetBlockchainInfo => state
.get_blockchain_info()
.map(|v| serde_json::to_value(v).unwrap()),

"getblockcount" => state
RpcMethods::GetBlockCount => state
.get_block_count()
.map(|v| serde_json::to_value(v).unwrap()),

"getblockfrompeer" => {
RpcMethods::GetBlockFromPeer => {
let hash = get_hash(&params, 0, "block_hash")?;

state.get_block(hash, 0).await?;

Ok(Value::Null)
}

"getblockhash" => {
RpcMethods::GetBlockHash => {
let height = get_numeric(&params, 0, "block_height")?;
state
.get_block_hash(height)
.map(|h| serde_json::to_value(h).unwrap())
}

"getblockheader" => {
RpcMethods::GetBlockHeader => {
let hash = get_hash(&params, 0, "block_hash")?;
let verbosity = get_optional_field(&params, 1, "verbosity", get_bool)?.unwrap_or(true);

Expand All @@ -265,7 +269,7 @@ async fn handle_json_rpc_request(
.map(|h| serde_json::to_value(h).unwrap())
}

"gettxout" => {
RpcMethods::GetTxOut => {
let txid = get_hash(&params, 0, "txid")?;
let vout = get_numeric(&params, 1, "vout")?;
let include_mempool =
Expand All @@ -276,7 +280,7 @@ async fn handle_json_rpc_request(
.map(|v| serde_json::to_value(v).unwrap())
}

"gettxoutproof" => {
RpcMethods::GetTxOutProof => {
let txids = get_hashes_array(&params, 0, "txids")?;
let block_hash = get_optional_field(&params, 1, "block_hash", get_hash)?;

Expand All @@ -290,7 +294,7 @@ async fn handle_json_rpc_request(
.expect("GetTxOutProof implements serde"))
}

"getrawtransaction" => {
RpcMethods::GetRawTransaction => {
let txid = get_hash(&params, 0, "txid")?;
let verbosity = get_optional_field(&params, 1, "verbosity", get_bool)?;

Expand All @@ -299,9 +303,9 @@ async fn handle_json_rpc_request(
.map(|v| serde_json::to_value(v).unwrap())
}

"getroots" => state.get_roots().map(|v| serde_json::to_value(v).unwrap()),
RpcMethods::GetRoots => state.get_roots().map(|v| serde_json::to_value(v).unwrap()),

"findtxout" => {
RpcMethods::FindTxOut => {
let txid = get_hash(&params, 0, "txid")?;
let vout = get_numeric(&params, 1, "vout")?;
let script = get_string(&params, 2, "script")?;
Expand All @@ -313,7 +317,7 @@ async fn handle_json_rpc_request(
}

// control
"getmemoryinfo" => {
RpcMethods::GetMemoryInfo => {
let mode =
get_optional_field(&params, 0, "mode", get_string)?.unwrap_or("stats".into());

Expand All @@ -322,37 +326,37 @@ async fn handle_json_rpc_request(
.map(|v| serde_json::to_value(v).unwrap())
}

"getrpcinfo" => state
RpcMethods::GetRpcInfo => state
.get_rpc_info()
.await
.map(|v| serde_json::to_value(v).unwrap()),

// help
// logging
"stop" => state.stop().await.map(|v| serde_json::to_value(v).unwrap()),
RpcMethods::Stop => state.stop().await.map(|v| serde_json::to_value(v).unwrap()),

"uptime" => {
RpcMethods::Uptime => {
let uptime = state.uptime();
Ok(serde_json::to_value(uptime).unwrap())
}

// network
"getpeerinfo" => state
RpcMethods::GetPeerInfo => state
.get_peer_info()
.await
.map(|v| serde_json::to_value(v).unwrap()),

"getconnectioncount" => state
RpcMethods::GetConnectionCount => state
.get_connection_count()
.await
.map(|v| serde_json::to_value(v).unwrap()),

"getnetworkinfo" => state
RpcMethods::GetNetworkInfo => state
.get_network_info()
.await
.map(|v| serde_json::to_value(v).unwrap()),

"addnode" => {
RpcMethods::AddNode => {
let node = get_string(&params, 0, "node")?;
let command = get_string(&params, 1, "command")?;
let v2transport =
Expand All @@ -364,7 +368,7 @@ async fn handle_json_rpc_request(
.map(|v| serde_json::to_value(v).unwrap())
}

"disconnectnode" => {
RpcMethods::DisconnectNode => {
let node_address = get_string(&params, 0, "node_address")?;
let node_id = get_optional_field(&params, 1, "node_id", get_numeric)?;

Expand All @@ -374,22 +378,22 @@ async fn handle_json_rpc_request(
.map(|v| serde_json::to_value(v).unwrap())
}

"ping" => {
RpcMethods::Ping => {
state.ping().await?;

Ok(serde_json::json!(null))
}

// wallet
"loaddescriptor" => {
RpcMethods::LoadDescriptor => {
let descriptor = get_string(&params, 0, "descriptor")?;

state
.load_descriptor(descriptor)
.map(|v| serde_json::to_value(v).unwrap())
}

"rescanblockchain" => {
RpcMethods::RescanBlockchain => {
let start_height = get_optional_field(&params, 0, "start_height", get_numeric)?;
let stop_height = get_optional_field(&params, 1, "stop_height", get_numeric)?;
let use_timestamp =
Expand All @@ -410,22 +414,17 @@ async fn handle_json_rpc_request(
.map(|v| serde_json::to_value(v).unwrap())
}

"sendrawtransaction" => {
RpcMethods::SendRawTransaction => {
let tx = get_string(&params, 0, "hex")?;
state
.send_raw_transaction(tx)
.await
.map(|v| serde_json::to_value(v).unwrap())
}

"listdescriptors" => state
RpcMethods::ListDescriptors => state
.list_descriptors()
.map(|v| serde_json::to_value(v).unwrap()),

_ => {
let error = JsonRpcError::MethodNotFound;
Err(error)
}
}
}

Expand Down
Loading