2022-07-23 09:16:23 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
2022-08-09 05:52:28 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2022-07-29 10:35:07 +00:00
|
|
|
"math/big"
|
2022-08-09 05:52:28 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-07-23 09:16:23 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-08-11 20:49:05 +00:00
|
|
|
dbm "github.com/tendermint/tm-db"
|
|
|
|
|
2022-07-23 09:16:23 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2022-08-09 05:52:28 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2022-07-23 09:16:23 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
2022-07-29 10:35:07 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2022-07-23 09:16:23 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2022-07-29 10:35:07 +00:00
|
|
|
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
|
2022-07-23 09:16:23 +00:00
|
|
|
|
2022-07-29 10:35:07 +00:00
|
|
|
"github.com/evmos/ethermint/app"
|
2022-08-09 05:52:28 +00:00
|
|
|
"github.com/evmos/ethermint/crypto/hd"
|
2022-07-29 10:35:07 +00:00
|
|
|
"github.com/evmos/ethermint/encoding"
|
2022-08-11 20:49:05 +00:00
|
|
|
"github.com/evmos/ethermint/indexer"
|
2022-07-23 09:16:23 +00:00
|
|
|
"github.com/evmos/ethermint/rpc/backend/mocks"
|
2022-08-09 05:52:28 +00:00
|
|
|
rpctypes "github.com/evmos/ethermint/rpc/types"
|
2022-10-11 18:43:32 +00:00
|
|
|
"github.com/evmos/ethermint/tests"
|
2022-07-23 09:16:23 +00:00
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BackendTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
backend *Backend
|
2022-10-11 18:43:32 +00:00
|
|
|
acc sdk.AccAddress
|
2022-07-23 09:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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{}{})
|
|
|
|
|
2022-08-09 05:52:28 +00:00
|
|
|
baseDir := suite.T().TempDir()
|
|
|
|
nodeDirName := fmt.Sprintf("node")
|
|
|
|
clientDir := filepath.Join(baseDir, nodeDirName, "evmoscli")
|
|
|
|
keyRing, err := suite.generateTestKeyring(clientDir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
// Create Account with set sequence
|
|
|
|
suite.acc = sdk.AccAddress(tests.GenerateAddress().Bytes())
|
|
|
|
accounts := map[string]client.TestAccount{}
|
|
|
|
accounts[suite.acc.String()] = client.TestAccount{
|
|
|
|
Address: suite.acc,
|
|
|
|
Num: uint64(1),
|
|
|
|
Seq: uint64(1),
|
|
|
|
}
|
|
|
|
|
2022-07-29 10:35:07 +00:00
|
|
|
encodingConfig := encoding.MakeConfig(app.ModuleBasics)
|
|
|
|
clientCtx := client.Context{}.WithChainID("ethermint_9000-1").
|
|
|
|
WithHeight(1).
|
2022-08-09 05:52:28 +00:00
|
|
|
WithTxConfig(encodingConfig.TxConfig).
|
|
|
|
WithKeyringDir(clientDir).
|
2022-10-11 18:43:32 +00:00
|
|
|
WithKeyring(keyRing).
|
|
|
|
WithAccountRetriever(client.TestAccountRetriever{Accounts: accounts})
|
2022-07-23 09:16:23 +00:00
|
|
|
|
2022-07-29 10:35:07 +00:00
|
|
|
allowUnprotectedTxs := false
|
2022-07-23 09:16:23 +00:00
|
|
|
|
2022-08-11 20:49:05 +00:00
|
|
|
idxer := indexer.NewKVIndexer(dbm.NewMemDB(), ctx.Logger, clientCtx)
|
|
|
|
|
|
|
|
suite.backend = NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, idxer)
|
2022-10-11 18:43:32 +00:00
|
|
|
suite.backend.queryClient.QueryClient = mocks.NewEVMQueryClient(suite.T())
|
2022-07-29 10:35:07 +00:00
|
|
|
suite.backend.clientCtx.Client = mocks.NewClient(suite.T())
|
2022-08-09 05:52:28 +00:00
|
|
|
suite.backend.ctx = rpctypes.ContextWithHeight(1)
|
2022-07-23 09:16:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 10:35:07 +00:00
|
|
|
// 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
|
2022-07-23 09:16:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 10:35:07 +00:00
|
|
|
// 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 {
|
2022-08-09 05:52:28 +00:00
|
|
|
rpcTx, err := rpctypes.NewRPCTransaction(
|
2022-07-29 10:35:07 +00:00
|
|
|
tx.AsTransaction(),
|
|
|
|
common.BytesToHash(header.Hash()),
|
|
|
|
uint64(header.Height),
|
|
|
|
uint64(0),
|
|
|
|
baseFee,
|
2022-11-16 17:59:12 +00:00
|
|
|
suite.backend.chainID,
|
2022-07-29 10:35:07 +00:00
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
ethRPCTxs = []interface{}{rpcTx}
|
|
|
|
} else {
|
|
|
|
ethRPCTxs = []interface{}{common.HexToHash(tx.Hash)}
|
|
|
|
}
|
|
|
|
}
|
2022-07-23 09:16:23 +00:00
|
|
|
|
2022-08-09 05:52:28 +00:00
|
|
|
return rpctypes.FormatBlock(
|
2022-07-29 10:35:07 +00:00
|
|
|
header,
|
|
|
|
resBlock.Block.Size(),
|
|
|
|
gasLimit,
|
|
|
|
gasUsed,
|
|
|
|
ethRPCTxs,
|
|
|
|
bloom,
|
|
|
|
common.BytesToAddress(validator.Bytes()),
|
|
|
|
baseFee,
|
|
|
|
)
|
2022-07-23 09:16:23 +00:00
|
|
|
}
|
2022-08-09 05:52:28 +00:00
|
|
|
|
|
|
|
func (suite *BackendTestSuite) generateTestKeyring(clientDir string) (keyring.Keyring, error) {
|
|
|
|
buf := bufio.NewReader(os.Stdin)
|
|
|
|
encCfg := encoding.MakeConfig(app.ModuleBasics)
|
|
|
|
return keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, clientDir, buf, encCfg.Codec, []keyring.Option{hd.EthSecp256k1Option()}...)
|
|
|
|
}
|