Skip to content

Commit 9aca575

Browse files
committed
Merge rust-bitcoin#191: Add model for getnodeaddresses
054862f Add model for getnodeaddresses (Jamil Lambert, PhD) Pull request description: GetNodeAddresses returns an Address. Create a bitcoin::Address model for it and modify the test to use it. Implement for v18 to v21. In v22 the return changes, label v22 to v28 as TODO. ACKs for top commit: tcharding: ACK 054862f Tree-SHA512: 771e760ef8330748c93d7e20731a8e46a6d51e4c1b4b9087e6ac38f07477d8441c041b053dd14803879e40414ed5bdc1ede2512b69ac7d486f15dd039d3c0923
2 parents 1aabd27 + 054862f commit 9aca575

File tree

27 files changed

+82
-27
lines changed

27 files changed

+82
-27
lines changed

integration_test/tests/network.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ fn network__get_network_info__modelled() {
3737
fn network__get_node_addresses() {
3838
let node = Node::with_wallet(Wallet::None, &[]);
3939
let json: GetNodeAddresses = node.client.get_node_addresses().expect("getnodeaddresses");
40-
assert!(json.0.len() <= 2500);
41-
if let Some(addr) = json.0.first() {
40+
let res: Result<mtype::GetNodeAddresses, _> = json.into_model();
41+
let model = res.expect("GetNodeAddresses into model");
42+
assert!(model.0.len() <= 2500);
43+
if let Some(addr) = model.0.first() {
4244
assert!(addr.port > 0);
43-
assert!(!addr.address.is_empty());
4445
assert!(addr.time > 1231006505);
4546
}
4647
}

types/src/model/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ pub use self::{
3434
BlockTemplateTransaction, GetBlockTemplate, GetPrioritisedTransactions,
3535
PrioritisedTransaction,
3636
},
37-
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},
37+
network::{
38+
GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork, GetNodeAddresses, NodeAddress,
39+
},
3840
raw_transactions::{
3941
AnalyzePsbt, AnalyzePsbtInput, AnalyzePsbtInputMissing, CombinePsbt, CombineRawTransaction,
4042
ConvertToPsbt, CreatePsbt, CreateRawTransaction, DecodePsbt, DecodeRawTransaction,

types/src/model/network.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
//! These structs model the types returned by the JSON-RPC API but have concrete types
66
//! and are not specific to a specific version of Bitcoin Core.
77
8-
use bitcoin::FeeRate;
8+
use bitcoin::address::NetworkUnchecked;
9+
use bitcoin::{Address, FeeRate};
910
use serde::{Deserialize, Serialize};
1011

1112
/// Models the result of JSON-RPC method `getnetworkinfo`.
@@ -64,3 +65,24 @@ pub struct GetNetworkInfoAddress {
6465
/// Relative score.
6566
pub score: u32,
6667
}
68+
69+
/// Result of JSON-RPC method `getnodeaddresses`.
70+
///
71+
/// > getnodeaddresses ( count )
72+
/// >
73+
/// > Return known addresses which can potentially be used to find new nodes in the network.
74+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
75+
pub struct GetNodeAddresses(pub Vec<NodeAddress>);
76+
77+
/// An item from the list returned by the JSON-RPC method `getnodeaddresses`.
78+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
79+
pub struct NodeAddress {
80+
/// Timestamp in seconds since epoch (Jan 1 1970 GMT) when the node was last seen.
81+
pub time: u64,
82+
/// The services offered.
83+
pub services: u64,
84+
/// The address of the node.
85+
pub address: Address<NetworkUnchecked>,
86+
/// The port of the node.
87+
pub port: u16,
88+
}

types/src/v18/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
//! | getconnectioncount | returns numeric | |
104104
//! | getnettotals | version | |
105105
//! | getnetworkinfo | version + model | |
106-
//! | getnodeaddresses | version | |
106+
//! | getnodeaddresses | version + model | |
107107
//! | getpeerinfo | version | |
108108
//! | listbanned | returns string | |
109109
//! | ping | returns nothing | |

types/src/v18/network/into.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
use bitcoin::address::NetworkUnchecked;
4+
use bitcoin::{address, Address};
5+
6+
use super::{GetNodeAddresses, NodeAddress};
7+
use crate::model;
8+
9+
impl GetNodeAddresses {
10+
/// Converts version specific type to a version nonspecific, more strongly typed type.
11+
pub fn into_model(self) -> Result<model::GetNodeAddresses, address::ParseError> {
12+
let nodes = self.0.into_iter().map(|n| n.into_model()).collect::<Result<_, _>>()?;
13+
Ok(model::GetNodeAddresses(nodes))
14+
}
15+
}
16+
17+
impl NodeAddress {
18+
/// Converts version specific type to a version nonspecific, more strongly typed type.
19+
pub fn into_model(self) -> Result<model::NodeAddress, address::ParseError> {
20+
let address = self.address.parse::<Address<NetworkUnchecked>>()?;
21+
Ok(model::NodeAddress {
22+
time: self.time,
23+
services: self.services,
24+
address,
25+
port: self.port,
26+
})
27+
}
28+
}

types/src/v18/network/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//!
55
//! Types for methods found under the `== Network ==` section of the API docs.
66
7+
mod into;
8+
79
use serde::{Deserialize, Serialize};
810

911
/// Result of JSON-RPC method `getnodeaddresses`.

types/src/v19/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
//! | getconnectioncount | returns numeric | |
104104
//! | getnettotals | version | |
105105
//! | getnetworkinfo | version + model | |
106-
//! | getnodeaddresses | version | |
106+
//! | getnodeaddresses | version + model | |
107107
//! | getpeerinfo | version | |
108108
//! | listbanned | returns string | |
109109
//! | ping | returns nothing | |

types/src/v20/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
//! | getconnectioncount | returns numeric | |
105105
//! | getnettotals | version | |
106106
//! | getnetworkinfo | version + model | |
107-
//! | getnodeaddresses | version | |
107+
//! | getnodeaddresses | version + model | |
108108
//! | getpeerinfo | version | |
109109
//! | listbanned | returns string | |
110110
//! | ping | returns nothing | |

types/src/v21/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
//! | getconnectioncount | returns numeric | |
106106
//! | getnettotals | version | |
107107
//! | getnetworkinfo | version + model | |
108-
//! | getnodeaddresses | version | |
108+
//! | getnodeaddresses | version + model | |
109109
//! | getpeerinfo | version | |
110110
//! | listbanned | returns string | |
111111
//! | ping | returns nothing | |

types/src/v22/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
//! | getconnectioncount | returns numeric | |
106106
//! | getnettotals | version | |
107107
//! | getnetworkinfo | version + model | |
108-
//! | getnodeaddresses | version | |
108+
//! | getnodeaddresses | version + model | TODO |
109109
//! | getpeerinfo | version | |
110110
//! | listbanned | returns string | |
111111
//! | ping | returns nothing | |

0 commit comments

Comments
 (0)