Skip to content

Commit

Permalink
Replace serde_json::Value for tree cache with string and add cache si…
Browse files Browse the repository at this point in the history
…ze metric
  • Loading branch information
popzxc committed Mar 23, 2022
1 parent 9da0da7 commit 256c065
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 37 deletions.
2 changes: 1 addition & 1 deletion core/bin/data_restore/src/data_restore_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl<T: Transport> DataRestoreDriver<T> {
interactor
.store_tree_cache(
self.tree_state.block_number,
serde_json::to_value(tree_cache).expect("failed to serialize tree cache"),
serde_json::to_string(&tree_cache).expect("failed to serialize tree cache"),
)
.await;
}
Expand Down
6 changes: 1 addition & 5 deletions core/bin/data_restore/src/database_storage_interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,7 @@ impl<'a> DatabaseStorageInteractor<'a> {
}
}

pub async fn store_tree_cache(
&mut self,
block_number: BlockNumber,
tree_cache: serde_json::Value,
) {
pub async fn store_tree_cache(&mut self, block_number: BlockNumber, tree_cache: String) {
self.storage
.chain()
.block_schema()
Expand Down
6 changes: 1 addition & 5 deletions core/bin/data_restore/src/inmemory_storage_interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,7 @@ impl InMemoryStorageInteractor {
None
}

pub async fn store_tree_cache(
&mut self,
_block_number: BlockNumber,
_tree_cache: serde_json::Value,
) {
pub async fn store_tree_cache(&mut self, _block_number: BlockNumber, _tree_cache: String) {
// Inmemory storage doesn't support caching.
}

Expand Down
6 changes: 1 addition & 5 deletions core/bin/data_restore/src/storage_interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,7 @@ impl StorageInteractor<'_> {
/// * `block_number` - The corresponding block number
/// * `tree_cache` - Merkle tree cache
///
pub async fn store_tree_cache(
&mut self,
block_number: BlockNumber,
tree_cache: serde_json::Value,
) {
pub async fn store_tree_cache(&mut self, block_number: BlockNumber, tree_cache: String) {
storage_interact!(self.store_tree_cache(block_number, tree_cache))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ impl<'a, 'b> StateRestoreDb for StateRestoreStorage<'a, 'b> {
block: BlockNumber,
account_tree_cache: SparseMerkleTreeSerializableCacheBN256,
) {
let encoded_tree_cache =
serde_json::to_value(account_tree_cache).expect("Unable to encode account tree cache");
let encoded_tree_cache = serde_json::to_string(&account_tree_cache)
.expect("Unable to encode account tree cache");
self.storage
.chain()
.block_schema()
Expand Down
2 changes: 1 addition & 1 deletion core/bin/zksync_witness_generator/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl DatabaseInterface for Database {
&self,
connection: &mut StorageProcessor<'_>,
block: BlockNumber,
tree_cache: serde_json::Value,
tree_cache: String,
) -> anyhow::Result<()> {
connection
.chain()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub trait DatabaseInterface: Send + Sync + Clone + 'static {
&self,
connection: &mut StorageProcessor<'_>,
block: BlockNumber,
tree_cache: serde_json::Value,
tree_cache: String,
) -> anyhow::Result<()>;

async fn store_witness(
Expand Down
4 changes: 1 addition & 3 deletions core/bin/zksync_witness_generator/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,11 @@ impl DatabaseInterface for MockDatabase {
&self,
_: &mut StorageProcessor<'_>,
block: BlockNumber,
tree_cache: serde_json::Value,
tree_cache: String,
) -> anyhow::Result<()> {
if *block == 0 {
return Ok(());
}
let tree_cache =
serde_json::to_string(&tree_cache).expect("Failed to serialize Account Tree Cache");

let mut account_tree_cache = self.account_tree_cache.write().await;
*account_tree_cache = AccountTreeCache {
Expand Down
13 changes: 6 additions & 7 deletions core/bin/zksync_witness_generator/src/witness_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,11 @@ impl<DB: DatabaseInterface> WitnessGenerator<DB> {
metrics::histogram!("witness_generator", start.elapsed(), "stage" => "recreate_tree_from_cache");

let start = Instant::now();
let account_tree_cache = circuit_account_tree.get_internals();
let tree_cache = serde_json::to_string(&circuit_account_tree.get_internals())?;
metrics::histogram!("tree_cache_size", tree_cache.len() as f64);

self.database
.store_account_tree_cache(
&mut storage,
block,
serde_json::to_value(account_tree_cache)?,
)
.store_account_tree_cache(&mut storage, block, tree_cache)
.await?;
metrics::histogram!("witness_generator", start.elapsed(), "stage" => "store_cache");
} else {
Expand All @@ -180,7 +178,8 @@ impl<DB: DatabaseInterface> WitnessGenerator<DB> {
metrics::histogram!("witness_generator", start.elapsed(), "stage" => "recreate_tree_from_scratch");

let start = Instant::now();
let tree_cache = serde_json::to_value(circuit_account_tree.get_internals())?;
let tree_cache = serde_json::to_string(&circuit_account_tree.get_internals())?;
metrics::histogram!("tree_cache_size", tree_cache.len() as f64);
metrics::histogram!("witness_generator", start.elapsed(), "stage" => "serialize_cache");

let start = Instant::now();
Expand Down
9 changes: 4 additions & 5 deletions core/lib/storage/src/chain/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,19 +1161,18 @@ impl<'a, 'c> BlockSchema<'a, 'c> {
Ok(())
}

/// Stores account tree cache for a block
/// Stores account tree cache for a block.
/// Expects `tree_cache` to be a valid encoded JSON.
pub async fn store_account_tree_cache(
&mut self,
block: BlockNumber,
tree_cache: serde_json::Value,
tree_cache: String,
) -> QueryResult<()> {
let start = Instant::now();
if *block == 0 {
return Ok(());
}

let tree_cache_str =
serde_json::to_string(&tree_cache).expect("Failed to serialize Account Tree Cache");
sqlx::query!(
"
INSERT INTO account_tree_cache (block, tree_cache)
Expand All @@ -1182,7 +1181,7 @@ impl<'a, 'c> BlockSchema<'a, 'c> {
DO NOTHING
",
*block as i64,
tree_cache_str,
tree_cache,
)
.execute(self.0.conn())
.await?;
Expand Down
10 changes: 8 additions & 2 deletions core/lib/storage/src/tests/chain/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,10 @@ async fn test_remove_new_account_tree_cache(mut storage: StorageProcessor<'_>) -
))
.await?;
BlockSchema(&mut storage)
.store_account_tree_cache(BlockNumber(block_number), serde_json::Value::default())
.store_account_tree_cache(
BlockNumber(block_number),
serde_json::Value::default().to_string(),
)
.await?;
}

Expand Down Expand Up @@ -1370,7 +1373,10 @@ async fn test_remove_old_account_tree_cache(mut storage: StorageProcessor<'_>) -
))
.await?;
BlockSchema(&mut storage)
.store_account_tree_cache(BlockNumber(block_number), serde_json::Value::default())
.store_account_tree_cache(
BlockNumber(block_number),
serde_json::Value::default().to_string(),
)
.await?;
}

Expand Down

0 comments on commit 256c065

Please sign in to comment.