ee806fc41f
* tests(json-rpc): wip evm_backend unit test setup * tests(json-rpc): wip evm_backend unit test setup * fix viper * wip query client mock * fix first backend test except error message * clean up * wip Context with Height * fix JSON RPC backend test setup * typo * refactor folder structure * tests(json-rpc):add BlockBloom tests * tests(json-rpc): remove unused malleate * tests(json-rpc): add BaseFee tests * refactor query tests * add client mock * add GetTendermintBlockByNumber tests * refactor mock tests * refactor * wip backend EthBlockFromTendermint test * wip backend EthBlockFromTendermint test * refactor backend EthBlockFromTendermint test * add TestGetTendermintBlockResultByNumber * add GetBlockByNumber tests * refactor mocks * fix spelling * add more tests and address comments
122 lines
3.2 KiB
Go
122 lines
3.2 KiB
Go
package backend
|
|
|
|
import (
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/server"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/stretchr/testify/suite"
|
|
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
|
|
"github.com/evmos/ethermint/app"
|
|
"github.com/evmos/ethermint/encoding"
|
|
"github.com/evmos/ethermint/rpc/backend/mocks"
|
|
ethrpc "github.com/evmos/ethermint/rpc/types"
|
|
rpc "github.com/evmos/ethermint/rpc/types"
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
|
)
|
|
|
|
type BackendTestSuite struct {
|
|
suite.Suite
|
|
backend *Backend
|
|
}
|
|
|
|
func TestBackendTestSuite(t *testing.T) {
|
|
suite.Run(t, new(BackendTestSuite))
|
|
}
|
|
|
|
// SetupTest is executed before every BackendTestSuite test
|
|
func (suite *BackendTestSuite) SetupTest() {
|
|
ctx := server.NewDefaultContext()
|
|
ctx.Viper.Set("telemetry.global-labels", []interface{}{})
|
|
|
|
encodingConfig := encoding.MakeConfig(app.ModuleBasics)
|
|
clientCtx := client.Context{}.WithChainID("ethermint_9000-1").
|
|
WithHeight(1).
|
|
WithTxConfig(encodingConfig.TxConfig)
|
|
|
|
allowUnprotectedTxs := false
|
|
|
|
suite.backend = NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs)
|
|
suite.backend.queryClient.QueryClient = mocks.NewQueryClient(suite.T())
|
|
suite.backend.clientCtx.Client = mocks.NewClient(suite.T())
|
|
suite.backend.ctx = rpc.ContextWithHeight(1)
|
|
}
|
|
|
|
// buildEthereumTx returns an example legacy Ethereum transaction
|
|
func (suite *BackendTestSuite) buildEthereumTx() (*evmtypes.MsgEthereumTx, []byte) {
|
|
msgEthereumTx := evmtypes.NewTx(
|
|
big.NewInt(1),
|
|
uint64(0),
|
|
&common.Address{},
|
|
big.NewInt(0),
|
|
100000,
|
|
big.NewInt(1),
|
|
nil,
|
|
nil,
|
|
nil,
|
|
nil,
|
|
)
|
|
|
|
// A valid msg should have empty `From`
|
|
msgEthereumTx.From = ""
|
|
|
|
txBuilder := suite.backend.clientCtx.TxConfig.NewTxBuilder()
|
|
err := txBuilder.SetMsgs(msgEthereumTx)
|
|
suite.Require().NoError(err)
|
|
|
|
bz, err := suite.backend.clientCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
|
|
suite.Require().NoError(err)
|
|
return msgEthereumTx, bz
|
|
}
|
|
|
|
// buildFormattedBlock returns a formatted block for testing
|
|
func (suite *BackendTestSuite) buildFormattedBlock(
|
|
blockRes *tmrpctypes.ResultBlockResults,
|
|
resBlock *tmrpctypes.ResultBlock,
|
|
fullTx bool,
|
|
tx *evmtypes.MsgEthereumTx,
|
|
validator sdk.AccAddress,
|
|
baseFee *big.Int,
|
|
) map[string]interface{} {
|
|
header := resBlock.Block.Header
|
|
gasLimit := int64(^uint32(0)) // for `MaxGas = -1` (DefaultConsensusParams)
|
|
gasUsed := new(big.Int).SetUint64(uint64(blockRes.TxsResults[0].GasUsed))
|
|
|
|
root := common.Hash{}.Bytes()
|
|
receipt := ethtypes.NewReceipt(root, false, gasUsed.Uint64())
|
|
bloom := ethtypes.CreateBloom(ethtypes.Receipts{receipt})
|
|
|
|
ethRPCTxs := []interface{}{}
|
|
if tx != nil {
|
|
if fullTx {
|
|
rpcTx, err := ethrpc.NewRPCTransaction(
|
|
tx.AsTransaction(),
|
|
common.BytesToHash(header.Hash()),
|
|
uint64(header.Height),
|
|
uint64(0),
|
|
baseFee,
|
|
)
|
|
suite.Require().NoError(err)
|
|
ethRPCTxs = []interface{}{rpcTx}
|
|
} else {
|
|
ethRPCTxs = []interface{}{common.HexToHash(tx.Hash)}
|
|
}
|
|
}
|
|
|
|
return ethrpc.FormatBlock(
|
|
header,
|
|
resBlock.Block.Size(),
|
|
gasLimit,
|
|
gasUsed,
|
|
ethRPCTxs,
|
|
bloom,
|
|
common.BytesToAddress(validator.Bytes()),
|
|
baseFee,
|
|
)
|
|
}
|