laconicd/x/evm/keeper/keeper_test.go
Thomas Nguy fada59551d
rpc, evm: fix eth_coinbase endpoint to return ethereum address of the validator node (#153)
* fix coinbase rpc endpoint to return ethereum address of the validator

* update changelog

* fix lint

* clean code and simplify logic

* fix changelog

* change request variable name and type

* add test

* fix proto comments

* fix proto comments

* Update x/evm/keeper/grpc_query.go

* Update x/evm/keeper/grpc_query.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
2021-06-22 06:14:40 -04:00

88 lines
2.7 KiB
Go

package keeper_test
import (
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
"testing"
"time"
"github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/ethermint/app"
ethermint "github.com/cosmos/ethermint/types"
"github.com/cosmos/ethermint/x/evm/types"
ethcmn "github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)
const addrHex = "0x756F45E3FA69347A9A973A725E3C98bC4db0b4c1"
const hex = "0x0d87a3a5f73140f46aac1bf419263e4e94e87c292f25007700ab7f2060e2af68"
var (
hash = ethcmn.FromHex(hex)
)
type KeeperTestSuite struct {
suite.Suite
ctx sdk.Context
app *app.EthermintApp
queryClient types.QueryClient
address ethcmn.Address
consAddress sdk.ConsAddress
}
func (suite *KeeperTestSuite) SetupTest() {
checkTx := false
suite.app = app.Setup(checkTx)
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 1, ChainID: "ethermint-1", Time: time.Now().UTC()})
suite.app.EvmKeeper.WithContext(suite.ctx)
suite.address = ethcmn.HexToAddress(addrHex)
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, suite.app.EvmKeeper)
suite.queryClient = types.NewQueryClient(queryHelper)
balance := ethermint.NewPhotonCoin(sdk.ZeroInt())
acc := &ethermint.EthAccount{
BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(suite.address.Bytes()), nil, 0, 0),
CodeHash: ethcrypto.Keccak256(nil),
}
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
suite.app.BankKeeper.SetBalance(suite.ctx, acc.GetAddress(), balance)
priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err)
valAddr := sdk.ValAddress(suite.address.Bytes())
validator, err := stakingtypes.NewValidator(valAddr, priv.PubKey(), stakingtypes.Description{})
suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
suite.app.StakingKeeper.SetValidator(suite.ctx, validator)
suite.consAddress = sdk.ConsAddress(priv.PubKey().Address())
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}
func (suite *KeeperTestSuite) TestChainConfig() {
config, found := suite.app.EvmKeeper.GetChainConfig(suite.ctx)
suite.Require().True(found)
suite.Require().Equal(types.DefaultChainConfig(), config)
config.EIP150Block = sdk.NewInt(100)
suite.app.EvmKeeper.SetChainConfig(suite.ctx, config)
newConfig, found := suite.app.EvmKeeper.GetChainConfig(suite.ctx)
suite.Require().True(found)
suite.Require().Equal(config, newConfig)
}