Skip to content

Commit

Permalink
ST-390: Fix for invalid denom for blocks<v12 update (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoThe1K authored Aug 15, 2024
1 parent 5721432 commit 6b3ab9d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 51 deletions.
9 changes: 1 addition & 8 deletions rpc/backend/evm_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,16 +659,9 @@ func (b *Backend) baseFee(ctx context.Context) (*big.Int, error) {
if err != nil {
return nil, err
}
// try v011
if res.BaseFee == nil {
res, err = b.GetEVMKeeper().BaseFeeV011(ctx, nil)
if err != nil {
return nil, err
}
}

if res.BaseFee == nil {
return nil, nil
return nil, fmt.Errorf("base fee not set")
}

return res.BaseFee.BigInt(), nil
Expand Down
12 changes: 11 additions & 1 deletion rpc/namespaces/ethereum/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,17 @@ func (e *PublicAPI) GetBalance(address common.Address, blockNrOrHash rpctypes.Bl
if err != nil {
return nil, err
}
balance := e.backend.GetEVMKeeper().GetBalance(sdkCtx, address)

req := &evmtypes.QueryBalanceRequest{
Address: address.Hex(),
}

bRes, err := e.backend.GetEVMKeeper().Balance(sdkCtx, req)
if err != nil {
return nil, err
}

balance, _ := new(big.Int).SetString(bRes.Balance, 10)

return (*hexutil.Big)(balance), nil
}
Expand Down
46 changes: 13 additions & 33 deletions x/evm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ func (k Keeper) Balance(c context.Context, req *types.QueryBalanceRequest) (*typ

ctx := sdk.UnwrapSDKContext(c)

balanceInt := k.GetBalance(ctx, common.HexToAddress(req.Address))
balanceInt := k.GetBalanceV011(ctx, common.HexToAddress(req.Address))
if balanceInt == nil {
balanceInt = k.GetBalance(ctx, common.HexToAddress(req.Address))
}

return &types.QueryBalanceResponse{
Balance: balanceInt.String(),
Expand Down Expand Up @@ -608,26 +611,14 @@ func (k *Keeper) traceTx(
func (k Keeper) BaseFee(c context.Context, _ *types.QueryBaseFeeRequest) (*types.QueryBaseFeeResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

params := k.GetParams(ctx)
ethCfg := params.ChainConfig.EthereumConfig()
baseFee := k.GetBaseFee(ctx, ethCfg)

res := &types.QueryBaseFeeResponse{}
if baseFee != nil {
aux := sdkmath.NewIntFromBigInt(baseFee)
res.BaseFee = &aux
// legacy compatible
params := k.GetParamsV011(ctx)
if params.EvmDenom == "" {
params = k.GetParams(ctx)
}

return res, nil
}

// BaseFeeV011 implements the Query/BaseFee gRPC method for old blocks
func (k Keeper) BaseFeeV011(c context.Context, _ *types.QueryBaseFeeRequest) (*types.QueryBaseFeeResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

params := k.GetParamsV011(ctx)
ethCfg := params.ChainConfig.EthereumConfig()
baseFee := k.GetBaseFeeV011(ctx, ethCfg)
baseFee := k.GetBaseFee(ctx, ethCfg)

res := &types.QueryBaseFeeResponse{}
if baseFee != nil {
Expand All @@ -643,22 +634,11 @@ func (k Keeper) BaseFeeParam(c context.Context, _ *types.QueryBaseFeeRequest) (*
ctx := sdk.UnwrapSDKContext(c)

res := &types.QueryBaseFeeResponse{}
baseFee := k.GetBaseFeeParam(ctx)

if baseFee != nil {
aux := sdkmath.NewIntFromBigInt(baseFee)
res.BaseFee = &aux
}

return res, nil
}

// BaseFeeParamV011 implements the Query/BaseFeeParam gRPC method for old blocks
func (k Keeper) BaseFeeParamV011(c context.Context, _ *types.QueryBaseFeeRequest) (*types.QueryBaseFeeResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

res := &types.QueryBaseFeeResponse{}
// legacy compatible
baseFee := k.GetBaseFeeParamV011(ctx)
if baseFee == nil {
baseFee = k.GetBaseFeeParam(ctx)
}

if baseFee != nil {
aux := sdkmath.NewIntFromBigInt(baseFee)
Expand Down
18 changes: 9 additions & 9 deletions x/evm/keeper/params_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/common"
"github.com/stratosnet/stratos-chain/x/evm/types"
)

Expand Down Expand Up @@ -36,14 +36,14 @@ func (k Keeper) GetBaseFeeParamV011(ctx sdk.Context) *big.Int {
return params.FeeMarketParams.BaseFee.BigInt()
}

func (k Keeper) GetBaseFeeV011(ctx sdk.Context, ethCfg *params.ChainConfig) *big.Int {
if !types.IsLondon(ethCfg, ctx.BlockHeight()) {
// GetBalanceV011 load account's balance of gas token for th old blocks before migrations
func (k *Keeper) GetBalanceV011(ctx sdk.Context, addr common.Address) *big.Int {
cosmosAddr := sdk.AccAddress(addr.Bytes())
params := k.GetParamsV011(ctx)
// fast check if block already not in v011
if params.EvmDenom == "" {
return nil
}
baseFee := k.GetBaseFeeParamV011(ctx)
if baseFee == nil {
// return 0 if feemarket not enabled.
baseFee = big.NewInt(0)
}
return baseFee
coin := k.bankKeeper.GetBalance(ctx, cosmosAddr, params.EvmDenom)
return coin.Amount.BigInt()
}

0 comments on commit 6b3ab9d

Please sign in to comment.