diff --git a/zcash_client_sqlite/CHANGELOG.md b/zcash_client_sqlite/CHANGELOG.md index ee38993f3..91f75a523 100644 --- a/zcash_client_sqlite/CHANGELOG.md +++ b/zcash_client_sqlite/CHANGELOG.md @@ -20,6 +20,8 @@ and this library adheres to Rust's notion of wallet did not contain enough observed blocks to satisfy the `min_confirmations` value specified; this situation is now treated as an error. - A `BlockConflict` variant has been added to `zcash_client_sqlite::error::SqliteClientError` +- `zcash_client_sqlite::FsBlockDb::write_block_metadata` now overwrites any + existing metadata entries that have the same height as a new entry. ### Removed - The empty `wallet::transact` module has been removed. diff --git a/zcash_client_sqlite/src/chain.rs b/zcash_client_sqlite/src/chain.rs index 8c8a1a272..2cf51c827 100644 --- a/zcash_client_sqlite/src/chain.rs +++ b/zcash_client_sqlite/src/chain.rs @@ -103,21 +103,40 @@ pub(crate) fn blockmetadb_insert( conn: &Connection, block_meta: &[BlockMeta], ) -> Result<(), rusqlite::Error> { + use rusqlite::named_params; + let mut stmt_insert = conn.prepare( - "INSERT INTO compactblocks_meta (height, blockhash, time, sapling_outputs_count, orchard_actions_count) - VALUES (?, ?, ?, ?, ?)" + "INSERT INTO compactblocks_meta ( + height, + blockhash, + time, + sapling_outputs_count, + orchard_actions_count + ) + VALUES ( + :height, + :blockhash, + :time, + :sapling_outputs_count, + :orchard_actions_count + ) + ON CONFLICT (height) DO UPDATE + SET blockhash = :blockhash, + time = :time, + sapling_outputs_count = :sapling_outputs_count, + orchard_actions_count = :orchard_actions_count", )?; conn.execute("BEGIN IMMEDIATE", [])?; let result = block_meta .iter() .map(|m| { - stmt_insert.execute(params![ - u32::from(m.height), - &m.block_hash.0[..], - m.block_time, - m.sapling_outputs_count, - m.orchard_actions_count, + stmt_insert.execute(named_params![ + ":height": u32::from(m.height), + ":blockhash": &m.block_hash.0[..], + ":time": m.block_time, + ":sapling_outputs_count": m.sapling_outputs_count, + ":orchard_actions_count": m.orchard_actions_count, ]) }) .collect::, _>>(); diff --git a/zcash_client_sqlite/src/lib.rs b/zcash_client_sqlite/src/lib.rs index 7dad65e90..eb28c5963 100644 --- a/zcash_client_sqlite/src/lib.rs +++ b/zcash_client_sqlite/src/lib.rs @@ -907,7 +907,8 @@ impl FsBlockDb { Ok(chain::blockmetadb_get_max_cached_height(&self.conn)?) } - /// Adds a set of block metadata entries to the metadata database. + /// Adds a set of block metadata entries to the metadata database, overwriting any + /// existing entries at the given block heights. /// /// This will return an error if any block file corresponding to one of these metadata records /// is absent from the blocks directory.