Configure synchronous mode per connection

This commit is contained in:
Hanh 2022-04-17 09:54:04 +08:00
parent 0e47881091
commit 5bdc29c262
8 changed files with 41 additions and 40 deletions

View File

@ -94,7 +94,7 @@ mod tests {
#[test]
fn test_contacts() {
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH).unwrap();
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH, true).unwrap();
let contact = Contact {
id: 0,
name: "hanh".to_string(),
@ -107,7 +107,7 @@ mod tests {
#[tokio::test]
async fn test_serialize() {
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH).unwrap();
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH, true).unwrap();
let contacts = db.get_unsaved_contacts().unwrap();
let memos = serialize_contacts(&contacts).unwrap();
for m in memos.iter() {

View File

@ -73,9 +73,10 @@ pub struct AccountBackup {
}
impl DbAdapter {
pub fn new(coin_type: CoinType, db_path: &str) -> anyhow::Result<DbAdapter> {
pub fn new(coin_type: CoinType, db_path: &str, sync: bool) -> anyhow::Result<DbAdapter> {
let connection = Connection::open(db_path)?;
// connection.execute("PRAGMA synchronous = off", NO_PARAMS)?;
let mode = if sync { "on" } else { "off" };
connection.execute(&format!("PRAGMA synchronous = {}", mode), NO_PARAMS)?;
Ok(DbAdapter { coin_type, connection })
}
@ -933,7 +934,7 @@ mod tests {
#[test]
fn test_db() {
let mut db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH).unwrap();
let mut db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH, true).unwrap();
db.init_db().unwrap();
db.trim_to_height(0).unwrap();
@ -970,7 +971,7 @@ mod tests {
#[test]
fn test_balance() {
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH).unwrap();
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH, true).unwrap();
let balance = db.get_balance(1).unwrap();
println!("{}", balance);
}

View File

@ -14,7 +14,7 @@ use zcash_params::coin::CoinType;
const DB_NAME: &str = "zec.db";
fn init() {
let db = DbAdapter::new(CoinType::Zcash, DB_NAME).unwrap();
let db = DbAdapter::new(CoinType::Zcash, DB_NAME, true).unwrap();
db.init_db().unwrap();
}
@ -24,7 +24,7 @@ async fn test() -> anyhow::Result<()> {
dotenv::dotenv().unwrap();
env_logger::init();
let seed = dotenv::var("SEED").unwrap();
let seed = dotenv::var("ZP_IVK").unwrap();
// let seed2 = dotenv::var("SEED2").unwrap();
// let ivk = dotenv::var("IVK").unwrap();
let address = dotenv::var("ADDRESS").unwrap();
@ -46,26 +46,26 @@ async fn test() -> anyhow::Result<()> {
}
}
let last_height = wallet.get_latest_height().await.unwrap();
let tx_id = wallet
.build_sign_send_multi_payment(
1,
last_height,
&[RecipientMemo {
address,
amount: 50000,
memo: Memo::from_str("test memo").unwrap(),
max_amount_per_note: 0,
}],
false,
2,
move |progress| {
println!("{}", progress.cur());
},
)
.await
.unwrap();
println!("TXID = {}", tx_id);
// let last_height = wallet.get_latest_height().await.unwrap();
// let tx_id = wallet
// .build_sign_send_multi_payment(
// 1,
// last_height,
// &[RecipientMemo {
// address,
// amount: 50000,
// memo: Memo::from_str("test memo").unwrap(),
// max_amount_per_note: 0,
// }],
// false,
// 2,
// move |progress| {
// println!("{}", progress.cur());
// },
// )
// .await
// .unwrap();
// println!("TXID = {}", tx_id);
// let last_height = wallet.get_latest_height().await.unwrap();
// let tx = wallet
@ -111,13 +111,13 @@ fn test_make_wallet() {
#[allow(dead_code)]
fn test_rewind() {
let mut db = DbAdapter::new(CoinType::Zcash, DB_NAME).unwrap();
let mut db = DbAdapter::new(CoinType::Zcash, DB_NAME, true).unwrap();
db.trim_to_height(1314000).unwrap();
}
#[allow(dead_code)]
fn test_get_balance() {
let db = DbAdapter::new(CoinType::Zcash, DB_NAME).unwrap();
let db = DbAdapter::new(CoinType::Zcash, DB_NAME, true).unwrap();
let balance = db.get_balance(1).unwrap();
println!("Balance = {}", (balance as f64) / 100_000_000.0);
}
@ -140,7 +140,7 @@ fn test_invalid_witness() {
#[allow(dead_code)]
fn w() {
let db = DbAdapter::new(CoinType::Zcash, "zec.db").unwrap();
let db = DbAdapter::new(CoinType::Zcash, "zec.db", true).unwrap();
// let w_b: Vec<u8> = db.connection.query_row("SELECT witness FROM sapling_witnesses WHERE note = 66 AND height = 1466097", NO_PARAMS, |row| row.get(0)).unwrap();
// let w = Witness::read(0, &*w_b).unwrap();
// print_witness2(&w);

View File

@ -48,7 +48,7 @@ impl MemPool {
}
pub fn set_account(&mut self, account: u32) -> anyhow::Result<()> {
let db = DbAdapter::new(self.coin_type, &self.db_path)?;
let db = DbAdapter::new(self.coin_type, &self.db_path, true)?;
let ivk = db.get_ivk(account)?;
self.account = account;
self.set_ivk(&ivk);
@ -85,7 +85,7 @@ impl MemPool {
}
pub fn clear(&mut self, height: u32) -> anyhow::Result<()> {
let db = DbAdapter::new(self.coin_type, &self.db_path)?;
let db = DbAdapter::new(self.coin_type, &self.db_path, true)?;
self.height = BlockHeight::from_u32(height);
self.nfs = db.get_nullifier_amounts(self.account, true)?;
self.transactions.clear();
@ -171,7 +171,7 @@ mod tests {
#[tokio::test]
async fn test_mempool() {
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH).unwrap();
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH, true).unwrap();
let ivk = db.get_ivk(1).unwrap();
let mut mempool = MemPool::new(CoinType::Zcash, "zec.db");
mempool.set_lwd_url(LWD_URL).unwrap();

View File

@ -76,7 +76,7 @@ mod tests {
#[tokio::test]
async fn test_fetch_quotes() {
let currency = "EUR";
let mut db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH).unwrap();
let mut db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH, true).unwrap();
let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()

View File

@ -103,7 +103,7 @@ pub async fn sync_async(
let mut client = connect_lightwalletd(&ld_url).await?;
let (start_height, mut prev_hash, vks) = {
let db = DbAdapter::new(coin_type, &db_path)?;
let db = DbAdapter::new(coin_type, &db_path, false)?;
let height = db.get_db_height()?;
let hash = db.get_db_hash(height)?;
let vks = db.get_fvks()?;
@ -143,7 +143,7 @@ pub async fn sync_async(
let proc_callback = progress_callback.clone();
let processor = tokio::spawn(async move {
let mut db = DbAdapter::new(coin_type, &db_path)?;
let mut db = DbAdapter::new(coin_type, &db_path2, false)?;
let mut nfs = db.get_nullifiers()?;
let (mut tree, mut witnesses) = db.get_tree()?;

View File

@ -179,7 +179,7 @@ pub async fn retrieve_tx_info(
let chain = get_coin_chain(coin_type);
chain.network().clone()
};
let db = DbAdapter::new(coin_type, db_path)?;
let db = DbAdapter::new(coin_type, db_path, false)?;
let nfs = db.get_nullifiers_raw()?;
let mut nf_map: HashMap<(u32, Vec<u8>), u64> = HashMap::new();
@ -275,7 +275,7 @@ mod tests {
hex::decode("b47da170329dc311b98892eac23e83025f8bb3ce10bb07535698c91fb37e1e54")
.unwrap();
let mut client = connect_lightwalletd(LWD_URL).await.unwrap();
let db = DbAdapter::new(CoinType::Zcash, "./zec.db").unwrap();
let db = DbAdapter::new(CoinType::Zcash, "./zec.db", true).unwrap();
let account = 1;
let nfs = db.get_nullifiers_raw().unwrap();
let mut nf_map: HashMap<(u32, Vec<u8>), u64> = HashMap::new();

View File

@ -139,7 +139,7 @@ pub fn decode_memo(memo: &str, recipient: &str, timestamp: u32, height: u32) ->
impl Wallet {
pub fn new(coin_type: CoinType, db_path: &str) -> Wallet {
let db = DbAdapter::new(coin_type, db_path).unwrap();
let db = DbAdapter::new(coin_type, db_path, true).unwrap();
let key_helpers = KeyHelpers::new(coin_type);
db.init_db().unwrap();
Wallet {