Skip to content

Commit

Permalink
add outputblock for claim data
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouop0 committed Jul 7, 2024
1 parent 6f91f15 commit a40c457
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions deploy/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ CREATE TABLE IF NOT EXISTS game_claim_data
`claim` varchar(64) NOT NULL,
`position` bigint NOT NULL,
`clock` bigint NOT NULL,
`output_block` bigint NOT NULL,
PRIMARY KEY (`id`),
KEY `credit_index` (`game_contract`, `data_index`)
);
Expand Down
1 change: 1 addition & 0 deletions docs/sql/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ CREATE TABLE IF NOT EXISTS game_claim_data
`claim` varchar(64) NOT NULL,
`position` bigint NOT NULL,
`clock` bigint NOT NULL,
`output_block` bigint NOT NULL,
PRIMARY KEY (`id`),
KEY `credit_index` (`game_contract`, `data_index`)
);
Expand Down
3 changes: 2 additions & 1 deletion docs/sql/postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ CREATE TABLE IF NOT EXISTS game_claim_data
bond bigint NOT NULL,
claim varchar(64) NOT NULL,
position bigint NOT NULL,
clock bigint NOT NULL
clock bigint NOT NULL,
output_block bigint NOT NUll,
);
CREATE INDEX if not exists dispute_game_data_index ON game_claim_data (game_contract, data_index);

Expand Down
37 changes: 36 additions & 1 deletion internal/handler/disputeGame.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"math/big"
"strings"

"github.com/ethereum-optimism/optimism/op-challenger/game/fault/types"

"github.com/pkg/errors"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -62,9 +64,28 @@ func (r *RetryDisputeGameClient) ProcessDisputeGameMove(ctx context.Context, evt
index := disputeGameMove.ParentIndex.Add(disputeGameMove.ParentIndex, big.NewInt(1))
data, err := r.Client.RetryClaimData(ctx, &bind.CallOpts{}, index)
if err != nil {
return fmt.Errorf("[processDisputeGameMove] contract: %s, index: %d move event get claim data err: %s", evt.ContractAddress, index, err)
return fmt.Errorf("[processDisputeGameMove] contract: %s, index: %d move event get claim data err: %s", evt.ContractAddress, index, errors.WithStack(err))
}

pos := types.NewPositionFromGIndex(data.Position)
splitDepth, err := r.Client.RetrySplitDepth(ctx, &bind.CallOpts{})
if err != nil {
return fmt.Errorf("[processDisputeGameMove] contract: %s, get splitDepth error: %s", evt.ContractAddress, errors.WithStack(err))
}
splitDepths := types.Depth(splitDepth.Uint64())
poststateBlock, err := r.Client.RetryL2BlockNumber(ctx, &bind.CallOpts{})
if err != nil {
return fmt.Errorf("[processDisputeGameMove] GET game poststateBlock err: %s", err)
}

prestateBlock, err := r.Client.RetryStartingBlockNumber(ctx, &bind.CallOpts{})
if err != nil {
return fmt.Errorf("[processDisputeGameMove] GET prestateBlock err: %s", err)
}
outputblock, err := claimedBlockNumber(pos, splitDepths, prestateBlock.Uint64(), poststateBlock.Uint64())
if err != nil {
return fmt.Errorf("[processDisputeGameMove] GET outputblock err: %s", err)
}
claimData := &schema.GameClaimData{
GameContract: evt.ContractAddress,
DataIndex: index.Int64(),
Expand All @@ -75,6 +96,7 @@ func (r *RetryDisputeGameClient) ProcessDisputeGameMove(ctx context.Context, evt
Claim: hex.EncodeToString(data.Claim[:]),
Position: data.Position.Uint64(),
Clock: data.Clock.Int64(),
OutputBlock: outputblock,
}
err = r.DB.Transaction(func(tx *gorm.DB) error {
err = tx.Save(claimData).Error
Expand Down Expand Up @@ -152,6 +174,7 @@ func (r *RetryDisputeGameClient) addDisputeGame(ctx context.Context, evt *schema
Claim: hex.EncodeToString(claimData.Claim[:]),
Position: claimData.Position.Uint64(),
Clock: claimData.Clock.Int64(),
OutputBlock: l2Block.Uint64(),
}

game := &schema.DisputeGame{
Expand Down Expand Up @@ -263,3 +286,15 @@ func (r *RetryDisputeGameClient) GetAllAddress(disputeGame *common.Address) (map
}
return addresses, nil
}

func claimedBlockNumber(pos types.Position, gameDepth types.Depth, prestateBlock, poststateBlock uint64) (uint64, error) {
traceIndex := pos.TraceIndex(gameDepth)
if !traceIndex.IsUint64() {
return 0, fmt.Errorf("trace index is greater than max uint64: %v", traceIndex)
}
outputBlock := traceIndex.Uint64() + prestateBlock + 1
if outputBlock > poststateBlock {
outputBlock = poststateBlock
}
return outputBlock, nil
}
1 change: 1 addition & 0 deletions internal/schema/game_claim_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type GameClaimData struct {
Claim string `json:"claim"`
Position uint64 `json:"position"`
Clock int64 `json:"clock"`
OutputBlock uint64 `json:"output_block"`
}

func (GameClaimData) TableName() string {
Expand Down
40 changes: 40 additions & 0 deletions pkg/contract/rate_retry_dispute_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,43 @@ func (s *RateAndRetryDisputeGameClient) credit(ctx context.Context, opts *bind.C
opts.Context = cCtx
return s.disputeGame.Credit(opts, address)
}

func (s *RateAndRetryDisputeGameClient) RetrySplitDepth(ctx context.Context, opts *bind.CallOpts) (*big.Int, error) {
return retry.Do(ctx, maxAttempts, s.strategy, func() (*big.Int, error) {
res, err := s.splitDepth(ctx, opts)
if err != nil {
log.Errorf("Failed to splitDepth info %s", err)
}
return res, err
})
}

func (s *RateAndRetryDisputeGameClient) splitDepth(ctx context.Context, opts *bind.CallOpts) (*big.Int, error) {
if err := s.rl.Wait(ctx); err != nil {
return nil, err
}
cCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
opts.Context = cCtx
return s.disputeGame.SplitDepth(opts)
}

func (s *RateAndRetryDisputeGameClient) RetryStartingBlockNumber(ctx context.Context, opts *bind.CallOpts) (*big.Int, error) {
return retry.Do(ctx, maxAttempts, s.strategy, func() (*big.Int, error) {
res, err := s.startingBlockNumber(ctx, opts)
if err != nil {
log.Errorf("Failed to splitDepth info %s", err)
}
return res, err
})
}

func (s *RateAndRetryDisputeGameClient) startingBlockNumber(ctx context.Context, opts *bind.CallOpts) (*big.Int, error) {
if err := s.rl.Wait(ctx); err != nil {
return nil, err
}
cCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
opts.Context = cCtx
return s.disputeGame.StartingBlockNumber(opts)
}

0 comments on commit a40c457

Please sign in to comment.