Skip to content

Commit

Permalink
Feat/db cmd (#18)
Browse files Browse the repository at this point in the history
* add initializing node height command

* update readme

* lint

* bump opinit version

* tidy

* delete future withdrawals & trees
  • Loading branch information
sh-cha authored Sep 20, 2024
1 parent 79cd4a8 commit c8917aa
Show file tree
Hide file tree
Showing 19 changed files with 275 additions and 18 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ To ensure compatibility with the node version, check the following versions:

| L1 Node | MiniMove | MiniWasm | MiniEVM |
| ------- | -------- | -------- | ------- |
| v0.4.7 | v0.4.1 | v0.4.1 | - |
| v0.4.9 | v0.4.2 | v0.4.2 | - |

### Build and Configure

Expand Down Expand Up @@ -73,3 +73,28 @@ To reset the bot database, use the following command:
```bash
opinitd reset-db [bot-name]
```

### Reset Node Heights

To reset bot's all height info to 0, use the following command:

```bash
opinitd reset-heights [bot-name]
```

### Reset Node Height

To reset bot's specific node height info to 0 , use the following command:

```bash
opinitd reset-height [bot-name] [node-type]

Executor node types:
- host
- child
- batch

Challenger node types:
- host
- child
```
4 changes: 2 additions & 2 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewBot(botType bottypes.BotType, logger *zap.Logger, homePath string, confi
return nil, err
}

db, err := db.NewDB(getDBPath(homePath, botType))
db, err := db.NewDB(GetDBPath(homePath, botType))
if err != nil {
return nil, err
}
Expand All @@ -67,6 +67,6 @@ func NewBot(botType bottypes.BotType, logger *zap.Logger, homePath string, confi
return nil, errors.New("not providing bot name")
}

func getDBPath(homePath string, botName bottypes.BotType) string {
func GetDBPath(homePath string, botName bottypes.BotType) string {
return fmt.Sprintf(homePath+"/%s.db", botName)
}
2 changes: 1 addition & 1 deletion challenger/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewChildV1(
}

func (ch *Child) Initialize(ctx context.Context, startHeight uint64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo, challenger challenger) error {
err := ch.BaseChild.Initialize(ctx, startHeight, startOutputIndex, bridgeInfo)
_, err := ch.BaseChild.Initialize(ctx, startHeight, startOutputIndex, bridgeInfo)
if err != nil {
return err
}
Expand Down
32 changes: 32 additions & 0 deletions challenger/db.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package challenger

import (
"fmt"
"slices"

challengertypes "github.com/initia-labs/opinit-bots/challenger/types"
"github.com/initia-labs/opinit-bots/node"
"github.com/initia-labs/opinit-bots/types"
"github.com/pkg/errors"
)

func (c *Challenger) PendingChallengeToRawKVs(challenges []challengertypes.Challenge, delete bool) ([]types.RawKV, error) {
Expand Down Expand Up @@ -80,3 +83,32 @@ func (c *Challenger) loadChallenges() (challenges []challengertypes.Challenge, e
slices.Reverse(challenges)
return
}

func ResetHeights(db types.DB) error {
dbs := []types.DB{
db.WithPrefix([]byte(types.HostName)),
db.WithPrefix([]byte(types.ChildName)),
}

for _, db := range dbs {
if err := node.DeleteSyncInfo(db); err != nil {
return err
}
fmt.Printf("reset height to 0 for node %s\n", string(db.GetPrefix()))
}
return nil
}

func ResetHeight(db types.DB, nodeName string) error {
if nodeName != types.HostName &&
nodeName != types.ChildName {
return errors.New("unknown node name")
}
nodeDB := db.WithPrefix([]byte(nodeName))
err := node.DeleteSyncInfo(nodeDB)
if err != nil {
return err
}
fmt.Printf("reset height to 0 for node %s\n", string(nodeDB.GetPrefix()))
return nil
}
73 changes: 73 additions & 0 deletions cmd/opinitd/reset.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package main

import (
"errors"
"os"
"path"

"github.com/spf13/cobra"

"github.com/initia-labs/opinit-bots/bot"
bottypes "github.com/initia-labs/opinit-bots/bot/types"
"github.com/initia-labs/opinit-bots/challenger"
"github.com/initia-labs/opinit-bots/db"
"github.com/initia-labs/opinit-bots/executor"
)

func resetDBCmd(ctx *cmdContext) *cobra.Command {
Expand Down Expand Up @@ -39,3 +44,71 @@ func resetDBCmd(ctx *cmdContext) *cobra.Command {
}
return cmd
}

func resetHeightsCmd(ctx *cmdContext) *cobra.Command {
cmd := &cobra.Command{
Use: "reset-heights [bot-name]",
Args: cobra.ExactArgs(1),
Short: "Reset bot's all height info.",
Long: `Reset bot's all height info.
`,
RunE: func(cmd *cobra.Command, args []string) error {
botType := bottypes.BotTypeFromString(args[0])
if err := botType.Validate(); err != nil {
return err
}

db, err := db.NewDB(bot.GetDBPath(ctx.homePath, botType))
if err != nil {
return err
}

switch botType {
case bottypes.BotTypeExecutor:
return executor.ResetHeights(db)
case bottypes.BotTypeChallenger:
return challenger.ResetHeights(db)
}
return errors.New("unknown bot type")
},
}
return cmd
}

func resetHeightCmd(ctx *cmdContext) *cobra.Command {
cmd := &cobra.Command{
Use: "reset-height [bot-name] [node-type]",
Args: cobra.ExactArgs(2),
Short: "Reset bot's node height info.",
Long: `Reset bot's node height info.
Executor node types:
- host
- child
- batch
Challenger node types:
- host
- child
`,
RunE: func(cmd *cobra.Command, args []string) error {
botType := bottypes.BotTypeFromString(args[0])
if err := botType.Validate(); err != nil {
return err
}

db, err := db.NewDB(bot.GetDBPath(ctx.homePath, botType))
if err != nil {
return err
}

switch botType {
case bottypes.BotTypeExecutor:
return executor.ResetHeight(db, args[1])
case bottypes.BotTypeChallenger:
return challenger.ResetHeight(db, args[1])
}
return errors.New("unknown bot type")
},
}
return cmd
}
2 changes: 2 additions & 0 deletions cmd/opinitd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func NewRootCmd() *cobra.Command {
startCmd(ctx),
keysCmd(ctx),
resetDBCmd(ctx),
resetHeightsCmd(ctx),
resetHeightCmd(ctx),
version.NewVersionCommand(),
)
return rootCmd
Expand Down
8 changes: 8 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,11 @@ func (db LevelDB) UnprefixedKey(key []byte) []byte {
func (db LevelDB) GetPath() string {
return db.path
}

func (db LevelDB) GetPrefix() []byte {
splits := bytes.Split(db.prefix, []byte{dbtypes.Splitter})
if len(splits) == 0 {
return nil
}
return splits[len(splits)-1]
}
8 changes: 6 additions & 2 deletions executor/batch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type BatchSubmitter struct {
LastBatchEndBlockNumber uint64
}

func NewBatchSubmitterV0(
func NewBatchSubmitterV1(
cfg nodetypes.NodeConfig,
batchCfg executortypes.BatchConfig,
db types.DB, logger *zap.Logger,
Expand All @@ -74,7 +74,7 @@ func NewBatchSubmitterV0(
}

ch := &BatchSubmitter{
version: 0,
version: 1,

node: node,

Expand Down Expand Up @@ -188,3 +188,7 @@ func (bs *BatchSubmitter) ChainID() string {
func (bs *BatchSubmitter) DA() executortypes.DANode {
return bs.da
}

func (bs BatchSubmitter) Node() *node.Node {
return bs.node
}
9 changes: 8 additions & 1 deletion executor/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,17 @@ func NewChildV1(
}

func (ch *Child) Initialize(ctx context.Context, startHeight uint64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error {
err := ch.BaseChild.Initialize(ctx, startHeight, startOutputIndex, bridgeInfo)
l2Sequence, err := ch.BaseChild.Initialize(ctx, startHeight, startOutputIndex, bridgeInfo)
if err != nil {
return err
}
if l2Sequence != 0 {
err = ch.DeleteFutureWithdrawals(l2Sequence)
if err != nil {
return err
}
}

ch.host = host
ch.registerHandlers()
return nil
Expand Down
13 changes: 13 additions & 0 deletions executor/child/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,16 @@ func (ch *Child) SetWithdrawal(sequence uint64, data executortypes.WithdrawalDat

return ch.DB().Set(executortypes.PrefixedWithdrawalKey(sequence), dataBytes)
}

func (ch *Child) DeleteFutureWithdrawals(fromSequence uint64) error {
return ch.DB().PrefixedIterate(executortypes.WithdrawalKey, func(key, _ []byte) (bool, error) {
sequence := dbtypes.ToUint64Key(key[len(key)-8:])
if sequence >= fromSequence {
err := ch.DB().Delete(key)
if err != nil {
return true, err
}
}
return false, nil
})
}
40 changes: 40 additions & 0 deletions executor/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package executor

import (
"fmt"

"github.com/initia-labs/opinit-bots/node"
"github.com/initia-labs/opinit-bots/types"
"github.com/pkg/errors"
)

func ResetHeights(db types.DB) error {
dbs := []types.DB{
db.WithPrefix([]byte(types.HostName)),
db.WithPrefix([]byte(types.ChildName)),
db.WithPrefix([]byte(types.BatchName)),
}

for _, db := range dbs {
if err := node.DeleteSyncInfo(db); err != nil {
return err
}
fmt.Printf("reset height to 0 for node %s\n", string(db.GetPrefix()))
}
return nil
}

func ResetHeight(db types.DB, nodeName string) error {
if nodeName != types.HostName &&
nodeName != types.ChildName &&
nodeName != types.BatchName {
return errors.New("unknown node name")
}
nodeDB := db.WithPrefix([]byte(nodeName))
err := node.DeleteSyncInfo(nodeDB)
if err != nil {
return err
}
fmt.Printf("reset height to 0 for node %s\n", string(nodeDB.GetPrefix()))
return nil
}
2 changes: 1 addition & 1 deletion executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server, logg
db.WithPrefix([]byte(types.ChildName)),
logger.Named(types.ChildName), cfg.L2Node.Bech32Prefix,
),
batch: batch.NewBatchSubmitterV0(
batch: batch.NewBatchSubmitterV1(
cfg.L2NodeConfig(homePath),
cfg.BatchConfig(), db.WithPrefix([]byte(types.BatchName)),
logger.Named(types.BatchName), cfg.L2Node.ChainID, homePath,
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/cometbft/cometbft v0.38.10
github.com/cosmos/cosmos-sdk v0.50.9
github.com/gofiber/fiber/v2 v2.52.5
github.com/initia-labs/OPinit v0.4.3
github.com/initia-labs/OPinit v0.4.4
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
Expand Down Expand Up @@ -37,7 +37,7 @@ require (
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/huandu/skiplist v1.2.0 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/initia-labs/OPinit/api v0.4.3 // indirect
github.com/initia-labs/OPinit/api v0.4.4 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,10 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/initia-labs/OPinit v0.4.3 h1:tFpcT9qeOLS49tFdEeK9ACEibeCEYd+V4Oz69gQPvp8=
github.com/initia-labs/OPinit v0.4.3/go.mod h1:1bf2//8NDHa2geXj81wm+2tLOW7zD6PQiGR6eakim00=
github.com/initia-labs/OPinit/api v0.4.3 h1:qljFODGw3F2ClGgJD4uiw1QXb3Px9tJX3jqWolPco/Q=
github.com/initia-labs/OPinit/api v0.4.3/go.mod h1:NorLLEBESDeLPQIzTFIT2XjvD/tkS1VRE6YL59TXYT0=
github.com/initia-labs/OPinit v0.4.4 h1:SIc0iPN3sokufbtkRNp7mPyddCriNzR7owilz4brg/4=
github.com/initia-labs/OPinit v0.4.4/go.mod h1:ZY1O3Ky3nUbH44WKt4AxR4WUoZsudthXCn9QyqeDCPU=
github.com/initia-labs/OPinit/api v0.4.4 h1:jA4BUh4OLR/FHDsOy9Ilqcd9kMTm1Ibasx/RlLUImXQ=
github.com/initia-labs/OPinit/api v0.4.4/go.mod h1:NorLLEBESDeLPQIzTFIT2XjvD/tkS1VRE6YL59TXYT0=
github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls=
github.com/jhump/protoreflect v1.15.3/go.mod h1:4ORHmSBmlCW8fh3xHmJMGyul1zNqZK4Elxc8qKP+p1k=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
Loading

0 comments on commit c8917aa

Please sign in to comment.