-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #784 from helium/andymck/one-beacon-attempt-per-wi…
…ndow track last beacon ts for schedule and recip separately. ensure only one beacon attempt per beaconing window
- Loading branch information
Showing
8 changed files
with
570 additions
and
87 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
create table last_beacon_recip ( | ||
id bytea primary key not null, | ||
timestamp timestamptz not null | ||
); | ||
-- seed beacon_recip with timestamps from last_beacon | ||
insert into last_beacon_recip (id, timestamp) | ||
select id, timestamp from last_beacon | ||
where timestamp > now() - interval '7 day'; |
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,53 @@ | ||
use chrono::{DateTime, Utc}; | ||
use helium_crypto::PublicKeyBinary; | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::{postgres::PgRow, FromRow, Postgres, Row, Transaction}; | ||
|
||
#[derive(Deserialize, Serialize, Debug)] | ||
pub struct LastBeaconReciprocity { | ||
pub id: PublicKeyBinary, | ||
pub timestamp: DateTime<Utc>, | ||
} | ||
|
||
impl FromRow<'_, PgRow> for LastBeaconReciprocity { | ||
fn from_row(row: &PgRow) -> sqlx::Result<Self> { | ||
Ok(Self { | ||
id: row.get::<Vec<u8>, &str>("id").into(), | ||
timestamp: row.get::<DateTime<Utc>, &str>("timestamp"), | ||
}) | ||
} | ||
} | ||
|
||
impl LastBeaconReciprocity { | ||
pub async fn get<'c, E>(executor: E, id: &PublicKeyBinary) -> anyhow::Result<Option<Self>> | ||
where | ||
E: sqlx::Executor<'c, Database = Postgres>, | ||
{ | ||
Ok(sqlx::query_as::<_, LastBeaconReciprocity>( | ||
r#" select * from last_beacon_recip where id = $1;"#, | ||
) | ||
.bind(id.as_ref()) | ||
.fetch_optional(executor) | ||
.await?) | ||
} | ||
|
||
pub async fn update_last_timestamp( | ||
txn: &mut Transaction<'_, Postgres>, | ||
id: &PublicKeyBinary, | ||
timestamp: DateTime<Utc>, | ||
) -> anyhow::Result<()> { | ||
let _ = sqlx::query( | ||
r#" | ||
insert into last_beacon_recip (id, timestamp) | ||
values ($1, $2) | ||
on conflict (id) do update set | ||
timestamp = EXCLUDED.timestamp | ||
"#, | ||
) | ||
.bind(id.as_ref()) | ||
.bind(timestamp) | ||
.execute(txn) | ||
.await?; | ||
Ok(()) | ||
} | ||
} |
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
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.