@@ -3,18 +3,17 @@ package api
33import (
44 "context"
55 "fmt"
6+ "math"
67 "math/big"
78 "net/http"
89
910 "github.com/ethereum-optimism/optimism/op-challenger/game/fault/types"
10- "github.com/ethereum-optimism/optimism/op-service/eth"
11- "github.com/ethereum-optimism/optimism/op-service/predeploys"
11+ "github.com/ethereum-optimism/optimism/op-service/sources" // 新增导入
1212 "github.com/ethereum/go-ethereum/accounts/abi/bind"
1313 "github.com/ethereum/go-ethereum/common"
1414 "github.com/ethereum/go-ethereum/ethclient"
1515 config "github.com/optimism-java/dispute-explorer/internal/types"
1616 "github.com/optimism-java/dispute-explorer/pkg/contract"
17- "github.com/pkg/errors"
1817
1918 "github.com/spf13/cast"
2019
@@ -25,18 +24,20 @@ import (
2524)
2625
2726type DisputeGameHandler struct {
28- Config * config.Config
29- DB * gorm.DB
30- L1RPC * ethclient.Client
31- L2RPC * ethclient.Client
27+ Config * config.Config
28+ DB * gorm.DB
29+ L1RPC * ethclient.Client
30+ L2RPC * ethclient.Client
31+ RollupClient * sources.RollupClient // 新增字段
3232}
3333
34- func NewDisputeGameHandler (db * gorm.DB , l1rpc * ethclient.Client , l2rpc * ethclient.Client , config * config.Config ) * DisputeGameHandler {
34+ func NewDisputeGameHandler (db * gorm.DB , l1rpc * ethclient.Client , l2rpc * ethclient.Client , config * config.Config , rollupClient * sources. RollupClient ) * DisputeGameHandler {
3535 return & DisputeGameHandler {
36- DB : db ,
37- L1RPC : l1rpc ,
38- L2RPC : l2rpc ,
39- Config : config ,
36+ DB : db ,
37+ L1RPC : l1rpc ,
38+ L2RPC : l2rpc ,
39+ Config : config ,
40+ RollupClient : rollupClient , // 新增赋值
4041 }
4142}
4243
@@ -294,22 +295,15 @@ func (h DisputeGameHandler) GetClaimRoot(c *gin.Context) {
294295}
295296
296297func (h DisputeGameHandler ) getClaimRoot (blockNumber int64 ) (string , error ) {
297- block , err := h .L2RPC .BlockByNumber (context .Background (), big .NewInt (cast .ToInt64 (blockNumber )))
298- if err != nil {
299- return "" , fmt .Errorf ("block number is nil %d" , blockNumber )
298+ if blockNumber < 0 {
299+ return "" , fmt .Errorf ("block number cannot be negative: %d" , blockNumber )
300300 }
301- var getProofResponse * eth.AccountResult
302- err = h .L2RPC .Client ().CallContext (context .Background (), & getProofResponse , "eth_getProof" ,
303- predeploys .L2ToL1MessagePasserAddr , []common.Hash {}, block .Hash ().String ())
301+
302+ output , err := h .RollupClient .OutputAtBlock (context .Background (), uint64 (blockNumber ))
304303 if err != nil {
305- return "" , fmt .Errorf ("call eth_getProof error:%s" , errors .WithStack (err ))
306- }
307- output := & eth.OutputV0 {
308- StateRoot : eth .Bytes32 (block .Root ()),
309- MessagePasserStorageRoot : eth .Bytes32 (getProofResponse .StorageHash ),
310- BlockHash : block .Hash (),
304+ return "" , fmt .Errorf ("failed to get output at block %d: %w" , blockNumber , err )
311305 }
312- return fmt . Sprint ( eth . OutputRoot ( output ) ), nil
306+ return output . OutputRoot . String ( ), nil
313307}
314308
315309type CalculateClaim struct {
@@ -374,7 +368,11 @@ func (h DisputeGameHandler) gamesClaimByPosition(req *CalculateClaim) (string, e
374368 outputBlock = poststateBlock .Uint64 ()
375369 }
376370
377- root , err := h .getClaimRoot (cast .ToInt64 (outputBlock ))
371+ if outputBlock > math .MaxInt64 {
372+ return "" , fmt .Errorf ("output block number too large: %d" , outputBlock )
373+ }
374+
375+ root , err := h .getClaimRoot (int64 (outputBlock ))
378376 if err != nil {
379377 return "" , err
380378 }
0 commit comments