Skip to content

Commit

Permalink
feat: refactor (#55)
Browse files Browse the repository at this point in the history
* refactor node process

* refactor node, broadcaster

* refactor provider

* refactor merkle, types

* refactor db

* refactor executor

* add migration function

* change iterate name & append splitter to key when iterating

* add LastProcessedBlockHeightKey migration

* only change key if it exists

* wrap errors to clarify them

* wrap errors on key, provider

* wrap errors & merge main

* makes log as production & add log-format flag

* fix reverse iterate finding prev inclusive start key

* db test

* db stage test

* save finalized tree when it exists

* merge cmd

* merkle test

* merkle types test

* host deposit & txhandler test

* host handler test

* keys test

* txutil test

* child deposit oracle test

* refactor child db and add l2 sequence checker into handleInitiateWithdrawal and test

* prepare tree, output test

* add version to working tree info & child withdrawal test

* child handle output test

* child db handler query test

* executor types test

* format

* delete oracle msg data from authz msg & oracle test

* update batch info test

* batch test

* add timestamp & tx hash to withdrawals (#54)

* add timestamp & tx hash to withdrawals

* fix test and lint

* delete last block height

---------

Co-authored-by: beer-1 <[email protected]>

* format

* add batch handler test & fix checkbatch test

* add some comments

* feat: make stage copiable and introduce allow to customize prefix of db (#56)

* make stage copiable and introduce allow to customize prefix of db

* add UTC to all time.Unix interface

* Update merkle/types/db.go comment

Co-authored-by: beer-1 <[email protected]>

* simplify delete tree logic

* Update merkle/merkle.go comment

Co-authored-by: beer-1 <[email protected]>

* Update merkle/types/db.go comment

Co-authored-by: beer-1 <[email protected]>

* Update merkle/db.go

Co-authored-by: beer-1 <[email protected]>

* Update merkle/types/db.go

Co-authored-by: beer-1 <[email protected]>

* withdrawal prefix migration

* iterate from sequence using start key

* use utc time

* change ctx name

* v0.1.11 migration command

* save finalized tree with its value method

* coderabbit loop syntax change suggestion

* block handler length verification

* format

* check if tx is inserted to block during waiting time

* skip calculating processed heights when nodes have synced height info

* get height comments

* query block time with block header

* Update node/tx_checker.go

Co-authored-by: beer-1 <[email protected]>

* Update node/tx_checker.go

Co-authored-by: beer-1 <[email protected]>

* Update challenger/child/withdraw.go

Co-authored-by: beer-1 <[email protected]>

* iterate future challenges from initial block time as start key

* processedEvents typo

* Update challenger/handler.go

Co-authored-by: beer-1 <[email protected]>

* let challenge query use next key

* reuse empty batch file function

* fix comment

* Update node/broadcaster/broadcaster.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* fix typo

* format

* Update challenger/query.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* query refactor

* Update challenger/query.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update challenger/query.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* error wrapping

---------

Co-authored-by: beer-1 <[email protected]>
Co-authored-by: beer-1 <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Dec 10, 2024
1 parent 1765f4b commit b5d3fd3
Show file tree
Hide file tree
Showing 161 changed files with 10,985 additions and 2,699 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ linters:
- dogsled
- dupl
- errcheck
- exportloopref
- copyloopvar
- goconst
- gofmt
- goimports
Expand All @@ -23,12 +23,12 @@ linters:
- typecheck
- unconvert
- unused

issues:
exclude-rules:
- path: _test\.go
linters:
- gosec
- unused
- linters:
- stylecheck
text: "ST1003:"
Expand Down
23 changes: 7 additions & 16 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ package bot
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"

"go.uber.org/zap"

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

func LoadJsonConfig(path string, config bottypes.Config) error {
Expand All @@ -35,36 +33,29 @@ func LoadJsonConfig(path string, config bottypes.Config) error {
return nil
}

func NewBot(botType bottypes.BotType, logger *zap.Logger, homePath string, configPath string) (bottypes.Bot, error) {
func NewBot(botType bottypes.BotType, db types.DB, configPath string) (bottypes.Bot, error) {
err := botType.Validate()
if err != nil {
return nil, err
}

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

switch botType {
case bottypes.BotTypeExecutor:
cfg := &executortypes.Config{}
err := LoadJsonConfig(configPath, cfg)
if err != nil {
return nil, err
}
return executor.NewExecutor(cfg, db, logger.Named("executor"), homePath), nil
server := server.NewServer(cfg.Server)
return executor.NewExecutor(cfg, db, server), nil
case bottypes.BotTypeChallenger:
cfg := &challengertypes.Config{}
err := LoadJsonConfig(configPath, cfg)
if err != nil {
return nil, err
}
return challenger.NewChallenger(cfg, db, logger.Named("challenger"), homePath), nil
server := server.NewServer(cfg.Server)
return challenger.NewChallenger(cfg, db, server), nil
}
return nil, errors.New("not providing bot name")
}

func GetDBPath(homePath string, botName bottypes.BotType) string {
return fmt.Sprintf(homePath+"/%s.db", botName)
}
7 changes: 3 additions & 4 deletions bot/types/bot.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package types

import (
"context"
"github.com/initia-labs/opinit-bots/types"
)

type Bot interface {
Initialize(context.Context) error
Start(context.Context) error
Close()
Initialize(types.Context) error
Start(types.Context) error
}
93 changes: 49 additions & 44 deletions challenger/challenger.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package challenger

import (
"context"
"strconv"
"sync"
"time"

"github.com/pkg/errors"

"github.com/gofiber/fiber/v2"
"github.com/initia-labs/opinit-bots/challenger/child"
challengerdb "github.com/initia-labs/opinit-bots/challenger/db"
"github.com/initia-labs/opinit-bots/challenger/host"
"github.com/initia-labs/opinit-bots/server"

Expand All @@ -32,9 +31,6 @@ type Challenger struct {
cfg *challengertypes.Config
db types.DB
server *server.Server
logger *zap.Logger

homePath string

challengeCh chan challengertypes.Challenge
challengeChStopped chan struct{}
Expand All @@ -44,9 +40,11 @@ type Challenger struct {
// status info
latestChallengesMu *sync.Mutex
latestChallenges []challengertypes.Challenge

stage types.CommitDB
}

func NewChallenger(cfg *challengertypes.Config, db types.DB, logger *zap.Logger, homePath string) *Challenger {
func NewChallenger(cfg *challengertypes.Config, db types.DB, sv *server.Server) *Challenger {
err := cfg.Validate()
if err != nil {
panic(err)
Expand All @@ -55,22 +53,17 @@ func NewChallenger(cfg *challengertypes.Config, db types.DB, logger *zap.Logger,
challengeCh := make(chan challengertypes.Challenge)
return &Challenger{
host: host.NewHostV1(
cfg.L1NodeConfig(homePath),
cfg.L1NodeConfig(),
db.WithPrefix([]byte(types.HostName)),
logger.Named(types.HostName),
),
child: child.NewChildV1(
cfg.L2NodeConfig(homePath),
cfg.L2NodeConfig(),
db.WithPrefix([]byte(types.ChildName)),
logger.Named(types.ChildName),
),

cfg: cfg,
db: db,
server: server.NewServer(cfg.Server),
logger: logger,

homePath: homePath,
server: sv,

challengeCh: challengeCh,
challengeChStopped: make(chan struct{}),
Expand All @@ -79,10 +72,12 @@ func NewChallenger(cfg *challengertypes.Config, db types.DB, logger *zap.Logger,

latestChallengesMu: &sync.Mutex{},
latestChallenges: make([]challengertypes.Challenge, 0),

stage: db.NewStage(),
}
}

func (c *Challenger) Initialize(ctx context.Context) error {
func (c *Challenger) Initialize(ctx types.Context) error {
childBridgeInfo, err := c.child.QueryBridgeInfo(ctx)
if err != nil {
return err
Expand All @@ -96,7 +91,7 @@ func (c *Challenger) Initialize(ctx context.Context) error {
return err
}

c.logger.Info(
ctx.Logger().Info(
"bridge info",
zap.Uint64("id", bridgeInfo.BridgeId),
zap.Duration("submission_interval", bridgeInfo.BridgeConfig.SubmissionInterval),
Expand Down Expand Up @@ -128,64 +123,57 @@ func (c *Challenger) Initialize(ctx context.Context) error {
if !initialBlockTime.IsZero() {
// The db state is reset to a specific height, so we also
// need to delete future challenges which are not applicable anymore.
err := c.DeleteFutureChallenges(initialBlockTime)
err := challengerdb.DeleteFutureChallenges(c.db, initialBlockTime)
if err != nil {
return err
}
}

c.RegisterQuerier()

c.pendingChallenges, err = c.loadPendingChallenges()
c.pendingChallenges, err = challengerdb.LoadPendingChallenges(c.db)
if err != nil {
return err
return errors.Wrap(err, "failed to load pending challenges")
}

c.latestChallenges, err = c.loadChallenges()
c.latestChallenges, err = challengerdb.LoadChallenges(c.db)
if err != nil {
return err
return errors.Wrap(err, "failed to load challenges")
}

return nil
}

func (c *Challenger) Start(ctx context.Context) error {
defer c.Close()

errGrp := types.ErrGrp(ctx)
errGrp.Go(func() (err error) {
func (c *Challenger) Start(ctx types.Context) error {
ctx.ErrGrp().Go(func() (err error) {
<-ctx.Done()
return c.server.Shutdown()
})

errGrp.Go(func() (err error) {
ctx.ErrGrp().Go(func() (err error) {
defer func() {
c.logger.Info("api server stopped")
ctx.Logger().Info("api server stopped")
}()
return c.server.Start()
})

errGrp.Go(func() error {
ctx.ErrGrp().Go(func() error {
for _, ch := range c.pendingChallenges {
c.challengeCh <- ch
}
return nil
})

errGrp.Go(func() (err error) {
ctx.ErrGrp().Go(func() (err error) {
defer func() {
c.logger.Info("challenge handler stopped")
ctx.Logger().Info("challenge handler stopped")
}()
return c.challengeHandler(ctx)
})

c.host.Start(ctx)
c.child.Start(ctx)
return errGrp.Wait()
}

func (c *Challenger) Close() {
c.db.Close()
return ctx.ErrGrp().Wait()
}

func (c *Challenger) RegisterQuerier() {
Expand All @@ -197,16 +185,25 @@ func (c *Challenger) RegisterQuerier() {

return ctx.JSON(status)
})
c.server.RegisterQuerier("/challenges/:page", func(ctx *fiber.Ctx) error {
pageStr := ctx.Params("page")
if pageStr == "" {
pageStr = "1"
c.server.RegisterQuerier("/challenges", func(ctx *fiber.Ctx) error {
next := ctx.Query("next", "")
limit := ctx.QueryInt("limit", 10)
if limit > 100 {
limit = 100
}
page, err := strconv.ParseUint(pageStr, 10, 64)

ulimit, err := types.SafeInt64ToUint64(int64(limit))
if err != nil {
return err
return errors.Wrap(err, "failed to convert limit")
}
res, err := c.QueryChallenges(page)

descOrder := true
orderStr := ctx.Query("order", "desc")
if orderStr == "asc" {
descOrder = false
}

res, err := c.QueryChallenges(next, ulimit, descOrder)
if err != nil {
return err
}
Expand All @@ -230,7 +227,11 @@ func (c *Challenger) RegisterQuerier() {
})
}

func (c *Challenger) getProcessedHeights(ctx context.Context, bridgeId uint64) (l1ProcessedHeight int64, l2ProcessedHeight int64, processedOutputIndex uint64, err error) {
func (c *Challenger) getProcessedHeights(ctx types.Context, bridgeId uint64) (l1ProcessedHeight int64, l2ProcessedHeight int64, processedOutputIndex uint64, err error) {
if c.host.Node().GetSyncedHeight() != 0 && c.child.Node().GetSyncedHeight() != 0 {
return 0, 0, 0, nil
}

var outputL1BlockNumber int64
// get the last submitted output height before the start height from the host
if c.cfg.L2StartHeight != 0 {
Expand Down Expand Up @@ -284,3 +285,7 @@ func (c *Challenger) getProcessedHeights(ctx context.Context, bridgeId uint64) (

return l1ProcessedHeight, l2ProcessedHeight, processedOutputIndex, err
}

func (c Challenger) DB() types.DB {
return c.db
}
Loading

0 comments on commit b5d3fd3

Please sign in to comment.