From 5b8ec380a006ccc0ccb38c11cc6832e1176015c0 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sat, 17 Sep 2022 02:37:10 +0000 Subject: [PATCH] zcash_client_sqlite: Fix `FsBlockDb::get_max_cached_height` The `MAX` SQLite function returns `null` when the table is empty. The code was expecting zero rows to be returned in this case, and was trying to parse the `null` as an integer. --- zcash_client_sqlite/src/chain.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/zcash_client_sqlite/src/chain.rs b/zcash_client_sqlite/src/chain.rs index e97a7e7dd..1388d1a8d 100644 --- a/zcash_client_sqlite/src/chain.rs +++ b/zcash_client_sqlite/src/chain.rs @@ -12,7 +12,7 @@ use crate::{error::SqliteClientError, BlockDb}; #[cfg(feature = "unstable")] use { crate::{BlockHash, FsBlockDb}, - rusqlite::{Connection, OptionalExtension, NO_PARAMS}, + rusqlite::{Connection, NO_PARAMS}, std::fs::File, std::io::BufReader, std::path::{Path, PathBuf}, @@ -149,11 +149,12 @@ pub(crate) fn blockmetadb_get_max_cached_height( "SELECT MAX(height) FROM compactblocks_meta", NO_PARAMS, |row| { - let h: u32 = row.get(0)?; - Ok(BlockHeight::from(h)) + // `SELECT MAX(_)` will always return a row, but it will return `null` if the + // table is empty, which has no integer type. We handle the optionality here. + let h: Option = row.get(0)?; + Ok(h.map(BlockHeight::from)) }, ) - .optional() } /// Implements a traversal of `limit` blocks of the filesystem-backed