Skip to content

Commit

Permalink
scaffold loading coin state
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Oct 24, 2024
1 parent 0f3f90e commit 9f84641
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
3 changes: 2 additions & 1 deletion warehouse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub mod scan;
pub mod warehouse_cli;
pub mod extract;
pub mod table_structs;
pub mod load;
pub mod load_account;
pub mod load_coin;
4 changes: 2 additions & 2 deletions warehouse/src/load.rs → warehouse/src/load_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ pub async fn batch_insert_account(
let chunks: Vec<&[WarehouseAccount]> = acc.chunks(batch_len).collect();

for c in chunks {
commit_batch_query(pool, c).await?;
impl_batch_insert(pool, c).await?;
}

Ok(())
}

// TODO: return specific commit errors for this batch
pub async fn commit_batch_query(pool: &SqlitePool, batch_accounts: &[WarehouseAccount]) -> Result<()> {
pub async fn impl_batch_insert(pool: &SqlitePool, batch_accounts: &[WarehouseAccount]) -> Result<()> {
let mut query_builder: QueryBuilder<Sqlite> = QueryBuilder::new(
// Note the trailing space; most calls to `QueryBuilder` don't automatically insert
"INSERT INTO users (account_address, is_legacy) ",
Expand Down
44 changes: 44 additions & 0 deletions warehouse/src/load_coin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::table_structs::WarehouseState;
use anyhow::Result;
use sqlx::{QueryBuilder, Sqlite, SqlitePool};


pub async fn batch_insert_account(
pool: &SqlitePool,
acc: &[WarehouseState],
batch_len: usize,
) -> Result<()> {
let chunks: Vec<&[WarehouseState]> = acc.chunks(batch_len).collect();

for c in chunks {
impl_batch_coin_insert(pool, c).await?;
}

Ok(())
}

// TODO: return specific commit errors for this batch
pub async fn impl_batch_coin_insert(pool: &SqlitePool, batch_accounts: &[WarehouseState]) -> Result<()> {
let filtered = batch_accounts.iter().filter(|el| {
el.balance.is_some()
});

let mut query_builder: QueryBuilder<Sqlite> = QueryBuilder::new(
r#"
INSERT INTO balance (account_address, balance, chain_timestamp, db_version, epoch_number)
"#,
);

query_builder.push_values(filtered, |mut b, acc| {
b.push_bind(acc.account.address.to_hex_literal()).push_bind(true)
.push_bind(acc.balance.as_ref().unwrap().legacy_balance.unwrap() as i64).push_bind(true)
.push_bind(0) // todo
.push_bind(0) // todo
.push_bind(0); // todo
});

let query = query_builder.build();
let _res = query.execute(pool).await?;

Ok(())
}
12 changes: 6 additions & 6 deletions warehouse/tests/test_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ async fn insert_one_account(pool: SqlitePool) -> anyhow::Result<()> {
address: marlon
};

libra_warehouse::load::insert_one_account(&pool, &acc).await?;
libra_warehouse::load_account::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());
assert!(libra_warehouse::load_account::insert_one_account(&pool, &acc).await.is_err());

Ok(())
}
Expand All @@ -42,7 +42,7 @@ async fn batch_insert(pool: SqlitePool) -> anyhow::Result<()>{
vec_acct.push(acc);
}

libra_warehouse::load::commit_batch_query(&pool, &vec_acct).await?;
libra_warehouse::load_account::impl_batch_insert(&pool, &vec_acct).await?;
Ok(())
}

Expand All @@ -62,10 +62,10 @@ async fn batch_duplicates_fail_gracefully(pool: SqlitePool) -> anyhow::Result<()
}

// should not fail if duplicates exists on same batch
libra_warehouse::load::commit_batch_query(&pool, &vec_acct).await?;
libra_warehouse::load_account::impl_batch_insert(&pool, &vec_acct).await?;

// also should not fail if duplicates are on separate batches
libra_warehouse::load::commit_batch_query(&pool, &vec_acct).await?;
libra_warehouse::load_account::impl_batch_insert(&pool, &vec_acct).await?;

Ok(())
}
Expand All @@ -81,7 +81,7 @@ async fn test_e2e_load_v5_snapshot(pool: SqlitePool) -> anyhow::Result<()> {
// NOTE: the parsing drops 1 blob, which is the 0x1 account, because it would not have the DiemAccount struct on it as a user address would have.
assert!(wa_vec.len() == 17338);

let res = libra_warehouse::load::load_account_state(&pool, &wa_vec).await?;
let res = libra_warehouse::load_account::load_account_state(&pool, &wa_vec).await?;

assert!(res == 17338);
Ok(())
Expand Down

0 comments on commit 9f84641

Please sign in to comment.