Skip to content

Commit

Permalink
[network] add version_string field to PeerInfo (#3471)
Browse files Browse the repository at this point in the history
* [network] add `version_string` field to PeerInfo

Related #3439
  • Loading branch information
coldnight authored Jun 16, 2022
1 parent f8422a0 commit f0d73fb
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/peer-watcher/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ fn main() {
info,
notif_protocols,
rpc_protocols,
version_string,
} => Some(PeerInfo::new(
remote.into(),
*info,
notif_protocols,
rpc_protocols,
version_string,
)),
_ => None,
}
Expand Down
1 change: 1 addition & 0 deletions network-p2p/src/protocol/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub enum Event {
info: Box<ChainInfo>,
notif_protocols: Vec<Cow<'static, str>>,
rpc_protocols: Vec<Cow<'static, str>>,
version_string: Option<String>,
},

/// Closed a substream with the given node. Always matches a corresponding previous
Expand Down
5 changes: 5 additions & 0 deletions network-p2p/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,11 @@ impl Future for NetworkWorker {
info,
notif_protocols,
rpc_protocols,
version_string: this
.network_service
.behaviour_mut()
.node(&remote)
.and_then(|i| i.client_version().map(|s| s.to_owned())),
});
}
Poll::Ready(SwarmEvent::Behaviour(BehaviourOut::NotificationStreamReplaced {
Expand Down
2 changes: 2 additions & 0 deletions network-p2p/src/service_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ fn test_support_protocol() {
info: _,
notif_protocols,
rpc_protocols,
version_string: _,
} = open_event1
{
assert_eq!(&remote, service2.peer_id());
Expand All @@ -594,6 +595,7 @@ fn test_support_protocol() {
info: _,
notif_protocols,
rpc_protocols,
version_string: _,
} = open_event2
{
assert_eq!(&remote, service1.peer_id());
Expand Down
4 changes: 4 additions & 0 deletions network/api/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,28 @@ fn test_peer_selector() {
ChainInfo::new(1.into(), HashValue::zero(), mock_chain_status(100.into())),
vec![],
vec![],
None,
),
PeerInfo::new(
PeerId::random(),
ChainInfo::new(1.into(), HashValue::zero(), mock_chain_status(99.into())),
vec![],
vec![],
None,
),
PeerInfo::new(
PeerId::random(),
ChainInfo::new(1.into(), HashValue::zero(), mock_chain_status(100.into())),
vec![],
vec![],
None,
),
PeerInfo::new(
PeerId::random(),
ChainInfo::new(1.into(), HashValue::zero(), mock_chain_status(1.into())),
vec![],
vec![],
None,
),
];

Expand Down
12 changes: 10 additions & 2 deletions network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl EventHandler<Self, Event> for NetworkActorService {
info,
notif_protocols,
rpc_protocols,
version_string,
} => {
//TODO Refactor PeerEvent for handle protocol and substream.
// Currently, every notification stream open will trigger a PeerEvent, so it will trigger repeat event.
Expand All @@ -134,8 +135,13 @@ impl EventHandler<Self, Event> for NetworkActorService {
remote, protocol, notif_protocols, rpc_protocols
);
let peer_event = PeerEvent::Open(remote.into(), info.clone());
self.inner
.on_peer_connected(remote.into(), *info, notif_protocols, rpc_protocols);
self.inner.on_peer_connected(
remote.into(),
*info,
notif_protocols,
rpc_protocols,
version_string,
);
ctx.broadcast(peer_event);
}
Event::NotificationStreamClosed { remote, .. } => {
Expand Down Expand Up @@ -542,6 +548,7 @@ impl Inner {
chain_info: ChainInfo,
notif_protocols: Vec<Cow<'static, str>>,
rpc_protocols: Vec<Cow<'static, str>>,
version_string: Option<String>,
) {
self.peers
.entry(peer_id.clone())
Expand All @@ -562,6 +569,7 @@ impl Inner {
chain_info,
notif_protocols,
rpc_protocols,
version_string,
))
});
}
Expand Down
1 change: 1 addition & 0 deletions network/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub fn build_network_worker(
.iter()
.map(|config| config.name.clone())
.collect(),
None,
);
let config = NetworkConfiguration {
listen_addresses: vec![network_config.listen()],
Expand Down
2 changes: 2 additions & 0 deletions rpc/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,7 @@ pub struct PeerInfoView {
pub chain_info: ChainInfoView,
pub notif_protocols: String,
pub rpc_protocols: String,
pub version_string: Option<String>,
}

impl From<PeerInfo> for PeerInfoView {
Expand All @@ -1162,6 +1163,7 @@ impl From<PeerInfo> for PeerInfoView {
chain_info: info.chain_info.into(),
notif_protocols: info.notif_protocols.join(","),
rpc_protocols: info.rpc_protocols.join(","),
version_string: info.version_string,
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions rpc/generated_rpc_schema/node.json
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@
},
"rpc_protocols": {
"type": "string"
},
"version_string": {
"type": [
"string",
"null"
]
}
}
},
Expand Down Expand Up @@ -611,6 +617,12 @@
},
"rpc_protocols": {
"type": "string"
},
"version_string": {
"type": [
"string",
"null"
]
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion sync/src/tasks/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl SyncNodeMocker {
chain.chain_info(),
NotificationMessage::protocols(),
G_RPC_INFO.clone().into_protocols(),
None,
);
let peer_selector = PeerSelector::new(vec![peer_info], PeerStrategy::default(), None);
Ok(Self::new_inner(
Expand All @@ -169,7 +170,7 @@ impl SyncNodeMocker {
) -> Result<Self> {
let chain = MockChain::new(net)?;
let peer_id = PeerId::random();
let peer_info = PeerInfo::new(peer_id.clone(), chain.chain_info(), vec![], vec![]);
let peer_info = PeerInfo::new(peer_id.clone(), chain.chain_info(), vec![], vec![], None);
let peer_selector = PeerSelector::new(vec![peer_info], PeerStrategy::default(), None);
Ok(Self::new_inner(
peer_id,
Expand Down Expand Up @@ -217,6 +218,7 @@ impl SyncNodeMocker {
self.chain_mocker.chain_info(),
vec![],
vec![],
None,
)
}

Expand Down
2 changes: 2 additions & 0 deletions sync/src/tasks/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ async fn test_sync_target() {
low_chain_info.clone(),
vec![],
vec![],
None,
));
node1.produce_block(10).unwrap();
let high_chain_info = node1.peer_info().chain_info().clone();
Expand All @@ -952,6 +953,7 @@ async fn test_sync_target() {
high_chain_info.clone(),
vec![],
vec![],
None,
));

let net2 = ChainNetwork::new_builtin(BuiltinNetworkID::Test);
Expand Down
4 changes: 4 additions & 0 deletions types/src/peer_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub struct PeerInfo {
pub chain_info: ChainInfo,
pub notif_protocols: Vec<Cow<'static, str>>,
pub rpc_protocols: Vec<Cow<'static, str>>,
pub version_string: Option<String>,
}

impl PeerInfo {
Expand All @@ -165,12 +166,14 @@ impl PeerInfo {
chain_info: ChainInfo,
notif_protocols: Vec<Cow<'static, str>>,
rpc_protocols: Vec<Cow<'static, str>>,
version_string: Option<String>,
) -> Self {
Self {
peer_id,
chain_info,
notif_protocols,
rpc_protocols,
version_string,
}
}

Expand Down Expand Up @@ -234,6 +237,7 @@ impl PeerInfo {
chain_info: ChainInfo::random(),
notif_protocols: vec![],
rpc_protocols: vec![],
version_string: None,
}
}
}
Expand Down

0 comments on commit f0d73fb

Please sign in to comment.