json-rpc layout
This commit is contained in:
parent
7b1e7eec2f
commit
1f75bbcb1f
|
@ -2,6 +2,7 @@
|
|||
|
||||
mod codes {
|
||||
// NOTE [ToDr] Codes from [-32099, -32000]
|
||||
pub const UNKNOWN: i64 = -32000;
|
||||
pub const EXECUTION_ERROR: i64 = -32015;
|
||||
pub const TRANSACTION_NOT_FOUND: i64 = -32096;
|
||||
pub const TRANSACTION_OUTPUT_NOT_FOUND: i64 = -32097;
|
||||
|
@ -97,3 +98,11 @@ pub fn node_not_added() -> Error {
|
|||
data: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unknown() -> Error {
|
||||
Error {
|
||||
code: ErrorCode::ServerError(codes::UNKNOWN),
|
||||
message: "Unknown error has occurred".into(),
|
||||
data: None,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
use std::sync::Arc;
|
||||
use std::net::SocketAddr;
|
||||
use std::net::{SocketAddr, IpAddr};
|
||||
use v1::traits::Network as NetworkRpc;
|
||||
use v1::types::AddNodeOperation;
|
||||
use v1::types::{AddNodeOperation, NodeInfo};
|
||||
use jsonrpc_core::Error;
|
||||
use jsonrpc_macros::Trailing;
|
||||
use v1::helpers::errors;
|
||||
use p2p;
|
||||
|
||||
|
@ -10,6 +11,8 @@ pub trait NetworkApi : Send + Sync + 'static {
|
|||
fn add_node(&self, socket_addr: SocketAddr) -> Result<(), p2p::NodeTableError>;
|
||||
fn remove_node(&self, socket_addr: SocketAddr) -> Result<(), p2p::NodeTableError>;
|
||||
fn connect(&self, socket_addr: SocketAddr);
|
||||
fn node_info(&self, node_addr: IpAddr) -> Result<NodeInfo, p2p::NodeTableError>;
|
||||
fn nodes_info(&self) -> Vec<NodeInfo>;
|
||||
}
|
||||
|
||||
impl<T> NetworkRpc for NetworkClient<T> where T: NetworkApi {
|
||||
|
@ -29,6 +32,23 @@ impl<T> NetworkRpc for NetworkClient<T> where T: NetworkApi {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn node_info(&self, _dns: bool, node_addr: Trailing<String>) -> Result<Vec<NodeInfo>, Error> {
|
||||
Ok(
|
||||
if node_addr.0.is_empty() {
|
||||
self.api.nodes_info()
|
||||
}
|
||||
else {
|
||||
let addr = try!(node_addr.0.parse().map_err(
|
||||
|_| errors::invalid_params("node", "Invalid ip address format, should be ip address (127.0.0.1)")));
|
||||
let node_info = try!(
|
||||
self.api.node_info(addr)
|
||||
.map_err(|_| errors::node_not_added())
|
||||
);
|
||||
vec![node_info]
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NetworkClient<T: NetworkApi> {
|
||||
|
@ -65,4 +85,12 @@ impl NetworkApi for NetworkClientCore {
|
|||
fn connect(&self, socket_addr: SocketAddr) {
|
||||
p2p::Context::connect_normal(self.p2p.clone(), socket_addr);
|
||||
}
|
||||
|
||||
fn node_info(&self, node_addr: IpAddr) -> Result<NodeInfo, p2p::NodeTableError> {
|
||||
Err(p2p::NodeTableError::NoAddressInTable)
|
||||
}
|
||||
|
||||
fn nodes_info(&self) -> Vec<NodeInfo> {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use jsonrpc_core::Error;
|
||||
use v1::types::AddNodeOperation;
|
||||
use jsonrpc_macros::Trailing;
|
||||
use v1::types::{AddNodeOperation, NodeInfo};
|
||||
|
||||
build_rpc_trait! {
|
||||
/// Parity-bitcoin network interface
|
||||
|
@ -11,9 +12,9 @@ build_rpc_trait! {
|
|||
#[rpc(name = "addnode")]
|
||||
fn add_node(&self, String, AddNodeOperation) -> Result<(), Error>;
|
||||
/// Query node(s) info
|
||||
/// @curl-example: curl --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getaddednodeinfo", "params": [true, "192.168.0.201"] }' -H 'content-type: application/json;' http://127.0.0.1:8332/
|
||||
/// @curl-example: curl --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getaddednodeinfo", "params": [true] }' -H 'content-type: application/json;' http://127.0.0.1:8332/
|
||||
/// @curl-example: curl --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getaddednodeinfo", "params": [true, "192.168.0.201"] }' -H 'content-type: application/json;' http://127.0.0.1:8332/
|
||||
#[rpc(name = "getaddednodeinfo")]
|
||||
fn node_info(&self, bool, String) -> Result<(), Error>;
|
||||
fn node_info(&self, bool, Trailing<String>) -> Result<Vec<NodeInfo>, Error>;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,4 +22,4 @@ pub use self::raw_block::RawBlock;
|
|||
pub use self::raw_transaction::RawTransaction;
|
||||
pub use self::script::ScriptType;
|
||||
pub use self::uint::U256;
|
||||
pub use self::nodes::AddNodeOperation;
|
||||
pub use self::nodes::{AddNodeOperation, NodeInfo};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use serde::{Deserialize, Deserializer};
|
||||
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum AddNodeOperation {
|
||||
|
@ -29,3 +29,30 @@ impl Deserialize for AddNodeOperation {
|
|||
deserializer.deserialize(DummyVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct NodeInfoAddress {
|
||||
address: String,
|
||||
connected: NodeInfoAddressConnectionType,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct NodeInfo {
|
||||
addednode: String,
|
||||
connected: bool,
|
||||
addresses: Vec<NodeInfoAddress>,
|
||||
}
|
||||
|
||||
pub enum NodeInfoAddressConnectionType {
|
||||
Inbound,
|
||||
Outbound,
|
||||
}
|
||||
|
||||
impl Serialize for NodeInfoAddressConnectionType {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
||||
match *self {
|
||||
NodeInfoAddressConnectionType::Inbound => "inbound".serialize(serializer),
|
||||
NodeInfoAddressConnectionType::Outbound => "outbound".serialize(serializer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue