-
Notifications
You must be signed in to change notification settings - Fork 135
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 retries to bridge gossip, with rfc network checks #1032
Merged
njgheorghita
merged 1 commit into
ethereum:master
from
njgheorghita:bridge-gossip-retry
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
use crate::stats::{BeaconSlotStats, HistoryBlockStats, StatsReporter}; | ||
use ethportal_api::jsonrpsee::core::Error; | ||
use ethportal_api::types::portal::{ContentInfo, TraceGossipInfo}; | ||
use ethportal_api::{ | ||
BeaconContentKey, BeaconContentValue, BeaconNetworkApiClient, HistoryContentKey, | ||
HistoryContentValue, HistoryNetworkApiClient, OverlayContentKey, PossibleBeaconContentValue, | ||
PossibleHistoryContentValue, | ||
}; | ||
use jsonrpsee::http_client::HttpClient; | ||
use std::sync::{Arc, Mutex}; | ||
use tokio::time::{sleep, Duration}; | ||
use tracing::{debug, warn}; | ||
|
||
const GOSSIP_RETRY_COUNT: u64 = 3; | ||
const RETRY_AFTER: Duration = Duration::from_secs(15); | ||
|
||
/// Gossip any given content key / value to the history network. | ||
pub async fn gossip_beacon_content( | ||
portal_clients: Arc<Vec<HttpClient>>, | ||
content_key: BeaconContentKey, | ||
content_value: BeaconContentValue, | ||
slot_stats: Arc<Mutex<BeaconSlotStats>>, | ||
) -> anyhow::Result<()> { | ||
let mut results: Vec<Result<(Vec<TraceGossipInfo>, u64), Error>> = vec![]; | ||
for client in portal_clients.as_ref() { | ||
let client = client.clone(); | ||
let content_key = content_key.clone(); | ||
let content_value = content_value.clone(); | ||
let result = tokio::spawn(beacon_trace_gossip(client, content_key, content_value)).await?; | ||
results.push(result); | ||
} | ||
if let Ok(mut data) = slot_stats.lock() { | ||
data.update(content_key, results.into()); | ||
} else { | ||
warn!("Error updating beacon gossip stats. Unable to acquire lock."); | ||
} | ||
Ok(()) | ||
} | ||
|
||
async fn beacon_trace_gossip( | ||
client: HttpClient, | ||
content_key: BeaconContentKey, | ||
content_value: BeaconContentValue, | ||
) -> Result<(Vec<TraceGossipInfo>, u64), Error> { | ||
let mut retry_count = 0; | ||
let mut traces = vec![]; | ||
while retry_count < GOSSIP_RETRY_COUNT { | ||
let result = BeaconNetworkApiClient::trace_gossip( | ||
&client, | ||
content_key.clone(), | ||
content_value.clone(), | ||
) | ||
.await; | ||
// check if content was successfully transferred to at least one peer on network | ||
if let Ok(trace) = result { | ||
traces.push(trace.clone()); | ||
if !trace.transferred.is_empty() { | ||
return Ok((traces, retry_count)); | ||
} | ||
} | ||
// if not, make rfc request to see if data is available on network | ||
let result = | ||
BeaconNetworkApiClient::recursive_find_content(&client, content_key.clone()).await; | ||
if let Ok(PossibleBeaconContentValue::ContentPresent(_)) = result { | ||
debug!("Found content on network, after failing to gossip, aborting gossip. content key={:?}", content_key.to_hex()); | ||
return Ok((traces, retry_count)); | ||
} | ||
retry_count += 1; | ||
debug!("Unable to locate content on network, after failing to gossip, retrying in {:?} seconds. content key={:?}", RETRY_AFTER, content_key.to_hex()); | ||
sleep(RETRY_AFTER).await; | ||
} | ||
warn!( | ||
"Failed to gossip beacon content, without succesfully locating data on network, after {} attempts: content key={:?}", | ||
GOSSIP_RETRY_COUNT, | ||
content_key.to_hex(), | ||
); | ||
Ok((traces, retry_count)) | ||
} | ||
|
||
/// Gossip any given content key / value to the history network. | ||
pub async fn gossip_history_content( | ||
portal_clients: &Vec<HttpClient>, | ||
content_key: HistoryContentKey, | ||
content_value: HistoryContentValue, | ||
block_stats: Arc<Mutex<HistoryBlockStats>>, | ||
) -> anyhow::Result<()> { | ||
let mut results: Vec<Result<(Vec<TraceGossipInfo>, u64), Error>> = vec![]; | ||
for client in portal_clients { | ||
let client = client.clone(); | ||
let content_key = content_key.clone(); | ||
let content_value = content_value.clone(); | ||
let result = tokio::spawn(history_trace_gossip(client, content_key, content_value)).await?; | ||
results.push(result); | ||
} | ||
if let Ok(mut data) = block_stats.lock() { | ||
data.update(content_key, results.into()); | ||
} else { | ||
warn!("Error updating history gossip stats. Unable to acquire lock."); | ||
} | ||
Ok(()) | ||
} | ||
|
||
// todo why doesn't history return PossibleHistoryContentValue? | ||
async fn history_trace_gossip( | ||
client: HttpClient, | ||
content_key: HistoryContentKey, | ||
content_value: HistoryContentValue, | ||
) -> Result<(Vec<TraceGossipInfo>, u64), Error> { | ||
let mut retry_count = 0; | ||
let mut traces = vec![]; | ||
while retry_count < GOSSIP_RETRY_COUNT { | ||
let result = HistoryNetworkApiClient::trace_gossip( | ||
&client, | ||
content_key.clone(), | ||
content_value.clone(), | ||
) | ||
.await; | ||
// check if content was successfully transferred to at least one peer on network | ||
if let Ok(trace) = result { | ||
traces.push(trace.clone()); | ||
if !trace.transferred.is_empty() { | ||
return Ok((traces, retry_count)); | ||
} | ||
} | ||
// if not, make rfc request to see if data is available on network | ||
let result = | ||
HistoryNetworkApiClient::recursive_find_content(&client, content_key.clone()).await; | ||
if let Ok(ContentInfo::Content { | ||
content: PossibleHistoryContentValue::ContentPresent(_), | ||
.. | ||
}) = result | ||
{ | ||
debug!("Found content on network, after failing to gossip, aborting gossip. content key={:?}", content_key.to_hex()); | ||
return Ok((traces, retry_count)); | ||
} | ||
retry_count += 1; | ||
debug!("Unable to locate content on network, after failing to gossip, retrying in {:?} seconds. content key={:?}", RETRY_AFTER, content_key.to_hex()); | ||
sleep(RETRY_AFTER).await; | ||
} | ||
warn!( | ||
"Failed to gossip history content, without succesfully locating data on network, after {} attempts: content key={:?}", | ||
GOSSIP_RETRY_COUNT, | ||
content_key.to_hex(), | ||
); | ||
Ok((traces, retry_count)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beacon should return
ContentInfo::Content
(same as the history below). They have identical jsonr-rpc implementations: beacon | history.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed that the subnetwork returns the same type, but the rpc deserialization of the response are different for the two endpoints: beacon | history