Skip to content

Commit

Permalink
fix(network): collect all spends and store the first two ordered spends
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Jun 18, 2024
1 parent f083fb7 commit 25dd060
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions sn_node/src/put_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl Node {
// if we have no spends to verify, return early
let unique_pubkey = match spends_for_key.as_slice() {
[] => {
warn!("Found no valid spends to verify uppon validation for {pretty_key:?}");
warn!("Found no valid spends to verify upon validation for {pretty_key:?}");
return Err(Error::InvalidRequest(format!(
"No spends to verify when validating {pretty_key:?}"
)));
Expand Down Expand Up @@ -633,12 +633,8 @@ impl Node {
"Validating before storing spend at {spend_addr:?} with unique key: {unique_pubkey}"
);

// if we already have a double spend locally, no need to check the rest
let local_spends = self.get_local_spends(spend_addr).await?;
if let [a, b, ..] = local_spends.as_slice() {
debug!("Got a double spend locally already, skipping check for: {unique_pubkey:?}");
return Ok((a.to_owned(), Some(b.to_owned())));
}
let mut all_verified_spends = BTreeSet::from_iter(local_spends.into_iter());

// get spends from the network at the address for that unique pubkey
let network_spends = match self.network.get_raw_spends(spend_addr).await {
Expand Down Expand Up @@ -672,26 +668,15 @@ impl Node {
}

// collect spends until we have a double spend or until we have all the results
let mut all_verified_spends = BTreeSet::from_iter(local_spends.into_iter());
while let Some(res) = tasks.join_next().await {
match res {
Ok((spend, Ok(()))) => {
info!("Successfully verified {spend:?}");
let _inserted = all_verified_spends.insert(spend);

// exit early if we have a double spend
if let [a, b, ..] = all_verified_spends
.iter()
.collect::<Vec<&SignedSpend>>()
.as_slice()
{
debug!("Got a double spend for {unique_pubkey:?}");
return Ok(((*a).clone(), Some((*b).clone())));
}
}
Ok((spend, Err(e))) => {
// an error here most probably means the received spend is invalid
warn!("Skipping spend {spend:?} as an error occured during validation: {e:?}");
warn!("Skipping spend {spend:?} as an error occurred during validation: {e:?}");
}
Err(e) => {
let s =
Expand All @@ -712,6 +697,10 @@ impl Node {
debug!("Got a single valid spend for {unique_pubkey:?}");
Ok((a.to_owned(), None))
}
[a, b] => {
warn!("Got a double spend for {unique_pubkey:?}");
Ok((a.to_owned(), Some(b.to_owned())))
}
_ => {
debug!(
"No valid spends found while validating Spend PUT. Who is sending us garbage?"
Expand Down

0 comments on commit 25dd060

Please sign in to comment.