Skip to content

Commit e53f456

Browse files
quakeCopilot
andcommitted
refactor RPC peer identity to pubkey
Replace RPC peer_id usage with pubkey, remove GetPeerPubkey bridge, and drop redundant peer_id fields from pending/open-record data structures. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 6fa66af commit e53f456

File tree

111 files changed

+240
-235
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+240
-235
lines changed

crates/fiber-lib/src/fiber/channel.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ use ractor::{
7878
};
7979
use secp256k1::{XOnlyPublicKey, SECP256K1};
8080
use serde::{Deserialize, Serialize};
81-
use serde_with::{serde_as, DisplayFromStr};
81+
use serde_with::serde_as;
8282
use std::collections::{HashMap, HashSet, VecDeque};
8383
use std::iter;
8484
#[cfg(test)]
@@ -7980,9 +7980,8 @@ pub struct ChannelOpenRecord {
79807980
/// channels, the temp ID is replaced by the computed new ID when `accept_channel` is
79817981
/// called.
79827982
pub channel_id: Hash256,
7983-
/// The remote peer involved in this channel-opening attempt.
7984-
#[serde_as(as = "DisplayFromStr")]
7985-
pub peer_id: PeerId,
7983+
/// The remote peer public key.
7984+
pub pubkey: Pubkey,
79867985
/// Whether the local node is the accepting side (received the `OpenChannel` request).
79877986
pub is_acceptor: bool,
79887987
/// Current status of the opening process.
@@ -8001,11 +8000,11 @@ pub struct ChannelOpenRecord {
80018000

80028001
impl ChannelOpenRecord {
80038002
/// Create a new outbound record in the `WaitingForPeer` state.
8004-
pub fn new(channel_id: Hash256, peer_id: PeerId, funding_amount: u128) -> Self {
8003+
pub fn new(channel_id: Hash256, pubkey: Pubkey, funding_amount: u128) -> Self {
80058004
let now = now_timestamp_as_millis_u64();
80068005
Self {
80078006
channel_id,
8008-
peer_id,
8007+
pubkey,
80098008
is_acceptor: false,
80108009
status: ChannelOpeningStatus::WaitingForPeer,
80118010
funding_amount,
@@ -8017,8 +8016,8 @@ impl ChannelOpenRecord {
80178016

80188017
/// Create a new inbound record in the `WaitingForPeer` state.
80198018
/// Used when a remote peer's `OpenChannel` request is queued for local acceptance.
8020-
pub fn new_inbound(channel_id: Hash256, peer_id: PeerId, remote_funding_amount: u128) -> Self {
8021-
let mut record = Self::new(channel_id, peer_id, remote_funding_amount);
8019+
pub fn new_inbound(channel_id: Hash256, pubkey: Pubkey, remote_funding_amount: u128) -> Self {
8020+
let mut record = Self::new(channel_id, pubkey, remote_funding_amount);
80228021
record.is_acceptor = true;
80238022
record
80248023
}

crates/fiber-lib/src/fiber/network.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ pub struct AcceptChannelResponse {
209209
pub struct PendingAcceptChannel {
210210
/// The temporary channel ID assigned by the initiator.
211211
pub channel_id: Hash256,
212-
/// The peer ID of the channel initiator.
213-
pub peer_id: PeerId,
212+
/// The public key of the channel initiator.
213+
pub pubkey: Pubkey,
214214
/// The amount of CKB or UDT the initiator is contributing to the channel.
215215
pub funding_amount: u128,
216216
/// UDT type script, if this is a UDT channel.
@@ -2020,16 +2020,15 @@ where
20202020
.collect::<Vec<_>>();
20212021
let _ = rpc.send(Ok(peers));
20222022
}
2023-
20242023
NetworkActorCommand::GetPendingAcceptChannels(rpc) => {
20252024
let pending = state
20262025
.to_be_accepted_channels
20272026
.map
20282027
.iter()
20292028
.map(
2030-
|(channel_id, (peer_id, open_channel))| PendingAcceptChannel {
2029+
|(channel_id, (pubkey, open_channel))| PendingAcceptChannel {
20312030
channel_id: *channel_id,
2032-
peer_id: peer_id.clone(),
2031+
pubkey: *pubkey,
20332032
funding_amount: open_channel.funding_amount,
20342033
udt_type_script: open_channel.funding_udt_type_script.clone(),
20352034
created_at: state
@@ -3376,7 +3375,7 @@ where
33763375
self.on_channel_created(temp_channel_id, &peer_id, channel.clone());
33773376

33783377
// Record the channel opening attempt so it can be queried via RPC.
3379-
let record = ChannelOpenRecord::new(temp_channel_id, peer_id, funding_amount);
3378+
let record = ChannelOpenRecord::new(temp_channel_id, remote_pubkey, funding_amount);
33803379
self.store.insert_channel_open_record(record);
33813380

33823381
Ok((channel, temp_channel_id))
@@ -3398,20 +3397,13 @@ where
33983397
tlc_expiry_delta,
33993398
} = accept_channel;
34003399

3401-
let (peer_id, open_channel) = self
3400+
let (remote_pubkey, open_channel) = self
34023401
.to_be_accepted_channels
34033402
.remove(&temp_channel_id)
3404-
.ok_or(ProcessingChannelError::InvalidParameter(format!(
3405-
"No channel with temp id {:?} found",
3406-
&temp_channel_id
3407-
)))?;
3408-
3409-
let remote_pubkey =
3410-
self.get_peer_pubkey(&peer_id)
3411-
.ok_or(ProcessingChannelError::InvalidParameter(format!(
3412-
"Peer {:?} pubkey not found",
3413-
&peer_id
3414-
)))?;
3403+
.ok_or(ProcessingChannelError::InvalidParameter(
3404+
format!("No channel with temp id {:?} found", &temp_channel_id),
3405+
))?;
3406+
let peer_id = remote_pubkey.tentacle_peer_id();
34153407

34163408
let shutdown_script =
34173409
shutdown_script.unwrap_or_else(|| self.default_shutdown_script.clone());
@@ -3955,7 +3947,7 @@ where
39553947
.to_be_accepted_channels
39563948
.map
39573949
.iter()
3958-
.filter(|(_, (peer_id, _))| peer_id == id)
3950+
.filter(|(_, (pubkey, _))| pubkey.tentacle_peer_id() == *id)
39593951
.map(|(channel_id, _)| *channel_id)
39603952
.collect();
39613953
for channel_id in failed_channels {
@@ -4200,6 +4192,15 @@ where
42004192
) -> ProcessingChannelResult {
42014193
let id = open_channel.channel_id;
42024194
let remote_funding_amount = open_channel.funding_amount;
4195+
let remote_pubkey = self
4196+
.peer_session_map
4197+
.get(&peer_id)
4198+
.map(|peer| peer.pubkey)
4199+
.or_else(|| self.get_peer_pubkey(&peer_id))
4200+
.ok_or(ProcessingChannelError::InvalidParameter(format!(
4201+
"Peer {:?} pubkey not found",
4202+
&peer_id
4203+
)))?;
42034204
let result = check_open_channel_parameters(
42044205
&open_channel.funding_udt_type_script,
42054206
&open_channel.shutdown_script,
@@ -4211,15 +4212,15 @@ where
42114212
)
42124213
.and_then(|_| {
42134214
self.to_be_accepted_channels
4214-
.try_insert(id, peer_id.clone(), open_channel)
4215+
.try_insert(id, remote_pubkey, open_channel)
42154216
});
42164217

42174218
match result {
42184219
Ok(_) => {
42194220
// Create a persistent record so the accepting side can see this pending channel
42204221
// via list_channels(only_pending=true) and across node restarts.
42214222
let record =
4222-
ChannelOpenRecord::new_inbound(id, peer_id.clone(), remote_funding_amount);
4223+
ChannelOpenRecord::new_inbound(id, remote_pubkey, remote_funding_amount);
42234224
self.store.insert_channel_open_record(record);
42244225

42254226
// Notify outside observers.
@@ -5036,7 +5037,7 @@ pub(crate) fn find_type(addr: &Multiaddr) -> TransportType {
50365037
struct ToBeAcceptedChannels {
50375038
total_number_limit: usize,
50385039
total_bytes_limit: usize,
5039-
map: HashMap<Hash256, (PeerId, OpenChannel)>,
5040+
map: HashMap<Hash256, (Pubkey, OpenChannel)>,
50405041
}
50415042

50425043
impl Default for ToBeAcceptedChannels {
@@ -5067,21 +5068,21 @@ impl ToBeAcceptedChannels {
50675068
}
50685069
}
50695070

5070-
fn remove(&mut self, id: &Hash256) -> Option<(PeerId, OpenChannel)> {
5071+
fn remove(&mut self, id: &Hash256) -> Option<(Pubkey, OpenChannel)> {
50715072
self.map.remove(id)
50725073
}
50735074

50745075
// insert and apply throttle control
50755076
fn try_insert(
50765077
&mut self,
50775078
id: Hash256,
5078-
peer_id: PeerId,
5079+
pubkey: Pubkey,
50795080
open_channel: OpenChannel,
50805081
) -> ProcessingChannelResult {
50815082
if let Some(existing_value) = self.map.get(&id) {
50825083
let err_message = format!(
50835084
"A channel from {:?} of id {:?} is already awaiting to be accepted",
5084-
&peer_id, &id,
5085+
&pubkey, &id,
50855086
);
50865087
warn!("{}: {:?}", err_message, existing_value);
50875088
return Err(ProcessingChannelError::RepeatedProcessing(err_message));
@@ -5092,7 +5093,7 @@ impl ToBeAcceptedChannels {
50925093
let (total_number, total_bytes) = self
50935094
.map
50945095
.values()
5095-
.filter(|(saved_peer_id, _)| *saved_peer_id == peer_id)
5096+
.filter(|(saved_pubkey, _)| *saved_pubkey == pubkey)
50965097
.fold(
50975098
(1, open_channel.mem_size()),
50985099
|(count, size), (_, saved_open_channel)| {
@@ -5113,9 +5114,9 @@ impl ToBeAcceptedChannels {
51135114

51145115
debug!(
51155116
"Channel from {:?} of id {:?} is now awaiting to be accepted: {:?}",
5116-
&peer_id, &id, &open_channel
5117+
&pubkey, &id, &open_channel
51175118
);
5118-
self.map.insert(id, (peer_id, open_channel));
5119+
self.map.insert(id, (pubkey, open_channel));
51195120
Ok(())
51205121
}
51215122
}

crates/fiber-lib/src/fiber/tests/rpc.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async fn test_rpc_basic() {
6666
.send_rpc_request(
6767
"list_channels",
6868
ListChannelsParams {
69-
peer_id: None,
69+
pubkey: None,
7070
include_closed: None,
7171
only_pending: None,
7272
},
@@ -214,13 +214,13 @@ async fn test_rpc_list_peers() {
214214
let list_peers: ListPeersResult = node_0.send_rpc_request("list_peers", ()).await.unwrap();
215215
assert_eq!(list_peers.peers.len(), 1);
216216
assert_eq!(list_peers.peers[0].pubkey, node_1.pubkey);
217-
let node_1_peer_id = list_peers.peers[0].peer_id.clone();
217+
let node_1_pubkey = list_peers.peers[0].pubkey;
218218

219219
let _res: () = node_0
220220
.send_rpc_request(
221221
"disconnect_peer",
222222
crate::rpc::peer::DisconnectPeerParams {
223-
peer_id: node_1_peer_id,
223+
pubkey: node_1_pubkey,
224224
},
225225
)
226226
.await
@@ -254,8 +254,6 @@ async fn test_rpc_list_peers() {
254254
dbg!("list_peers: {:?}", &list_peers);
255255
assert!(list_peers.peers.iter().any(|p| p.pubkey == node_1.pubkey));
256256
assert!(list_peers.peers.iter().any(|p| p.pubkey == node_3.pubkey));
257-
assert!(list_peers.peers.iter().any(|p| p.peer_id == node_1.peer_id));
258-
assert!(list_peers.peers.iter().any(|p| p.peer_id == node_3.peer_id));
259257
}
260258

261259
#[tokio::test]
@@ -342,7 +340,7 @@ async fn test_rpc_shutdown_channels() {
342340
.send_rpc_request(
343341
"list_channels",
344342
ListChannelsParams {
345-
peer_id: None,
343+
pubkey: None,
346344
include_closed: None,
347345
only_pending: None,
348346
},
@@ -371,7 +369,7 @@ async fn test_rpc_shutdown_channels() {
371369
.send_rpc_request(
372370
"list_channels",
373371
ListChannelsParams {
374-
peer_id: None,
372+
pubkey: None,
375373
include_closed: Some(true),
376374
only_pending: None,
377375
},
@@ -413,7 +411,7 @@ async fn test_rpc_shutdown_channels() {
413411
.send_rpc_request(
414412
"list_channels",
415413
ListChannelsParams {
416-
peer_id: None,
414+
pubkey: None,
417415
include_closed: Some(true),
418416
only_pending: None,
419417
},
@@ -604,7 +602,7 @@ async fn test_rpc_basic_with_auth() {
604602
.send_rpc_request(
605603
"list_channels",
606604
ListChannelsParams {
607-
peer_id: None,
605+
pubkey: None,
608606
include_closed: None,
609607
only_pending: None,
610608
},

crates/fiber-lib/src/rpc/README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,8 @@ Attempts to open a channel with a peer.
202202

203203
##### Params
204204

205-
* `peer_id` - <em>`PeerId`</em>, The peer ID to open a channel with (base58 string, derived from the peer's `Pubkey`).
205+
* `pubkey` - <em>[Pubkey](#type-pubkey)</em>, The public key of the peer to open a channel with.
206206
The peer must be connected through the [connect_peer](#peer-connect_peer) rpc first.
207-
You can obtain a peer's `peer_id` from the `list_peers` RPC.
208207
* `funding_amount` - <em>`u128`</em>, The amount of CKB or UDT to fund the channel with.
209208
* `public` - <em>`Option<bool>`</em>, Whether this is a public channel (will be broadcasted to network, and can be used to forward TLCs), an optional parameter, default value is true.
210209
* `one_way` - <em>`Option<bool>`</em>, Whether this is a one-way channel (will not be broadcasted to network, and can only be used to send payment one way), an optional parameter, default value is false.
@@ -298,7 +297,7 @@ Lists all channels.
298297

299298
##### Params
300299

301-
* `peer_id` - <em>`Option<PeerId>`</em>, The peer ID to list channels for (base58 string, derived from the peer's `Pubkey`).
300+
* `pubkey` - <em>Option<[Pubkey](#type-pubkey)></em>, The public key to list channels for.
302301
An optional parameter, if not provided, all channels will be listed.
303302
* `include_closed` - <em>`Option<bool>`</em>, Whether to include closed channels in the list, an optional parameter, default value is false
304303
* `only_pending` - <em>`Option<bool>`</em>, When set to true, only return channels that are still being opened (non-final states:
@@ -887,7 +886,7 @@ Disconnect from a peer.
887886

888887
##### Params
889888

890-
* `peer_id` - <em>`PeerId`</em>, The peer ID of the peer to disconnect (base58 string, derived from the peer's `Pubkey`).
889+
* `pubkey` - <em>[Pubkey](#type-pubkey)</em>, The public key of the peer to disconnect.
891890

892891
##### Returns
893892

@@ -1144,7 +1143,7 @@ The channel data structure
11441143
* `is_one_way` - <em>`bool`</em>, Is this channel one-way?
11451144
Combines with is_acceptor to determine if the channel able to send payment to the counterparty or not.
11461145
* `channel_outpoint` - <em>`Option<OutPoint>`</em>, The outpoint of the channel
1147-
* `peer_id` - <em>`PeerId`</em>, The peer ID of the channel counterparty (base58 string, derived from the peer's `Pubkey`).
1146+
* `pubkey` - <em>[Pubkey](#type-pubkey)</em>, The public key of the channel counterparty.
11481147
* `funding_udt_type_script` - <em>`Option<Script>`</em>, The UDT type script of the channel
11491148
* `state` - <em>[ChannelState](#type-channelstate)</em>, The state of the channel
11501149
* `local_balance` - <em>`u128`</em>, The local balance of the channel
@@ -1430,8 +1429,6 @@ The information about a peer connected to the node.
14301429
#### Fields
14311430

14321431
* `pubkey` - <em>[Pubkey](#type-pubkey)</em>, The identity public key of the peer (also known as `node_id`).
1433-
* `peer_id` - <em>`PeerId`</em>, The peer ID of the peer (base58 string, derived by hashing the `pubkey` above).
1434-
This is used for P2P transport connections, e.g. when calling `open_channel` or `disconnect_peer`.
14351432
* `address` - <em>`MultiAddr`</em>, The multi-address associated with the connecting peer.
14361433
Note: this is only the address which used for connecting to the peer, not all addresses of the peer.
14371434
The `graph_nodes` in Graph rpc module will return all addresses of the peer.

0 commit comments

Comments
 (0)