Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(e2e): add e2e tests for the evidence module #1779

Merged
merged 12 commits into from
Oct 10, 2022
5 changes: 4 additions & 1 deletion tests/e2e/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
tmrand "github.com/tendermint/tendermint/libs/rand"

gaia "github.com/cosmos/gaia/v8/app"
"github.com/cosmos/gaia/v8/app/params"
tmrand "github.com/tendermint/tendermint/libs/rand"
)

const (
Expand All @@ -40,6 +42,7 @@ func init() {
)

authvesting.RegisterInterfaces(encodingConfig.InterfaceRegistry)
evidencetypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
cdc = encodingConfig.Codec
}

Expand Down
29 changes: 29 additions & 0 deletions tests/e2e/e2e_evidence_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package e2e
Copy link
Contributor

@mmulji-ic mmulji-ic Oct 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more complicated scenario would be to have blocks with the following:

  • DuplicateVoteEvidence
  • LightClientAttackEvidence
    ... and check for those as well. Future todo?


import (
"fmt"

"github.com/cosmos/cosmos-sdk/x/evidence/exported"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
)

func (s *IntegrationTestSuite) TestEvidence() {
s.Run("test evidence queries", func() {
var (
valIdx = 0
chain = s.chainA
chainAPI = fmt.Sprintf("http://%s", s.valResources[chain.id][valIdx].GetHostPort("1317/tcp"))
)
res, err := queryAllEvidence(chainAPI)
s.Require().NoError(err)
s.Require().Equal(numberOfEvidences, len(res.Evidence))
for _, evidence := range res.Evidence {
var exportedEvidence exported.Evidence
err := cdc.UnpackAny(evidence, &exportedEvidence)
s.Require().NoError(err)
eq, ok := exportedEvidence.(*evidencetypes.Equivocation)
s.Require().True(ok)
s.execQueryEvidence(chain, valIdx, eq.Hash().String())
}
})
}
10 changes: 6 additions & 4 deletions tests/e2e/e2e_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
grouptypes "github.com/cosmos/cosmos-sdk/x/group"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
icamauth "github.com/cosmos/gaia/v8/x/icamauth/types"
"github.com/ory/dockertest/v3/docker"

icamauth "github.com/cosmos/gaia/v8/x/icamauth/types"
)

const (
Expand Down Expand Up @@ -72,8 +74,8 @@ func (s *IntegrationTestSuite) execVestingTx(
s.T().Logf("%s - Executing gaiad %s with %v", c.id, method, args)
gaiaCommand := []string{
gaiadBinary,
"tx",
"vesting",
txCommand,
vestingtypes.ModuleName,
method,
"-y",
}
Expand Down Expand Up @@ -732,7 +734,7 @@ func (s *IntegrationTestSuite) registerICA(owner, connectionID string) {

s.executeGaiaTxCommand(ctx, s.chainA, registerICAcmd, 0, s.defaultExecValidation(s.chainA, 0))

s.T().Logf("%s reigstered an interchain account on chain %s from chain %s", owner, s.chainB.id, s.chainA.id)
s.T().Logf("%s registered an interchain account on chain %s from chain %s", owner, s.chainB.id, s.chainA.id)
}

func (s *IntegrationTestSuite) executeGaiaTxCommand(ctx context.Context, c *chain, gaiaCommand []string, valIdx int, validation func([]byte, []byte) bool) {
Expand Down
30 changes: 30 additions & 0 deletions tests/e2e/e2e_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package e2e

import (
"context"
"time"

evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
)

func (s *IntegrationTestSuite) execQueryEvidence(c *chain, valIdx int, hash string) (res evidencetypes.Equivocation) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

s.T().Logf("querying evidence %s on chain %s", hash, c.id)

gaiaCommand := []string{
gaiadBinary,
queryCommand,
evidencetypes.ModuleName,
hash,
}

s.executeGaiaTxCommand(ctx, c, gaiaCommand, valIdx, func(stdOut []byte, stdErr []byte) bool {
// TODO parse evidence after fix the SDK
// https://github.com/cosmos/cosmos-sdk/issues/13444
// s.Require().NoError(yaml.Unmarshal(stdOut, &res))
return true
})
return res
}
45 changes: 26 additions & 19 deletions tests/e2e/e2e_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import (

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client/flags"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/server"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -25,12 +27,12 @@ import (
authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
gov "github.com/cosmos/cosmos-sdk/x/gov/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/cosmos-sdk/x/group"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/cosmos/gaia/v8/app/params"
ibcclienttypes "github.com/cosmos/ibc-go/v5/modules/core/02-client/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"
"github.com/ory/dockertest/v3"
Expand All @@ -41,11 +43,14 @@ import (
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/libs/rand"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"

"github.com/cosmos/gaia/v8/app/params"
)

const (
gaiadBinary = "gaiad"
txCommand = "tx"
queryCommand = "query"
keysCommand = "keys"
gaiaHomePath = "/home/nonroot/.gaia"
photonDenom = "photon"
Expand All @@ -61,6 +66,7 @@ const (
govProposalBlockBuffer = 35
relayerAccountIndex = 0
icaOwnerAccountIndex = 1
numberOfEvidences = 10
)

var (
Expand All @@ -75,30 +81,12 @@ var (
proposalCounter = 0
govSendMsgRecipientAddress = Address()
sendGovAmount = sdk.NewInt64Coin(uatomDenom, 10)
fundGovAmount = sdk.NewInt64Coin(uatomDenom, 1000)
proposalSendMsg = &govtypes.MsgSubmitProposal{
InitialDeposit: sdk.Coins{depositAmount},
Metadata: b64.StdEncoding.EncodeToString([]byte("Testing 1, 2, 3!")),
}
)

type UpgradePlan struct {
Name string `json:"name"`
Height int `json:"height"`
Info string `json:"info"`
}

type SoftwareUpgrade struct {
Type string `json:"@type"`
Authority string `json:"authority"`
Plan UpgradePlan `json:"plan"`
}

type CancelSoftwareUpgrade struct {
Type string `json:"@type"`
Authority string `json:"authority"`
}

type IntegrationTestSuite struct {
suite.Suite

Expand Down Expand Up @@ -352,6 +340,25 @@ func (s *IntegrationTestSuite) initGenesis(c *chain, vestingMnemonic string) {
appGenState[authtypes.ModuleName] = authGenState
appGenState[banktypes.ModuleName] = bankGenState

var evidenceGenState evidencetypes.GenesisState
s.Require().NoError(cdc.UnmarshalJSON(appGenState[evidencetypes.ModuleName], &evidenceGenState))

evidenceGenState.Evidence = make([]*codectypes.Any, numberOfEvidences)
for i := range evidenceGenState.Evidence {
pk := ed25519.GenPrivKey()
evidence := &evidencetypes.Equivocation{
Height: 1,
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: sdk.ConsAddress(pk.PubKey().Address().Bytes()).String(),
}
evidenceGenState.Evidence[i], err = codectypes.NewAnyWithValue(evidence)
s.Require().NoError(err)
}

appGenState[evidencetypes.ModuleName], err = cdc.MarshalJSON(&evidenceGenState)
s.Require().NoError(err)

var genUtilGenState genutiltypes.GenesisState
s.Require().NoError(cdc.UnmarshalJSON(appGenState[genutiltypes.ModuleName], &genUtilGenState))

Expand Down
28 changes: 28 additions & 0 deletions tests/e2e/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/cosmos/cosmos-sdk/x/group"
staketypes "github.com/cosmos/cosmos-sdk/x/staking/types"

globalfee "github.com/cosmos/gaia/v8/x/globalfee/types"
icamauth "github.com/cosmos/gaia/v8/x/icamauth/types"
)
Expand Down Expand Up @@ -312,3 +314,29 @@ func queryPeriodicVestingAccount(endpoint, address string) (authvesting.Periodic
}
return *acc, nil
}

func queryEvidence(endpoint, hash string) (evidencetypes.QueryEvidenceResponse, error) {
var res evidencetypes.QueryEvidenceResponse
body, err := httpGet(fmt.Sprintf("%s/cosmos/evidence/v1beta1/evidence/%s", endpoint, hash))
if err != nil {
return res, err
}

if err = cdc.UnmarshalJSON(body, &res); err != nil {
return res, err
}
return res, nil
}

func queryAllEvidence(endpoint string) (evidencetypes.QueryAllEvidenceResponse, error) {
var res evidencetypes.QueryAllEvidenceResponse
body, err := httpGet(fmt.Sprintf("%s/cosmos/evidence/v1beta1/evidence", endpoint))
if err != nil {
return res, err
}

if err = cdc.UnmarshalJSON(body, &res); err != nil {
return res, err
}
return res, nil
}