Skip to content

Commit

Permalink
Fix packet verifiers (#423)
Browse files Browse the repository at this point in the history
* Fix verifiers

* Use amount as num_dcs in construct for ValidDataTransferSession

* Address changes
  • Loading branch information
Matthew Plant authored Mar 30, 2023
1 parent f6fca67 commit 0b124c4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 45 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 2 additions & 24 deletions iot_packet_verifier/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use helium_proto::{
};
use sqlx::{Postgres, Transaction};
use std::{
collections::{hash_map::Entry, HashMap, HashSet},
collections::{hash_map::Entry, HashMap},
fmt::Debug,
mem,
};
Expand All @@ -28,13 +28,6 @@ pub struct Verifier<D, C> {
pub config_server: C,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
struct PacketId {
ts: u64,
oui: u64,
hash: Vec<u8>,
}

#[derive(thiserror::Error, Debug)]
pub enum VerificationError<DE, CE, BE, VPE, IPE> {
#[error("Debit error: {0}")]
Expand Down Expand Up @@ -69,24 +62,11 @@ where
IP: PacketWriter<InvalidPacket>,
{
let mut org_cache = HashMap::<u64, PublicKeyBinary>::new();
// This may need to be in the database so that we can set last_verified_report
// after this function.
let mut packets_seen = HashSet::<PacketId>::new();

tokio::pin!(reports);

while let Some(report) = reports.next().await {
let debit_amount = payload_size_to_dc(report.payload_size as u64);
let report_ts = report.timestamp();
let packet_id = PacketId {
ts: report_ts,
oui: report.oui,
hash: report.payload_hash.clone(),
};
if packets_seen.contains(&packet_id) {
continue;
}
packets_seen.insert(packet_id);

let payer = self
.config_server
Expand All @@ -105,11 +85,11 @@ where
.map_err(VerificationError::BurnError)?;
valid_packets
.write(ValidPacket {
packet_timestamp: report.timestamp(),
payload_size: report.payload_size,
gateway: report.gateway.into(),
payload_hash: report.payload_hash,
num_dcs: debit_amount as u32,
packet_timestamp: report_ts,
})
.await
.map_err(VerificationError::ValidPacketWriterError)?;
Expand Down Expand Up @@ -438,11 +418,9 @@ mod test {
// Packets for second OUI
packet_report(1, 0, 24, vec![4]),
packet_report(1, 1, 48, vec![5]),
packet_report(1, 1, 48, vec![5]),
packet_report(1, 2, 1, vec![6]),
// Packets for third OUI
packet_report(2, 0, 24, vec![7]),
packet_report(2, 0, 24, vec![7]),
];
// Set up orgs:
let mut orgs = MockConfigServer::default();
Expand Down
28 changes: 12 additions & 16 deletions mobile_packet_verifier/src/burner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,6 @@ pub struct DataTransferSession {
download_bytes: i64,
first_timestamp: DateTime<Utc>,
last_timestamp: DateTime<Utc>,
num_dcs: i64,
}

impl From<DataTransferSession> for ValidDataTransferSession {
fn from(ds: DataTransferSession) -> Self {
Self {
pub_key: ds.pub_key.into(),
payer: ds.payer.into(),
upload_bytes: ds.upload_bytes as u64,
download_bytes: ds.download_bytes as u64,
first_timestamp: ds.first_timestamp.encode_timestamp_millis(),
last_timestamp: ds.last_timestamp.encode_timestamp_millis(),
num_dcs: ds.num_dcs as u64,
}
}
}

#[derive(Default)]
Expand Down Expand Up @@ -236,7 +221,18 @@ impl Burner {

for session in sessions {
self.valid_sessions
.write(ValidDataTransferSession::from(session), &[])
.write(
ValidDataTransferSession {
pub_key: session.pub_key.into(),
payer: session.payer.into(),
upload_bytes: session.upload_bytes as u64,
download_bytes: session.download_bytes as u64,
num_dcs: amount,
first_timestamp: session.first_timestamp.encode_timestamp_millis(),
last_timestamp: session.last_timestamp.encode_timestamp_millis(),
},
&[],
)
.await?;
}
}
Expand Down

0 comments on commit 0b124c4

Please sign in to comment.