Skip to content

Commit

Permalink
fix(#9): workaround for getting latest block failure
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeNinjaX committed Dec 4, 2024
1 parent 4312d81 commit e0ec03a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
9 changes: 7 additions & 2 deletions ethereum/rpc/api/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions ethereum/rpc/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,21 @@ 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)
}

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)
}

Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions ethereum/rpc/types/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e0ec03a

Please sign in to comment.