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

Add database CLI tools #1902

Merged
merged 16 commits into from
Aug 9, 2024
Merged

Add database CLI tools #1902

merged 16 commits into from
Aug 9, 2024

Conversation

weiihann
Copy link
Contributor

@weiihann weiihann commented Jun 10, 2024

Description

This PR adds a new CLI tool for database-related operations (i.e. juno db [commands]). Under the db category, there are 2 subcommand:

  1. juno db info - display the blockchain information stored in the database
{
  "network": "sepolia",
  "chain_height": 71958,
  "latest_block_hash": "0x69fa4f792a98cab20d5fe5040401a8377ae75234ee2446590e1b68d3abfa9e2",
  "latest_state_root": "0x7c5dd3a676f5b6f603227b1f1b77f8d95a96b05abc93c09f0715d27c7169f1c",
  "l1_height": 66367,
  "l1_block_hash": "0x335b4136b1da8317a1a72450ddb2df813ae828cb2ee4347d4c21d2d8adee0dc",
  "l1_state_root": "0x4ff66961f76f33cedc65124cbb3021e370cd966434c13245f1d75015e136ba8"
}
  1. juno db size - display storage information for each bucket in the db
Calculating size of StateTrie, remaining buckets: 23
Calculating size of Peer, remaining buckets: 22
Calculating size of ContractClassHash, remaining buckets: 21
Calculating size of ContractStorage, remaining buckets: 20
Calculating size of Class, remaining buckets: 19
Calculating size of ContractNonce, remaining buckets: 18
Calculating size of ChainHeight, remaining buckets: 17
Calculating size of BlockHeaderNumbersByHash, remaining buckets: 16
Calculating size of BlockHeadersByNumber, remaining buckets: 15
Calculating size of TransactionBlockNumbersAndIndicesByHash, remaining buckets: 14
Calculating size of TransactionsByBlockNumberAndIndex, remaining buckets: 13
Calculating size of ReceiptsByBlockNumberAndIndex, remaining buckets: 12
Calculating size of StateUpdatesByBlockNumber, remaining buckets: 11
Calculating size of ClassesTrie, remaining buckets: 10
Calculating size of ContractStorageHistory, remaining buckets: 9
Calculating size of ContractNonceHistory, remaining buckets: 8
Calculating size of ContractClassHashHistory, remaining buckets: 7
Calculating size of ContractDeploymentHeight, remaining buckets: 6
Calculating size of L1Height, remaining buckets: 5
Calculating size of SchemaVersion, remaining buckets: 4
Calculating size of Pending, remaining buckets: 3
Calculating size of BlockCommitments, remaining buckets: 2
Calculating size of Temporary, remaining buckets: 1
Calculating size of SchemaIntermediateState, remaining buckets: 0
+-----------------------------------------+------------+----------+
|                 BUCKET                  |    SIZE    |  COUNT   |
+-----------------------------------------+------------+----------+
| StateTrie                               | 9.44 MiB   |   143032 |
| Peer                                    | 0.00 B     |        0 |
| ContractClassHash                       | 4.43 MiB   |    71516 |
| ContractStorage                         | 662.14 MiB |  5970298 |
| Class                                   | 5.80 GiB   |    11139 |
| ContractNonce                           | 4.43 MiB   |    71516 |
| ChainHeight                             | 9.00 B     |        1 |
| BlockHeaderNumbersByHash                | 2.88 MiB   |    73536 |
| BlockHeadersByNumber                    | 115.86 MiB |    73536 |
| TransactionBlockNumbersAndIndicesByHash | 91.42 MiB  |  1956435 |
| TransactionsByBlockNumberAndIndex       | 4.03 GiB   |  1956435 |
| ReceiptsByBlockNumberAndIndex           | 4.82 GiB   |  1956435 |
| StateUpdatesByBlockNumber               | 688.26 MiB |    73536 |
| ClassesTrie                             | 1.14 MiB   |    17370 |
| ContractStorageHistory                  | 848.86 MiB |  8477125 |
| ContractNonceHistory                    | 43.72 MiB  |   628061 |
| ContractClassHashHistory                | 57.53 KiB  |      807 |
| ContractDeploymentHeight                | 2.80 MiB   |    71516 |
| L1Height                                | 0.00 B     |        0 |
| SchemaVersion                           | 9.00 B     |        1 |
| Pending                                 | 1.56 MiB   |        1 |
| BlockCommitments                        | 8.56 MiB   |    73536 |
| Temporary                               | 0.00 B     |        0 |
| SchemaIntermediateState                 | 2.00 B     |        1 |
+-----------------------------------------+------------+----------+
|                  TOTAL                  | 17.08 GIB  | 21625833 |
+-----------------------------------------+------------+----------+
+-----------------+----------+----------+
|      STATE      |   SIZE   |  COUNT   |
+-----------------+----------+----------+
| Without history | 6.46 GiB |  6267501 |
| With history    | 7.33 GiB | 15373494 |
+-----------------+----------+----------+

Copy link

codecov bot commented Jun 10, 2024

Codecov Report

Attention: Patch coverage is 72.46377% with 38 lines in your changes missing coverage. Please review.

Project coverage is 75.59%. Comparing base (7205822) to head (c15d540).

Files Patch % Lines
cmd/juno/dbcmd.go 73.68% 17 Missing and 13 partials ⚠️
utils/size.go 45.45% 6 Missing ⚠️
db/pebble/db.go 83.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1902      +/-   ##
==========================================
+ Coverage   75.45%   75.59%   +0.14%     
==========================================
  Files         100      101       +1     
  Lines        8983     9064      +81     
==========================================
+ Hits         6778     6852      +74     
+ Misses       1602     1593       -9     
- Partials      603      619      +16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@weiihann weiihann force-pushed the weiihann/db-cmd branch 2 times, most recently from 053dae8 to 90f00f4 Compare June 10, 2024 06:35
@kirugan
Copy link
Contributor

kirugan commented Jun 10, 2024

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

@weiihann
Copy link
Contributor Author

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.

@weiihann weiihann force-pushed the weiihann/db-cmd branch 2 times, most recently from 57a1fe3 to c4f9a82 Compare June 10, 2024 15:17
@kirugan
Copy link
Contributor

kirugan commented Jun 10, 2024

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 weiihann force-pushed the weiihann/db-cmd branch 3 times, most recently from cda476d to 02689e0 Compare June 11, 2024 03:34
@weiihann
Copy link
Contributor Author

weiihann commented Jun 11, 2024

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

Added the network field.

clients/feeder/feeder.go Outdated Show resolved Hide resolved
clients/feeder/feeder.go Outdated Show resolved Hide resolved
@weiihann
Copy link
Contributor Author

TODO for myself: do not find the target directory using working directory, binary will not be able to run anywhere. Perhaps use embed directive.

@weiihann weiihann force-pushed the weiihann/db-cmd branch 2 times, most recently from 920b0c0 to 2c7c7c2 Compare July 26, 2024 11:02
@weiihann
Copy link
Contributor Author

Calculating DB size has some differences between this implementation and #1773:

  1. Formats the data size into metric prefixes (i.e. MiB, GiB, etc)
  2. Adds count of each KV pair
  3. Outputs result in table format
  4. Put this subcommand under the subcommand of db ./juno db size instead of ./juno db-size

@weiihann
Copy link
Contributor Author

Tests fail because #1978 has to be merged.

@weiihann weiihann changed the title cmd/juno: add CLI tools for db Add database CLI tools Aug 5, 2024
cmd/juno/dbcmd.go Outdated Show resolved Hide resolved
utils/size.go Outdated Show resolved Hide resolved
utils/const.go Outdated Show resolved Hide resolved
cmd/juno/dbcmd.go Outdated Show resolved Hide resolved
@rianhughes rianhughes self-requested a review August 9, 2024 07:23
cmd/juno/dbcmd.go Outdated Show resolved Hide resolved
@kirugan kirugan merged commit 3cf58a1 into NethermindEth:main Aug 9, 2024
11 checks passed
AnkushinDaniil pushed a commit that referenced this pull request Aug 12, 2024
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.

None yet

3 participants