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 db revert cmd #2216

Merged
merged 14 commits into from
Oct 16, 2024
94 changes: 80 additions & 14 deletions cmd/juno/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"github.com/spf13/cobra"
)

const (
dbRevertToBlockF = "to-block"
)

type DBInfo struct {
Network string `json:"network"`
ChainHeight uint64 `json:"chain_height"`
Expand All @@ -33,7 +37,7 @@
}

dbCmd.PersistentFlags().String(dbPathF, defaultDBPath, dbPathUsage)
dbCmd.AddCommand(DBInfoCmd(), DBSizeCmd())
dbCmd.AddCommand(DBInfoCmd(), DBSizeCmd(), DBRevertCmd())
return dbCmd
}

Expand All @@ -55,21 +59,29 @@
}
}

func DBRevertCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "revert",
Short: "Revert current head to given position",
Long: `This subcommand revert all data related to all blocks till given so it becomes new head.`,
RunE: dbRevert,
}
cmd.Flags().Uint64(dbRevertToBlockF, 0, "New head (this block won't be reverted)")

return cmd
}

func dbInfo(cmd *cobra.Command, args []string) error {
dbPath, err := cmd.Flags().GetString(dbPathF)
if err != nil {
return err
}

if _, err = os.Stat(dbPath); os.IsNotExist(err) {
fmt.Fprintln(cmd.OutOrStdout(), "Database path does not exist")
return nil
}

database, err := pebble.New(dbPath)
database, err := openDB(dbPath)
if err != nil {
return fmt.Errorf("open DB: %w", err)
return err

Check warning on line 82 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L82

Added line #L82 was not covered by tests
}
defer database.Close()

chain := blockchain.New(database, nil)
info := DBInfo{}
Expand Down Expand Up @@ -110,6 +122,50 @@
return nil
}

func dbRevert(cmd *cobra.Command, args []string) error {
dbPath, err := cmd.Flags().GetString(dbPathF)
if err != nil {
return err

Check warning on line 128 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L125-L128

Added lines #L125 - L128 were not covered by tests
}

revertToBlock, err := cmd.Flags().GetUint64(dbRevertToBlockF)
if err != nil {
return err

Check warning on line 133 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L131-L133

Added lines #L131 - L133 were not covered by tests
}

if revertToBlock == 0 {
return fmt.Errorf("--%v cannot be 0", dbRevertToBlockF)

Check warning on line 137 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L136-L137

Added lines #L136 - L137 were not covered by tests
}

database, err := openDB(dbPath)
if err != nil {
return err

Check warning on line 142 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L140-L142

Added lines #L140 - L142 were not covered by tests
}
defer database.Close()

Check warning on line 144 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L144

Added line #L144 was not covered by tests

for {
chain := blockchain.New(database, nil)
head, err := chain.Head()
if err != nil {
return fmt.Errorf("failed to get the latest block information: %v", err)

Check warning on line 150 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L146-L150

Added lines #L146 - L150 were not covered by tests
}

if head.Number == revertToBlock {
fmt.Fprintf(cmd.OutOrStdout(), "Successfully reverted all blocks to %d\n", revertToBlock)
break

Check warning on line 155 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L153-L155

Added lines #L153 - L155 were not covered by tests
}

err = chain.RevertHead()
if err != nil {
return fmt.Errorf("failed to revert head at block %d: %v", head.Number, err)

Check warning on line 160 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L158-L160

Added lines #L158 - L160 were not covered by tests
}

fmt.Fprintf(cmd.OutOrStdout(), "Reverted head at block %d\n", head.Number)

Check warning on line 163 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L163

Added line #L163 was not covered by tests
}

return nil

Check warning on line 166 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L166

Added line #L166 was not covered by tests
}

func dbSize(cmd *cobra.Command, args []string) error {
dbPath, err := cmd.Flags().GetString(dbPathF)
if err != nil {
Expand All @@ -120,15 +176,11 @@
return fmt.Errorf("--%v cannot be empty", dbPathF)
}

if _, err = os.Stat(dbPath); os.IsNotExist(err) {
fmt.Fprintln(cmd.OutOrStdout(), "Database path does not exist")
return nil
}

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

var (
totalSize utils.DataSize
Expand Down Expand Up @@ -201,3 +253,17 @@

return "unknown"
}

func openDB(path string) (db.DB, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return nil, fmt.Errorf("database path does not exist")

Check warning on line 260 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L260

Added line #L260 was not covered by tests
}

database, err := pebble.New(path)
if err != nil {
return nil, fmt.Errorf("failed to open db: %w", err)

Check warning on line 265 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L265

Added line #L265 was not covered by tests
}

return database, nil
}