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`
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.

View File

@ -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::<Result<Vec<_>, _>>();

View File

@ -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.