2019-11-06 13:08:02 -08:00
|
|
|
package evidence_test
|
|
|
|
|
|
|
|
import (
|
2020-07-29 11:22:51 -07:00
|
|
|
"fmt"
|
2019-11-06 13:08:02 -08:00
|
|
|
"testing"
|
|
|
|
|
2020-02-24 08:05:21 -08:00
|
|
|
"github.com/stretchr/testify/suite"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-02-24 08:05:21 -08:00
|
|
|
"github.com/tendermint/tendermint/types/time"
|
2019-11-06 13:08:02 -08:00
|
|
|
|
2020-09-25 01:41:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
2019-11-06 13:08:02 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/evidence"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
|
2020-06-14 02:58:48 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/evidence/keeper"
|
2020-02-24 08:05:21 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/evidence/types"
|
2019-11-06 13:08:02 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type GenesisTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
ctx sdk.Context
|
2020-06-14 02:58:48 -07:00
|
|
|
keeper keeper.Keeper
|
2019-11-06 13:08:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *GenesisTestSuite) SetupTest() {
|
|
|
|
checkTx := false
|
|
|
|
app := simapp.Setup(checkTx)
|
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
suite.ctx = app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 1})
|
2020-02-24 08:05:21 -08:00
|
|
|
suite.keeper = app.EvidenceKeeper
|
2019-11-06 13:08:02 -08:00
|
|
|
}
|
|
|
|
|
2020-07-29 11:22:51 -07:00
|
|
|
func (suite *GenesisTestSuite) TestInitGenesis() {
|
|
|
|
var (
|
2020-08-11 07:22:30 -07:00
|
|
|
genesisState *types.GenesisState
|
2020-07-29 11:22:51 -07:00
|
|
|
testEvidence []exported.Evidence
|
|
|
|
pk = ed25519.GenPrivKey()
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
malleate func()
|
|
|
|
expPass bool
|
|
|
|
posttests func()
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid",
|
|
|
|
func() {
|
|
|
|
testEvidence = make([]exported.Evidence, 100)
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
testEvidence[i] = &types.Equivocation{
|
|
|
|
Height: int64(i + 1),
|
|
|
|
Power: 100,
|
|
|
|
Time: time.Now().UTC(),
|
2020-09-25 03:25:37 -07:00
|
|
|
ConsensusAddress: pk.PubKey().Address().String(),
|
2020-07-29 11:22:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
genesisState = types.NewGenesisState(testEvidence)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
func() {
|
|
|
|
for _, e := range testEvidence {
|
|
|
|
_, ok := suite.keeper.GetEvidence(suite.ctx, e.Hash())
|
|
|
|
suite.True(ok)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"invalid",
|
|
|
|
func() {
|
|
|
|
testEvidence = make([]exported.Evidence, 100)
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
testEvidence[i] = &types.Equivocation{
|
|
|
|
Power: 100,
|
|
|
|
Time: time.Now().UTC(),
|
2020-09-25 03:25:37 -07:00
|
|
|
ConsensusAddress: pk.PubKey().Address().String(),
|
2020-07-29 11:22:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
genesisState = types.NewGenesisState(testEvidence)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
func() {
|
|
|
|
suite.Empty(suite.keeper.GetAllEvidence(suite.ctx))
|
|
|
|
},
|
|
|
|
},
|
2019-11-06 13:08:02 -08:00
|
|
|
}
|
|
|
|
|
2020-07-29 11:22:51 -07:00
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
suite.SetupTest()
|
|
|
|
|
|
|
|
tc.malleate()
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.NotPanics(func() {
|
|
|
|
evidence.InitGenesis(suite.ctx, suite.keeper, genesisState)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
suite.Panics(func() {
|
|
|
|
evidence.InitGenesis(suite.ctx, suite.keeper, genesisState)
|
|
|
|
})
|
|
|
|
}
|
2019-11-06 13:08:02 -08:00
|
|
|
|
2020-07-29 11:22:51 -07:00
|
|
|
tc.posttests()
|
|
|
|
})
|
2019-11-06 13:08:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 11:22:51 -07:00
|
|
|
func (suite *GenesisTestSuite) TestExportGenesis() {
|
2019-11-06 13:08:02 -08:00
|
|
|
pk := ed25519.GenPrivKey()
|
|
|
|
|
2020-07-29 11:22:51 -07:00
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
malleate func()
|
|
|
|
expPass bool
|
|
|
|
posttests func()
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"success",
|
|
|
|
func() {
|
|
|
|
suite.keeper.SetEvidence(suite.ctx, &types.Equivocation{
|
|
|
|
Height: 1,
|
|
|
|
Power: 100,
|
|
|
|
Time: time.Now().UTC(),
|
2020-09-25 03:25:37 -07:00
|
|
|
ConsensusAddress: pk.PubKey().Address().String(),
|
2020-07-29 11:22:51 -07:00
|
|
|
})
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
func() {},
|
|
|
|
},
|
2019-11-06 13:08:02 -08:00
|
|
|
}
|
|
|
|
|
2020-07-29 11:22:51 -07:00
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
suite.SetupTest()
|
2019-11-06 13:08:02 -08:00
|
|
|
|
2020-07-29 11:22:51 -07:00
|
|
|
tc.malleate()
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.NotPanics(func() {
|
|
|
|
evidence.ExportGenesis(suite.ctx, suite.keeper)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
suite.Panics(func() {
|
|
|
|
evidence.ExportGenesis(suite.ctx, suite.keeper)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
tc.posttests()
|
|
|
|
})
|
|
|
|
}
|
2019-11-06 13:08:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenesisTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(GenesisTestSuite))
|
|
|
|
}
|