Skip to content

Commit 887b8c1

Browse files
authored
Fix/db migration2 (#73)
* merkle db from child db * delete query error & return tx time info instead of integer * update readme * print log error * format
1 parent 4910bc0 commit 887b8c1

File tree

14 files changed

+79
-56
lines changed

14 files changed

+79
-56
lines changed

challenger/challenger.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (c *Challenger) Initialize(ctx types.Context) error {
129129
}
130130
}
131131

132-
c.RegisterQuerier()
132+
c.RegisterQuerier(ctx)
133133

134134
c.pendingChallenges, err = challengerdb.LoadPendingChallenges(c.db)
135135
if err != nil {
@@ -176,18 +176,19 @@ func (c *Challenger) Start(ctx types.Context) error {
176176
return ctx.ErrGrp().Wait()
177177
}
178178

179-
func (c *Challenger) RegisterQuerier() {
180-
c.server.RegisterQuerier("/status", func(ctx *fiber.Ctx) error {
179+
func (c *Challenger) RegisterQuerier(ctx types.Context) {
180+
c.server.RegisterQuerier("/status", func(fiberCtx *fiber.Ctx) error {
181181
status, err := c.GetStatus()
182182
if err != nil {
183-
return err
183+
ctx.Logger().Error("failed to query status", zap.Error(err))
184+
return errors.New("failed to query status")
184185
}
185186

186-
return ctx.JSON(status)
187+
return fiberCtx.JSON(status)
187188
})
188-
c.server.RegisterQuerier("/challenges", func(ctx *fiber.Ctx) error {
189-
offset := ctx.Query("offset", "")
190-
limit := ctx.QueryInt("limit", 10)
189+
c.server.RegisterQuerier("/challenges", func(fiberCtx *fiber.Ctx) error {
190+
offset := fiberCtx.Query("offset", "")
191+
limit := fiberCtx.QueryInt("limit", 10)
191192
if limit > 100 {
192193
limit = 100
193194
}
@@ -198,42 +199,46 @@ func (c *Challenger) RegisterQuerier() {
198199
}
199200

200201
descOrder := true
201-
orderStr := ctx.Query("order", "desc")
202+
orderStr := fiberCtx.Query("order", "desc")
202203
if orderStr == "asc" {
203204
descOrder = false
204205
}
205206

206207
res, err := c.QueryChallenges(offset, ulimit, descOrder)
207208
if err != nil {
208-
return err
209+
ctx.Logger().Error("failed to query challenges", zap.Error(err))
210+
return errors.New("failed to query challenges")
209211
}
210-
return ctx.JSON(res)
212+
return fiberCtx.JSON(res)
211213
})
212214

213-
c.server.RegisterQuerier("/pending_events/host", func(ctx *fiber.Ctx) error {
215+
c.server.RegisterQuerier("/pending_events/host", func(fiberCtx *fiber.Ctx) error {
214216
pendingEvents, err := c.host.GetAllPendingEvents()
215217
if err != nil {
216-
return err
218+
ctx.Logger().Error("failed to query pending events", zap.Error(err))
219+
return errors.New("failed to query pending events")
217220
}
218-
return ctx.JSON(pendingEvents)
221+
return fiberCtx.JSON(pendingEvents)
219222
})
220223

221-
c.server.RegisterQuerier("/pending_events/child", func(ctx *fiber.Ctx) error {
224+
c.server.RegisterQuerier("/pending_events/child", func(fiberCtx *fiber.Ctx) error {
222225
pendingEvents, err := c.child.GetAllPendingEvents()
223226
if err != nil {
224-
return err
227+
ctx.Logger().Error("failed to query pending events", zap.Error(err))
228+
return errors.New("failed to query pending events")
225229
}
226-
return ctx.JSON(pendingEvents)
230+
return fiberCtx.JSON(pendingEvents)
227231
})
228232

229-
c.server.RegisterQuerier("/syncing", func(ctx *fiber.Ctx) error {
233+
c.server.RegisterQuerier("/syncing", func(fiberCtx *fiber.Ctx) error {
230234
status, err := c.GetStatus()
231235
if err != nil {
232-
return err
236+
ctx.Logger().Error("failed to query status", zap.Error(err))
237+
return errors.New("failed to query status")
233238
}
234239
hostSync := status.Host.Node.Syncing != nil && *status.Host.Node.Syncing
235240
childSync := status.Child.Node.Syncing != nil && *status.Child.Node.Syncing
236-
return ctx.JSON(hostSync || childSync)
241+
return fiberCtx.JSON(hostSync || childSync)
237242
})
238243
}
239244

e2e/opbot.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import (
1414
"github.com/docker/docker/client"
1515
"go.uber.org/zap"
1616

17-
sdk "github.com/cosmos/cosmos-sdk/types"
1817
"github.com/initia-labs/opinit-bots/executor"
1918
executortypes "github.com/initia-labs/opinit-bots/executor/types"
2019
"github.com/strangelove-ventures/interchaintest/v8/ibc"
2120

21+
sdk "github.com/cosmos/cosmos-sdk/types"
22+
2223
"github.com/pkg/errors"
2324
)
2425

executor/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ type QueryWithdrawalResponse struct {
242242
LastBlockHash []byte `json:"last_block_hash"`
243243

244244
// extra info
245-
TxTime int64 `json:"tx_time"`
246-
TxHeight int64 `json:"tx_height"`
247-
TxHash string `json:"tx_hash"`
245+
TxTime time.Time `json:"tx_time"`
246+
TxHeight int64 `json:"tx_height"`
247+
TxHash string `json:"tx_hash"`
248248
}
249249
```
250250

@@ -433,9 +433,9 @@ type QueryWithdrawalResponse struct {
433433
LastBlockHash []byte `json:"last_block_hash"`
434434

435435
// extra info
436-
TxTime int64 `json:"tx_time"`
437-
TxHeight int64 `json:"tx_height"`
438-
TxHash string `json:"tx_hash"`
436+
TxTime time.Time `json:"tx_time"`
437+
TxHeight int64 `json:"tx_height"`
438+
TxHash string `json:"tx_hash"`
439439
}
440440
```
441441

executor/batchsubmitter/data_conversion_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ import (
1717
"github.com/stretchr/testify/require"
1818
"go.uber.org/zap"
1919

20+
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
21+
ibctmlightclients "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
22+
2023
"github.com/cosmos/cosmos-sdk/client/tx"
2124
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
2225
sdk "github.com/cosmos/cosmos-sdk/types"
2326
"github.com/cosmos/cosmos-sdk/x/authz"
24-
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
25-
ibctmlightclients "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
2627
)
2728

2829
func TestEmptyOracleData(t *testing.T) {

executor/child/query.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package child
22

33
import (
4+
"time"
5+
46
"cosmossdk.io/math"
57

68
"github.com/pkg/errors"
@@ -28,7 +30,7 @@ func (ch Child) QueryWithdrawal(sequence uint64) (executortypes.QueryWithdrawalR
2830
Amount: amount,
2931
Version: []byte{ch.Version()},
3032
TxHeight: withdrawal.TxHeight,
31-
TxTime: withdrawal.TxTime,
33+
TxTime: time.Unix(0, withdrawal.TxTime),
3234
TxHash: withdrawal.TxHash,
3335
}
3436

executor/child/query_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"testing"
7+
"time"
78

89
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
910
"github.com/initia-labs/opinit-bots/db"
@@ -168,7 +169,7 @@ func TestQueryWithdrawal(t *testing.T) {
168169
Version: []byte{0},
169170
StorageRoot: []byte("000000000000000000000000hash1234"),
170171
LastBlockHash: []byte("00000000000000000000000blockid10"),
171-
TxTime: 1,
172+
TxTime: time.Unix(0, 1),
172173
TxHeight: 1,
173174
TxHash: "txHash1",
174175
},
@@ -192,7 +193,7 @@ func TestQueryWithdrawal(t *testing.T) {
192193
Version: []byte{0},
193194
StorageRoot: []byte("00000000000000000000hash56789999"),
194195
LastBlockHash: []byte("0000000000000000000000blockid100"),
195-
TxTime: 5,
196+
TxTime: time.Unix(0, 5),
196197
TxHeight: 5,
197198
TxHash: "txHash5",
198199
},
@@ -222,7 +223,7 @@ func TestQueryWithdrawal(t *testing.T) {
222223
Version: []byte{0},
223224
StorageRoot: []byte("00000000000000000000hash56789999"),
224225
LastBlockHash: []byte("0000000000000000000000blockid100"),
225-
TxTime: 7,
226+
TxTime: time.Unix(0, 7),
226227
TxHeight: 7,
227228
TxHash: "txHash7",
228229
},
@@ -242,7 +243,7 @@ func TestQueryWithdrawal(t *testing.T) {
242243
Version: []byte{0},
243244
StorageRoot: nil,
244245
LastBlockHash: nil,
245-
TxTime: 11,
246+
TxTime: time.Unix(0, 11),
246247
TxHeight: 11,
247248
TxHash: "txHash11",
248249
},
@@ -320,7 +321,7 @@ func TestQueryWithdrawals(t *testing.T) {
320321
Version: []byte{0},
321322
StorageRoot: []byte("000000000000000000000000hash1234"),
322323
LastBlockHash: []byte("00000000000000000000000blockid10"),
323-
TxTime: 1,
324+
TxTime: time.Unix(0, 1),
324325
TxHeight: 1,
325326
TxHash: "txHash1",
326327
},
@@ -338,7 +339,7 @@ func TestQueryWithdrawals(t *testing.T) {
338339
Version: []byte{0},
339340
StorageRoot: []byte("000000000000000000000000hash1234"),
340341
LastBlockHash: []byte("00000000000000000000000blockid10"),
341-
TxTime: 2,
342+
TxTime: time.Unix(0, 2),
342343
TxHeight: 2,
343344
TxHash: "txHash2",
344345
},
@@ -356,7 +357,7 @@ func TestQueryWithdrawals(t *testing.T) {
356357
Version: []byte{0},
357358
StorageRoot: []byte("000000000000000000000000hash1234"),
358359
LastBlockHash: []byte("00000000000000000000000blockid10"),
359-
TxTime: 3,
360+
TxTime: time.Unix(0, 3),
360361
TxHeight: 3,
361362
TxHash: "txHash3",
362363
},
@@ -399,7 +400,7 @@ func TestQueryWithdrawals(t *testing.T) {
399400
Version: []byte{0},
400401
StorageRoot: []byte("000000000000000000000000hash1234"),
401402
LastBlockHash: []byte("00000000000000000000000blockid10"),
402-
TxTime: 1,
403+
TxTime: time.Unix(0, 1),
403404
TxHeight: 1,
404405
TxHash: "txHash1",
405406
},
@@ -426,7 +427,7 @@ func TestQueryWithdrawals(t *testing.T) {
426427
Version: []byte{0},
427428
StorageRoot: nil,
428429
LastBlockHash: nil,
429-
TxTime: 11,
430+
TxTime: time.Unix(0, 11),
430431
TxHeight: 11,
431432
TxHash: "txHash11",
432433
},
@@ -443,7 +444,7 @@ func TestQueryWithdrawals(t *testing.T) {
443444
Version: []byte{0},
444445
StorageRoot: []byte("000000000000000000000000hash1010"),
445446
LastBlockHash: []byte("000000000000000000000blockid1000"),
446-
TxTime: 10,
447+
TxTime: time.Unix(0, 10),
447448
TxHeight: 10,
448449
TxHash: "txHash10",
449450
},
@@ -462,7 +463,7 @@ func TestQueryWithdrawals(t *testing.T) {
462463
Version: []byte{0},
463464
StorageRoot: []byte("00000000000000000000hash56789999"),
464465
LastBlockHash: []byte("0000000000000000000000blockid100"),
465-
TxTime: 9,
466+
TxTime: time.Unix(0, 9),
466467
TxHeight: 9,
467468
TxHash: "txHash9",
468469
},
@@ -481,7 +482,7 @@ func TestQueryWithdrawals(t *testing.T) {
481482
Version: []byte{0},
482483
StorageRoot: []byte("00000000000000000000hash56789999"),
483484
LastBlockHash: []byte("0000000000000000000000blockid100"),
484-
TxTime: 8,
485+
TxTime: time.Unix(0, 8),
485486
TxHeight: 8,
486487
TxHash: "txHash8",
487488
},
@@ -500,7 +501,7 @@ func TestQueryWithdrawals(t *testing.T) {
500501
Version: []byte{0},
501502
StorageRoot: []byte("00000000000000000000hash56789999"),
502503
LastBlockHash: []byte("0000000000000000000000blockid100"),
503-
TxTime: 7,
504+
TxTime: time.Unix(0, 7),
504505
TxHeight: 7,
505506
TxHash: "txHash7",
506507
},

executor/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func Migration0110(db types.DB) error {
271271

272272
func Migration0111(db types.DB) error {
273273
childDB := db.WithPrefix([]byte(types.ChildName))
274-
merkleDB := db.WithPrefix([]byte(types.MerkleName))
274+
merkleDB := childDB.WithPrefix([]byte(types.MerkleName))
275275

276276
err := merkleDB.Iterate(nil, nil, func(key, value []byte) (bool, error) {
277277
err := childDB.Set(key, value)

executor/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (ex *Executor) Initialize(ctx types.Context) error {
108108
return errors.Wrap(err, "failed to make DA node")
109109
}
110110
ex.batchSubmitter.SetDANode(da)
111-
ex.RegisterQuerier()
111+
ex.RegisterQuerier(ctx)
112112
return nil
113113
}
114114

executor/querier.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"github.com/gofiber/fiber/v2"
77
"github.com/initia-labs/opinit-bots/types"
88
"github.com/pkg/errors"
9+
"go.uber.org/zap"
910
)
1011

11-
func (ex *Executor) RegisterQuerier() {
12+
func (ex *Executor) RegisterQuerier(ctx types.Context) {
1213
ex.server.RegisterQuerier("/withdrawal/:sequence", func(c *fiber.Ctx) error {
1314
sequenceStr := c.Params("sequence")
1415
if sequenceStr == "" {
@@ -20,7 +21,8 @@ func (ex *Executor) RegisterQuerier() {
2021
}
2122
res, err := ex.child.QueryWithdrawal(sequence)
2223
if err != nil {
23-
return err
24+
ctx.Logger().Error("failed to query withdrawal", zap.Error(err))
25+
return errors.New("failed to query withdrawal")
2426
}
2527
return c.JSON(res)
2628
})
@@ -54,22 +56,25 @@ func (ex *Executor) RegisterQuerier() {
5456
}
5557
res, err := ex.child.QueryWithdrawals(address, uoffset, ulimit, descOrder)
5658
if err != nil {
57-
return err
59+
ctx.Logger().Error("failed to query withdrawals", zap.Error(err))
60+
return errors.New("failed to query withdrawals")
5861
}
5962
return c.JSON(res)
6063
})
6164

6265
ex.server.RegisterQuerier("/status", func(c *fiber.Ctx) error {
6366
status, err := ex.GetStatus()
6467
if err != nil {
65-
return err
68+
ctx.Logger().Error("failed to query status", zap.Error(err))
69+
return errors.New("failed to query status")
6670
}
6771
return c.JSON(status)
6872
})
6973
ex.server.RegisterQuerier("/syncing", func(c *fiber.Ctx) error {
7074
status, err := ex.GetStatus()
7175
if err != nil {
72-
return err
76+
ctx.Logger().Error("failed to query status", zap.Error(err))
77+
return errors.New("failed to query status")
7378
}
7479
hostSync := status.Host.Node.Syncing != nil && *status.Host.Node.Syncing
7580
childSync := status.Child.Node.Syncing != nil && *status.Child.Node.Syncing

executor/types/query.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package types
22

3-
import "github.com/cosmos/cosmos-sdk/types"
3+
import (
4+
"time"
5+
6+
"github.com/cosmos/cosmos-sdk/types"
7+
)
48

59
type QueryWithdrawalResponse struct {
610
// fields required to withdraw funds
@@ -16,9 +20,9 @@ type QueryWithdrawalResponse struct {
1620
LastBlockHash []byte `json:"last_block_hash"`
1721

1822
// extra info
19-
TxTime int64 `json:"tx_time"`
20-
TxHeight int64 `json:"tx_height"`
21-
TxHash string `json:"tx_hash"`
23+
TxTime time.Time `json:"tx_time"`
24+
TxHeight int64 `json:"tx_height"`
25+
TxHash string `json:"tx_hash"`
2226
}
2327

2428
type QueryWithdrawalsResponse struct {

provider/child/parse_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"testing"
66

77
abcitypes "github.com/cometbft/cometbft/abci/types"
8-
sdk "github.com/cosmos/cosmos-sdk/types"
98
"github.com/stretchr/testify/require"
9+
10+
sdk "github.com/cosmos/cosmos-sdk/types"
1011
)
1112

1213
func TestParseDepositEvents(t *testing.T) {

0 commit comments

Comments
 (0)