Skip to content
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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

varun-doshi
Copy link

@varun-doshi varun-doshi commented Dec 2, 2024

Short description of what this PR does.

Sets a new Type StorageIndex for the unique index for the trie nodes

A 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.

@varun-doshi varun-doshi requested a review from a team as a code owner December 2, 2024 14:20
Copy link
Contributor

@kkovaacs kkovaacs left a 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.

crates/merkle-tree/src/merkle_node.rs Outdated Show resolved Hide resolved
@varun-doshi
Copy link
Author

Resolved comments

And did linting on some files

@kkovaacs
Copy link
Contributor

kkovaacs commented Dec 4, 2024

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 justfile for the proper command to format the code.

@varun-doshi
Copy link
Author

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 justfile for the proper command to format the code.

Ahh apologies for the wrong linting...it should be fixed now

@varun-doshi varun-doshi requested a review from kkovaacs December 4, 2024 14:22
@@ -27,6 +27,7 @@ pub mod prelude;
pub mod receipt;
pub mod signature;
pub mod state_update;
pub mod storage_index;
Copy link
Contributor

@sistemd sistemd Dec 6, 2024

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.

Copy link
Author

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

Copy link
Author

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

Copy link
Contributor

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.

Copy link
Author

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

@varun-doshi varun-doshi requested a review from sistemd December 10, 2024 07:57
Copy link
Contributor

@kkovaacs kkovaacs left a 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 a TrieStorageIndex
  • TrieUpdate::nodes_removed should be a Vec<TrieStorageIndex>
  • NodeRef::TrieStorageIndex should contain a TrieStorageIndex
  • StoredNode u64 ids are also storage indices
  • Transaction::trie_node() and Transaction::trie_node_hash() are also taking storage indices as their index argument

@@ -66,7 +67,7 @@ impl<'tx> ContractsStorageTree<'tx> {
block: Some(block),
contract,
};
let tree = MerkleTree::new(root);
let tree = MerkleTree::new(TrieStorageIndex::new(root));
Copy link
Contributor

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().

@varun-doshi varun-doshi requested a review from kkovaacs December 11, 2024 15:09
@varun-doshi
Copy link
Author

@kkovaacs friendly ping for review

@kkovaacs
Copy link
Contributor

@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.

@varun-doshi
Copy link
Author

varun-doshi commented Dec 13, 2024

@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 StorageIndex changes.

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?

@vbar
Copy link
Contributor

vbar commented Dec 16, 2024

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?

@varun-doshi
Copy link
Author

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

Copy link
Contributor

@kkovaacs kkovaacs left a 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.

@@ -173,7 +174,7 @@ impl<'tx> StorageCommitmentTree<'tx> {
block: Some(block),
};

let tree = MerkleTree::new(root);
let tree = MerkleTree::new(TrieStorageIndex::new(root.get()));
Copy link
Contributor

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.

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()),
Copy link
Contributor

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.

@@ -589,12 +603,12 @@ impl<H: FeltHash, const HEIGHT: usize> MerkleTree<H, HEIGHT> {
height += 1;

let left = storage
.hash(left)
.hash(left.get())
Copy link
Contributor

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(),
Copy link
Contributor

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(),
Copy link
Contributor

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))
Copy link
Contributor

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.

@@ -7,7 +7,7 @@ mod prelude;

mod bloom;
pub use bloom::BLOCK_RANGE_LEN;
mod connection;
pub mod connection;
Copy link
Contributor

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?

@varun-doshi varun-doshi requested a review from kkovaacs December 22, 2024 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants