Use WAL Db

This commit is contained in:
Hanh 2022-04-20 00:47:08 +08:00
parent 542e2f3fb3
commit 7107826cea
8 changed files with 22 additions and 20 deletions

View File

@ -94,7 +94,7 @@ mod tests {
#[test]
fn test_contacts() {
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH, true).unwrap();
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH).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, true).unwrap();
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH).unwrap();
let contacts = db.get_unsaved_contacts().unwrap();
let memos = serialize_contacts(&contacts).unwrap();
for m in memos.iter() {

View File

@ -73,10 +73,12 @@ pub struct AccountBackup {
}
impl DbAdapter {
pub fn new(coin_type: CoinType, db_path: &str, sync: bool) -> anyhow::Result<DbAdapter> {
pub fn new(coin_type: CoinType, db_path: &str) -> anyhow::Result<DbAdapter> {
let connection = Connection::open(db_path)?;
let mode = if sync { "on" } else { "off" };
connection.execute(&format!("PRAGMA synchronous = {}", mode), NO_PARAMS)?;
connection.query_row("PRAGMA journal_mode = WAL", NO_PARAMS, |_| {
Ok(())
})?;
connection.execute("PRAGMA synchronous = NORMAL", NO_PARAMS)?;
Ok(DbAdapter { coin_type, connection })
}
@ -942,7 +944,7 @@ mod tests {
#[test]
fn test_db() {
let mut db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH, true).unwrap();
let mut db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH).unwrap();
db.init_db().unwrap();
db.trim_to_height(0).unwrap();
@ -979,7 +981,7 @@ mod tests {
#[test]
fn test_balance() {
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH, true).unwrap();
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH).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, true).unwrap();
let db = DbAdapter::new(CoinType::Zcash, DB_NAME).unwrap();
db.init_db().unwrap();
}
@ -111,13 +111,13 @@ fn test_make_wallet() {
#[allow(dead_code)]
fn test_rewind() {
let mut db = DbAdapter::new(CoinType::Zcash, DB_NAME, true).unwrap();
let mut db = DbAdapter::new(CoinType::Zcash, DB_NAME).unwrap();
db.trim_to_height(1314000).unwrap();
}
#[allow(dead_code)]
fn test_get_balance() {
let db = DbAdapter::new(CoinType::Zcash, DB_NAME, true).unwrap();
let db = DbAdapter::new(CoinType::Zcash, DB_NAME).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", true).unwrap();
let db = DbAdapter::new(CoinType::Zcash, "zec.db").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, true)?;
let db = DbAdapter::new(self.coin_type, &self.db_path)?;
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, true)?;
let db = DbAdapter::new(self.coin_type, &self.db_path)?;
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, true).unwrap();
let db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH).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, true).unwrap();
let mut db = DbAdapter::new(CoinType::Zcash, DEFAULT_DB_PATH).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, false)?;
let db = DbAdapter::new(coin_type, &db_path)?;
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_path2, false)?;
let mut db = DbAdapter::new(coin_type, &db_path2)?;
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, false)?;
let db = DbAdapter::new(coin_type, db_path)?;
let nfs = db.get_nullifiers_raw()?;
let mut nf_map: HashMap<(u32, Vec<u8>), u64> = HashMap::new();
@ -277,7 +277,7 @@ mod tests {
hex::decode("b47da170329dc311b98892eac23e83025f8bb3ce10bb07535698c91fb37e1e54")
.unwrap();
let mut client = connect_lightwalletd(LWD_URL).await.unwrap();
let db = DbAdapter::new(CoinType::Zcash, "./zec.db", true).unwrap();
let db = DbAdapter::new(CoinType::Zcash, "./zec.db").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, true).unwrap();
let db = DbAdapter::new(coin_type, db_path).unwrap();
let key_helpers = KeyHelpers::new(coin_type);
db.init_db().unwrap();
Wallet {