Skip to content

Commit

Permalink
Move DBSize to dbsize.go
Browse files Browse the repository at this point in the history
  • Loading branch information
IronGauntlets committed Jul 19, 2024
1 parent 2285347 commit 3608694
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 80 deletions.
88 changes: 88 additions & 0 deletions cmd/juno/dbsize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"fmt"

"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/utils"
"github.com/spf13/cobra"
)

func DBSize() *cobra.Command {
dbSizeCmd := &cobra.Command{
Use: "db-size",
Short: "Calculate's Juno's DB size.",
RunE: func(cmd *cobra.Command, args []string) error {
dbPath, err := cmd.Flags().GetString(dbPathF)
if err != nil {
return err
}

if dbPath == "" {
return fmt.Errorf("--%v cannot be empty", dbPathF)
}

pebbleDB, err := pebble.New(dbPath)
if err != nil {
return err
}

var totalSize, stateSizeWithoutHistory, stateSizeWithHistory uint

_, err = fmt.Fprintln(cmd.OutOrStdout(), "Total number of DB buckets:", len(db.BucketValues()))
if err != nil {
return err
}

var bucketSize uint
for _, b := range db.BucketValues() {
bucketSize, err = pebble.CalculatePrefixSize(cmd.Context(), pebbleDB.(*pebble.DB), []byte{byte(b)})
if err != nil {
return err
}

_, err = fmt.Fprintln(cmd.OutOrStdout(), uint(b)+1, "Size of", b, "=", bucketSize)
if err != nil {
return err
}

totalSize += bucketSize

if utils.AnyOf(b, db.StateTrie, db.ContractStorage, db.Class, db.ContractNonce,
db.ContractDeploymentHeight) {
stateSizeWithoutHistory += bucketSize
stateSizeWithHistory += bucketSize
}

if utils.AnyOf(b, db.ContractStorageHistory, db.ContractNonceHistory, db.ContractClassHashHistory) {
stateSizeWithHistory += bucketSize
}
}

_, err = fmt.Fprintln(cmd.OutOrStdout())
if err != nil {
return err
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), "State size without history =", stateSizeWithoutHistory)
if err != nil {
return err
}

_, err = fmt.Fprintln(cmd.OutOrStdout(), "State size with history =", stateSizeWithHistory)
if err != nil {
return err
}

_, err = fmt.Fprintln(cmd.OutOrStdout(), "Total DB size =", totalSize)
return err
},
}

// Persistent Flag was not used from the Juno command because GenP2PKeyPair would also inherit it while PersistentPreRun was not used
// because none of the subcommand required access to the node.Config.
defaultDBPath, dbPathShort := "", "p"
dbSizeCmd.Flags().StringP(dbPathF, dbPathShort, defaultDBPath, dbPathUsage)

return dbSizeCmd
}
80 changes: 0 additions & 80 deletions cmd/juno/juno.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"syscall"
"time"

"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/db/pebble"
_ "github.com/NethermindEth/juno/jemalloc"
"github.com/NethermindEth/juno/node"
"github.com/NethermindEth/juno/utils"
Expand Down Expand Up @@ -359,81 +357,3 @@ func NewCmd(config *node.Config, run func(*cobra.Command, []string) error) *cobr

return junoCmd
}

func DBSize() *cobra.Command {
dbSizeCmd := &cobra.Command{
Use: "db-size",
Short: "Calculate's Juno's DB size.",
RunE: func(cmd *cobra.Command, args []string) error {
dbPath, err := cmd.Flags().GetString(dbPathF)
if err != nil {
return err
}

if dbPath == "" {
return fmt.Errorf("--%v cannot be empty", dbPathF)
}

pebbleDB, err := pebble.New(dbPath)
if err != nil {
return err
}

var totalSize, stateSizeWithoutHistory, stateSizeWithHistory uint

_, err = fmt.Fprintln(cmd.OutOrStdout(), "Total number of DB buckets:", len(db.BucketValues()))
if err != nil {
return err
}

var bucketSize uint
for _, b := range db.BucketValues() {
bucketSize, err = pebble.CalculatePrefixSize(cmd.Context(), pebbleDB.(*pebble.DB), []byte{byte(b)})
if err != nil {
return err
}

_, err = fmt.Fprintln(cmd.OutOrStdout(), uint(b)+1, "Size of", b, "=", bucketSize)
if err != nil {
return err
}

totalSize += bucketSize

if utils.AnyOf(db.StateTrie, db.ContractStorage, db.Class, db.ContractNonce,
db.ContractDeploymentHeight, b) {
stateSizeWithoutHistory += bucketSize
stateSizeWithHistory += bucketSize
}

if utils.AnyOf(db.ContractStorageHistory, db.ContractNonceHistory, db.ContractClassHashHistory, b) {
stateSizeWithHistory += bucketSize
}
}

_, err = fmt.Fprintln(cmd.OutOrStdout())
if err != nil {
return err
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), "State size without history =", stateSizeWithoutHistory)
if err != nil {
return err
}

_, err = fmt.Fprintln(cmd.OutOrStdout(), "State size with history =", stateSizeWithHistory)
if err != nil {
return err
}

_, err = fmt.Fprintln(cmd.OutOrStdout(), "Total DB size =", totalSize)
return err
},
}

// Persistent Flag was not used from the Juno command because GenP2PKeyPair would also inherit it while PersistentPreRun was not used
// because none of the subcommand required access to the node.Config.
defaultDBPath, dbPathShort := "", "p"
dbSizeCmd.Flags().StringP(dbPathF, dbPathShort, defaultDBPath, dbPathUsage)

return dbSizeCmd
}

0 comments on commit 3608694

Please sign in to comment.