-
Notifications
You must be signed in to change notification settings - Fork 194
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
fix(katana): compute genesis state root in a separate provider #3045
Conversation
Ohayo sensei! WalkthroughThis pull request refactors and modularizes the blockchain commitment and trie update logic. In the backend module, new functions for committing blocks—including a genesis block—are introduced, along with a helper to update the block hash registry. A dedicated Changes
Sequence Diagram(s)sequenceDiagram
participant C as Client
participant B as Backend
participant T as TrieWriter
C->>B: commit_block(provider, header, txs, receipts, state_updates)
B->>B: update_block_hash_registry_contract(state_updates, block_number)
B->>T: Process trie updates via GenesisTrieWriter
B->>C: Return sealed block
sequenceDiagram
participant C as Client
participant B as Backend
participant G as GenesisTrieWriter
C->>B: commit_genesis_block(provider, header, txs, receipts, state_updates)
B->>G: Handle trie update for genesis block
B->>C: Return sealed block
Possibly Related PRs
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (5)
crates/katana/core/src/backend/mod.rs (3)
265-277
: Ohayo sensei, let's be mindful of the "hacky" approach.
You're employingGenesisTrieWriter
to avoid overwriting an existing database for the already initialized genesis. This solves the immediate problem, but consider a more elegant solution or a dedicated check in the future.
494-495
: Ohayo sensei! Watch for concurrency or race conditions inupdate_block_hash_registry_contract
.
Storing older block hashes with a buffer is a clever approach. The comment mentions “Fix quick!”, so let's keep an eye on potential concurrency with forked mode or when older blocks are absent.Also applies to: 497-522
549-625
: Ohayo sensei!GenesisTrieWriter
effectively manages a volatile trie.
The approach of building contract leafs in memory and committing them helps avoid polluting the main DB. Just ensure no parallel usage by multiple threads without synchronization to avoid data races.crates/katana/trie/src/lib.rs (2)
16-16
: Ohayo! Consider being more selective with exports, sensei!The wildcard export
pub use contracts::*;
exposes all public entities from the contracts module. While this provides access to the newContractLeaf
struct, it might expose more implementation details than necessary. Consider explicitly listing the required exports.-pub use contracts::*; +pub use contracts::{ContractLeaf, ContractsTrie};
20-25
: Ohayo! Documentation needs updating, sensei!The documentation references
ContractsTrie
directly, but with the expanded exports, it should be updated to mention other important types that are now publicly available, such asContractLeaf
./// This struct is not meant to be used directly, and instead use the specific tries that have -/// been derived from it, [`ClassesTrie`], [`ContractsTrie`], or [`StoragesTrie`]. +/// been derived from it, [`ClassesTrie`], [`ContractsTrie`], [`ContractLeaf`], or [`StoragesTrie`].
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
crates/katana/core/src/backend/mod.rs
(6 hunks)crates/katana/storage/provider/src/providers/db/trie.rs
(1 hunks)crates/katana/trie/src/contracts.rs
(1 hunks)crates/katana/trie/src/lib.rs
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
- GitHub Check: build
- GitHub Check: docs
- GitHub Check: ensure-wasm
🔇 Additional comments (9)
crates/katana/core/src/backend/mod.rs (6)
1-1
: Ohayo sensei! Good expansion of imports here.
These new imports (BTreeMap, HashMap, ClassHash, CompiledClassHash, and the katana_trie references) neatly set the stage for the enhanced trie-handling logic. No issues spotted.Also applies to: 12-12, 24-28
118-119
: Ohayo sensei! Nice refactor to callcommit_block
with a dedicated provider.
This approach centralizes block finalization logic and ensures the block is computed consistently. No concerns here.
278-282
: Ohayo sensei! Good mismatch check for existing vs. provided genesis hash.
Ensuring these hashes match prevents corrupting local state. Great safety net.
288-294
: Ohayo sensei! Fallback logic for empty DB is neat.
Switching tocommit_genesis_block
with the real provider if no local genesis block is found ensures a proper chain start without rewriting the DB.
524-537
: Ohayo sensei!commit_block
function looks well-structured.
Registering the block hash (contract address 0x1) before computing the rest clarifies the flow. Great job.
539-547
: Ohayo sensei! Separatingcommit_genesis_block
is a clean solution.
Keeping genesis logic distinct reduces the risk of accidental overwrites in normal block commits. Nicely done.crates/katana/trie/src/contracts.rs (1)
49-54
: Ohayo sensei!ContractLeaf
struct is a welcome addition.
Having class hash, storage root, and nonce in one structure centralizes contract state. Looks good to me.crates/katana/storage/provider/src/providers/db/trie.rs (1)
10-12
: Ohayo sensei! Neat refactor to importContractLeaf
fromkatana_trie
.
Removing local duplication reduces maintenance overhead. Good code reuse.crates/katana/trie/src/lib.rs (1)
1-178
: Ohayo! Changes look good, sensei!The modifications support the PR's objective of fixing genesis state root computation by making necessary types available while maintaining the core trie functionality intact.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3045 +/- ##
==========================================
+ Coverage 57.68% 57.76% +0.07%
==========================================
Files 437 437
Lines 59231 59305 +74
==========================================
+ Hits 34169 34257 +88
+ Misses 25062 25048 -14 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
crates/katana/core/tests/backend.rs (1)
83-91
: Consider adding error case coverage, sensei!While the happy path is well tested, consider adding test cases for potential error scenarios such as:
- Database corruption between initializations
- Invalid chain specifications
- Network issues during initialization
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
crates/katana/core/tests/backend.rs
(1 hunks)
🔇 Additional comments (1)
crates/katana/core/tests/backend.rs (1)
80-91
: Ohayo! Well-structured test implementation, sensei!The new test case effectively validates the fix for computing genesis state root with an existing database. The test structure is clean and uses parameterized testing appropriately.
When
katana --chain <ID>
is started with an already initialized database ie--db-dir <PATH>
, the chain's genesis block hash corresponds will be compared to the existing block hash in the provided database. To perform this comparison, the genesis state root needs to computed first. Currently, the computation is done against the same database that we provide (remember the database has already been initialized, and may have other state apart from the genesis) and the trie computation logic (the call totrie_insert_declared_classes
andtrie_insert_contract_updates
in thecompute_new_state_root
function) will automatically writes the updates into the db. Thus, OVERWRITING the already established tries and resulted in a completely different state root value.So the fix is to perform the check first - checking whether there's a genesis block or not in the database: If yes, then we compute the genesis state using a separate provider to avoid overwriting the db. Else, (meaning the db is empty and hasn't been initialized yet) we compute it using the db storage provider.
Summary by CodeRabbit
New Features
Refactor