Skip to content

Commit c8917aa

Browse files
authored
Feat/db cmd (#18)
* add initializing node height command * update readme * lint * bump opinit version * tidy * delete future withdrawals & trees
1 parent 79cd4a8 commit c8917aa

File tree

19 files changed

+275
-18
lines changed

19 files changed

+275
-18
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ To ensure compatibility with the node version, check the following versions:
1919

2020
| L1 Node | MiniMove | MiniWasm | MiniEVM |
2121
| ------- | -------- | -------- | ------- |
22-
| v0.4.7 | v0.4.1 | v0.4.1 | - |
22+
| v0.4.9 | v0.4.2 | v0.4.2 | - |
2323

2424
### Build and Configure
2525

@@ -73,3 +73,28 @@ To reset the bot database, use the following command:
7373
```bash
7474
opinitd reset-db [bot-name]
7575
```
76+
77+
### Reset Node Heights
78+
79+
To reset bot's all height info to 0, use the following command:
80+
81+
```bash
82+
opinitd reset-heights [bot-name]
83+
```
84+
85+
### Reset Node Height
86+
87+
To reset bot's specific node height info to 0 , use the following command:
88+
89+
```bash
90+
opinitd reset-height [bot-name] [node-type]
91+
92+
Executor node types:
93+
- host
94+
- child
95+
- batch
96+
97+
Challenger node types:
98+
- host
99+
- child
100+
```

bot/bot.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NewBot(botType bottypes.BotType, logger *zap.Logger, homePath string, confi
4242
return nil, err
4343
}
4444

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

70-
func getDBPath(homePath string, botName bottypes.BotType) string {
70+
func GetDBPath(homePath string, botName bottypes.BotType) string {
7171
return fmt.Sprintf(homePath+"/%s.db", botName)
7272
}

challenger/child/child.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func NewChildV1(
5757
}
5858

5959
func (ch *Child) Initialize(ctx context.Context, startHeight uint64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo, challenger challenger) error {
60-
err := ch.BaseChild.Initialize(ctx, startHeight, startOutputIndex, bridgeInfo)
60+
_, err := ch.BaseChild.Initialize(ctx, startHeight, startOutputIndex, bridgeInfo)
6161
if err != nil {
6262
return err
6363
}

challenger/db.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package challenger
22

33
import (
4+
"fmt"
45
"slices"
56

67
challengertypes "github.com/initia-labs/opinit-bots/challenger/types"
8+
"github.com/initia-labs/opinit-bots/node"
79
"github.com/initia-labs/opinit-bots/types"
10+
"github.com/pkg/errors"
811
)
912

1013
func (c *Challenger) PendingChallengeToRawKVs(challenges []challengertypes.Challenge, delete bool) ([]types.RawKV, error) {
@@ -80,3 +83,32 @@ func (c *Challenger) loadChallenges() (challenges []challengertypes.Challenge, e
8083
slices.Reverse(challenges)
8184
return
8285
}
86+
87+
func ResetHeights(db types.DB) error {
88+
dbs := []types.DB{
89+
db.WithPrefix([]byte(types.HostName)),
90+
db.WithPrefix([]byte(types.ChildName)),
91+
}
92+
93+
for _, db := range dbs {
94+
if err := node.DeleteSyncInfo(db); err != nil {
95+
return err
96+
}
97+
fmt.Printf("reset height to 0 for node %s\n", string(db.GetPrefix()))
98+
}
99+
return nil
100+
}
101+
102+
func ResetHeight(db types.DB, nodeName string) error {
103+
if nodeName != types.HostName &&
104+
nodeName != types.ChildName {
105+
return errors.New("unknown node name")
106+
}
107+
nodeDB := db.WithPrefix([]byte(nodeName))
108+
err := node.DeleteSyncInfo(nodeDB)
109+
if err != nil {
110+
return err
111+
}
112+
fmt.Printf("reset height to 0 for node %s\n", string(nodeDB.GetPrefix()))
113+
return nil
114+
}

cmd/opinitd/reset.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package main
22

33
import (
4+
"errors"
45
"os"
56
"path"
67

78
"github.com/spf13/cobra"
89

10+
"github.com/initia-labs/opinit-bots/bot"
911
bottypes "github.com/initia-labs/opinit-bots/bot/types"
12+
"github.com/initia-labs/opinit-bots/challenger"
13+
"github.com/initia-labs/opinit-bots/db"
14+
"github.com/initia-labs/opinit-bots/executor"
1015
)
1116

1217
func resetDBCmd(ctx *cmdContext) *cobra.Command {
@@ -39,3 +44,71 @@ func resetDBCmd(ctx *cmdContext) *cobra.Command {
3944
}
4045
return cmd
4146
}
47+
48+
func resetHeightsCmd(ctx *cmdContext) *cobra.Command {
49+
cmd := &cobra.Command{
50+
Use: "reset-heights [bot-name]",
51+
Args: cobra.ExactArgs(1),
52+
Short: "Reset bot's all height info.",
53+
Long: `Reset bot's all height info.
54+
`,
55+
RunE: func(cmd *cobra.Command, args []string) error {
56+
botType := bottypes.BotTypeFromString(args[0])
57+
if err := botType.Validate(); err != nil {
58+
return err
59+
}
60+
61+
db, err := db.NewDB(bot.GetDBPath(ctx.homePath, botType))
62+
if err != nil {
63+
return err
64+
}
65+
66+
switch botType {
67+
case bottypes.BotTypeExecutor:
68+
return executor.ResetHeights(db)
69+
case bottypes.BotTypeChallenger:
70+
return challenger.ResetHeights(db)
71+
}
72+
return errors.New("unknown bot type")
73+
},
74+
}
75+
return cmd
76+
}
77+
78+
func resetHeightCmd(ctx *cmdContext) *cobra.Command {
79+
cmd := &cobra.Command{
80+
Use: "reset-height [bot-name] [node-type]",
81+
Args: cobra.ExactArgs(2),
82+
Short: "Reset bot's node height info.",
83+
Long: `Reset bot's node height info.
84+
Executor node types:
85+
- host
86+
- child
87+
- batch
88+
89+
Challenger node types:
90+
- host
91+
- child
92+
`,
93+
RunE: func(cmd *cobra.Command, args []string) error {
94+
botType := bottypes.BotTypeFromString(args[0])
95+
if err := botType.Validate(); err != nil {
96+
return err
97+
}
98+
99+
db, err := db.NewDB(bot.GetDBPath(ctx.homePath, botType))
100+
if err != nil {
101+
return err
102+
}
103+
104+
switch botType {
105+
case bottypes.BotTypeExecutor:
106+
return executor.ResetHeight(db, args[1])
107+
case bottypes.BotTypeChallenger:
108+
return challenger.ResetHeight(db, args[1])
109+
}
110+
return errors.New("unknown bot type")
111+
},
112+
}
113+
return cmd
114+
}

cmd/opinitd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func NewRootCmd() *cobra.Command {
4646
startCmd(ctx),
4747
keysCmd(ctx),
4848
resetDBCmd(ctx),
49+
resetHeightsCmd(ctx),
50+
resetHeightCmd(ctx),
4951
version.NewVersionCommand(),
5052
)
5153
return rootCmd

db/db.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,11 @@ func (db LevelDB) UnprefixedKey(key []byte) []byte {
161161
func (db LevelDB) GetPath() string {
162162
return db.path
163163
}
164+
165+
func (db LevelDB) GetPrefix() []byte {
166+
splits := bytes.Split(db.prefix, []byte{dbtypes.Splitter})
167+
if len(splits) == 0 {
168+
return nil
169+
}
170+
return splits[len(splits)-1]
171+
}

executor/batch/batch.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type BatchSubmitter struct {
5555
LastBatchEndBlockNumber uint64
5656
}
5757

58-
func NewBatchSubmitterV0(
58+
func NewBatchSubmitterV1(
5959
cfg nodetypes.NodeConfig,
6060
batchCfg executortypes.BatchConfig,
6161
db types.DB, logger *zap.Logger,
@@ -74,7 +74,7 @@ func NewBatchSubmitterV0(
7474
}
7575

7676
ch := &BatchSubmitter{
77-
version: 0,
77+
version: 1,
7878

7979
node: node,
8080

@@ -188,3 +188,7 @@ func (bs *BatchSubmitter) ChainID() string {
188188
func (bs *BatchSubmitter) DA() executortypes.DANode {
189189
return bs.da
190190
}
191+
192+
func (bs BatchSubmitter) Node() *node.Node {
193+
return bs.node
194+
}

executor/child/child.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,17 @@ func NewChildV1(
5959
}
6060

6161
func (ch *Child) Initialize(ctx context.Context, startHeight uint64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error {
62-
err := ch.BaseChild.Initialize(ctx, startHeight, startOutputIndex, bridgeInfo)
62+
l2Sequence, err := ch.BaseChild.Initialize(ctx, startHeight, startOutputIndex, bridgeInfo)
6363
if err != nil {
6464
return err
6565
}
66+
if l2Sequence != 0 {
67+
err = ch.DeleteFutureWithdrawals(l2Sequence)
68+
if err != nil {
69+
return err
70+
}
71+
}
72+
6673
ch.host = host
6774
ch.registerHandlers()
6875
return nil

executor/child/withdraw.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,16 @@ func (ch *Child) SetWithdrawal(sequence uint64, data executortypes.WithdrawalDat
190190

191191
return ch.DB().Set(executortypes.PrefixedWithdrawalKey(sequence), dataBytes)
192192
}
193+
194+
func (ch *Child) DeleteFutureWithdrawals(fromSequence uint64) error {
195+
return ch.DB().PrefixedIterate(executortypes.WithdrawalKey, func(key, _ []byte) (bool, error) {
196+
sequence := dbtypes.ToUint64Key(key[len(key)-8:])
197+
if sequence >= fromSequence {
198+
err := ch.DB().Delete(key)
199+
if err != nil {
200+
return true, err
201+
}
202+
}
203+
return false, nil
204+
})
205+
}

executor/db.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package executor
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/initia-labs/opinit-bots/node"
7+
"github.com/initia-labs/opinit-bots/types"
8+
"github.com/pkg/errors"
9+
)
10+
11+
func ResetHeights(db types.DB) error {
12+
dbs := []types.DB{
13+
db.WithPrefix([]byte(types.HostName)),
14+
db.WithPrefix([]byte(types.ChildName)),
15+
db.WithPrefix([]byte(types.BatchName)),
16+
}
17+
18+
for _, db := range dbs {
19+
if err := node.DeleteSyncInfo(db); err != nil {
20+
return err
21+
}
22+
fmt.Printf("reset height to 0 for node %s\n", string(db.GetPrefix()))
23+
}
24+
return nil
25+
}
26+
27+
func ResetHeight(db types.DB, nodeName string) error {
28+
if nodeName != types.HostName &&
29+
nodeName != types.ChildName &&
30+
nodeName != types.BatchName {
31+
return errors.New("unknown node name")
32+
}
33+
nodeDB := db.WithPrefix([]byte(nodeName))
34+
err := node.DeleteSyncInfo(nodeDB)
35+
if err != nil {
36+
return err
37+
}
38+
fmt.Printf("reset height to 0 for node %s\n", string(nodeDB.GetPrefix()))
39+
return nil
40+
}

executor/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server, logg
5858
db.WithPrefix([]byte(types.ChildName)),
5959
logger.Named(types.ChildName), cfg.L2Node.Bech32Prefix,
6060
),
61-
batch: batch.NewBatchSubmitterV0(
61+
batch: batch.NewBatchSubmitterV1(
6262
cfg.L2NodeConfig(homePath),
6363
cfg.BatchConfig(), db.WithPrefix([]byte(types.BatchName)),
6464
logger.Named(types.BatchName), cfg.L2Node.ChainID, homePath,

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/cometbft/cometbft v0.38.10
88
github.com/cosmos/cosmos-sdk v0.50.9
99
github.com/gofiber/fiber/v2 v2.52.5
10-
github.com/initia-labs/OPinit v0.4.3
10+
github.com/initia-labs/OPinit v0.4.4
1111
github.com/pkg/errors v0.9.1
1212
github.com/spf13/cobra v1.8.1
1313
github.com/spf13/viper v1.19.0
@@ -37,7 +37,7 @@ require (
3737
github.com/hashicorp/yamux v0.1.1 // indirect
3838
github.com/huandu/skiplist v1.2.0 // indirect
3939
github.com/improbable-eng/grpc-web v0.15.0 // indirect
40-
github.com/initia-labs/OPinit/api v0.4.3 // indirect
40+
github.com/initia-labs/OPinit/api v0.4.4 // indirect
4141
github.com/leodido/go-urn v1.4.0 // indirect
4242
github.com/lib/pq v1.10.9 // indirect
4343
github.com/mattn/go-runewidth v0.0.15 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,10 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
476476
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
477477
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
478478
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
479-
github.com/initia-labs/OPinit v0.4.3 h1:tFpcT9qeOLS49tFdEeK9ACEibeCEYd+V4Oz69gQPvp8=
480-
github.com/initia-labs/OPinit v0.4.3/go.mod h1:1bf2//8NDHa2geXj81wm+2tLOW7zD6PQiGR6eakim00=
481-
github.com/initia-labs/OPinit/api v0.4.3 h1:qljFODGw3F2ClGgJD4uiw1QXb3Px9tJX3jqWolPco/Q=
482-
github.com/initia-labs/OPinit/api v0.4.3/go.mod h1:NorLLEBESDeLPQIzTFIT2XjvD/tkS1VRE6YL59TXYT0=
479+
github.com/initia-labs/OPinit v0.4.4 h1:SIc0iPN3sokufbtkRNp7mPyddCriNzR7owilz4brg/4=
480+
github.com/initia-labs/OPinit v0.4.4/go.mod h1:ZY1O3Ky3nUbH44WKt4AxR4WUoZsudthXCn9QyqeDCPU=
481+
github.com/initia-labs/OPinit/api v0.4.4 h1:jA4BUh4OLR/FHDsOy9Ilqcd9kMTm1Ibasx/RlLUImXQ=
482+
github.com/initia-labs/OPinit/api v0.4.4/go.mod h1:NorLLEBESDeLPQIzTFIT2XjvD/tkS1VRE6YL59TXYT0=
483483
github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls=
484484
github.com/jhump/protoreflect v1.15.3/go.mod h1:4ORHmSBmlCW8fh3xHmJMGyul1zNqZK4Elxc8qKP+p1k=
485485
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=

0 commit comments

Comments
 (0)