Skip to content

Commit

Permalink
feat:add filter for dispute games, fix:sync process event error
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouop0 committed Sep 22, 2024
1 parent a530417 commit eaeb88b
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 21 deletions.
13 changes: 13 additions & 0 deletions internal/handler/disputeGame.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func (r *RetryDisputeGameClient) ProcessDisputeGameMove(ctx context.Context, evt
return fmt.Errorf("[processDisputeGameMove] contract: %s, index: %d move event get claim data err: %s",
evt.ContractAddress, storageClaimSize, errors.WithStack(err))
}
var game schema.DisputeGame
rest := r.DB.Where("game_contract=?", evt.ContractAddress).First(&game)
if rest.Error != nil {
return fmt.Errorf("[processDisputeGameMove] find game error: %s", errors.WithStack(rest.Error))
}

pos := types.NewPositionFromGIndex(data.Position)
splitDepth, err := r.Client.RetrySplitDepth(ctx, &bind.CallOpts{})
Expand All @@ -94,6 +99,8 @@ func (r *RetryDisputeGameClient) ProcessDisputeGameMove(ctx context.Context, evt
outputblock, _ = claimedBlockNumber(pos, splitDepths, prestateBlock.Uint64(), poststateBlock.Uint64())
}

game.ClaimDataLen = storageClaimSize + 1

claimData := &schema.GameClaimData{
GameContract: evt.ContractAddress,
DataIndex: storageClaimSize,
Expand All @@ -118,6 +125,10 @@ func (r *RetryDisputeGameClient) ProcessDisputeGameMove(ctx context.Context, evt
if err != nil {
return fmt.Errorf("[processDisputeGameMove] save event err: %s\n ", err)
}
err = tx.Save(game).Error
if err != nil {
return fmt.Errorf("[processDisputeGameMove] update game claim len err: %s\n ", err)
}
return nil
})
if err != nil {
Expand Down Expand Up @@ -206,6 +217,8 @@ func (r *RetryDisputeGameClient) addDisputeGame(ctx context.Context, evt *schema
L2BlockNumber: l2Block.Int64(),
Status: schema.DisputeGameStatusInProgress,
OnChainStatus: schema.DisputeGameOnChainStatusValid,
ClaimDataLen: 1,
GetLenStatus: false,
}
err = r.DB.Transaction(func(tx *gorm.DB) error {
err = tx.Save(gameClaim).Error
Expand Down
2 changes: 2 additions & 0 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ func Run(ctx *svc.ServiceContext) {
go SyncCredit(ctx)
// calculate lost bond
go CalculateLostBond(ctx)
// sync claim len
go SyncClaimDataLen(ctx)
}
42 changes: 42 additions & 0 deletions internal/handler/syncClaimDataLen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package handler

import (
"time"

"github.com/optimism-java/dispute-explorer/internal/schema"
"github.com/optimism-java/dispute-explorer/internal/svc"
"github.com/optimism-java/dispute-explorer/pkg/log"
"github.com/pkg/errors"
)

// SyncClaimDataLen Compensation processing historical data
func SyncClaimDataLen(ctx *svc.ServiceContext) {
for {
var disputeGames []schema.DisputeGame
err := ctx.DB.Where("get_len_status = ? and status != ?",
false, schema.DisputeGameStatusInProgress).Order("block_number").Limit(50).Find(&disputeGames).Error
if err != nil {
time.Sleep(3 * time.Second)
continue
}
if len(disputeGames) == 0 {
log.Infof("[Handler.SyncClaimDataLen] Pending games count is 0\n")
time.Sleep(2 * time.Second)
continue
}

for _, disputeGame := range disputeGames {
var claimDataLen int64
ctx.DB.Model(schema.GameClaimData{}).Where("game_contract = ? and on_chain_status = ?",
disputeGame.GameContract, schema.GameClaimDataOnChainStatusValid).Count(&claimDataLen)

disputeGame.ClaimDataLen = claimDataLen
disputeGame.GetLenStatus = true

err := ctx.DB.Save(disputeGame).Error
if err != nil {
log.Errorf("[Handler.SyncClaimDataLen] update claim len err", errors.WithStack(err))
}
}
}
}
32 changes: 13 additions & 19 deletions internal/handler/syncDispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package handler
import (
"fmt"
"strings"
"sync"
"time"

"github.com/optimism-java/dispute-explorer/internal/blockchain"
Expand Down Expand Up @@ -32,27 +31,22 @@ func SyncDispute(ctx *svc.ServiceContext) {
continue
}

var wg sync.WaitGroup
for _, event := range events {
wg.Add(1)
go func(_wg *sync.WaitGroup, ctx *svc.ServiceContext, event schema.SyncEvent) {
defer _wg.Done()
if event.Status == schema.EventPending {
// add events & block.status= valid
err = HandlePendingEvent(ctx, event)
if err != nil {
log.Errorf("[Handler.SyncEvent] HandlePendingBlock err: %s\n", errors.WithStack(err))
}
} else if event.Status == schema.EventRollback {
// event.status=rollback & block.status=invalid
err = HandleRollbackEvent(ctx, event)
if err != nil {
log.Errorf("[Handler.SyncEvent] HandleRollbackBlock err: %s\n", errors.WithStack(err))
}
if event.Status == schema.EventPending {
// add events & block.status= valid
err = HandlePendingEvent(ctx, event)
if err != nil {
log.Errorf("[Handler.SyncEvent] HandlePendingBlock err: %s\n", errors.WithStack(err))
}
}(&wg, ctx, event)
} else if event.Status == schema.EventRollback {
// event.status=rollback & block.status=invalid
err = HandleRollbackEvent(ctx, event)
if err != nil {
log.Errorf("[Handler.SyncEvent] HandleRollbackBlock err: %s\n", errors.WithStack(err))
}
}
}
wg.Wait()
time.Sleep(3 * time.Second)
}
}

Expand Down
2 changes: 2 additions & 0 deletions internal/schema/dispute_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type DisputeGame struct {
Computed bool `json:"computed"`
CalculateLost bool `json:"calculate_lost"`
OnChainStatus string `json:"on_chain_status"`
ClaimDataLen int64 `json:"claim_data_len"`
GetLenStatus bool `json:"get_len_status"`
}

func (DisputeGame) TableName() string {
Expand Down
4 changes: 2 additions & 2 deletions internal/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type Config struct {
L2RPCUrl string `env:"L2_RPC_URL" envDefault:"https://opt-sepolia.g.alchemy.com/v2/RT1mCGRyVMx1F-XlY4Es4Zz-Q8Jrasg6"`
RPCRateLimit int `env:"RPC_RATE_LIMIT" envDefault:"15"`
RPCRateBurst int `env:"RPC_RATE_BURST" envDefault:"5"`
FromBlockNumber int64 `env:"FROM_BLOCK_NUMBER" envDefault:"6670522"`
FromBlockHash string `env:"FROM_BLOCK_HASH" envDefault:"0x7989e1ff25d1fb6e3391f51cba7c0438f487516eabd4a568f6cc778da73c145f"`
FromBlockNumber int64 `env:"FROM_BLOCK_NUMBER" envDefault:"5515562"`
FromBlockHash string `env:"FROM_BLOCK_HASH" envDefault:"0x5205c17557759edaef9120f56af802aeaa2827a60d674a0413e77e9c515bdfba"`
DisputeGameProxyContract string `env:"DISPUTE_GAME_PROXY_CONTRACT" envDefault:"0x05F9613aDB30026FFd634f38e5C4dFd30a197Fa1"`
APIPort string `env:"API_PORT" envDefault:"8088"`
}
Expand Down
2 changes: 2 additions & 0 deletions migration/version/migration_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
v4 "github.com/optimism-java/dispute-explorer/migration/version/v4"
v5 "github.com/optimism-java/dispute-explorer/migration/version/v5"
v6 "github.com/optimism-java/dispute-explorer/migration/version/v6"
v7 "github.com/optimism-java/dispute-explorer/migration/version/v7"
)

var ModelSchemaList = []*gormigrate.Migration{
Expand All @@ -17,4 +18,5 @@ var ModelSchemaList = []*gormigrate.Migration{
&v4.UpdateClaimDataPositionColumnTable,
&v5.AddOnChainStatusForDisputeGameTable,
&v6.UpdateClaimDataClockColumnTable,
&v7.AddClaimDataLenForDisputeGameTable,
}
25 changes: 25 additions & 0 deletions migration/version/v7/add_claim_data_len_for_dispute_game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package v7

import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

var AddClaimDataLenForDisputeGameTable = gormigrate.Migration{
ID: "v7",
Migrate: func(tx *gorm.DB) error {
type DisputeGame struct {
ClaimDataLen int64 `json:"claim_data_len" gorm:"type:bigint;notnull;index:dispute_claim_data_len_index;default:1"`
GetLenStatus bool `json:"get_len_status" gorm:"type:tinyint(1);notnull;default:0"`
}
return tx.Table("dispute_game").AutoMigrate(&DisputeGame{})
},
Rollback: func(db *gorm.DB) error {
err := db.Migrator().DropColumn("dispute_game", "claim_data_len")
if err != nil {
return err
}
err = db.Migrator().DropColumn("dispute_game", "get_len_status")
return err
},
}

0 comments on commit eaeb88b

Please sign in to comment.