Skip to content

Commit

Permalink
fix(network): add chain id to Kademlia discovery protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
AlonLStarkWare authored Aug 11, 2024
1 parent fb793a7 commit dbaa9ff
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions config/papyrus/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@
"privacy": "TemporaryValue",
"value": true
},
"network.chain_id": {
"description": "The chain to follow. For more details see https://docs.starknet.io/documentation/architecture_and_concepts/Blocks/transactions/#chain-id.",
"pointer_target": "chain_id",
"privacy": "Public"
},
"network.idle_connection_timeout": {
"description": "Amount of time in seconds that a connection with no active sessions will stay alive.",
"privacy": "Public",
Expand Down
1 change: 1 addition & 0 deletions crates/papyrus_network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ papyrus_common.workspace = true
papyrus_config.workspace = true
replace_with.workspace = true
serde = { workspace = true, features = ["derive"] }
starknet_api = { path = "../starknet_api", version = "0.13.0-rc.0" }
thiserror.workspace = true
tokio = { workspace = true, features = ["full", "sync"] }
tokio-retry.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_network/src/bin_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn build_swarm<Behaviour: NetworkBehaviour>(
listen_addresses: Vec<String>,
idle_connection_timeout: Duration,
secret_key: Option<Vec<u8>>,
behaviour: impl Fn(Keypair) -> Behaviour,
behaviour: impl FnOnce(Keypair) -> Behaviour,
) -> Swarm<Behaviour>
where
{
Expand Down
9 changes: 7 additions & 2 deletions crates/papyrus_network/src/discovery/flow_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use libp2p::swarm::behaviour::toggle::Toggle;
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
use libp2p::{identify, kad, Multiaddr, Swarm};
use libp2p_swarm_test::SwarmExt;
use starknet_api::core::ChainId;

use super::Behaviour;
use crate::mixed_behaviour;
Expand All @@ -24,8 +25,12 @@ struct DiscoveryMixedBehaviour {

impl DiscoveryMixedBehaviour {
pub fn new(key: Keypair, bootstrap_peer_multiaddr: Option<Multiaddr>) -> Self {
let mixed_behaviour =
MixedBehaviour::new(key, bootstrap_peer_multiaddr, Default::default());
let mixed_behaviour = MixedBehaviour::new(
key,
bootstrap_peer_multiaddr,
Default::default(),
ChainId::Mainnet,
);
Self {
identify: mixed_behaviour.identify,
kademlia: mixed_behaviour.kademlia,
Expand Down
8 changes: 7 additions & 1 deletion crates/papyrus_network/src/e2e_broadcast_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use libp2p::core::multiaddr::Protocol;
use libp2p::swarm::SwarmEvent;
use libp2p::{Multiaddr, Swarm};
use libp2p_swarm_test::SwarmExt;
use starknet_api::core::ChainId;

use crate::gossipsub_impl::Topic;
use crate::mixed_behaviour::MixedBehaviour;
Expand All @@ -16,7 +17,12 @@ const TIMEOUT: Duration = Duration::from_secs(1);

async fn create_swarm(bootstrap_peer_multiaddr: Option<Multiaddr>) -> Swarm<MixedBehaviour> {
let mut swarm = Swarm::new_ephemeral(|keypair| {
MixedBehaviour::new(keypair.clone(), bootstrap_peer_multiaddr, sqmr::Config::default())
MixedBehaviour::new(
keypair.clone(),
bootstrap_peer_multiaddr,
sqmr::Config::default(),
ChainId::Mainnet,
)
});
// Not using SwarmExt::listen because it panics if the swarm emits other events
let expected_listener_id = swarm.listen_on(Protocol::Memory(0).into()).unwrap();
Expand Down
9 changes: 9 additions & 0 deletions crates/papyrus_network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use papyrus_config::dumping::{ser_optional_param, ser_param, SerializeConfig};
use papyrus_config::validators::validate_vec_u256;
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use serde::{Deserialize, Serialize};
use starknet_api::core::ChainId;
use validator::Validate;

// TODO: add peer manager config to the network config
Expand All @@ -43,6 +44,7 @@ pub struct NetworkConfig {
#[validate(custom = "validate_vec_u256")]
#[serde(deserialize_with = "deserialize_optional_vec_u8")]
pub(crate) secret_key: Option<Vec<u8>>,
pub chain_id: ChainId,
}

impl SerializeConfig for NetworkConfig {
Expand Down Expand Up @@ -73,6 +75,12 @@ impl SerializeConfig for NetworkConfig {
alive.",
ParamPrivacyInput::Public,
),
ser_param(
"chain_id",
&self.chain_id,
"The chain to follow. For more details see https://docs.starknet.io/documentation/architecture_and_concepts/Blocks/transactions/#chain-id.",
ParamPrivacyInput::Public,
),
]);
config.extend(ser_optional_param(
&self.bootstrap_peer_multiaddr,
Expand Down Expand Up @@ -101,6 +109,7 @@ impl Default for NetworkConfig {
idle_connection_timeout: Duration::from_secs(120),
bootstrap_peer_multiaddr: None,
secret_key: None,
chain_id: ChainId::Mainnet,
}
}
}
15 changes: 13 additions & 2 deletions crates/papyrus_network/src/mixed_behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use libp2p::kad::store::MemoryStore;
use libp2p::swarm::behaviour::toggle::Toggle;
use libp2p::swarm::dial_opts::DialOpts;
use libp2p::swarm::NetworkBehaviour;
use libp2p::{gossipsub, identify, kad, Multiaddr, PeerId};
use libp2p::{gossipsub, identify, kad, Multiaddr, PeerId, StreamProtocol};
use starknet_api::core::ChainId;

use crate::discovery::identify_impl::{IdentifyToOtherBehaviourEvent, IDENTIFY_PROTOCOL_VERSION};
use crate::discovery::kad_impl::KadToOtherBehaviourEvent;
Expand Down Expand Up @@ -61,9 +62,15 @@ impl MixedBehaviour {
keypair: Keypair,
bootstrap_peer_multiaddr: Option<Multiaddr>,
streamed_bytes_config: sqmr::Config,
chain_id: ChainId,
) -> Self {
let public_key = keypair.public();
let local_peer_id = PeerId::from_public_key(&public_key);
let mut kademlia_config = kad::Config::default();
kademlia_config.set_protocol_names(vec![
StreamProtocol::try_from_owned(format!("/starknet/kad/{}/1.0.0", chain_id))
.expect("Failed to create StreamProtocol from a string that starts with /"),
]);
Self {
peer_manager: peer_manager::PeerManager::new(PeerManagerConfig::default()),
discovery: bootstrap_peer_multiaddr
Expand All @@ -82,7 +89,11 @@ impl MixedBehaviour {
public_key,
)),
// TODO: change kademlia protocol name
kademlia: kad::Behaviour::new(local_peer_id, MemoryStore::new(local_peer_id)),
kademlia: kad::Behaviour::with_config(
local_peer_id,
MemoryStore::new(local_peer_id),
kademlia_config,
),
sqmr: sqmr::Behaviour::new(streamed_bytes_config),
gossipsub: gossipsub::Behaviour::new(
gossipsub::MessageAuthenticity::Signed(keypair),
Expand Down
2 changes: 2 additions & 0 deletions crates/papyrus_network/src/network_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ impl NetworkManager {
idle_connection_timeout,
bootstrap_peer_multiaddr,
secret_key,
chain_id,
} = config;

let listen_addresses = vec![
Expand All @@ -561,6 +562,7 @@ impl NetworkManager {
key,
bootstrap_peer_multiaddr.clone(),
sqmr::Config { session_timeout },
chain_id,
)
});
Self::generic_new(swarm)
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_node/src/config/pointers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ lazy_static! {
&ChainId::Mainnet,
"The chain to follow. For more details see https://docs.starknet.io/documentation/architecture_and_concepts/Blocks/transactions/#chain-id.",
),
vec!["storage.db_config.chain_id".to_owned(), "rpc.chain_id".to_owned()],
vec!["storage.db_config.chain_id".to_owned(), "rpc.chain_id".to_owned(), "network.chain_id".to_owned()],
),
(
ser_pointer_target_param(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ expression: dumped_default_config
"value": true,
"privacy": "TemporaryValue"
},
"network.chain_id": {
"description": "The chain to follow. For more details see https://docs.starknet.io/documentation/architecture_and_concepts/Blocks/transactions/#chain-id.",
"value": "SN_MAIN",
"privacy": "Public"
},
"network.idle_connection_timeout": {
"description": "Amount of time in seconds that a connection with no active sessions will stay alive.",
"value": {
Expand Down

0 comments on commit dbaa9ff

Please sign in to comment.