Skip to content

Commit e186995

Browse files
authored
Add beacon network crate (#715)
1 parent b948217 commit e186995

File tree

13 files changed

+614
-2
lines changed

13 files changed

+614
-2
lines changed

Cargo.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ members = [
5353
"ethportal-peertest",
5454
"light-client",
5555
"rpc",
56+
"trin-beacon",
5657
"trin-cli",
5758
"trin-history",
5859
"trin-state",

docker/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ COPY ./portalnet ./portalnet
2121
COPY ./light-client ./light-client
2222
COPY ./rpc ./rpc
2323
COPY ./src ./src
24+
COPY ./trin-beacon ./trin-beacon
2425
COPY ./trin-bridge ./trin-bridge
2526
COPY ./trin-cli ./trin-cli
2627
COPY ./trin-history ./trin-history

docker/Dockerfile.bridge

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ COPY ./Cargo.toml ./Cargo.toml
1818
COPY ./portalnet ./portalnet
1919
COPY ./light-client ./light-client
2020
COPY ./src ./src
21+
COPY ./trin-beacon ./trin-beacon
2122
COPY ./trin-bridge ./trin-bridge
2223
COPY ./trin-cli ./trin-cli
2324
COPY ./trin-history ./trin-history

newsfragments/715.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add beacon portal network crate

trin-beacon/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "trin-beacon"
3+
version = "0.1.0"
4+
edition = "2021"
5+
repository = "https://github.com/ethereum/trin/tree/master/trin-beacon"
6+
license = "GPL-3.0"
7+
readme = "README.md"
8+
keywords = ["ethereum", "portal-network"]
9+
categories = ["cryptography::cryptocurrencies"]
10+
description = "Beacon network subprotocol for Trin."
11+
authors = ["https://github.com/ethereum/trin/graphs/contributors"]
12+
13+
14+
[dependencies]
15+
anyhow = "1.0.68"
16+
async-trait = "0.1.53"
17+
discv5 = { version = "0.2.1", features = ["serde"]}
18+
ethportal-api = {path = "../ethportal-api"}
19+
eth2_ssz = "0.4.0"
20+
parking_lot = "0.11.2"
21+
portalnet = { path = "../portalnet" }
22+
serde_json = "1.0.89"
23+
tokio = {version = "1.14.0", features = ["full"]}
24+
tracing = "0.1.36"
25+
trin-types = { path = "../trin-types" }
26+
trin-validation = { path = "../trin-validation" }
27+
trin-utils = { path = "../trin-utils" }
28+
utp-rs = "0.1.0-alpha.4"

trin-beacon/src/events.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use crate::network::BeaconNetwork;
2+
use discv5::TalkRequest;
3+
use portalnet::types::messages::Message;
4+
use std::sync::Arc;
5+
use tokio::sync::mpsc::UnboundedReceiver;
6+
use tracing::{error, warn, Instrument};
7+
8+
pub struct BeaconEvents {
9+
pub network: Arc<BeaconNetwork>,
10+
pub event_rx: UnboundedReceiver<TalkRequest>,
11+
}
12+
13+
impl BeaconEvents {
14+
pub async fn start(mut self) {
15+
loop {
16+
tokio::select! {
17+
Some(talk_request) = self.event_rx.recv() => {
18+
self.handle_beacon_talk_request(talk_request);
19+
}
20+
}
21+
}
22+
}
23+
24+
/// Handle beacon network TalkRequest event
25+
fn handle_beacon_talk_request(&self, talk_request: TalkRequest) {
26+
let network = Arc::clone(&self.network);
27+
let talk_request_id = talk_request.id().clone();
28+
tokio::spawn(async move {
29+
let reply = match network
30+
.overlay
31+
.process_one_request(&talk_request)
32+
.instrument(tracing::info_span!("beacon_network", req = %talk_request_id))
33+
.await
34+
{
35+
Ok(response) => Message::from(response).into(),
36+
Err(error) => {
37+
error!(
38+
error = %error,
39+
request.discv5.id = %talk_request_id,
40+
"Error processing portal beacon request, responding with empty TALKRESP"
41+
);
42+
// Return an empty TALKRESP if there was an error executing the request
43+
"".into()
44+
}
45+
};
46+
if let Err(error) = talk_request.respond(reply) {
47+
warn!(error = %error, request.discv5.id = %talk_request_id, "Error responding to TALKREQ");
48+
}
49+
});
50+
}
51+
}

0 commit comments

Comments
 (0)