Skip to content

Commit

Permalink
account loading with deduplication
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Oct 23, 2024
1 parent f7f3440 commit 69a2970
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion warehouse/sql/migrations/1_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ CREATE TABLE balance (
account_address CHAR(64) REFERENCES users(account_address) ON DELETE CASCADE,
balance BIGINT NOT NULL,
chain_timestamp TIMESTAMP NOT NULL,
height BIGINT NOT NULL,
db_version BIGINT NOT NULL,
epoch_number BIGINT NOT NULL
);
1 change: 1 addition & 0 deletions warehouse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod scan;
pub mod warehouse_cli;
pub mod extract;
pub mod table_structs;
pub mod load;
27 changes: 27 additions & 0 deletions warehouse/src/load.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::table_structs::{WarehouseAccount, WarehouseState};
use anyhow::Result;
use sqlx::{sqlite::SqliteQueryResult, SqlitePool};

pub async fn load_account_state(pool: &SqlitePool, accounts: Vec<WarehouseState>) -> Result<()> {
// insert missing accounts
for ws in accounts.iter() {
insert_one_account(pool, &ws.account).await?;
}

// increment the balance changes
Ok(())
}

pub async fn insert_one_account(pool: &SqlitePool, acc: &WarehouseAccount) -> Result<SqliteQueryResult> {

let res = sqlx::query(r#"
INSERT INTO users (account_address, is_legacy)
VALUES ($1,$2)
"#)
.bind(acc.address.to_string())
.bind(true)
.execute(pool)
.await?;

Ok(res)
}
19 changes: 19 additions & 0 deletions warehouse/tests/test_load.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use libra_types::exports::AccountAddress;
use libra_warehouse::table_structs::WarehouseAccount;
use sqlx::SqlitePool;

#[sqlx::test]
async fn insert_one_account(pool: SqlitePool) -> anyhow::Result<()> {
libra_warehouse::migrate::maybe_init(&pool).await?;
let marlon = AccountAddress::random();
let acc = WarehouseAccount {
address: marlon
};

libra_warehouse::load::insert_one_account(&pool, &acc).await?;

// second time should error if we are using the same account
assert!(libra_warehouse::load::insert_one_account(&pool, &acc).await.is_err());

Ok(())
}
2 changes: 1 addition & 1 deletion warehouse/tests/test_migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn can_init(pool: SqlitePool) -> anyhow::Result<()> {

let id = sqlx::query(
r#"
INSERT INTO balance (account_address, balance, chain_timestamp, height, epoch_number)
INSERT INTO balance (account_address, balance, chain_timestamp, db_version, epoch_number)
VALUES ("00000000000000000000000000000000e8953084617dd5c6071cf2918215e183", 11, 22222222, 600, 1)
"#,
)
Expand Down

0 comments on commit 69a2970

Please sign in to comment.