Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
restaure event
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-nguy committed Feb 17, 2022
1 parent 1d48659 commit b7e1c3e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 25 deletions.
23 changes: 20 additions & 3 deletions rpc/ethereum/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,16 +968,33 @@ func (e *EVMBackend) BaseFee(height int64) (*big.Int, error) {
}

// Checks the feemarket param NoBaseFee settings, return 0 if it is enabled.
res, err := e.queryClient.FeeMarket.BaseFee(types.ContextWithHeight(height), &feemarkettypes.QueryBaseFeeRequest{})
resParams, err := e.queryClient.FeeMarket.Params(types.ContextWithHeight(height), &feemarkettypes.QueryParamsRequest{})
if err != nil {
return nil, err
}

if res.BaseFee == nil {
if resParams.Params.NoBaseFee {
return big.NewInt(0), nil
}

return res.BaseFee.BigInt(), nil
blockRes, err := e.clientCtx.Client.BlockResults(e.ctx, &height)
if err != nil {
return nil, err
}

baseFee := types.BaseFeeFromEvents(blockRes.BeginBlockEvents)
if baseFee != nil {
return baseFee, nil
}

// If we cannot find in events, we tried to get it from the state.
// It will return feemarket.baseFee if london is activated but feemarket is not enable
res, err := e.queryClient.FeeMarket.BaseFee(types.ContextWithHeight(height), &feemarkettypes.QueryBaseFeeRequest{})
if err == nil && res.BaseFee != nil {
return res.BaseFee.BigInt(), nil
}

return nil, nil
}

// GetEthereumMsgsFromTendermintBlock returns all real MsgEthereumTxs from a Tendermint block.
Expand Down
14 changes: 2 additions & 12 deletions rpc/ethereum/namespaces/eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package filters
import (
"context"
"fmt"
"math/big"
"sync"
"time"

Expand Down Expand Up @@ -37,7 +36,6 @@ type Backend interface {
RPCFilterCap() int32
RPCLogsCap() int32
RPCBlockRangeCap() int32
BaseFee(height int64) (*big.Int, error)
}

// consider a filter inactive if it has not been polled for within deadline
Expand Down Expand Up @@ -260,11 +258,7 @@ func (api *PublicFilterAPI) NewBlockFilter() rpc.ID {
continue
}

baseFee, err := api.backend.BaseFee(data.Header.Height)
if err != nil {
api.logger.Debug("cannot retrieve base fee", "err", fmt.Sprintf("%v", err))
continue
}
baseFee := types.BaseFeeFromEvents(data.ResultBeginBlock.Events)

header := types.EthHeaderFromTendermint(data.Header, ethtypes.Bloom{}, baseFee)
api.filtersMu.Lock()
Expand Down Expand Up @@ -316,11 +310,7 @@ func (api *PublicFilterAPI) NewHeads(ctx context.Context) (*rpc.Subscription, er
continue
}

baseFee, err := api.backend.BaseFee(data.Header.Height)
if err != nil {
api.logger.Debug("cannot retrieve base fee", "err", fmt.Sprintf("%v", err))
continue
}
baseFee := types.BaseFeeFromEvents(data.ResultBeginBlock.Events)

// TODO: fetch bloom from events
header := types.EthHeaderFromTendermint(data.Header, ethtypes.Bloom{}, baseFee)
Expand Down
25 changes: 24 additions & 1 deletion rpc/ethereum/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
"github.com/cosmos/cosmos-sdk/client"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

evmtypes "github.com/tharsis/ethermint/x/evm/types"
feemarkettypes "github.com/tharsis/ethermint/x/feemarket/types"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
ethtypes "github.com/ethereum/go-ethereum/core/types"
evmtypes "github.com/tharsis/ethermint/x/evm/types"
)

// RawTxToEthTx returns a evm MsgEthereum transaction from raw tx bytes.
Expand Down Expand Up @@ -232,6 +234,27 @@ func NewRPCTransaction(
return result, nil
}

// BaseFeeFromEvents parses the feemarket basefee from cosmos events
func BaseFeeFromEvents(events []abci.Event) *big.Int {
for _, event := range events {
if event.Type != feemarkettypes.EventTypeFeeMarket {
continue
}

for _, attr := range event.Attributes {
if bytes.Equal(attr.Key, []byte(feemarkettypes.AttributeKeyBaseFee)) {
result, success := new(big.Int).SetString(string(attr.Value), 10)
if success {
return result
}

return nil
}
}
}
return nil
}

// FindTxAttributes returns the msg index of the eth tx in cosmos tx, and the attributes,
// returns -1 and nil if not found.
func FindTxAttributes(events []abci.Event, txHash string) (int, map[string]string) {
Expand Down
12 changes: 11 additions & 1 deletion x/feemarket/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package keeper
import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tharsis/ethermint/x/feemarket/types"

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

// BeginBlock updates base fee
Expand All @@ -17,6 +19,14 @@ func (k *Keeper) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
}

k.SetBaseFee(ctx, baseFee)

// Store current base fee in event
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeFeeMarket,
sdk.NewAttribute(types.AttributeKeyBaseFee, baseFee.String()),
),
})
}

// EndBlock update block gas used.
Expand Down
8 changes: 0 additions & 8 deletions x/feemarket/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,10 @@ func (k Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.Q
}

// BaseFee implements the Query/BaseFee gRPC method
// return empty if base fee is not enabled
func (k Keeper) BaseFee(c context.Context, _ *types.QueryBaseFeeRequest) (*types.QueryBaseFeeResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

res := &types.QueryBaseFeeResponse{}

params := k.GetParams(ctx)

if params.NoBaseFee {
return res, nil
}

baseFee := k.GetBaseFee(ctx)

if baseFee != nil {
Expand Down
1 change: 1 addition & 0 deletions x/feemarket/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
// ----------------------------------------------------------------------------

// GetConstantFee get's the base fee from the paramSpace
// return nil if base fee is not enabled
func (k Keeper) GetBaseFee(ctx sdk.Context) *big.Int {
params := k.GetParams(ctx)
if params.NoBaseFee {
Expand Down

0 comments on commit b7e1c3e

Please sign in to comment.