Skip to content

Commit

Permalink
Merge #1512
Browse files Browse the repository at this point in the history
1512: Fix loading the last block number in state keeper r=Deniallugo a=popzxc



Co-authored-by: Igor Aleksanov <[email protected]>
  • Loading branch information
bors-matterlabs-dev[bot] and popzxc authored Apr 16, 2021
2 parents 9bde5a6 + 1b149c5 commit af3cf31
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
14 changes: 14 additions & 0 deletions core/bin/zksync_core/src/state_keeper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,19 @@ impl ZkSyncStateInitParams {
}
}
}

// We have to load actual number of the last committed block, since above we load the block number from state,
// and in case of empty block being sealed (that may happen because of bug).
// Note that if this block is greater than the `block_number`, it means that some empty blocks were committed,
// so the root hash has not changed and we don't need to update the tree in order to get the right root hash.
let last_actually_committed_block_number = storage
.chain()
.block_schema()
.get_last_saved_block()
.await?;

let block_number = std::cmp::max(last_actually_committed_block_number, block_number);

if *block_number != 0 {
let storage_root_hash = storage
.chain()
Expand All @@ -289,6 +302,7 @@ impl ZkSyncStateInitParams {
"restored root_hash is different"
);
}

Ok(block_number)
}

Expand Down
18 changes: 18 additions & 0 deletions core/lib/storage/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -3668,6 +3668,24 @@
]
}
},
"bf88992c521353535925401702028307ba3d79dfa2e60d939a117e7aae5a6403": {
"query": "SELECT MAX(number) FROM blocks",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "max",
"type_info": "Int8"
}
],
"parameters": {
"Left": []
},
"nullable": [
null
]
}
},
"c0bc09d944da0d6a2eb2108185c757ff16440ed9c3d1fb2835cf3d4f552078f2": {
"query": "SELECT * FROM executed_priority_operations WHERE block_number = $1",
"describe": {
Expand Down
14 changes: 13 additions & 1 deletion core/lib/storage/src/chain/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,19 @@ impl<'a, 'c> BlockSchema<'a, 'c> {
result
}

/// Returns the number of last block
/// Returns the number of last block saved to the database.
pub async fn get_last_saved_block(&mut self) -> QueryResult<BlockNumber> {
let start = Instant::now();
let count = sqlx::query!("SELECT MAX(number) FROM blocks")
.fetch_one(self.0.conn())
.await?
.max
.unwrap_or(0);
metrics::histogram!("sql.chain.block.get_last_committed_block", start.elapsed());
Ok(BlockNumber(count as u32))
}

/// Returns the number of last block for which an aggregated operation exists.
pub async fn get_last_committed_block(&mut self) -> QueryResult<BlockNumber> {
let start = Instant::now();
let result = OperationsSchema(self.0)
Expand Down

0 comments on commit af3cf31

Please sign in to comment.