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

Implement SSV Handshake Protocol #125

Open
wants to merge 51 commits into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
e5a10ad
handshake
diegomrsantos Feb 4, 2025
1b36113
remove length prefixing
diegomrsantos Feb 5, 2025
8dfba38
fixing serialization
diegomrsantos Feb 5, 2025
fec6579
fix tag
diegomrsantos Feb 5, 2025
2f898bc
delete node_info from network types
diegomrsantos Feb 5, 2025
335de8c
remove unnecessary deps
diegomrsantos Feb 7, 2025
537a988
handle handshake event
diegomrsantos Feb 5, 2025
5162ae6
many fixes
diegomrsantos Feb 5, 2025
60c1c13
remove record
diegomrsantos Feb 5, 2025
98b97bc
delete record mod and move things around
diegomrsantos Feb 6, 2025
1d41a3e
improve error handling
diegomrsantos Feb 6, 2025
b2bd4bf
remove pending_handshakes
diegomrsantos Feb 6, 2025
573dd54
fix wrong network id
diegomrsantos Feb 6, 2025
0114cdc
add NotifyHandler again
diegomrsantos Feb 6, 2025
e65e5b4
small change
diegomrsantos Feb 6, 2025
5d2f6df
use NodeInfoProvider trait
diegomrsantos Feb 7, 2025
97f6534
rename HandshakeEvent
diegomrsantos Feb 7, 2025
f755ea5
rename HandshakeBehaviour
diegomrsantos Feb 7, 2025
8f2e545
move behaviour to mod.rs
diegomrsantos Feb 7, 2025
d0ab738
create envelope mod
diegomrsantos Feb 7, 2025
88e8a92
improve error handling
diegomrsantos Feb 7, 2025
0c42c26
simplify error with thiserror
diegomrsantos Feb 7, 2025
c096635
improve error handling
diegomrsantos Feb 7, 2025
71f0de5
rename HandshakeError
diegomrsantos Feb 7, 2025
7f47b7d
cargo clippy
diegomrsantos Feb 7, 2025
6411d4c
fix request handling
diegomrsantos Feb 7, 2025
288e0a2
fix problems after rebase
diegomrsantos Feb 7, 2025
4be67aa
use quick-protobuf
diegomrsantos Feb 10, 2025
cb9363c
cargo fmt
diegomrsantos Feb 10, 2025
399499c
cargo sort
diegomrsantos Feb 10, 2025
3ac485b
cargo clippy
diegomrsantos Feb 10, 2025
2a47eb0
limit the msg payload while reading
diegomrsantos Feb 10, 2025
a2611e1
protocol spec
diegomrsantos Feb 10, 2025
b53d49e
remove deps
diegomrsantos Feb 10, 2025
aab349e
remove println!
diegomrsantos Feb 10, 2025
cc5d0e3
update comments
diegomrsantos Feb 10, 2025
c26750e
update wordlist.txt
diegomrsantos Feb 10, 2025
c75a225
ignore case in spellcheck
diegomrsantos Feb 10, 2025
d9791ce
Revert "ignore case in spellcheck"
diegomrsantos Feb 10, 2025
40dc739
changes after review
diegomrsantos Feb 10, 2025
ca67c05
use ssv_network_config
diegomrsantos Feb 10, 2025
d38776d
create and use DomainType
diegomrsantos Feb 11, 2025
db25bae
remove print
diegomrsantos Feb 11, 2025
5fd4866
cargo fmt and clippy
diegomrsantos Feb 11, 2025
026d99f
cargo sort
diegomrsantos Feb 11, 2025
c4ed901
Merge branch 'unstable' into handshake
diegomrsantos Feb 11, 2025
cb4b102
only send handshake request if we initiated the connection
diegomrsantos Feb 12, 2025
195e742
remove trait and create NodeInfoManager
diegomrsantos Feb 12, 2025
2954f08
cargo fmt
diegomrsantos Feb 12, 2025
4e12f5e
Bubble up all other ToSwarm events
diegomrsantos Feb 12, 2025
04cc5bf
use RwLock instead of Mutex
diegomrsantos Feb 12, 2025
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
12 changes: 11 additions & 1 deletion .github/wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ Holesky
Mainnet
lifecycle
Syncer

JSON
Protobuf
Responder
responder
Prepends
Secp
NodeMetadata
NodeInfo
subnets
holesky
responder's
27 changes: 26 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion anchor/client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub const DEFAULT_EXECUTION_NODE_WS: &str = "ws://localhost:8545/";
/// The default Data directory, relative to the users home directory
pub const DEFAULT_ROOT_DIR: &str = ".anchor";
/// Default network, used to partition the data storage
pub const DEFAULT_HARDCODED_NETWORK: &str = "mainnet";
pub const DEFAULT_HARDCODED_NETWORK: &str = "holesky";
dknopik marked this conversation as resolved.
Show resolved Hide resolved
/// Base directory name for unnamed testnets passed through the --testnet-dir flag
pub const CUSTOM_TESTNET_DIR: &str = "custom";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x00000502
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x00000001
32 changes: 31 additions & 1 deletion anchor/common/ssv_network_config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloy::hex;
use alloy::primitives::Address;
use enr::{CombinedKey, Enr};
use eth2_network_config::Eth2NetworkConfig;
Expand All @@ -22,21 +23,46 @@ macro_rules! get_hardcoded {
include_str_for_net!($network, "ssv_boot_enr.yaml"),
include_str_for_net!($network, "ssv_contract_address.txt"),
include_str_for_net!($network, "ssv_contract_block.txt"),
include_str_for_net!($network, "ssv_domain_type.txt"),
)
};
}

#[derive(Clone, Debug, Default)]
pub struct DomainType(pub [u8; 4]);

impl FromStr for DomainType {
type Err = String;

fn from_str(hex_str: &str) -> Result<Self, Self::Err> {
let bytes = hex::decode(hex_str).map_err(|_| "Invalid domain type hex")?;
if bytes.len() != 4 {
return Err("Domain type must be 4 bytes".into());
}
let mut domain_type = [0; 4];
domain_type.copy_from_slice(&bytes);
Ok(Self(domain_type))
}
}

impl From<DomainType> for String {
fn from(domain_type: DomainType) -> Self {
hex::encode(domain_type.0)
}
}

#[derive(Clone, Debug)]
pub struct SsvNetworkConfig {
pub eth2_network: Eth2NetworkConfig,
pub ssv_boot_nodes: Option<Vec<Enr<CombinedKey>>>,
pub ssv_contract: Address,
pub ssv_contract_block: u64,
pub ssv_domain_type: DomainType,
}

impl SsvNetworkConfig {
pub fn constant(name: &str) -> Result<Option<Self>, String> {
let (enr_yaml, address, block) = match name {
let (enr_yaml, address, block, domain_type) = match name {
"mainnet" => get_hardcoded!(mainnet),
"holesky" => get_hardcoded!(holesky),
_ => return Ok(None),
Expand All @@ -55,6 +81,9 @@ impl SsvNetworkConfig {
ssv_contract_block: block
.parse()
.map_err(|_| "Unable to parse built-in block!")?,
ssv_domain_type: domain_type
.parse()
.map_err(|_| "Unable to parse built-in domain type!")?,
}))
}

Expand All @@ -76,6 +105,7 @@ impl SsvNetworkConfig {
ssv_boot_nodes,
ssv_contract: read(&base_dir.join("ssv_contract_address.txt"))?,
ssv_contract_block: read(&base_dir.join("ssv_contract_block.txt"))?,
ssv_domain_type: read(&base_dir.join("ssv_domain_type.txt"))?,
eth2_network: Eth2NetworkConfig::load(base_dir)?,
})
}
Expand Down
7 changes: 6 additions & 1 deletion anchor/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = { workspace = true }
authors = ["Sigma Prime <[email protected]>"]

[dependencies]
async-trait = "0.1.85"
dirs = { workspace = true }
discv5 = { workspace = true }
ethereum_ssz = "0.8.1"
Expand All @@ -22,15 +23,19 @@ libp2p = { version = "0.54", default-features = false, features = [
"gossipsub",
"quic",
"ping",
"request-response",
] }
lighthouse_network = { workspace = true }
quick-protobuf = "0.8.1"
serde = { workspace = true }
serde_json = "1.0.137"
ssv_network_config = { workspace = true }
ssz_types = "0.10"
subnet_tracker = { workspace = true }
task_executor = { workspace = true }
thiserror = "1.0.69"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use workspace dependencies

Copy link
Author

@diegomrsantos diegomrsantos Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not defined in the root. Should it be defined there and inherited even if it's used only here for now?

Copy link
Member

@dknopik dknopik Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, good question. We should align with the others on that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rule of thumb I use: if it's only used by a subcrate import it there, when it becomes used by two or more, move it to workspace

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been taking the approach to keep it local if its unlikely to be used in other crates.

If its a general crate, its generally best to put in the workspace dependencies, in case for some reason some other crate decides to use it.

For this specific one, it seems fairly general and conceivable that another crate MIGHT use it in the future, so might be worth chucking in workspace deps.

tokio = { workspace = true }
tracing = { workspace = true }
types = { workspace = true }
version = { workspace = true }

[dev-dependencies]
Expand Down
3 changes: 3 additions & 0 deletions anchor/network/src/behaviour.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::discovery::Discovery;
use crate::handshake;
use libp2p::swarm::NetworkBehaviour;
use libp2p::{gossipsub, identify, ping};

Expand All @@ -12,4 +13,6 @@ pub struct AnchorBehaviour {
pub gossipsub: gossipsub::Behaviour,
/// Discv5 Discovery protocol.
pub discovery: Discovery,

pub handshake: handshake::Behaviour,
}
7 changes: 5 additions & 2 deletions anchor/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use discv5::Enr;
use libp2p::Multiaddr;
use lighthouse_network::types::GossipKind;
use lighthouse_network::{ListenAddr, ListenAddress};
use serde::{Deserialize, Serialize};
use ssv_network_config::DomainType;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::num::NonZeroU16;
use std::path::PathBuf;
Expand All @@ -16,7 +16,7 @@ pub const DEFAULT_DISC_PORT: u16 = 9100u16;
pub const DEFAULT_QUIC_PORT: u16 = 9101u16;

/// Configuration for setting up the p2p network.
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone)]
pub struct Config {
/// Data directory where node's keyfile is stored
pub network_dir: PathBuf,
Expand Down Expand Up @@ -66,6 +66,8 @@ pub struct Config {

/// Target number of connected peers.
pub target_peers: usize,

pub domain_type: DomainType,
}

impl Default for Config {
Expand Down Expand Up @@ -100,6 +102,7 @@ impl Default for Config {
disable_discovery: false,
disable_quic_support: false,
topics: vec![],
domain_type: DomainType::default(),
}
}
}
Loading