-
Notifications
You must be signed in to change notification settings - Fork 23
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 #709 from helium/andymck/reciprocity-check
reciprocity checks
- Loading branch information
Showing
11 changed files
with
903 additions
and
100 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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_witness ( | ||
id bytea primary key not null, | ||
timestamp timestamptz not null | ||
); | ||
-- seed last_witness with timestamps from last_beacon | ||
insert into last_witness (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,104 @@ | ||
use chrono::{DateTime, Utc}; | ||
|
||
use helium_crypto::PublicKeyBinary; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug)] | ||
#[sqlx(type_name = "last_witness")] | ||
pub struct LastWitness { | ||
pub id: Vec<u8>, | ||
pub timestamp: DateTime<Utc>, | ||
} | ||
|
||
impl LastWitness { | ||
pub async fn insert_kv<'c, E>(executor: E, id: &[u8], val: &str) -> anyhow::Result<Self> | ||
where | ||
E: sqlx::Executor<'c, Database = sqlx::Postgres>, | ||
{ | ||
Ok(sqlx::query_as::<_, Self>( | ||
r#" insert into last_witness ( id, timestamp ) | ||
values ($1, $2) | ||
on conflict (key) do nothing | ||
returning *; | ||
"#, | ||
) | ||
.bind(id) | ||
.bind(val) | ||
.fetch_one(executor) | ||
.await?) | ||
} | ||
|
||
pub async fn get<'c, E>(executor: E, id: &[u8]) -> anyhow::Result<Option<Self>> | ||
where | ||
E: sqlx::Executor<'c, Database = sqlx::Postgres>, | ||
{ | ||
Ok( | ||
sqlx::query_as::<_, LastWitness>(r#" select * from last_witness where id = $1;"#) | ||
.bind(id) | ||
.fetch_optional(executor) | ||
.await?, | ||
) | ||
} | ||
|
||
pub async fn last_timestamp<'c, E>( | ||
executor: E, | ||
id: &[u8], | ||
) -> anyhow::Result<Option<DateTime<Utc>>> | ||
where | ||
E: sqlx::Executor<'c, Database = sqlx::Postgres>, | ||
{ | ||
let height = sqlx::query_scalar( | ||
r#" | ||
select timestamp from last_witness | ||
where id = $1 | ||
"#, | ||
) | ||
.bind(id) | ||
.fetch_optional(executor) | ||
.await?; | ||
Ok(height) | ||
} | ||
|
||
pub async fn update_last_timestamp<'c, E>( | ||
executor: E, | ||
id: &[u8], | ||
timestamp: DateTime<Utc>, | ||
) -> anyhow::Result<()> | ||
where | ||
E: sqlx::Executor<'c, Database = sqlx::Postgres>, | ||
{ | ||
let _ = sqlx::query( | ||
r#" | ||
insert into last_witness (id, timestamp) | ||
values ($1, $2) | ||
on conflict (id) do update set | ||
timestamp = EXCLUDED.timestamp | ||
"#, | ||
) | ||
.bind(id) | ||
.bind(timestamp) | ||
.execute(executor) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn bulk_update_last_timestamps( | ||
db: impl sqlx::PgExecutor<'_> + sqlx::Acquire<'_, Database = sqlx::Postgres> + Copy, | ||
ids: Vec<(PublicKeyBinary, DateTime<Utc>)>, | ||
) -> anyhow::Result<()> { | ||
const NUMBER_OF_FIELDS_IN_QUERY: u16 = 2; | ||
const MAX_BATCH_ENTRIES: usize = (u16::MAX / NUMBER_OF_FIELDS_IN_QUERY) as usize; | ||
let mut txn = db.begin().await?; | ||
for updates in ids.chunks(MAX_BATCH_ENTRIES) { | ||
let mut query_builder: sqlx::QueryBuilder<sqlx::Postgres> = | ||
sqlx::QueryBuilder::new(" insert into last_witness (id, timestamp) "); | ||
query_builder.push_values(updates, |mut builder, (id, ts)| { | ||
builder.push_bind(id.as_ref()).push_bind(ts); | ||
}); | ||
query_builder.push(" on conflict (id) do update set timestamp = EXCLUDED.timestamp "); | ||
query_builder.build().execute(&mut *txn).await?; | ||
} | ||
txn.commit().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
Oops, something went wrong.