bitcoind_request/command/
get_node_addresses.rs

1/*
2getnodeaddresses ( count )
3
4Return known addresses which can potentially be used to find new nodes in the network
5
6Arguments:
71. count    (numeric, optional, default=1) The maximum number of addresses to return. Specify 0 to return all known addresses.
8
9Result:
10[                         (json array)
11  {                       (json object)
12    "time" : xxx,         (numeric) The UNIX epoch time of when the node was last seen
13    "services" : n,       (numeric) The services offered
14    "address" : "str",    (string) The address of the node
15    "port" : n            (numeric) The port of the node
16  },
17  ...
18]
19
20Examples:
21> bitcoin-cli getnodeaddresses 8
22> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getnodeaddresses", "params": [8]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
23 */
24use crate::command::CallableCommand;
25use crate::{client::Client, command::request::request};
26use serde::{Deserialize, Serialize};
27use serde_json::value::to_raw_value;
28
29#[derive(Serialize, Deserialize, Debug)]
30pub struct NodeAddress {
31    pub time: u64,       // The UNIX epoch time of when the node was last seen
32    pub services: u64,   // The services offered
33    pub address: String, // The address of the node
34    pub port: u64,       // The port of the node
35    // TODO: Use enum
36    pub network: String, // The network (ipv4, ipv6, onion, i2p) the node connected through
37}
38#[derive(Serialize, Deserialize, Debug)]
39pub struct GetNodeAddressesCommandResponse(pub Vec<NodeAddress>);
40
41pub enum CountArg {
42    MaxAddresses(u64),
43    AllAddresses,
44}
45pub enum NetworkArg {
46    All,
47    Ipv4,
48    Ipv6,
49    Onion,
50    I2p,
51}
52pub struct GetNodeAddressesCommand {
53    count: CountArg,
54    network: NetworkArg,
55}
56
57impl GetNodeAddressesCommand {
58    pub fn new() -> Self {
59        GetNodeAddressesCommand {
60            count: CountArg::MaxAddresses(1),
61            network: NetworkArg::All,
62        }
63    }
64    pub fn set_count(mut self, count: CountArg) -> Self {
65        self.count = count;
66        self
67    }
68    pub fn set_network(mut self, network: NetworkArg) -> Self {
69        self.network = network;
70        self
71    }
72}
73
74impl CallableCommand for GetNodeAddressesCommand {
75    type Response = GetNodeAddressesCommandResponse;
76    fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
77        let count_arg = match &self.count {
78            CountArg::MaxAddresses(count) => count,
79            CountArg::AllAddresses => &0,
80        };
81        let maybe_network_arg = match &self.network {
82            NetworkArg::All => None,
83            NetworkArg::Ipv4 => Some("ipv4"),
84            NetworkArg::Ipv6 => Some("ipv6"),
85            NetworkArg::Onion => Some("onion"),
86            NetworkArg::I2p => Some("i2p"),
87        };
88        let count_arg_raw_value = to_raw_value(count_arg).unwrap();
89
90        let params = match maybe_network_arg {
91            Some(network_arg) => {
92                let network_arg_raw_value = to_raw_value(network_arg).unwrap();
93                vec![count_arg_raw_value, network_arg_raw_value]
94            }
95            None => vec![count_arg_raw_value],
96        };
97        let command = "getnodeaddresses";
98        let r = request(client, command, params);
99        let response: GetNodeAddressesCommandResponse = r.result()?;
100        Ok(response)
101    }
102}