Skip to content

Commit

Permalink
Find duplicate hashes with slice windows
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane committed Jan 24, 2024
1 parent 9a1ddb4 commit f8fa2d3
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions crates/net/eth-wire/src/types/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,23 +474,17 @@ impl Decodable for NewPooledTransactionHashes68 {

// 4. checks for duplicate hash values
if hashes.len() > 1 {
let mut hashes_clone: SmallVec<[TxHash; 6]> = hashes.clone().into();
hashes_clone.sort();

// unlike method `dedup` on vec, breaks early if any duplicate found
let mut prev = hashes_clone[0];
for (i, &hash) in (&hashes_clone[1..]).iter().enumerate() {
if hash == prev {
for (i, hash_pair) in (&hashes).windows(2).enumerate() {
if hash_pair[0] == hash_pair[1] {
debug!(target: "net::eth-wire",
ty=types[i + 1],
size=sizes[i + 1],
hash=%hash,
ty=types[i],
size=sizes[i],
hash=%hash_pair[0],
err=ValidationError68::DUPLICATE_TX_HASH,
"invalid eth68 announcement, duplicate hash"
);
return Err(alloy_rlp::Error::Custom(ValidationError68::DUPLICATE_TX_HASH))
}
prev = hash;
}
}

Expand Down

0 comments on commit f8fa2d3

Please sign in to comment.