From e0ec03a4f675a289e576558726f11c48d2524dfa Mon Sep 17 00:00:00 2001 From: Luke Date: Wed, 4 Dec 2024 15:24:21 +0800 Subject: [PATCH] fix(#9): workaround for getting latest block failure --- ethereum/rpc/api/eth.go | 9 +++++++-- ethereum/rpc/backend.go | 16 ++++++++++++---- ethereum/rpc/types/backend.go | 2 ++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/ethereum/rpc/api/eth.go b/ethereum/rpc/api/eth.go index e0fba87..45e46ca 100644 --- a/ethereum/rpc/api/eth.go +++ b/ethereum/rpc/api/eth.go @@ -655,12 +655,17 @@ func SubmitTransaction(ctx context.Context, logger log.Logger, b rpctypes.Trancs return tx.Hash(), nil } - head, err := b.CurrentHeader() + blockNum, err := b.BlockNumber() if err != nil { return common.Hash{}, err } - signer := types.MakeSigner(b.ChainConfig(), head.Number, head.Time) + blockTime, err := b.BlockTimeByNumber(int64(blockNum)) + if err != nil { + return common.Hash{}, err + } + + signer := types.MakeSigner(b.ChainConfig(), big.NewInt(int64(blockNum)), blockTime) from, err := types.Sender(signer, tx) if err != nil { return common.Hash{}, err diff --git a/ethereum/rpc/backend.go b/ethereum/rpc/backend.go index 316d853..316f3bd 100644 --- a/ethereum/rpc/backend.go +++ b/ethereum/rpc/backend.go @@ -181,7 +181,7 @@ func (b *BackendImpl) GetBalance(address common.Address, blockNrOrHash rpc.Block return (*hexutil.Big)(val.BigInt()), nil } -func (b *BackendImpl) ArtBlockByNumber(_ context.Context, number rpc.BlockNumber) (*rpctypes.Block, error) { +func (b *BackendImpl) ArtBlockByNumber(ctx context.Context, number rpc.BlockNumber) (*rpctypes.Block, error) { resBlock, err := b.CosmosBlockByNumber(number) if err != nil || resBlock == nil { return nil, fmt.Errorf("query block failed, block number %d, %w", number, err) @@ -189,6 +189,13 @@ func (b *BackendImpl) ArtBlockByNumber(_ context.Context, number rpc.BlockNumber blockRes, err := b.CosmosBlockResultByNumber(&resBlock.Block.Height) if err != nil { + // TODO remove this. This is a workaround. + // see https://github.com/rollkit/rollkit/issues/1935 + // https://github.com/artela-network/artela-rollkit/issues/9#issuecomment-2513769394 + if number == rpc.LatestBlockNumber && resBlock.Block.Height > 1 { + // try to fetch previous block + return b.ArtBlockByNumber(ctx, rpc.BlockNumber(resBlock.Block.Height-1)) + } return nil, fmt.Errorf("block result not found for height %d", resBlock.Block.Height) } @@ -212,7 +219,7 @@ func (b *BackendImpl) BlockByHash(_ context.Context, hash common.Hash) (*rpctype func (b *BackendImpl) ChainConfig() *params.ChainConfig { cfg, err := b.chainConfig() if err != nil { - return nil + panic(err) } return cfg } @@ -233,12 +240,13 @@ func (b *BackendImpl) chainConfig() (*params.ChainConfig, error) { return nil, err } - currentHeader, err := b.CurrentHeader() + blockNum, err := b.BlockNumber() if err != nil { + b.logger.Info("get BlockNumber failed", err) return nil, err } - return params.Params.ChainConfig.EthereumConfig(currentHeader.Number.Int64(), b.chainID), nil + return params.Params.ChainConfig.EthereumConfig(int64(blockNum), b.chainID), nil } func (b *BackendImpl) ChainDb() ethdb.Database { diff --git a/ethereum/rpc/types/backend.go b/ethereum/rpc/types/backend.go index 85d9648..a107308 100644 --- a/ethereum/rpc/types/backend.go +++ b/ethereum/rpc/types/backend.go @@ -49,6 +49,8 @@ type ( DoCall(args TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash) (*evmtypes.MsgEthereumTxResponse, error) EstimateGas(ctx context.Context, args TransactionArgs, blockNrOrHash *rpc.BlockNumberOrHash) (hexutil.Uint64, error) + BlockNumber() (hexutil.Uint64, error) + BlockTimeByNumber(blockNum int64) (uint64, error) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)