From c046299ad4d4e3513c6a3bda79ea374475acb9c4 Mon Sep 17 00:00:00 2001 From: Bobbin Threadbare Date: Thu, 18 Jan 2024 01:21:45 -0800 Subject: [PATCH] fix: state sync, u64 conversions, nullifier checks --- store/src/db/sql.rs | 43 ++++++++++++++++++++++++----------------- store/src/migrations.rs | 2 -- store/src/state.rs | 14 +++++++------- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/store/src/db/sql.rs b/store/src/db/sql.rs index 10ef11a3b..425e65797 100644 --- a/store/src/db/sql.rs +++ b/store/src/db/sql.rs @@ -98,8 +98,8 @@ pub fn select_notes(conn: &mut Connection) -> Result, anyhow::Error> { block_num: row.get(0)?, note_index: row.get(1)?, note_hash: Some(note_hash), - sender: row.get(3)?, - tag: row.get(4)?, + sender: column_value_as_u64(row, 3)?, + tag: column_value_as_u64(row, 4)?, num_assets: row.get(5)?, merkle_path: Some(merkle_path), }) @@ -122,8 +122,8 @@ pub fn select_accounts(conn: &mut Connection) -> Result, anyhow let account_hash_data = row.get_ref(1)?.as_blob()?; let account_hash = Digest::decode(account_hash_data)?; - let account_id_data: i64 = row.get(0)?; - let account_id = AccountIdProto::from(account_id_data as u64); + let account_id_data = column_value_as_u64(row, 0)?; + let account_id = AccountIdProto::from(account_id_data); accounts.push(AccountInfo { account_id: Some(account_id), @@ -291,9 +291,9 @@ pub fn insert_notes( note.block_num, note.note_index, note.note_hash.clone().ok_or(StateError::NoteMissingHash)?.encode_to_vec(), - note.sender, - note.tag, - note.num_assets, + u64_to_value(note.sender), + u64_to_value(note.tag), + note.num_assets as u8, note.merkle_path .clone() .ok_or(StateError::NoteMissingMerklePath)? @@ -364,8 +364,8 @@ pub fn select_notes_since_block_by_tag_and_sender( let note_index = row.get(1)?; let note_hash_data = row.get_ref(2)?.as_blob()?; let note_hash = Some(decode_protobuf_digest(note_hash_data)?); - let sender = row.get(3)?; - let tag = row.get(4)?; + let sender = column_value_as_u64(row, 3)?; + let tag = column_value_as_u64(row, 4)?; let num_assets = row.get(5)?; let merkle_path_data = row.get_ref(6)?.as_blob()?; let merkle_path = Some(MerklePath::decode(merkle_path_data)?); @@ -443,8 +443,8 @@ pub fn select_accounts_by_block_range( let mut result = Vec::new(); while let Some(row) = rows.next()? { - let account_id_data: u64 = row.get(0)?; - let account_id: account::AccountId = account_id_data.into(); + let account_id_data = column_value_as_u64(row, 0)?; + let account_id: account::AccountId = (account_id_data).into(); let account_hash_data = row.get_ref(1)?.as_blob()?; let account_hash = Digest::decode(account_hash_data)?; let block_num = row.get(2)?; @@ -472,13 +472,7 @@ pub fn select_account_hashes( let mut result = Vec::new(); while let Some(row) = rows.next()? { - let account_id: u64 = { - // sqlite doesn't support `u64` so we store `u64` as `i64`. - // Here, we do the reverse. - let account_id: i64 = row.get(0)?; - - account_id as u64 - }; + let account_id = column_value_as_u64(row, 0)?; let account_hash_data = row.get_ref(1)?.as_blob()?; let account_hash = Digest::decode(account_hash_data)?; @@ -557,6 +551,7 @@ pub fn apply_block( count += insert_nullifiers_for_block(transaction, nullifiers, block_header.block_num)?; Ok(count) } + // UTILITIES // ================================================================================================ @@ -591,3 +586,15 @@ fn u32_to_value(v: u32) -> Value { let v: i64 = v.into(); Value::Integer(v) } + +/// Gets a `u64`` value from the database. +/// +/// Sqlite uses `i64` as its internal representation format, and so when retreiving +/// we need to make sure we cast as `u64` to get the original value +fn column_value_as_u64( + row: &rusqlite::Row<'_>, + index: I, +) -> rusqlite::Result { + let value: i64 = row.get(index)?; + Ok(value as u64) +} diff --git a/store/src/migrations.rs b/store/src/migrations.rs index 304078ebf..c3a1d4f2d 100644 --- a/store/src/migrations.rs +++ b/store/src/migrations.rs @@ -28,8 +28,6 @@ pub static MIGRATIONS: Lazy = Lazy::new(|| { PRIMARY KEY (block_num, note_index), CONSTRAINT notes_block_number_is_u32 CHECK (block_num >= 0 AND block_num < 4294967296), CONSTRAINT notes_note_index_is_u32 CHECK (note_index >= 0 AND note_index < 4294967296), - CONSTRAINT notes_sender_is_felt CHECK (sender >= 0 AND sender <= 18446744069414584321), - CONSTRAINT notes_tag_is_felt CHECK (tag >= 0 AND tag <= 18446744069414584321), CONSTRAINT notes_num_assets_is_u8 CHECK (num_assets >= 0 AND num_assets < 256), FOREIGN KEY (block_num) REFERENCES block_header (block_num) ) STRICT, WITHOUT ROWID; diff --git a/store/src/state.rs b/store/src/state.rs index 8e9c3b0a6..1b1ba26a1 100644 --- a/store/src/state.rs +++ b/store/src/state.rs @@ -242,9 +242,10 @@ impl State { nullifier_tree.insert(*nullifier, nullifier_data); } - if nullifier_tree.root() != new_block.nullifier_root() { - return Err(StateError::NewBlockInvalidNullifierRoot.into()); - } + // FIXME: Re-add when nullifiers start getting updated + // if nullifier_tree.root() != new_block.nullifier_root() { + // return Err(StateError::NewBlockInvalidNullifierRoot.into()); + // } nullifier_tree }; @@ -355,10 +356,9 @@ impl State { .chain_mmr .get_delta(block_num as usize, state_sync.block_header.block_num as usize)?; - let proof = inner.chain_mmr.open( - state_sync.block_header.block_num as usize, - state_sync.block_header.block_num as usize + 1, - )?; + let proof = inner + .chain_mmr + .open(block_num as usize, state_sync.block_header.block_num as usize)?; (delta, proof.merkle_path) };