Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assorted store fixes #156

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions store/src/db/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ pub fn select_notes(conn: &mut Connection) -> Result<Vec<Note>, 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),
})
Expand All @@ -122,8 +122,8 @@ pub fn select_accounts(conn: &mut Connection) -> Result<Vec<AccountInfo>, 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),
Expand Down Expand Up @@ -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)?
Expand Down Expand Up @@ -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)?);
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -557,6 +551,7 @@ pub fn apply_block(
count += insert_nullifiers_for_block(transaction, nullifiers, block_header.block_num)?;
Ok(count)
}

// UTILITIES
// ================================================================================================

Expand Down Expand Up @@ -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<I: rusqlite::RowIndex>(
row: &rusqlite::Row<'_>,
index: I,
) -> rusqlite::Result<u64> {
let value: i64 = row.get(index)?;
Ok(value as u64)
}
2 changes: 0 additions & 2 deletions store/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ pub static MIGRATIONS: Lazy<Migrations> = 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;
Expand Down
14 changes: 7 additions & 7 deletions store/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down Expand Up @@ -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)
};
Expand Down
Loading