NethermindEth / juno

Starknet client implementation.
https://juno.nethermind.io
Apache License 2.0
371 stars 158 forks source link

cmd/juno: add CLI tools for db #1902

Open weiihann opened 2 weeks ago

weiihann commented 2 weeks ago

Description

This PR adds a new CLI tool for database-related operations (i.e. juno db [commands]). Under the db category, there's currently one subcommand added which is info (i.e. juno db info) to display the blockchain information stored in the database.

Rationale

The current available methods to obtain blockchain information require users to run a Juno node. It's more convenient to read the data directly from the database. In the future, we may support more DB operations such as manual CRUD, pebbleDB statistics, import/export and more.

Example

Command: juno db info --db-path db | jq

{
  "network": "sepolia",
  "chain_height": 71958,
  "latest_block_hash": "0x69fa4f792a98cab20d5fe5040401a8377ae75234ee2446590e1b68d3abfa9e2",
  "latest_state_root": "0x7c5dd3a676f5b6f603227b1f1b77f8d95a96b05abc93c09f0715d27c7169f1c",
  "l1_height": 66367,
  "l1_block_hash": "0x335b4136b1da8317a1a72450ddb2df813ae828cb2ee4347d4c21d2d8adee0dc",
  "l1_state_root": "0x4ff66961f76f33cedc65124cbb3021e370cd966434c13245f1d75015e136ba8"
}
codecov[bot] commented 2 weeks ago

Codecov Report

Attention: Patch coverage is 70.27027% with 22 lines in your changes missing coverage. Please review.

Project coverage is 75.29%. Comparing base (4e6dcd7) to head (8c7c091).

Files Patch % Lines
cmd/juno/dbcmd.go 72.54% 8 Missing and 6 partials :warning:
clients/feeder/feeder.go 46.66% 4 Missing and 4 partials :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1902 +/- ## ========================================== - Coverage 75.43% 75.29% -0.15% ========================================== Files 97 98 +1 Lines 8342 8407 +65 ========================================== + Hits 6293 6330 +37 - Misses 1519 1535 +16 - Partials 530 542 +12 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

kirugan commented 2 weeks ago

Can we also show to which network db corresponds to? it would be great if there is some snapshot stored without identification

weiihann commented 2 weeks ago

Can we also show to which network db corresponds to? it would be great if there is some snapshot stored without identification

I think that's a good addition. From my understanding, the network ID isn't stored directly in the db. Let me figure out how to pull that information.

kirugan commented 2 weeks ago

Can we also show to which network db corresponds to? it would be great if there is some snapshot stored without identification

I think that's a good addition. From my understanding, the network ID isn't stored directly in the db. Let me figure out how to pull that information.

That's right you can modify existing approach for your case:

// Verify that cfg.Network is compatible with the database.
    head, err := chain.Head()
    if err != nil && !errors.Is(err, db.ErrKeyNotFound) {
        return nil, fmt.Errorf("get head block from database: %v", err)
    }
    if head != nil {
        // We assume that there is at least one transaction in the block or that it is a pre-0.7 block.
        if _, err = core.VerifyBlockHash(head, &cfg.Network); err != nil {
            return nil, errors.New("unable to verify latest block hash; are the database and --network option compatible?")
        }
    }

You should iterate over networks to figure out (_, err := core.VerifyBlockHash(...); err == nil) to which one it corresponds

weiihann commented 2 weeks ago

You should iterate over networks to figure out (_, err := core.VerifyBlockHash(...); err == nil) to which one it corresponds

Added the network field.