Skip to content

Commit bde0f1e

Browse files
committed
Merge remote-tracking branch 'origin/release-v7.0.0' into unstable
2 parents ca8eaea + 54aef2d commit bde0f1e

File tree

21 files changed

+364
-48
lines changed

21 files changed

+364
-48
lines changed

Cargo.lock

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ validator_metrics = { path = "validator_client/validator_metrics" }
278278
validator_store = { path = "validator_client/validator_store" }
279279
validator_test_rig = { path = "testing/validator_test_rig" }
280280
warp_utils = { path = "common/warp_utils" }
281-
xdelta3 = { git = "http://github.com/sigp/xdelta3-rs", rev = "50d63cdf1878e5cf3538e9aae5eed34a22c64e4a" }
281+
xdelta3 = { git = "http://github.com/sigp/xdelta3-rs", rev = "4db64086bb02e9febb584ba93b9d16bb2ae3825a" }
282282
zstd = "0.13"
283283

284284
[profile.maxperf]

beacon_node/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "beacon_node"
3-
version = "7.0.0-beta.4"
3+
version = "7.0.0-beta.5"
44
authors = [
55
"Paul Hauner <[email protected]>",
66
"Age Manning <[email protected]",

beacon_node/beacon_chain/src/builder.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -820,10 +820,30 @@ where
820820
));
821821
}
822822

823-
let validator_pubkey_cache = self.validator_pubkey_cache.map(Ok).unwrap_or_else(|| {
824-
ValidatorPubkeyCache::new(&head_snapshot.beacon_state, store.clone())
825-
.map_err(|e| format!("Unable to init validator pubkey cache: {:?}", e))
826-
})?;
823+
let validator_pubkey_cache = self
824+
.validator_pubkey_cache
825+
.map(|mut validator_pubkey_cache| {
826+
// If any validators weren't persisted to disk on previous runs, this will use the head state to
827+
// "top-up" the in-memory validator cache and its on-disk representation with any missing validators.
828+
let pubkey_store_ops = validator_pubkey_cache
829+
.import_new_pubkeys(&head_snapshot.beacon_state)
830+
.map_err(|e| format!("Unable to top-up persisted pubkey cache {:?}", e))?;
831+
if !pubkey_store_ops.is_empty() {
832+
// Write any missed validators to disk
833+
debug!(
834+
missing_validators = pubkey_store_ops.len(),
835+
"Topping up validator pubkey cache"
836+
);
837+
store
838+
.do_atomically_with_block_and_blobs_cache(pubkey_store_ops)
839+
.map_err(|e| format!("Unable to write pubkeys to disk {:?}", e))?;
840+
}
841+
Ok(validator_pubkey_cache)
842+
})
843+
.unwrap_or_else(|| {
844+
ValidatorPubkeyCache::new(&head_snapshot.beacon_state, store.clone())
845+
.map_err(|e| format!("Unable to init validator pubkey cache: {:?}", e))
846+
})?;
827847

828848
let migrator_config = self.store_migrator_config.unwrap_or_default();
829849
let store_migrator =

beacon_node/http_api/src/lib.rs

+70-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use eth2::types::{
5454
use eth2::{CONSENSUS_VERSION_HEADER, CONTENT_TYPE_HEADER, SSZ_CONTENT_TYPE_HEADER};
5555
use health_metrics::observe::Observe;
5656
use lighthouse_network::rpc::methods::MetaData;
57-
use lighthouse_network::{types::SyncState, EnrExt, NetworkGlobals, PeerId, PubsubMessage};
57+
use lighthouse_network::{types::SyncState, Enr, EnrExt, NetworkGlobals, PeerId, PubsubMessage};
5858
use lighthouse_version::version_with_platform;
5959
use logging::{crit, SSELoggingComponents};
6060
use network::{NetworkMessage, NetworkSenders, ValidatorSubscriptionMessage};
@@ -72,6 +72,7 @@ use std::future::Future;
7272
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
7373
use std::path::PathBuf;
7474
use std::pin::Pin;
75+
use std::str::FromStr;
7576
use std::sync::Arc;
7677
use sysinfo::{System, SystemExt};
7778
use system_health::{observe_nat, observe_system_health_bn};
@@ -3586,7 +3587,7 @@ pub fn serve<T: BeaconChainTypes>(
35863587
.and(task_spawner_filter.clone())
35873588
.and(chain_filter.clone())
35883589
.and(warp_utils::json::json())
3589-
.and(network_tx_filter)
3590+
.and(network_tx_filter.clone())
35903591
.then(
35913592
|not_synced_filter: Result<(), Rejection>,
35923593
task_spawner: TaskSpawner<T::EthSpec>,
@@ -4015,6 +4016,71 @@ pub fn serve<T: BeaconChainTypes>(
40154016
},
40164017
);
40174018

4019+
// POST lighthouse/add_peer
4020+
let post_lighthouse_add_peer = warp::path("lighthouse")
4021+
.and(warp::path("add_peer"))
4022+
.and(warp::path::end())
4023+
.and(warp_utils::json::json())
4024+
.and(task_spawner_filter.clone())
4025+
.and(network_globals.clone())
4026+
.and(network_tx_filter.clone())
4027+
.then(
4028+
|request_data: api_types::AdminPeer,
4029+
task_spawner: TaskSpawner<T::EthSpec>,
4030+
network_globals: Arc<NetworkGlobals<T::EthSpec>>,
4031+
network_tx: UnboundedSender<NetworkMessage<T::EthSpec>>| {
4032+
task_spawner.blocking_json_task(Priority::P0, move || {
4033+
let enr = Enr::from_str(&request_data.enr).map_err(|e| {
4034+
warp_utils::reject::custom_bad_request(format!("invalid enr error {}", e))
4035+
})?;
4036+
info!(
4037+
peer_id = %enr.peer_id(),
4038+
multiaddr = ?enr.multiaddr(),
4039+
"Adding trusted peer"
4040+
);
4041+
network_globals.add_trusted_peer(enr.clone());
4042+
4043+
publish_network_message(&network_tx, NetworkMessage::ConnectTrustedPeer(enr))?;
4044+
4045+
Ok(())
4046+
})
4047+
},
4048+
);
4049+
4050+
// POST lighthouse/remove_peer
4051+
let post_lighthouse_remove_peer = warp::path("lighthouse")
4052+
.and(warp::path("remove_peer"))
4053+
.and(warp::path::end())
4054+
.and(warp_utils::json::json())
4055+
.and(task_spawner_filter.clone())
4056+
.and(network_globals.clone())
4057+
.and(network_tx_filter.clone())
4058+
.then(
4059+
|request_data: api_types::AdminPeer,
4060+
task_spawner: TaskSpawner<T::EthSpec>,
4061+
network_globals: Arc<NetworkGlobals<T::EthSpec>>,
4062+
network_tx: UnboundedSender<NetworkMessage<T::EthSpec>>| {
4063+
task_spawner.blocking_json_task(Priority::P0, move || {
4064+
let enr = Enr::from_str(&request_data.enr).map_err(|e| {
4065+
warp_utils::reject::custom_bad_request(format!("invalid enr error {}", e))
4066+
})?;
4067+
info!(
4068+
peer_id = %enr.peer_id(),
4069+
multiaddr = ?enr.multiaddr(),
4070+
"Removing trusted peer"
4071+
);
4072+
network_globals.remove_trusted_peer(enr.clone());
4073+
4074+
publish_network_message(
4075+
&network_tx,
4076+
NetworkMessage::DisconnectTrustedPeer(enr),
4077+
)?;
4078+
4079+
Ok(())
4080+
})
4081+
},
4082+
);
4083+
40184084
// POST lighthouse/liveness
40194085
let post_lighthouse_liveness = warp::path("lighthouse")
40204086
.and(warp::path("liveness"))
@@ -4774,6 +4840,8 @@ pub fn serve<T: BeaconChainTypes>(
47744840
.uor(post_lighthouse_ui_validator_info)
47754841
.uor(post_lighthouse_finalize)
47764842
.uor(post_lighthouse_compaction)
4843+
.uor(post_lighthouse_add_peer)
4844+
.uor(post_lighthouse_remove_peer)
47774845
.recover(warp_utils::reject::handle_rejection),
47784846
),
47794847
)

beacon_node/http_api/tests/tests.rs

+23
Original file line numberDiff line numberDiff line change
@@ -5750,6 +5750,27 @@ impl ApiTester {
57505750
self
57515751
}
57525752

5753+
pub async fn test_post_lighthouse_add_remove_peer(self) -> Self {
5754+
let trusted_peers = self.ctx.network_globals.as_ref().unwrap().trusted_peers();
5755+
// Check that there aren't any trusted peers on startup
5756+
assert!(trusted_peers.is_empty());
5757+
let enr = AdminPeer {enr: "enr:-QESuEDpVVjo8dmDuneRhLnXdIGY3e9NQiaG4sJR3GS-VMQCQDsmBYoQhJRaPeZzPlTsZj2F8v-iV4lKJEYIRIyztqexHodhdHRuZXRziAwAAAAAAAAAhmNsaWVudNiKTGlnaHRob3VzZYw3LjAuMC1iZXRhLjSEZXRoMpDS8Zl_YAAJEAAIAAAAAAAAgmlkgnY0gmlwhIe11XmDaXA2kCoBBPkAOitZAAAAAAAAAAKEcXVpY4IjKYVxdWljNoIjg4lzZWNwMjU2azGhA43ihEr9BUVVnIHIfFqBR3Izs4YRHHPsTqIbUgEb3Hc8iHN5bmNuZXRzD4N0Y3CCIyiEdGNwNoIjgoN1ZHCCIyiEdWRwNoIjgg".to_string()};
5758+
self.client
5759+
.post_lighthouse_add_peer(enr.clone())
5760+
.await
5761+
.unwrap();
5762+
let trusted_peers = self.ctx.network_globals.as_ref().unwrap().trusted_peers();
5763+
// Should have 1 trusted peer
5764+
assert_eq!(trusted_peers.len(), 1);
5765+
5766+
self.client.post_lighthouse_remove_peer(enr).await.unwrap();
5767+
let trusted_peers = self.ctx.network_globals.as_ref().unwrap().trusted_peers();
5768+
// Should be empty after removing
5769+
assert!(trusted_peers.is_empty());
5770+
5771+
self
5772+
}
5773+
57535774
pub async fn test_post_lighthouse_liveness(self) -> Self {
57545775
let epoch = self.chain.epoch().unwrap();
57555776
let head_state = self.chain.head_beacon_state_cloned();
@@ -7314,6 +7335,8 @@ async fn lighthouse_endpoints() {
73147335
.test_post_lighthouse_database_reconstruct()
73157336
.await
73167337
.test_post_lighthouse_liveness()
7338+
.await
7339+
.test_post_lighthouse_add_remove_peer()
73177340
.await;
73187341
}
73197342

beacon_node/lighthouse_network/src/peer_manager/mod.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ pub struct PeerManager<E: EthSpec> {
114114
metrics_enabled: bool,
115115
/// Keeps track of whether the QUIC protocol is enabled or not.
116116
quic_enabled: bool,
117+
trusted_peers: HashSet<Enr>,
117118
}
118119

119120
/// The events that the `PeerManager` outputs (requests).
@@ -192,6 +193,7 @@ impl<E: EthSpec> PeerManager<E> {
192193
discovery_enabled,
193194
metrics_enabled,
194195
quic_enabled,
196+
trusted_peers: Default::default(),
195197
})
196198
}
197199

@@ -888,7 +890,7 @@ impl<E: EthSpec> PeerManager<E> {
888890
}
889891

890892
// Gracefully disconnects a peer without banning them.
891-
fn disconnect_peer(&mut self, peer_id: PeerId, reason: GoodbyeReason) {
893+
pub fn disconnect_peer(&mut self, peer_id: PeerId, reason: GoodbyeReason) {
892894
self.events
893895
.push(PeerManagerEvent::DisconnectPeer(peer_id, reason));
894896
self.network_globals
@@ -936,6 +938,13 @@ impl<E: EthSpec> PeerManager<E> {
936938
}
937939
}
938940

941+
fn maintain_trusted_peers(&mut self) {
942+
let trusted_peers = self.trusted_peers.clone();
943+
for trusted_peer in trusted_peers {
944+
self.dial_peer(trusted_peer);
945+
}
946+
}
947+
939948
/// This function checks the status of our current peers and optionally requests a discovery
940949
/// query if we need to find more peers to maintain the current number of peers
941950
fn maintain_peer_count(&mut self, dialing_peers: usize) {
@@ -1233,6 +1242,7 @@ impl<E: EthSpec> PeerManager<E> {
12331242
fn heartbeat(&mut self) {
12341243
// Optionally run a discovery query if we need more peers.
12351244
self.maintain_peer_count(0);
1245+
self.maintain_trusted_peers();
12361246

12371247
// Cleans up the connection state of dialing peers.
12381248
// Libp2p dials peer-ids, but sometimes the response is from another peer-id or libp2p
@@ -1469,6 +1479,14 @@ impl<E: EthSpec> PeerManager<E> {
14691479
)
14701480
})
14711481
}
1482+
1483+
pub fn add_trusted_peer(&mut self, enr: Enr) {
1484+
self.trusted_peers.insert(enr);
1485+
}
1486+
1487+
pub fn remove_trusted_peer(&mut self, enr: Enr) {
1488+
self.trusted_peers.remove(&enr);
1489+
}
14721490
}
14731491

14741492
enum ConnectingType {

beacon_node/lighthouse_network/src/peer_manager/peerdb.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::net::IpAddr;
99
use std::time::Instant;
1010
use std::{cmp::Ordering, fmt::Display};
1111
use std::{
12-
collections::{HashMap, HashSet},
12+
collections::{hash_map::Entry, HashMap, HashSet},
1313
fmt::Formatter,
1414
};
1515
use sync_status::SyncStatus;
@@ -77,6 +77,33 @@ impl<E: EthSpec> PeerDB<E> {
7777
self.peers.iter()
7878
}
7979

80+
pub fn set_trusted_peer(&mut self, enr: Enr) {
81+
match self.peers.entry(enr.peer_id()) {
82+
Entry::Occupied(mut info) => {
83+
let entry = info.get_mut();
84+
entry.score = Score::max_score();
85+
entry.is_trusted = true;
86+
}
87+
Entry::Vacant(entry) => {
88+
entry.insert(PeerInfo::trusted_peer_info());
89+
}
90+
}
91+
}
92+
93+
pub fn unset_trusted_peer(&mut self, enr: Enr) {
94+
if let Some(info) = self.peers.get_mut(&enr.peer_id()) {
95+
info.is_trusted = false;
96+
info.score = Score::default();
97+
}
98+
}
99+
100+
pub fn trusted_peers(&self) -> Vec<PeerId> {
101+
self.peers
102+
.iter()
103+
.filter_map(|(id, info)| if info.is_trusted { Some(*id) } else { None })
104+
.collect()
105+
}
106+
80107
/// Gives the ids of all known peers.
81108
pub fn peer_ids(&self) -> impl Iterator<Item = &PeerId> {
82109
self.peers.keys()

beacon_node/lighthouse_network/src/peer_manager/peerdb/peer_info.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use PeerConnectionStatus::*;
2222
#[serde(bound = "E: EthSpec")]
2323
pub struct PeerInfo<E: EthSpec> {
2424
/// The peers reputation
25-
score: Score,
25+
pub(crate) score: Score,
2626
/// Client managing this peer
2727
client: Client,
2828
/// Connection status of this peer
@@ -51,7 +51,7 @@ pub struct PeerInfo<E: EthSpec> {
5151
#[serde(skip)]
5252
min_ttl: Option<Instant>,
5353
/// Is the peer a trusted peer.
54-
is_trusted: bool,
54+
pub(crate) is_trusted: bool,
5555
/// Direction of the first connection of the last (or current) connected session with this peer.
5656
/// None if this peer was never connected.
5757
connection_direction: Option<ConnectionDirection>,

beacon_node/lighthouse_network/src/service/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,21 @@ impl<E: EthSpec> Network<E> {
14751475
}
14761476
}
14771477

1478+
/// Adds the given `enr` to the trusted peers mapping and tries to dial it
1479+
/// every heartbeat to maintain the connection.
1480+
pub fn dial_trusted_peer(&mut self, enr: Enr) {
1481+
self.peer_manager_mut().add_trusted_peer(enr.clone());
1482+
self.peer_manager_mut().dial_peer(enr);
1483+
}
1484+
1485+
/// Remove the given peer from the trusted peers mapping if it exists and disconnect
1486+
/// from it.
1487+
pub fn remove_trusted_peer(&mut self, enr: Enr) {
1488+
self.peer_manager_mut().remove_trusted_peer(enr.clone());
1489+
self.peer_manager_mut()
1490+
.disconnect_peer(enr.peer_id(), GoodbyeReason::TooManyPeers);
1491+
}
1492+
14781493
/* Sub-behaviour event handling functions */
14791494

14801495
/// Handle a gossipsub event.

0 commit comments

Comments
 (0)