Skip to content

Commit

Permalink
add keeper and proposal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sairam kola authored and Sairam kola committed Jul 28, 2023
1 parent baf024d commit 8f4141d
Show file tree
Hide file tree
Showing 2 changed files with 471 additions and 7 deletions.
111 changes: 104 additions & 7 deletions x/govshuttle/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,42 @@ import (
"encoding/json"
"math/big"
"testing"
"time"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/version"

"github.com/Canto-Network/Canto/v6/app"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/evmos/ethermint/crypto/ethsecp256k1"
"github.com/evmos/ethermint/server/config"
evm "github.com/evmos/ethermint/x/evm/types"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"

//used for deploying contracts
"github.com/Canto-Network/Canto/v6/contracts"
"github.com/Canto-Network/Canto/v6/x/erc20/types"
"github.com/Canto-Network/Canto/v6/x/govshuttle/types"
ethtypes "github.com/ethereum/go-ethereum/core/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
)

type KeeperTestSuite struct {
suite.Suite //top level testing suite

ctx sdk.Context
app *app.Canto
address common.Address

queryClient types.QueryClient
ctx sdk.Context
app *app.Canto
address common.Address
consAddress sdk.ConsAddress
queryClientEvm evm.QueryClient
signer keyring.Signer
}
Expand All @@ -43,8 +51,13 @@ func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, s)
}

//Test Helpers
// Test Helpers
func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) {

// consensus key
priv, err := ethsecp256k1.GenerateKey()
require.NoError(t, err)
suite.consAddress = sdk.ConsAddress(priv.PubKey().Address())
checkTx := false

feemarketGenesis := feemarkettypes.DefaultGenesisState()
Expand All @@ -53,6 +66,30 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) {

//init app
suite.app = app.Setup(checkTx, feemarketGenesis)
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{
Height: 1,
ChainID: "canto_9001-1",
Time: time.Now().UTC(),
ProposerAddress: suite.consAddress.Bytes(),

Version: tmversion.Consensus{
Block: version.BlockProtocol,
},
LastBlockId: tmproto.BlockID{
Hash: tmhash.Sum([]byte("block_id")),
PartSetHeader: tmproto.PartSetHeader{
Total: 11,
Hash: tmhash.Sum([]byte("partset_header")),
},
},
AppHash: tmhash.Sum([]byte("app")),
DataHash: tmhash.Sum([]byte("data")),
EvidenceHash: tmhash.Sum([]byte("evidence")),
ValidatorsHash: tmhash.Sum([]byte("validators")),
NextValidatorsHash: tmhash.Sum([]byte("next_validators")),
ConsensusHash: tmhash.Sum([]byte("consensus")),
LastResultsHash: tmhash.Sum([]byte("last_result")),
})
}

func (suite *KeeperTestSuite) SetupTest() {
Expand Down Expand Up @@ -119,3 +156,63 @@ func (suite *KeeperTestSuite) DeployCaller() (common.Address, error) {
func (suite *KeeperTestSuite) DeployCallee() {

}

var _ types.GovKeeper = &MockGovKeeper{}

type MockGovKeeper struct {
mock.Mock
}

func (m *MockGovKeeper) GetProposalID(ctx sdk.Context) (uint64, error) {
args := m.Called(mock.Anything)
return args.Get(0).(uint64), args.Error(1)
}

var _ types.AccountKeeper = &MockAccountKeeper{}

type MockAccountKeeper struct {
mock.Mock
}

func (m *MockAccountKeeper) GetSequence(ctx sdk.Context, addr sdk.AccAddress) (uint64, error) {
args := m.Called(mock.Anything, mock.Anything)
return args.Get(0).(uint64), args.Error(1)
}

func (m *MockAccountKeeper) GetModuleAddress(moduleName string) sdk.AccAddress {
args := m.Called(mock.Anything)
return args.Get(0).(sdk.AccAddress)
}

var _ types.ERC20Keeper = &MockERC20Keeper{}

type MockERC20Keeper struct {
mock.Mock
}

func (m *MockERC20Keeper) CallEVM(ctx sdk.Context, abi abi.ABI, from common.Address, contract common.Address, commit bool, method string, args ...interface{}) (*evmtypes.MsgEthereumTxResponse, error) {
resArgs := m.Called(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
if resArgs.Get(0) == nil {
return nil, resArgs.Error(1)
}
return resArgs.Get(0).(*evmtypes.MsgEthereumTxResponse), resArgs.Error(1)
}

func (m *MockERC20Keeper) CallEVMWithData(ctx sdk.Context, from common.Address, contract *common.Address, data []byte, commit bool) (*evmtypes.MsgEthereumTxResponse, error) {
args := m.Called(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*evmtypes.MsgEthereumTxResponse), args.Error(1)
}

func (suite *KeeperTestSuite) TestKeeper() {
address, found := suite.app.GovshuttleKeeper.GetPort(suite.ctx)
suite.Require().Equal(false, found)
suite.Require().Equal(common.Address{}, address)
testAddress := common.HexToAddress("0x648a5Aa0C4FbF2C1CF5a3B432c2766EeaF8E402d")
suite.app.GovshuttleKeeper.SetPort(suite.ctx, testAddress)
address, found = suite.app.GovshuttleKeeper.GetPort(suite.ctx)
suite.Require().Equal(true, found)
suite.Require().Equal(testAddress, address)
}
Loading

0 comments on commit 8f4141d

Please sign in to comment.