Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into validate
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Oct 4, 2024
2 parents 08e943b + c4cef0f commit 7e402e3
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 110 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (cli) [#524](https://github.com/crypto-org-chain/ethermint/pull/524) Allow tx evm raw run for generate only when offline with evm-denom flag.
* (rpc) [#527](https://github.com/crypto-org-chain/ethermint/pull/527) Fix balance consistency between trace-block and state machine.
* (rpc) [#536](https://github.com/crypto-org-chain/ethermint/pull/536) Fix validate basic after transaction conversion with raw field.
* (cli) [#537](https://github.com/crypto-org-chain/ethermint/pull/537) Fix unsuppored sign mode SIGN_MODE_TEXTUAL for bank transfer.

### Improvements

Expand All @@ -91,7 +92,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (ante) [#504](https://github.com/crypto-org-chain/ethermint/pull/504) Optimize AnteHandle method to skip checks if disabledMsgs is empty.
* [#517](https://github.com/crypto-org-chain/ethermint/pull/517) Add check for integer overflow to ensure safe conversion.
* [#522](https://github.com/crypto-org-chain/ethermint/pull/522) block-stm executor support optional pre-estimations.
* [#526](https://github.com/crypto-org-chain/ethermint/pull/526) Avoid unnecessary block result in header related api call.
* [#526](https://github.com/crypto-org-chain/ethermint/pull/526), [#535](https://github.com/crypto-org-chain/ethermint/pull/535) Avoid unnecessary block result in header related api call.
* [#533](https://github.com/crypto-org-chain/ethermint/pull/533) Bump cosmos-sdk to v0.50.10, cometbft to v0.38.13 and ibc-go to v8.5.1.

## v0.21.x-cronos
Expand Down
18 changes: 18 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/http"
"os"
"path/filepath"
"slices"
"sort"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
Expand All @@ -31,6 +32,7 @@ import (
"cosmossdk.io/core/appmodule"
runtimeservices "github.com/cosmos/cosmos-sdk/runtime/services"
"github.com/cosmos/cosmos-sdk/server"
sigtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/gogoproto/proto"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -75,6 +77,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/posthandler"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
txmodule "github.com/cosmos/cosmos-sdk/x/auth/tx/config"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
Expand Down Expand Up @@ -380,6 +383,21 @@ func NewEthermintApp(
authAddr,
logger,
)
// optional: enable sign mode textual by overwriting the default tx config (after setting the bank keeper)
enabledSignModes := slices.Clone(authtx.DefaultSignModes)
enabledSignModes = append(enabledSignModes, sigtypes.SignMode_SIGN_MODE_TEXTUAL)
txConfigOpts := authtx.ConfigOptions{
EnabledSignModes: enabledSignModes,
TextualCoinMetadataQueryFn: txmodule.NewBankKeeperCoinMetadataQueryFn(app.BankKeeper),
}
txConfig, err := authtx.NewTxConfigWithOptions(
appCodec,
txConfigOpts,
)
if err != nil {
panic(err)
}
app.txConfig = txConfig
app.StakingKeeper = stakingkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[stakingtypes.StoreKey]),
Expand Down
21 changes: 15 additions & 6 deletions cmd/ethermintd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"io"
"os"
"slices"

"github.com/spf13/cobra"

Expand All @@ -43,6 +44,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/cosmos-sdk/client/debug"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/x/auth/types"
Expand All @@ -65,14 +67,13 @@ const (
ChainID = "ethermint_9000-1"
)

type emptyAppOptions struct{}

func (ao emptyAppOptions) Get(_ string) interface{} { return nil }

// NewRootCmd creates a new root command for simd. It is called once in the
// main function.
func NewRootCmd() (*cobra.Command, ethermint.EncodingConfig) {
tempApp := app.NewEthermintApp(cmtlog.NewNopLogger(), dbm.NewMemDB(), nil, true, emptyAppOptions{})
tempApp := app.NewEthermintApp(
cmtlog.NewNopLogger(), dbm.NewMemDB(), nil, true,
simtestutil.NewAppOptionsWithFlagHome(app.DefaultNodeHome),
)
encodingConfig := tempApp.EncodingConfig()
initClientCtx := client.Context{}.
WithCodec(encodingConfig.Codec).
Expand All @@ -86,6 +87,11 @@ func NewRootCmd() (*cobra.Command, ethermint.EncodingConfig) {
WithKeyringOptions(hd.EthSecp256k1Option()).
WithViper(EnvPrefix)

initClientCtx, err := clientcfg.ReadDefaultValuesFromDefaultClientConfig(initClientCtx)
if err != nil {
panic(err)
}

eip712.SetEncodingConfig(encodingConfig)

rootCmd := &cobra.Command{
Expand All @@ -96,6 +102,7 @@ func NewRootCmd() (*cobra.Command, ethermint.EncodingConfig) {
cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.ErrOrStderr())

initClientCtx = initClientCtx.WithCmdContext(cmd.Context())
initClientCtx, err := client.ReadPersistentCommandFlags(initClientCtx, cmd.Flags())
if err != nil {
return err
Expand All @@ -110,8 +117,10 @@ func NewRootCmd() (*cobra.Command, ethermint.EncodingConfig) {
// sets the RPC client needed for SIGN_MODE_TEXTUAL. This sign mode
// is only available if the client is online.
if !initClientCtx.Offline {
enabledSignModes := slices.Clone(tx.DefaultSignModes)
enabledSignModes = append(enabledSignModes, signing.SignMode_SIGN_MODE_TEXTUAL)
txConfigOpts := tx.ConfigOptions{
EnabledSignModes: append(tx.DefaultSignModes, signing.SignMode_SIGN_MODE_TEXTUAL),
EnabledSignModes: enabledSignModes,
TextualCoinMetadataQueryFn: txmodule.NewGRPCCoinMetadataQueryFn(initClientCtx),
}
txConfig, err := tx.NewTxConfigWithOptions(
Expand Down
4 changes: 2 additions & 2 deletions rpc/backend/account_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (b *Backend) GetProof(address common.Address, storageKeys []string, blockNr
}

height := blockNum.Int64()
_, err = b.TendermintBlockByNumber(blockNum)
_, err = b.TendermintHeaderByNumber(blockNum)
if err != nil {
// the error message imitates geth behavior
return nil, errors.New("header not found")
Expand Down Expand Up @@ -163,7 +163,7 @@ func (b *Backend) GetBalance(address common.Address, blockNrOrHash rpctypes.Bloc
Address: address.String(),
}

_, err = b.TendermintBlockByNumber(blockNum)
_, err = b.TendermintHeaderByNumber(blockNum)
if err != nil {
return nil, err
}
Expand Down
29 changes: 18 additions & 11 deletions rpc/backend/account_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func (suite *BackendTestSuite) TestGetProof() {
rpctypes.BlockNumberOrHash{BlockNumber: &blockNrInvalid},
func(bn rpctypes.BlockNumber, addr common.Address) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlock(client, bn.Int64(), nil)
height := bn.Int64()
RegisterHeader(client, &height, nil)
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterAccount(queryClient, addr, blockNrInvalid.Int64())
},
Expand All @@ -111,7 +112,8 @@ func (suite *BackendTestSuite) TestGetProof() {
rpctypes.BlockNumberOrHash{BlockNumber: &blockNrInvalid},
func(bn rpctypes.BlockNumber, addr common.Address) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlockError(client, bn.Int64())
height := bn.Int64()
RegisterHeaderError(client, &height)
},
false,
&rpctypes.AccountResult{},
Expand All @@ -122,12 +124,12 @@ func (suite *BackendTestSuite) TestGetProof() {
[]string{"0x0"},
rpctypes.BlockNumberOrHash{BlockNumber: &blockNr},
func(bn rpctypes.BlockNumber, addr common.Address) {
suite.backend.ctx = rpctypes.ContextWithHeight(bn.Int64())

height := bn.Int64()
suite.backend.ctx = rpctypes.ContextWithHeight(height)
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlock(client, bn.Int64(), nil)
RegisterHeader(client, &height, nil)
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterAccount(queryClient, addr, bn.Int64())
RegisterAccount(queryClient, addr, height)

// Use the IAVL height if a valid tendermint height is passed in.
iavlHeight := bn.Int64()
Expand Down Expand Up @@ -270,7 +272,8 @@ func (suite *BackendTestSuite) TestGetBalance() {
rpctypes.BlockNumberOrHash{BlockNumber: &blockNr},
func(bn rpctypes.BlockNumber, addr common.Address) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlockError(client, bn.Int64())
height := bn.Int64()
RegisterHeaderError(client, &height)
},
false,
nil,
Expand All @@ -281,7 +284,8 @@ func (suite *BackendTestSuite) TestGetBalance() {
rpctypes.BlockNumberOrHash{BlockNumber: &blockNr},
func(bn rpctypes.BlockNumber, addr common.Address) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlock(client, bn.Int64(), nil)
height := bn.Int64()
RegisterHeader(client, &height, nil)
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBalanceError(queryClient, addr, bn.Int64())
},
Expand All @@ -294,7 +298,8 @@ func (suite *BackendTestSuite) TestGetBalance() {
rpctypes.BlockNumberOrHash{BlockNumber: &blockNr},
func(bn rpctypes.BlockNumber, addr common.Address) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlock(client, bn.Int64(), nil)
height := bn.Int64()
RegisterHeader(client, &height, nil)
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBalanceInvalid(queryClient, addr, bn.Int64())
},
Expand All @@ -307,7 +312,8 @@ func (suite *BackendTestSuite) TestGetBalance() {
rpctypes.BlockNumberOrHash{BlockNumber: &blockNr},
func(bn rpctypes.BlockNumber, addr common.Address) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlock(client, bn.Int64(), nil)
height := bn.Int64()
RegisterHeader(client, &height, nil)
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBalanceNegative(queryClient, addr, bn.Int64())
},
Expand All @@ -320,7 +326,8 @@ func (suite *BackendTestSuite) TestGetBalance() {
rpctypes.BlockNumberOrHash{BlockNumber: &blockNr},
func(bn rpctypes.BlockNumber, addr common.Address) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlock(client, bn.Int64(), nil)
height := bn.Int64()
RegisterHeader(client, &height, nil)
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBalance(queryClient, addr, bn.Int64())
},
Expand Down
66 changes: 47 additions & 19 deletions rpc/backend/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,30 +173,52 @@ func (b *Backend) GetBlockTransactionCount(block *tmrpctypes.ResultBlock) *hexut
// TendermintBlockByNumber returns a Tendermint-formatted block for a given
// block number
func (b *Backend) TendermintBlockByNumber(blockNum rpctypes.BlockNumber) (*tmrpctypes.ResultBlock, error) {
height, err := b.getHeightByBlockNum(blockNum)
if err != nil {
return nil, err
}
resBlock, err := b.clientCtx.Client.Block(b.ctx, &height)
if err != nil {
b.logger.Debug("tendermint client failed to get block", "height", height, "error", err.Error())
return nil, err
}

if resBlock.Block == nil {
b.logger.Debug("TendermintBlockByNumber block not found", "height", height)
return nil, nil
}

return resBlock, nil
}

func (b *Backend) getHeightByBlockNum(blockNum rpctypes.BlockNumber) (int64, error) {
height := blockNum.Int64()
if height <= 0 {
// fetch the latest block number from the app state, more accurate than the tendermint block store state.
n, err := b.BlockNumber()
if err != nil {
return nil, err
return 0, err
}
height, err = ethermint.SafeHexToInt64(n)
if err != nil {
return nil, err
return 0, err
}
}
resBlock, err := b.clientCtx.Client.Block(b.ctx, &height)
return height, nil
}

// TendermintHeaderByNumber returns a Tendermint-formatted header for a given
// block number
func (b *Backend) TendermintHeaderByNumber(blockNum rpctypes.BlockNumber) (*tmrpctypes.ResultHeader, error) {
height, err := b.getHeightByBlockNum(blockNum)
if err != nil {
b.logger.Debug("tendermint client failed to get block", "height", height, "error", err.Error())
return nil, err
}

if resBlock.Block == nil {
b.logger.Debug("TendermintBlockByNumber block not found", "height", height)
return nil, nil
sc, ok := b.clientCtx.Client.(tmrpcclient.SignClient)
if !ok {
return nil, errors.New("invalid rpc client")
}

return resBlock, nil
return sc.Header(b.ctx, &height)
}

// TendermintBlockResultByNumber returns a Tendermint-formatted block result
Expand Down Expand Up @@ -257,6 +279,9 @@ func (b *Backend) BlockNumberFromTendermintByHash(blockHash common.Hash) (*big.I
if err != nil {
return nil, err
}
if resHeader.Header == nil {
return nil, errors.Errorf("header not found for hash %s", blockHash.Hex())
}
return big.NewInt(resHeader.Header.Height), nil
}

Expand Down Expand Up @@ -302,35 +327,35 @@ func (b *Backend) EthMsgsFromTendermintBlock(

// HeaderByNumber returns the block header identified by height.
func (b *Backend) HeaderByNumber(blockNum rpctypes.BlockNumber) (*ethtypes.Header, error) {
resBlock, err := b.TendermintBlockByNumber(blockNum)
res, err := b.TendermintHeaderByNumber(blockNum)
if err != nil {
return nil, err
}

if resBlock == nil {
return nil, errors.Errorf("block not found for height %d", blockNum)
if res == nil || res.Header == nil {
return nil, errors.Errorf("header not found for height %d", blockNum)
}

blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height)
blockRes, err := b.TendermintBlockResultByNumber(&res.Header.Height)
if err != nil {
return nil, fmt.Errorf("block result not found for height %d", resBlock.Block.Height)
return nil, fmt.Errorf("header result not found for height %d", res.Header.Height)
}

bloom, err := b.BlockBloom(blockRes)
if err != nil {
b.logger.Debug("HeaderByNumber BlockBloom failed", "height", resBlock.Block.Height)
b.logger.Debug("HeaderByNumber BlockBloom failed", "height", res.Header.Height)
}

baseFee, err := b.BaseFee(blockRes)
if err != nil {
// handle the error for pruned node.
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", resBlock.Block.Height, "error", err)
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", res.Header.Height, "error", err)
}
validator, err := b.getValidatorAccount(&resBlock.Block.Header)
validator, err := b.getValidatorAccount(res.Header)
if err != nil {
return nil, err
}
ethHeader := rpctypes.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFee, validator)
ethHeader := rpctypes.EthHeaderFromTendermint(*res.Header, bloom, baseFee, validator)
return ethHeader, nil
}

Expand All @@ -344,6 +369,9 @@ func (b *Backend) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error)
if err != nil {
return nil, err
}
if resHeader.Header == nil {
return nil, errors.Errorf("header not found for hash %s", blockHash.Hex())
}
blockRes, err := b.TendermintBlockResultByNumber(&resHeader.Header.Height)
if err != nil {
return nil, errors.Errorf("block result not found for height %d", resHeader.Header.Height)
Expand Down
Loading

0 comments on commit 7e402e3

Please sign in to comment.