Просмотр исходного кода

drk: properly handle trees updates

skoupidi 1 год назад
Родитель
Сommit
aa046342cc
2 измененных файлов с 4 добавлено и 15 удалено
  1. 3 8
      bin/drk/src/dao.rs
  2. 1 7
      bin/drk/src/money.rs

+ 3 - 8
bin/drk/src/dao.rs

@@ -372,7 +372,7 @@ impl Drk {
         // a bit better and safer.
         // For now, on success, we don't care what's returned, but in the future
         // we should actually check it.
-        if self.wallet.query_single(&DAO_TREES_TABLE, &[DAO_TREES_COL_DAOS_TREE], &[]).is_err() {
+        if self.get_dao_trees().await.is_err() {
             println!("Initializing DAO Merkle trees");
             let tree = MerkleTree::new(1);
             self.put_dao_trees(&tree, &tree).await?;
@@ -388,14 +388,9 @@ impl Drk {
         daos_tree: &MerkleTree,
         proposals_tree: &MerkleTree,
     ) -> WalletDbResult<()> {
-        // First we remove old records
-        let query = format!("DELETE FROM {};", *DAO_TREES_TABLE);
-        self.wallet.exec_sql(&query, &[])?;
-
-        // then we insert the new one
         let query = format!(
-            "INSERT INTO {} ({}, {}) VALUES (?1, ?2);",
-            *DAO_TREES_TABLE, DAO_TREES_COL_DAOS_TREE, DAO_TREES_COL_PROPOSALS_TREE,
+            "UPDATE {} SET {} = ?1, {} = ?2;",
+            *DAO_TREES_TABLE, DAO_TREES_COL_DAOS_TREE, DAO_TREES_COL_PROPOSALS_TREE
         );
         self.wallet.exec_sql(
             &query,

+ 1 - 7
bin/drk/src/money.rs

@@ -641,13 +641,7 @@ impl Drk {
 
     /// Replace the Money Merkle tree in the wallet.
     pub async fn put_money_tree(&self, tree: &MerkleTree) -> WalletDbResult<()> {
-        // First we remove old record
-        let query = format!("DELETE FROM {};", *MONEY_TREE_TABLE);
-        self.wallet.exec_sql(&query, &[])?;
-
-        // then we insert the new one
-        let query =
-            format!("INSERT INTO {} ({}) VALUES (?1);", *MONEY_TREE_TABLE, MONEY_TREE_COL_TREE,);
+        let query = format!("UPDATE {} SET {} = ?1;", *MONEY_TREE_TABLE, MONEY_TREE_COL_TREE);
         self.wallet.exec_sql(&query, rusqlite::params![serialize_async(tree).await])
     }