Skip to content
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
35 changes: 17 additions & 18 deletions crates/sqlite-store/src/account/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::collections::BTreeMap;
use std::rc::Rc;
use std::string::{String, ToString};
use std::string::ToString;
use std::sync::{Arc, RwLock};
use std::vec::Vec;

Expand Down Expand Up @@ -93,11 +93,10 @@ impl SqliteStore {
conn: &mut Connection,
account_commitment: Word,
) -> Result<Option<AccountHeader>, StoreError> {
let account_commitment_str: String = account_commitment.to_string();
Ok(query_historical_account_headers(
conn,
"account_commitment = ?",
params![account_commitment_str],
params![account_commitment.to_bytes()],
)?
.pop()
.map(|(header, _)| header))
Expand Down Expand Up @@ -388,7 +387,7 @@ impl SqliteStore {
const QUERY: &str =
insert_sql!(foreign_account_code { account_id, code_commitment } | REPLACE);

tx.execute(QUERY, params![account_id.to_bytes(), code.commitment().to_string()])
tx.execute(QUERY, params![account_id.to_bytes(), code.commitment().to_bytes()])
.into_store_error()?;

Self::insert_account_code(&tx, code)?;
Expand Down Expand Up @@ -426,7 +425,7 @@ impl SqliteStore {
account_code: &AccountCode,
) -> Result<(), StoreError> {
const QUERY: &str = insert_sql!(account_code { commitment, code } | IGNORE);
tx.execute(QUERY, params![account_code.commitment().to_hex(), account_code.to_bytes()])
tx.execute(QUERY, params![account_code.commitment().to_bytes(), account_code.to_bytes()])
.into_store_error()?;
Ok(())
}
Expand Down Expand Up @@ -515,7 +514,7 @@ impl SqliteStore {
let commitment_params = Rc::new(
discarded_states
.iter()
.map(|(_, commitment)| Value::from(commitment.to_hex()))
.map(|(_, commitment)| Value::Blob(commitment.to_bytes()))
.collect::<Vec<_>>(),
);

Expand Down Expand Up @@ -893,10 +892,10 @@ impl SqliteStore {
// got a past update and shouldn't lock the account.
const LOCK_CONDITION: &str = "WHERE id = :account_id AND NOT EXISTS (SELECT 1 FROM historical_account_headers WHERE id = :account_id AND account_commitment = :digest)";
let account_id_bytes = account_id.to_bytes();
let digest_str = mismatched_digest.to_string();
let digest_bytes = mismatched_digest.to_bytes();
let params = named_params! {
":account_id": account_id_bytes,
":digest": digest_str
":digest": digest_bytes
};

let query = format!("UPDATE latest_account_headers SET locked = true {LOCK_CONDITION}");
Expand Down Expand Up @@ -924,11 +923,11 @@ impl SqliteStore {
watched: bool,
) -> Result<(), StoreError> {
let id = new_header.id().to_bytes();
let code_commitment = new_header.code_commitment().to_string();
let storage_commitment = new_header.storage_commitment().to_string();
let vault_root = new_header.vault_root().to_string();
let code_commitment = new_header.code_commitment().to_bytes();
let storage_commitment = new_header.storage_commitment().to_bytes();
let vault_root = new_header.vault_root().to_bytes();
let nonce = u64_to_value(new_header.nonce().as_canonical_u64());
let commitment = new_header.to_commitment().to_string();
let commitment = new_header.to_commitment().to_bytes();
let account_seed = account_seed.map(|seed| seed.to_bytes());

const LATEST_QUERY: &str = insert_sql!(
Expand Down Expand Up @@ -1007,11 +1006,11 @@ impl SqliteStore {

// Archive the old header to historical.
let old_id = old_header.id().to_bytes();
let old_code_commitment = old_header.code_commitment().to_string();
let old_storage_commitment = old_header.storage_commitment().to_string();
let old_vault_root = old_header.vault_root().to_string();
let old_code_commitment = old_header.code_commitment().to_bytes();
let old_storage_commitment = old_header.storage_commitment().to_bytes();
let old_vault_root = old_header.vault_root().to_bytes();
let old_nonce = u64_to_value(old_header.nonce().as_canonical_u64());
let old_commitment = old_header.to_commitment().to_string();
let old_commitment = old_header.to_commitment().to_bytes();
let replaced_at_nonce = u64_to_value(new_header.nonce().as_canonical_u64());

const HISTORICAL_QUERY: &str = insert_sql!(
Expand Down Expand Up @@ -1064,7 +1063,7 @@ impl SqliteStore {
let mut total_deleted: usize = 0;

// Collect code commitments from headers we are about to delete.
let candidate_code_commitments: Vec<String> = {
let candidate_code_commitments: Vec<Vec<u8>> = {
let mut stmt = tx
.prepare(
"SELECT DISTINCT code_commitment FROM historical_account_headers \
Expand All @@ -1074,7 +1073,7 @@ impl SqliteStore {
let rows = stmt
.query_map(params![&account_id_bytes, &boundary_val], |row| row.get(0))
.into_store_error()?;
rows.collect::<Result<Vec<String>, _>>().into_store_error()?
rows.collect::<Result<Vec<Vec<u8>>, _>>().into_store_error()?
};

// Delete historical entries.
Expand Down
56 changes: 30 additions & 26 deletions crates/sqlite-store/src/account/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ use crate::sql_error::SqlResultExt;
pub(crate) struct SerializedHeaderData {
pub id: Vec<u8>,
pub nonce: u64,
pub vault_root: String,
pub storage_commitment: String,
pub code_commitment: String,
pub vault_root: Vec<u8>,
pub storage_commitment: Vec<u8>,
pub code_commitment: Vec<u8>,
pub account_seed: Option<Vec<u8>>,
pub locked: bool,
}
Expand Down Expand Up @@ -59,9 +59,9 @@ pub(crate) fn parse_accounts(
AccountId::read_from_bytes(&id)
.expect("Conversion from stored AccountID should not panic"),
nonce,
Word::try_from(&vault_root)?,
Word::try_from(&storage_commitment)?,
Word::try_from(&code_commitment)?,
Word::read_from_bytes(&vault_root)?,
Word::read_from_bytes(&storage_commitment)?,
Word::read_from_bytes(&code_commitment)?,
),
status,
))
Expand All @@ -84,9 +84,9 @@ pub(crate) fn query_latest_account_headers(
.query_map(params, |row| {
let id: Vec<u8> = row.get(0)?;
let nonce: u64 = column_value_as_u64(row, 1)?;
let vault_root: String = row.get(2)?;
let storage_commitment: String = row.get(3)?;
let code_commitment: String = row.get(4)?;
let vault_root: Vec<u8> = row.get(2)?;
let storage_commitment: Vec<u8> = row.get(3)?;
let code_commitment: Vec<u8> = row.get(4)?;
let account_seed: Option<Vec<u8>> = row.get(5)?;
let locked: bool = row.get(6)?;
let watched: bool = row.get(7)?;
Expand Down Expand Up @@ -132,9 +132,9 @@ pub(crate) fn query_historical_account_headers(
.query_map(params, |row| {
let id: Vec<u8> = row.get(0)?;
let nonce: u64 = column_value_as_u64(row, 1)?;
let vault_root: String = row.get(2)?;
let storage_commitment: String = row.get(3)?;
let code_commitment: String = row.get(4)?;
let vault_root: Vec<u8> = row.get(2)?;
let storage_commitment: Vec<u8> = row.get(3)?;
let code_commitment: Vec<u8> = row.get(4)?;
let account_seed: Option<Vec<u8>> = row.get(5)?;
let locked: bool = row.get(6)?;

Expand Down Expand Up @@ -163,7 +163,7 @@ pub(crate) fn query_account_code(

conn.prepare_cached(CODE_QUERY)
.into_store_error()?
.query_map(params![commitment.to_hex()], |row| {
.query_map(params![commitment.to_bytes()], |row| {
let code: Vec<u8> = row.get(0)?;
Ok(code)
})
Expand Down Expand Up @@ -207,15 +207,15 @@ pub(crate) fn query_vault_assets(
conn.prepare(VAULT_QUERY)
.into_store_error()?
.query_map(params![account_id.to_bytes()], |row| {
let vault_key: String = row.get(0)?;
let asset: String = row.get(1)?;
let vault_key: Vec<u8> = row.get(0)?;
let asset: Vec<u8> = row.get(1)?;
Ok((vault_key, asset))
})
.into_store_error()?
.map(|result| {
let (vault_key_str, asset_str): (String, String) = result.into_store_error()?;
let key_word = Word::try_from(vault_key_str)?;
let value_word = Word::try_from(asset_str)?;
let (vault_key_bytes, asset_bytes): (Vec<u8>, Vec<u8>) = result.into_store_error()?;
let key_word = Word::read_from_bytes(&vault_key_bytes)?;
let value_word = Word::read_from_bytes(&asset_bytes)?;
Ok(Asset::from_key_value_words(key_word, value_word)?)
})
.collect::<Result<Vec<Asset>, StoreError>>()
Expand Down Expand Up @@ -248,7 +248,7 @@ pub(crate) fn query_storage_slots(
format!("{base_query} AND slot_name IN ({placeholders})")
},
AccountStorageFilter::Root(root) => {
values_params.push(Value::Text(root.to_hex()));
values_params.push(Value::Blob(root.to_bytes()));
format!("{base_query} AND slot_value = ?2")
},
};
Expand All @@ -257,7 +257,7 @@ pub(crate) fn query_storage_slots(
let storage_values = stmt
.query_map(params_from_iter(values_params.iter()), |row| {
let slot_name: String = row.get(0)?;
let value: String = row.get(1)?;
let value: Vec<u8> = row.get(1)?;
let slot_type: u8 = row.get(2)?;
Ok((slot_name, value, slot_type))
})
Expand All @@ -268,7 +268,7 @@ pub(crate) fn query_storage_slots(
.map_err(|err| StoreError::ParsingError(err.to_string()))?;
let slot_type = StorageSlotType::try_from(slot_type)
.map_err(|e| StoreError::ParsingError(e.to_string()))?;
Ok((slot_name, Word::try_from(value)?, slot_type))
Ok((slot_name, Word::read_from_bytes(&value)?, slot_type))
})
.collect::<Result<Vec<(StorageSlotName, Word, StorageSlotType)>, StoreError>>()?;

Expand Down Expand Up @@ -332,8 +332,8 @@ pub(crate) fn query_storage_maps(
let map_entries = stmt
.query_map(params_from_iter(map_params.iter()), |row| {
let slot_name: String = row.get(0)?;
let key: String = row.get(1)?;
let value: String = row.get(2)?;
let key: Vec<u8> = row.get(1)?;
let value: Vec<u8> = row.get(2)?;

Ok((slot_name, key, value))
})
Expand All @@ -342,7 +342,11 @@ pub(crate) fn query_storage_maps(
let (slot_name, key, value) = result.into_store_error()?;
let slot_name = StorageSlotName::new(slot_name)
.map_err(|err| StoreError::ParsingError(err.to_string()))?;
Ok((slot_name, StorageMapKey::new(Word::try_from(key)?), Word::try_from(value)?))
Ok((
slot_name,
StorageMapKey::new(Word::read_from_bytes(&key)?),
Word::read_from_bytes(&value)?,
))
})
.collect::<Result<Vec<(StorageSlotName, StorageMapKey, Word)>, StoreError>>()?;

Expand All @@ -366,7 +370,7 @@ pub(crate) fn query_storage_values(
.into_store_error()?
.query_map(params![account_id.to_bytes()], |row| {
let slot_name: String = row.get(0)?;
let value: String = row.get(1)?;
let value: Vec<u8> = row.get(1)?;
let slot_type: u8 = row.get(2)?;
Ok((slot_name, value, slot_type))
})
Expand All @@ -377,7 +381,7 @@ pub(crate) fn query_storage_values(
.map_err(|err| StoreError::ParsingError(err.to_string()))?;
let slot_type = StorageSlotType::try_from(slot_type)
.map_err(|e| StoreError::ParsingError(e.to_string()))?;
Ok((slot_name, (slot_type, Word::try_from(value)?)))
Ok((slot_name, (slot_type, Word::read_from_bytes(&value)?)))
})
.collect()
}
44 changes: 28 additions & 16 deletions crates/sqlite-store/src/account/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use miden_client::account::{
StorageSlotType,
};
use miden_client::store::{AccountSmtForest, StoreError};
use miden_client::{EMPTY_WORD, Serializable, Word};
use miden_client::{Deserializable, EMPTY_WORD, Serializable, Word};
use rusqlite::types::Value;
use rusqlite::{OptionalExtension, Transaction, params};

Expand Down Expand Up @@ -53,15 +53,15 @@ impl SqliteStore {
.into_store_error()?
.query_map(params![account_id.to_bytes(), Rc::new(map_slot_names)], |row| {
let name: String = row.get(0)?;
let value: String = row.get(1)?;
let value: Vec<u8> = row.get(1)?;
Ok((name, value))
})
.into_store_error()?
.map(|result| {
let (name, value) = result.into_store_error()?;
let slot_name = StorageSlotName::new(name)
.map_err(|err| StoreError::ParsingError(err.to_string()))?;
Ok((slot_name, Word::try_from(value)?))
Ok((slot_name, Word::read_from_bytes(&value)?))
})
.collect()
}
Expand Down Expand Up @@ -94,11 +94,16 @@ impl SqliteStore {

for slot in account_storage {
let slot_name_str = slot.name().to_string();
let slot_value_hex = slot.value().to_hex();
let slot_value_bytes = slot.value().to_bytes();
let slot_type_val = slot.slot_type() as u8;

latest_slot_stmt
.execute(params![&account_id_bytes, &slot_name_str, &slot_value_hex, slot_type_val])
.execute(params![
&account_id_bytes,
&slot_name_str,
&slot_value_bytes,
slot_type_val
])
.into_store_error()?;

if let StorageSlotContent::Map(map) = slot.content() {
Expand All @@ -107,8 +112,8 @@ impl SqliteStore {
.execute(params![
&account_id_bytes,
&slot_name_str,
key.to_hex(),
value.to_hex(),
key.to_bytes(),
value.to_bytes(),
])
.into_store_error()?;
}
Expand Down Expand Up @@ -185,11 +190,11 @@ impl SqliteStore {

for (slot_name, (value, slot_type)) in updated_slots {
let slot_name_str = slot_name.to_string();
let slot_value_hex = value.to_hex();
let slot_value_bytes = value.to_bytes();
let slot_type_val = *slot_type as u8;

// Read old slot value from latest (NULL if slot is new)
let old_slot_value: Option<String> = tx
let old_slot_value: Option<Vec<u8>> = tx
.query_row(READ_OLD_SLOT, params![&account_id_bytes, &slot_name_str], |row| {
row.get(0)
})
Expand All @@ -210,7 +215,12 @@ impl SqliteStore {

// Update latest slot
latest_slot_stmt
.execute(params![&account_id_bytes, &slot_name_str, &slot_value_hex, slot_type_val])
.execute(params![
&account_id_bytes,
&slot_name_str,
&slot_value_bytes,
slot_type_val
])
.into_store_error()?;

if let Some(changed_entries) = delta_map_entries.get(slot_name) {
Expand Down Expand Up @@ -243,13 +253,13 @@ impl SqliteStore {
const DELETE_LATEST_MAP_ENTRY: &str = "DELETE FROM latest_storage_map_entries WHERE account_id = ? AND slot_name = ? AND key = ?";

for (key, value) in changed_entries {
let key_hex = key.to_hex();
let key_bytes = key.to_bytes();

// Read old map entry value from latest (NULL if entry is new)
let old_entry_value: Option<String> = tx
let old_entry_value: Option<Vec<u8>> = tx
.query_row(
READ_OLD_MAP_ENTRY,
params![account_id_bytes, slot_name_str, &key_hex],
params![account_id_bytes, slot_name_str, &key_bytes],
|row| row.get(0),
)
.optional()
Expand All @@ -262,7 +272,7 @@ impl SqliteStore {
account_id_bytes,
nonce_val,
slot_name_str,
&key_hex,
&key_bytes,
old_entry_value,
])
.into_store_error()?;
Expand All @@ -271,12 +281,14 @@ impl SqliteStore {
if *value == EMPTY_WORD {
tx.execute(
DELETE_LATEST_MAP_ENTRY,
params![account_id_bytes, slot_name_str, &key_hex],
params![account_id_bytes, slot_name_str, &key_bytes],
)
.into_store_error()?;
} else {
latest_map_stmt
.execute(params![account_id_bytes, slot_name_str, &key_hex, value.to_hex(),])
.execute(
params![account_id_bytes, slot_name_str, &key_bytes, value.to_bytes(),],
)
.into_store_error()?;
}
}
Expand Down
Loading
Loading