-
Notifications
You must be signed in to change notification settings - Fork 245
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
feat: Add StorageIndex newtype #2428
base: main
Are you sure you want to change the base?
Conversation
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.
Looks promising, thanks!
There are a few more places where the new StorageIndex
type should be used though (which would probably also warrant moving it to the common
crate).
In particular pathfinder_storage::connection::trie
has a few methods that are returning an Option<u64>
-- these are in fact storage indices, so the return type should probably be an Option<StorageIndex>
instead. Also, MerkleTree::new()
should probably take a StorageIndex
as well, and InternalNode::Unresolved
should contain a StorageIndex
, too.
Resolved comments And did linting on some files |
Actually, that linting might make this PR pretty much impossible to review (too many unrelated changes). Please note that we're using nightly rustfmt for formatting. Please check our |
Ahh apologies for the wrong linting...it should be fixed now |
crates/common/src/lib.rs
Outdated
@@ -27,6 +27,7 @@ pub mod prelude; | |||
pub mod receipt; | |||
pub mod signature; | |||
pub mod state_update; | |||
pub mod storage_index; |
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.
I think this fits better into the merkle-tree
crate since that's the only place where it's relevant.
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.
storage_index would also be used in pathfinder-storage
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.
If i move storage_index
to the pathfinder-merkle-tree
crate and then try to import this in the pathfinder-storage
crate ,it creates a circular dependency issue
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.
In that case can you maybe move the type to the storage
crate and please rename it to MerkleStorageIndex
or TrieStorageIndex
?
These storage indices are not really defined by the protocol, they have nothing to do with starknet per se; they're an implementation detail of our storage mechanism. In common
we're trying to only keep the most core types for the protocol.
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.
Understood
Moved to storage
crate and renamed to TrieStorageIndex
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.
Generally, this PR looks really promising to me. Now that we have the basics right I think we should make even more changes in connection::trie
. In general, we should be wary of u64
popping up in function signatures anywhere.
I'm not sure this list is exhaustive, but the general idea is that wherever we're actually having a storage index we should use TrieStorageIndex
instead of u64
:
RootIndexUpdate::Updated
should now contain aTrieStorageIndex
TrieUpdate::nodes_removed
should be aVec<TrieStorageIndex>
NodeRef::TrieStorageIndex
should contain aTrieStorageIndex
StoredNode
u64
ids are also storage indicesTransaction::trie_node()
andTransaction::trie_node_hash()
are also taking storage indices as theirindex
argument
crates/merkle-tree/src/contract.rs
Outdated
@@ -66,7 +67,7 @@ impl<'tx> ContractsStorageTree<'tx> { | |||
block: Some(block), | |||
contract, | |||
}; | |||
let tree = MerkleTree::new(root); | |||
let tree = MerkleTree::new(TrieStorageIndex::new(root)); |
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.
Looks like tx.contract_root_index()
is still returning a trie storage index as an u64. It should be modified to return a Result<Option<TrieStorageIndex>>
like class_root_index()
and storage_root_index()
.
@kkovaacs friendly ping for review |
@varun-doshi Could you please check (and fix) the test failures It seems there are a few tests that are failing with the latest state. |
Fixed the issues caused by the However, I ran into some protobuf issues on my local test. I then pull upstream main to a new branch and tested it there as well and it threw the same issue. Can you confirm if thats a failing test on main or its on my side? |
Well, if you mean #2446 , that should be fixed in main now (by #2447 ) - do tests work for you when you merge that? |
All good now |
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.
Another step forward, nice! Now I hope we're getting really close to using TrieStorageIndex
everywhere we're using storage indices.
crates/merkle-tree/src/contract.rs
Outdated
@@ -173,7 +174,7 @@ impl<'tx> StorageCommitmentTree<'tx> { | |||
block: Some(block), | |||
}; | |||
|
|||
let tree = MerkleTree::new(root); | |||
let tree = MerkleTree::new(TrieStorageIndex::new(root.get())); |
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.
Do we need the TrieStorageIndex
conversion here? As far as I've seen tx.storage_root_index()
is now returning an Option<TrieStorageIndex>
now so this seems to be redundant.
crates/merkle-tree/src/tree.rs
Outdated
let root = Some(Rc::new(RefCell::new(InternalNode::Unresolved(root)))); | ||
pub fn new(root: TrieStorageIndex) -> Self { | ||
let root = Some(Rc::new(RefCell::new(InternalNode::Unresolved( | ||
TrieStorageIndex::new(root.get()), |
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.
This seems to be a redundant conversion here. root
is already a TrieStorageIndex
.
crates/merkle-tree/src/tree.rs
Outdated
@@ -589,12 +603,12 @@ impl<H: FeltHash, const HEIGHT: usize> MerkleTree<H, HEIGHT> { | |||
height += 1; | |||
|
|||
let left = storage | |||
.hash(left) | |||
.hash(left.get()) |
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.
Can't we change the pathfinder_merkle_tree::storage::Storage
trait to use TrieStorageIndex
as arguments to get()
and hash()
? That seems to be another place where we're still having trie storage indices passed as u64
. That would also allow us removing many of these conversions in this file.
tx, | ||
block_number, | ||
contract_addresses, | ||
storage_root_idx.get(), |
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.
This is also suspicious: StorageCommitmentTree::get_proofs()
is taking a root index as an u64
here -- we should avoid that.
&tx, | ||
header.number, | ||
input.class_hash, | ||
class_root_idx.get(), |
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.
Same here: root index passed as an u64
.
@@ -594,7 +614,7 @@ impl Transaction<'_> { | |||
.prepare_cached(&format!("SELECT hash FROM {table} WHERE idx = ?")) | |||
.context("Creating get statement")?; | |||
|
|||
stmt.query_row(params![&index], |row| row.get_felt(0)) | |||
stmt.query_row(params![&index.get()], |row| row.get_felt(0)) |
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.
Implementing pathfinder_storage::params::ToSql
would make it possible to use a TrieStorageIndex
as a parameter directly. Please check the to_sql_int!()
macro to implement ToSql
.
crates/storage/src/lib.rs
Outdated
@@ -7,7 +7,7 @@ mod prelude; | |||
|
|||
mod bloom; | |||
pub use bloom::BLOCK_RANGE_LEN; | |||
mod connection; | |||
pub mod connection; |
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.
Can we avoid making the connection
module public? We already expose all the contents via pub use connection::*
from the crate root so this seems redundant?
Short description of what this PR does.
Sets a new Type
StorageIndex
for the unique index for the trie nodesA longer description, include motivation and intent if possible. Detail any caveats or follow-up steps still required.
Ref #2332
Adds a new explicit type StorageIndex to store the unique index for the trie nodes in the database.
Consider motivating any new dependencies.