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
2 changes: 2 additions & 0 deletions tests/e2e/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
gaia "github.com/cosmos/gaia/v8/app"
"github.com/cosmos/gaia/v8/app/params"
Expand Down Expand Up @@ -38,6 +39,7 @@ func init() {
&ed25519.PubKey{},
)

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("teste query evidences", func() {
Pantani marked this conversation as resolved.
Show resolved Hide resolved
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())
}
})
}
23 changes: 23 additions & 0 deletions tests/e2e/e2e_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
"github.com/ory/dockertest/v3/docker"
)

Expand Down Expand Up @@ -602,6 +603,28 @@ func (s *IntegrationTestSuite) registerICA(owner, connectionID string) {
s.T().Logf("%s reigstered an interchain account on chain %s from chain %s", owner, s.chainB.id, s.chainA.id)
Pantani marked this conversation as resolved.
Show resolved Hide resolved
}

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,
"q",
"evidence",
hash,
}

s.executeGaiaTxCommand(ctx, c, gaiaCommand, valIdx, func(stdOut []byte, stdErr []byte) bool {
Pantani marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: we might want to rename or rework executeGaiaTxCommand for cli commands that don't require validation of the tx hash...keys and query subcommands would fall under this category.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree! I also moved the other API queries to the querie.go file in this PR #1780. The evidence API seems not to be working well. I opened an issue in the cosmos-sdk repo (cosmos/cosmos-sdk#13444). We can only query by CLI now, my idea was to try to put all CLI exec commands in the same file, but we can move to e2e_query_test.go. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah sounds great 👍

// 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
}

func (s *IntegrationTestSuite) executeGaiaTxCommand(ctx context.Context, c *chain, gaiaCommand []string, valIdx int, validation func([]byte, []byte) bool) {
if validation == nil {
validation = s.defaultExecValidation(s.chainA, 0)
Expand Down
24 changes: 24 additions & 0 deletions tests/e2e/e2e_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ 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/keys/ed25519"
"github.com/cosmos/cosmos-sdk/server"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/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"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/gaia/v8/app/params"
Expand Down Expand Up @@ -52,6 +55,7 @@ const (
govProposalBlockBuffer = 35
relayerAccountIndex = 0
icaOwnerAccountIndex = 1
numberOfEvidences = 10
)

var (
Expand Down Expand Up @@ -277,6 +281,26 @@ func (s *IntegrationTestSuite) initGenesis(c *chain) {
s.Require().NoError(err)
appGenState[banktypes.ModuleName] = bz

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)
}

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

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 @@ -10,6 +10,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/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"
staketypes "github.com/cosmos/cosmos-sdk/x/staking/types"
globalfee "github.com/cosmos/gaia/v8/x/globalfee/types"
Expand Down Expand Up @@ -170,3 +171,30 @@ func queryDelegatorWithdrawalAddress(endpoint string, delegatorAddr string) (dis
}
return res, nil
}

func queryEvidence(endpoint, hash string) (evidencetypes.QueryEvidenceResponse, error) {
var res evidencetypes.QueryEvidenceResponse
url := fmt.Sprintf("%s/cosmos/evidence/v1beta1/evidence/%s", endpoint, hash)
body, err := httpGet(url)
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
}