-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add poc-injector working logic (#71)
* Add working logic for poc_iot_injector - Construct poc_receipt v2 transaction - Add txn_service for txn submission - Add reward_shares to poc_receipt & poc_witnesses
- Loading branch information
Showing
18 changed files
with
631 additions
and
211 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 |
---|---|---|
|
@@ -7,6 +7,7 @@ flamegraph.* | |
*.json | ||
.envrc | ||
.env | ||
*.DS_STORE | ||
|
||
!/minio/bucket-policy.json | ||
*.bak |
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
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,27 @@ | ||
-- This extension gives us `uuid_generate_v1mc()` which generates UUIDs that cluster better than `gen_random_uuid()` | ||
-- while still being difficult to predict and enumerate. | ||
-- Also, while unlikely, `gen_random_uuid()` can in theory produce collisions which can trigger spurious errors on | ||
-- insertion, whereas it's much less likely with `uuid_generate_v1mc()`. | ||
create extension if not exists "uuid-ossp"; | ||
|
||
create or replace function set_updated_at() | ||
returns trigger as | ||
$$ | ||
begin | ||
NEW.updated_at = now(); | ||
return NEW; | ||
end; | ||
$$ language plpgsql; | ||
|
||
create or replace function trigger_updated_at(tablename regclass) | ||
returns void as | ||
$$ | ||
begin | ||
execute format('CREATE TRIGGER set_updated_at | ||
BEFORE UPDATE | ||
ON %s | ||
FOR EACH ROW | ||
WHEN (OLD is distinct from NEW) | ||
EXECUTE FUNCTION set_updated_at();', tablename); | ||
end; | ||
$$ language plpgsql; |
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,4 @@ | ||
create table meta ( | ||
key text primary key not null, | ||
value text | ||
); |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
RUST_LOG=info | ||
API_SOCKET_ADDR=0.0.0.0:8080 | ||
BUCKET=examplenet-poc-iot-injector | ||
METRICS_SCRAPE_ENDPOINT=127.0.0.1:19000 | ||
ENTROPY_STORE=/tmp/ | ||
ENTROPY_URL=https://quicknode.url.pro:443/token | ||
DATABASE_URL=db_url | ||
FOLLOWER_URI=http://127.0.0.1:8080 | ||
BUCKET=examplenet-pociot-verified | ||
POC_ORACLE_KEY=/tmp/poc_oracle_key | ||
LAST_POC_SUBMISSION_TS=0000000000 |
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,49 @@ | ||
use crate::{keypair::load_from_file, receipt_txn::handle_report_msg, Result}; | ||
use chrono::{NaiveDateTime, TimeZone, Utc}; | ||
use file_store::{FileStore, FileType}; | ||
use futures::stream::{self, StreamExt}; | ||
use helium_proto::{blockchain_txn::Txn, BlockchainTxn, Message}; | ||
|
||
/// Generate poc rewards | ||
#[derive(Debug, clap::Args)] | ||
pub struct Cmd { | ||
/// Required start time to look for (inclusive) | ||
#[clap(long)] | ||
after: NaiveDateTime, | ||
/// Required before time to look for (inclusive) | ||
#[clap(long)] | ||
before: NaiveDateTime, | ||
} | ||
|
||
impl Cmd { | ||
pub async fn run(&self) -> Result { | ||
let store = FileStore::from_env().await?; | ||
|
||
let after_utc = Utc.from_utc_datetime(&self.after); | ||
let before_utc = Utc.from_utc_datetime(&self.before); | ||
|
||
let file_list = store | ||
.list_all(FileType::LoraValidPoc, after_utc, before_utc) | ||
.await?; | ||
|
||
let mut stream = store.source(stream::iter(file_list).map(Ok).boxed()); | ||
let ts = before_utc.timestamp_millis(); | ||
|
||
let poc_injector_kp_path = | ||
std::env::var("POC_ORACLE_KEY").unwrap_or_else(|_| String::from("/tmp/poc_oracle_key")); | ||
let poc_oracle_key = load_from_file(&poc_injector_kp_path)?; | ||
|
||
while let Some(Ok(msg)) = stream.next().await { | ||
if let Ok(Some((txn, _hash, _hash_b64_url))) = | ||
handle_report_msg(msg, &poc_oracle_key, ts) | ||
{ | ||
let tx = BlockchainTxn { | ||
txn: Some(Txn::PocReceiptsV2(txn)), | ||
}; | ||
|
||
tracing::debug!("txn_bin: {:?}", tx.encode_to_vec()); | ||
} | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod generate; | ||
pub mod server; |
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,37 @@ | ||
use crate::{keypair::load_from_file, mk_db_pool, server::Server, Result}; | ||
use tokio::signal; | ||
|
||
/// Start rewards server | ||
#[derive(Debug, clap::Args)] | ||
pub struct Cmd {} | ||
|
||
impl Cmd { | ||
pub async fn run(&self) -> Result { | ||
// Install the prometheus metrics exporter | ||
poc_metrics::install_metrics(); | ||
|
||
// Create database pool | ||
let pool = mk_db_pool(10).await?; | ||
sqlx::migrate!().run(&pool).await?; | ||
|
||
// Configure shutdown trigger | ||
let (shutdown_trigger, shutdown_listener) = triggered::trigger(); | ||
tokio::spawn(async move { | ||
let _ = signal::ctrl_c().await; | ||
shutdown_trigger.trigger() | ||
}); | ||
|
||
// injector server keypair from env | ||
let poc_injector_kp_path = | ||
std::env::var("POC_ORACLE_KEY").unwrap_or_else(|_| String::from("/tmp/poc_oracle_key")); | ||
let poc_oracle_key = load_from_file(&poc_injector_kp_path)?; | ||
|
||
// poc_iot_injector server | ||
let mut poc_iot_injector_server = Server::new(pool, poc_oracle_key).await?; | ||
|
||
poc_iot_injector_server | ||
.run(shutdown_listener.clone()) | ||
.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.