Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add RPC calls for p2p module and tests for them #52

Merged
merged 20 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Run clippy
run: cargo clippy --all -- -D warnings
run: cargo clippy --all --all-targets -- -D warnings


fmt:
Expand Down
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions celestia/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::env;
use anyhow::{Context, Result};
use celestia_node::node::{Node, NodeConfig};
use celestia_rpc::prelude::*;
use libp2p::{core::upgrade::Version, identity, noise, tcp, yamux, Multiaddr, Transport};
use libp2p::{core::upgrade::Version, identity, noise, tcp, yamux, Transport};

const WS_URL: &str = "ws://localhost:26658";

Expand All @@ -15,15 +15,11 @@ pub async fn run() -> Result<()> {
let auth_token = env::var("CELESTIA_NODE_AUTH_TOKEN_ADMIN")?;
let client = celestia_rpc::client::new_websocket(WS_URL, Some(&auth_token)).await?;
let bridge_info = client.p2p_info().await?;
let bridge_maddrs: Vec<Multiaddr> = bridge_info
.addrs
.into_iter()
.map(|addr| addr.parse().context("Parsing addr failed"))
.collect::<Result<_>>()?;
println!("bridge id: {:?}", bridge_info.id);
println!("bridge listens on: {bridge_maddrs:?}");
println!("bridge listens on: {:?}", bridge_info.addrs);

let bridge_ma = bridge_maddrs
let bridge_ma = bridge_info
.addrs
.into_iter()
.find(|ma| ma.protocol_stack().any(|protocol| protocol == "tcp"))
.context("Bridge doesn't listen on tcp")?;
Expand Down
9 changes: 2 additions & 7 deletions node/tests/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,9 @@ async fn get_bridge_tcp_ma() -> Multiaddr {
.unwrap();

let bridge_info = client.p2p_info().await.unwrap();
let bridge_maddrs: Vec<Multiaddr> = bridge_info
.addrs
.into_iter()
.map(|addr| addr.parse())
.collect::<Result<_, _>>()
.unwrap();

bridge_maddrs
bridge_info
.addrs
.into_iter()
.find(|ma| ma.protocol_stack().any(|protocol| protocol == "tcp"))
.expect("Bridge doesn't listen on tcp")
Expand Down
7 changes: 7 additions & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ thiserror = "1.0.40"
jsonrpsee = { version = "0.20", features = ["client-core", "macros"] }
celestia-types = { workspace = true }
serde = { version = "1.0.188", features = ["derive"] }
libp2p = { version = "0.52.3", optional = true }
oblique marked this conversation as resolved.
Show resolved Hide resolved

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
http = "0.2.9"
Expand All @@ -19,3 +20,9 @@ anyhow = "1.0.71"
dotenvy = "0.15.7"
rand = "0.8.5"
tokio = { version = "1.32.0", features = ["rt", "macros"] }
futures = "0.3.28"
log = "0.4"

[features]
default = ["libp2p"]
libp2p = ["dep:libp2p", "celestia-types/libp2p"]
1 change: 1 addition & 0 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod blob;
pub mod client;
mod error;
mod header;
#[cfg(feature = "libp2p")]
pub mod p2p;
mod share;
mod state;
Expand Down
69 changes: 59 additions & 10 deletions rpc/src/p2p.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,66 @@
use celestia_types::p2p::{
AddrInfo, BandwidthStats, Connectedness, PeerId, Reachability, ResourceManagerStats,
};
use jsonrpsee::proc_macros::rpc;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct AddrInfo {
#[serde(rename = "ID")]
pub id: String,
// TODO: multiaddr
#[serde(rename = "Addrs")]
pub addrs: Vec<String>,
}

#[rpc(client)]
pub trait P2P {
#[method(name = "p2p.BandwidthForPeer")]
async fn p2p_bandwidth_for_peer(&self, peer_id: &PeerId) -> Result<BandwidthStats, Error>;
oblique marked this conversation as resolved.
Show resolved Hide resolved

#[method(name = "p2p.BandwidthForProtocol")]
async fn p2p_bandwidth_for_protocol(&self, protocol_id: &str) -> Result<BandwidthStats, Error>;

#[method(name = "p2p.BandwidthStats")]
async fn p2p_bandwidth_stats(&self) -> Result<BandwidthStats, Error>;

/// This method does not report errors due to a workaround to a go-jsonrpc bug, see https://github.com/eigerco/celestia-node-rs/issues/53
#[method(name = "p2p.BlockPeer")]
async fn p2p_block_peer(&self, peer_id: &PeerId);

/// This method does not report errors due to a workaround to a go-jsonrpc bug, see https://github.com/eigerco/celestia-node-rs/issues/53
#[method(name = "p2p.ClosePeer")]
async fn p2p_close_peer(&self, peer_id: &PeerId);

/// This method does not report errors due to a workaround to a go-jsonrpc bug, see https://github.com/eigerco/celestia-node-rs/issues/53
#[method(name = "p2p.Connect")]
async fn p2p_connect(&self, address: &AddrInfo);

#[method(name = "p2p.Connectedness")]
async fn p2p_connectedness(&self, peer_id: &PeerId) -> Result<Connectedness, Error>;

#[method(name = "p2p.Info")]
async fn p2p_info(&self) -> Result<AddrInfo, Error>;

#[method(name = "p2p.IsProtected")]
async fn p2p_is_protected(&self, peer_id: &PeerId, tag: &str) -> Result<bool, Error>;

#[method(name = "p2p.ListBlockedPeers")]
async fn p2p_list_blocked_peers(&self) -> Result<Vec<PeerId>, Error>;

#[method(name = "p2p.NATStatus")]
async fn p2p_nat_status(&self) -> Result<Reachability, Error>;

#[method(name = "p2p.PeerInfo")]
async fn p2p_peer_info(&self, peer_id: &PeerId) -> Result<AddrInfo, Error>;

#[method(name = "p2p.Peers")]
async fn p2p_peers(&self) -> Result<Vec<PeerId>, Error>;

/// This method does not report errors due to a workaround to a go-jsonrpc bug, see https://github.com/eigerco/celestia-node-rs/issues/53
#[method(name = "p2p.Protect")]
async fn p2p_protect(&self, peer_id: &PeerId, tag: &str);

zvolin marked this conversation as resolved.
Show resolved Hide resolved
#[method(name = "p2p.PubSubPeers")]
async fn p2p_pub_sub_peers(&self, topic: &str) -> Result<Option<Vec<PeerId>>, Error>;
oblique marked this conversation as resolved.
Show resolved Hide resolved

#[method(name = "p2p.ResourceState")]
async fn p2p_resource_state(&self) -> Result<ResourceManagerStats, Error>;

/// This method does not report errors due to a workaround to a go-jsonrpc bug, see https://github.com/eigerco/celestia-node-rs/issues/53
#[method(name = "p2p.UnblockPeer")]
async fn p2p_unblock_peer(&self, peer_id: &PeerId);
oblique marked this conversation as resolved.
Show resolved Hide resolved

#[method(name = "p2p.Unprotect")]
async fn p2p_unprotect(&self, peer_id: &PeerId, tag: &str) -> Result<bool, Error>;
}
Loading
Loading