Use an upsert for `FsBlockDb::write_block_metadata`

This commit is contained in:
Jack Grigg 2023-07-19 16:38:03 +00:00
parent f4221889fe
commit 963496d0ab
3 changed files with 31 additions and 9 deletions

View File

@ -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` wallet did not contain enough observed blocks to satisfy the `min_confirmations`
value specified; this situation is now treated as an error. value specified; this situation is now treated as an error.
- A `BlockConflict` variant has been added to `zcash_client_sqlite::error::SqliteClientError` - 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 ### Removed
- The empty `wallet::transact` module has been removed. - The empty `wallet::transact` module has been removed.

View File

@ -103,21 +103,40 @@ pub(crate) fn blockmetadb_insert(
conn: &Connection, conn: &Connection,
block_meta: &[BlockMeta], block_meta: &[BlockMeta],
) -> Result<(), rusqlite::Error> { ) -> Result<(), rusqlite::Error> {
use rusqlite::named_params;
let mut stmt_insert = conn.prepare( let mut stmt_insert = conn.prepare(
"INSERT INTO compactblocks_meta (height, blockhash, time, sapling_outputs_count, orchard_actions_count) "INSERT INTO compactblocks_meta (
VALUES (?, ?, ?, ?, ?)" 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", [])?; conn.execute("BEGIN IMMEDIATE", [])?;
let result = block_meta let result = block_meta
.iter() .iter()
.map(|m| { .map(|m| {
stmt_insert.execute(params![ stmt_insert.execute(named_params![
u32::from(m.height), ":height": u32::from(m.height),
&m.block_hash.0[..], ":blockhash": &m.block_hash.0[..],
m.block_time, ":time": m.block_time,
m.sapling_outputs_count, ":sapling_outputs_count": m.sapling_outputs_count,
m.orchard_actions_count, ":orchard_actions_count": m.orchard_actions_count,
]) ])
}) })
.collect::<Result<Vec<_>, _>>(); .collect::<Result<Vec<_>, _>>();

View File

@ -907,7 +907,8 @@ impl FsBlockDb {
Ok(chain::blockmetadb_get_max_cached_height(&self.conn)?) 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 /// This will return an error if any block file corresponding to one of these metadata records
/// is absent from the blocks directory. /// is absent from the blocks directory.