From 2da954ff616e227fc99c502125ff145165fd4080 Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 29 Jul 2020 20:22:51 +0200 Subject: [PATCH] x/evidence genesis protobuf migration (#6864) * Migrate evidence genesis state to proto * Fix lint error * Remove commented code * Clean up * Add UnpackInterfaces to evidence GenesisState * Add cosmos_proto.accepts_interface to evidence any and fix lint error * Add test for x/evidence ExportGenesis and use table tests * Update x/evidence/types/genesis.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update proto/cosmos/evidence/evidence.proto Co-authored-by: Alexander Bezobchuk * Keep []Evidence as return type in GenEvidences * Add GenesisState Validate tests * Add test case for NewGenesisState * Add tests for GenesisState UnpackInterfaces Co-authored-by: SaReN Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Alexander Bezobchuk --- proto/cosmos/evidence/evidence.proto | 3 +- proto/cosmos/evidence/genesis.proto | 11 + x/evidence/genesis.go | 28 ++- x/evidence/genesis_test.go | 138 ++++++++--- x/evidence/simulation/genesis.go | 2 +- x/evidence/simulation/genesis_test.go | 25 -- x/evidence/types/evidence.pb.go | 54 +++-- x/evidence/types/genesis.go | 47 +++- x/evidence/types/genesis.pb.go | 332 ++++++++++++++++++++++++++ x/evidence/types/genesis_test.go | 184 ++++++++++++-- x/staking/types/staking.pb.go | 11 +- 11 files changed, 708 insertions(+), 127 deletions(-) create mode 100644 proto/cosmos/evidence/genesis.proto create mode 100644 x/evidence/types/genesis.pb.go diff --git a/proto/cosmos/evidence/evidence.proto b/proto/cosmos/evidence/evidence.proto index a418e965c749..94e3728feffe 100644 --- a/proto/cosmos/evidence/evidence.proto +++ b/proto/cosmos/evidence/evidence.proto @@ -7,13 +7,14 @@ option (gogoproto.equal_all) = true; import "gogoproto/gogo.proto"; import "google/protobuf/timestamp.proto"; import "google/protobuf/any.proto"; +import "cosmos_proto/cosmos.proto"; // MsgSubmitEvidence defines an sdk.Msg type that supports submitting arbitrary // Evidence. message MsgSubmitEvidence { option (gogoproto.goproto_getters) = false; bytes submitter = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"]; - google.protobuf.Any evidence = 2; + google.protobuf.Any evidence = 2 [(cosmos_proto.accepts_interface) = "Evidence"]; } // Equivocation implements the Evidence interface and defines evidence of double diff --git a/proto/cosmos/evidence/genesis.proto b/proto/cosmos/evidence/genesis.proto new file mode 100644 index 000000000000..8d9f9feb71ed --- /dev/null +++ b/proto/cosmos/evidence/genesis.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; +package cosmos.evidence; + +option go_package = "github.com/cosmos/cosmos-sdk/x/evidence/types"; + +import "google/protobuf/any.proto"; + +// GenesisState defines the evidence module's genesis state. +message GenesisState { + repeated google.protobuf.Any evidence = 1; +} diff --git a/x/evidence/genesis.go b/x/evidence/genesis.go index 4f8ca4852bc7..a2625ea349ed 100644 --- a/x/evidence/genesis.go +++ b/x/evidence/genesis.go @@ -3,9 +3,12 @@ package evidence import ( "fmt" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/evidence/exported" "github.com/cosmos/cosmos-sdk/x/evidence/keeper" "github.com/cosmos/cosmos-sdk/x/evidence/types" + "github.com/gogo/protobuf/proto" ) // InitGenesis initializes the evidence module's state from a provided genesis @@ -16,17 +19,34 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, gs types.GenesisState) { } for _, e := range gs.Evidence { - if _, ok := k.GetEvidence(ctx, e.Hash()); ok { - panic(fmt.Sprintf("evidence with hash %s already exists", e.Hash())) + evi, ok := e.GetCachedValue().(exported.Evidence) + if !ok { + panic("expected evidence") + } + if _, ok := k.GetEvidence(ctx, evi.Hash()); ok { + panic(fmt.Sprintf("evidence with hash %s already exists", evi.Hash())) } - k.SetEvidence(ctx, e) + k.SetEvidence(ctx, evi) } } // ExportGenesis returns the evidence module's exported genesis. func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState { + e := k.GetAllEvidence(ctx) + evidence := make([]*codectypes.Any, len(e)) + for i, evi := range e { + msg, ok := evi.(proto.Message) + if !ok { + panic(fmt.Errorf("cannot proto marshal %T", evi)) + } + any, err := codectypes.NewAnyWithValue(msg) + if err != nil { + panic(err) + } + evidence[i] = any + } return types.GenesisState{ - Evidence: k.GetAllEvidence(ctx), + Evidence: evidence, } } diff --git a/x/evidence/genesis_test.go b/x/evidence/genesis_test.go index 381321224072..866da26f9a59 100644 --- a/x/evidence/genesis_test.go +++ b/x/evidence/genesis_test.go @@ -1,6 +1,7 @@ package evidence_test import ( + "fmt" "testing" "github.com/stretchr/testify/suite" @@ -31,46 +32,125 @@ func (suite *GenesisTestSuite) SetupTest() { suite.keeper = app.EvidenceKeeper } -func (suite *GenesisTestSuite) TestInitGenesis_Valid() { - pk := ed25519.GenPrivKey() - - 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(), - ConsensusAddress: pk.PubKey().Address().Bytes(), - } +func (suite *GenesisTestSuite) TestInitGenesis() { + var ( + genesisState types.GenesisState + 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(), + ConsensusAddress: pk.PubKey().Address().Bytes(), + } + } + 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(), + ConsensusAddress: pk.PubKey().Address().Bytes(), + } + } + genesisState = types.NewGenesisState(testEvidence) + }, + false, + func() { + suite.Empty(suite.keeper.GetAllEvidence(suite.ctx)) + }, + }, } - suite.NotPanics(func() { - evidence.InitGenesis(suite.ctx, suite.keeper, types.NewGenesisState(testEvidence)) - }) + 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) + }) + } - for _, e := range testEvidence { - _, ok := suite.keeper.GetEvidence(suite.ctx, e.Hash()) - suite.True(ok) + tc.posttests() + }) } } -func (suite *GenesisTestSuite) TestInitGenesis_Invalid() { +func (suite *GenesisTestSuite) TestExportGenesis() { pk := ed25519.GenPrivKey() - testEvidence := make([]exported.Evidence, 100) - for i := 0; i < 100; i++ { - testEvidence[i] = &types.Equivocation{ - Power: 100, - Time: time.Now().UTC(), - ConsensusAddress: pk.PubKey().Address().Bytes(), - } + 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(), + ConsensusAddress: pk.PubKey().Address().Bytes(), + }) + }, + true, + func() {}, + }, } - suite.Panics(func() { - evidence.InitGenesis(suite.ctx, suite.keeper, types.NewGenesisState(testEvidence)) - }) + for _, tc := range testCases { + suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { + suite.SetupTest() - suite.Empty(suite.keeper.GetAllEvidence(suite.ctx)) + 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() + }) + } } func TestGenesisTestSuite(t *testing.T) { diff --git a/x/evidence/simulation/genesis.go b/x/evidence/simulation/genesis.go index 3ca8c07a6adb..9d1aa4c6c966 100644 --- a/x/evidence/simulation/genesis.go +++ b/x/evidence/simulation/genesis.go @@ -31,7 +31,7 @@ func RandomizedGenState(simState *module.SimulationState) { func(r *rand.Rand) { ev = GenEvidences(r, simState.Accounts) }, ) - evidenceGenesis := types.GenesisState{Evidence: ev} + evidenceGenesis := types.NewGenesisState(ev) fmt.Printf("Selected randomly generated %s parameters:\n%s\n", types.ModuleName, codec.MustMarshalJSONIndent(simState.Cdc, evidenceGenesis)) simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(evidenceGenesis) diff --git a/x/evidence/simulation/genesis_test.go b/x/evidence/simulation/genesis_test.go index aa1259b5d022..633f7a8e0ff7 100644 --- a/x/evidence/simulation/genesis_test.go +++ b/x/evidence/simulation/genesis_test.go @@ -18,9 +18,6 @@ import ( // Abonormal scenarios are not tested here. func TestRandomizedGenState(t *testing.T) { cdc := codec.New() - // Make sure to register cdc. - // Otherwise RandomizedGenState will panic! - types.RegisterCodec(cdc) s := rand.NewSource(1) r := rand.New(s) @@ -42,25 +39,3 @@ func TestRandomizedGenState(t *testing.T) { require.Len(t, evidenceGenesis.Evidence, 0) } - -// TestRandomizedGenState tests the execution of RandomizedGenState -// without registering the evidence interfaces. -// We expect the test to panic. -func TestRandomizedGenState1(t *testing.T) { - cdc := codec.New() - - s := rand.NewSource(1) - r := rand.New(s) - - simState := module.SimulationState{ - AppParams: make(simtypes.AppParams), - Cdc: cdc, - Rand: r, - NumBonded: 3, - Accounts: simtypes.RandomAccounts(r, 3), - InitialStake: 1000, - GenState: make(map[string]json.RawMessage), - } - - require.Panicsf(t, func() { simulation.RandomizedGenState(&simState) }, "failed to marshal JSON: Unregistered interface exported.Evidence") -} diff --git a/x/evidence/types/evidence.pb.go b/x/evidence/types/evidence.pb.go index 6baa6aabb85b..6ffe3d3698e8 100644 --- a/x/evidence/types/evidence.pb.go +++ b/x/evidence/types/evidence.pb.go @@ -12,6 +12,7 @@ import ( proto "github.com/gogo/protobuf/proto" github_com_gogo_protobuf_types "github.com/gogo/protobuf/types" _ "github.com/golang/protobuf/ptypes/timestamp" + _ "github.com/regen-network/cosmos-proto" io "io" math "math" math_bits "math/bits" @@ -119,32 +120,33 @@ func init() { func init() { proto.RegisterFile("cosmos/evidence/evidence.proto", fileDescriptor_a2cafccc38cf08ce) } var fileDescriptor_a2cafccc38cf08ce = []byte{ - // 400 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4b, 0xce, 0x2f, 0xce, - 0xcd, 0x2f, 0xd6, 0x4f, 0x2d, 0xcb, 0x4c, 0x49, 0xcd, 0x4b, 0x4e, 0x85, 0x33, 0xf4, 0x0a, 0x8a, - 0xf2, 0x4b, 0xf2, 0x85, 0xf8, 0x21, 0xf2, 0x7a, 0x30, 0x61, 0x29, 0x91, 0xf4, 0xfc, 0xf4, 0x7c, - 0xb0, 0x9c, 0x3e, 0x88, 0x05, 0x51, 0x26, 0x25, 0x9f, 0x9e, 0x9f, 0x9f, 0x9e, 0x93, 0xaa, 0x0f, - 0xe6, 0x25, 0x95, 0xa6, 0xe9, 0x97, 0x64, 0xe6, 0xa6, 0x16, 0x97, 0x24, 0xe6, 0x16, 0x40, 0x15, - 0x48, 0xa2, 0x2b, 0x48, 0xcc, 0xab, 0x84, 0x48, 0x29, 0xcd, 0x61, 0xe4, 0x12, 0xf4, 0x2d, 0x4e, - 0x0f, 0x2e, 0x4d, 0xca, 0xcd, 0x2c, 0x71, 0x85, 0xda, 0x23, 0xe4, 0xcf, 0xc5, 0x59, 0x0c, 0x16, - 0x29, 0x49, 0x2d, 0x92, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x71, 0x32, 0xfc, 0x75, 0x4f, 0x5e, 0x37, - 0x3d, 0xb3, 0x24, 0xa3, 0x34, 0x49, 0x2f, 0x39, 0x3f, 0x57, 0x1f, 0xea, 0x74, 0x08, 0x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0x12, 0x80, 0x9b, 0x02, 0x15, 0xb1, 0xe2, 0x00, 0xf9, 0x7d, - 0xc6, 0x02, 0x79, 0x06, 0x27, 0xef, 0x15, 0x8f, 0xe4, 0x18, 0x4f, 0x3c, 0x92, 0x63, 0xbc, 0xf0, - 0x48, 0x8e, 0xf1, 0xc1, 0x23, 0x39, 0xc6, 0x09, 0x8f, 0xe5, 0x18, 0x2e, 0x3c, 0x96, 0x63, 0xb8, - 0xf1, 0x58, 0x8e, 0x21, 0x0a, 0x7f, 0x7c, 0x54, 0x20, 0xd2, 0x15, 0xd8, 0xd2, 0x24, 0x36, 0x70, - 0x68, 0x18, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x56, 0x68, 0x52, 0xd8, 0x77, 0x02, 0x00, 0x00, + // 416 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x52, 0x3f, 0xeb, 0xd3, 0x40, + 0x18, 0xce, 0xf9, 0xab, 0x3f, 0xe2, 0x59, 0xd0, 0x5f, 0x28, 0x12, 0x3b, 0xdc, 0x95, 0x4c, 0x5d, + 0x7a, 0x41, 0x5d, 0xa4, 0x83, 0xd0, 0x48, 0x27, 0x51, 0x21, 0x3a, 0xb9, 0x94, 0xfc, 0x39, 0xd3, + 0x60, 0x93, 0x8b, 0xb9, 0x4b, 0x35, 0xf8, 0x05, 0x1c, 0x3b, 0x3a, 0x76, 0x14, 0x67, 0x3f, 0x44, + 0x71, 0xea, 0xe8, 0x14, 0x25, 0xfd, 0x06, 0x8e, 0x05, 0x41, 0x7a, 0xb9, 0xb4, 0x60, 0x41, 0x9c, + 0x72, 0xef, 0xfb, 0x3e, 0xf7, 0x3c, 0xcf, 0xfb, 0xe4, 0x20, 0x0a, 0x18, 0x4f, 0x18, 0xb7, 0xe9, + 0x32, 0x0e, 0x69, 0x1a, 0xd0, 0xe3, 0x81, 0x64, 0x39, 0x13, 0xcc, 0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, 0x76, 0x8e, 0xbe, 0xa9, 0xb0, 0xb6, 0xfa, 0x81, 0x81, 0x2b, 0x6f, 0x18, 0x3d, 0x78, + 0x3d, 0x63, 0xef, 0x68, 0x6e, 0x5e, 0x48, 0xc2, 0xa6, 0x30, 0x3e, 0xc0, 0xab, 0x80, 0xa5, 0x9c, + 0xa6, 0xbc, 0xe0, 0x33, 0xaf, 0x59, 0xcf, 0xec, 0xc8, 0x5c, 0x9e, 0xfd, 0xaa, 0xb0, 0x59, 0x7a, + 0xc9, 0x62, 0x6c, 0x9d, 0x41, 0xac, 0x7d, 0x85, 0xc9, 0x7f, 0x64, 0xf6, 0x98, 0xa5, 0xbc, 0x0d, + 0xed, 0xf6, 0x91, 0x45, 0x75, 0xc6, 0xfa, 0x61, 0xf7, 0x4f, 0x6b, 0xac, 0x39, 0x4f, 0x3e, 0xd7, + 0x08, 0x6c, 0x6a, 0x04, 0xb6, 0x35, 0x02, 0x3f, 0x6b, 0x04, 0x56, 0x3b, 0xa4, 0x6d, 0x77, 0x48, + 0xfb, 0xbe, 0x43, 0xda, 0xab, 0x7f, 0xff, 0x9d, 0xf7, 0xa7, 0x07, 0x28, 0x45, 0xfd, 0x4b, 0x99, + 0xc6, 0x83, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x46, 0x6e, 0x6c, 0xb7, 0xa0, 0x02, 0x00, 0x00, } func (this *MsgSubmitEvidence) Equal(that interface{}) bool { diff --git a/x/evidence/types/genesis.go b/x/evidence/types/genesis.go index 2d2e3556f26f..6f25accd3030 100644 --- a/x/evidence/types/genesis.go +++ b/x/evidence/types/genesis.go @@ -3,40 +3,63 @@ package types import ( "fmt" + "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/x/evidence/exported" + proto "github.com/gogo/protobuf/proto" ) -// DONTCOVER - -// GenesisState defines the evidence module's genesis state. -type GenesisState struct { - Evidence []exported.Evidence `json:"evidence" yaml:"evidence"` -} +var _ types.UnpackInterfacesMessage = GenesisState{} +// NewGenesisState creates a new genesis state for the evidence module. func NewGenesisState(e []exported.Evidence) GenesisState { + evidence := make([]*types.Any, len(e)) + for i, evi := range e { + msg, ok := evi.(proto.Message) + if !ok { + panic(fmt.Errorf("cannot proto marshal %T", evi)) + } + any, err := types.NewAnyWithValue(msg) + if err != nil { + panic(err) + } + evidence[i] = any + } return GenesisState{ - Evidence: e, + Evidence: evidence, } } // DefaultGenesisState returns the evidence module's default genesis state. func DefaultGenesisState() GenesisState { return GenesisState{ - Evidence: []exported.Evidence{}, + Evidence: []*types.Any{}, } } // Validate performs basic gensis state validation returning an error upon any // failure. func (gs GenesisState) Validate() error { - for i, e := range gs.Evidence { - if e == nil { - return fmt.Errorf("evidence %d cannot be nil", i) + for _, e := range gs.Evidence { + evi, ok := e.GetCachedValue().(exported.Evidence) + if !ok { + return fmt.Errorf("expected evidence") } - if err := e.ValidateBasic(); err != nil { + if err := evi.ValidateBasic(); err != nil { return err } } return nil } + +// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces +func (gs GenesisState) UnpackInterfaces(unpacker types.AnyUnpacker) error { + for _, any := range gs.Evidence { + var evi exported.Evidence + err := unpacker.UnpackAny(any, &evi) + if err != nil { + return err + } + } + return nil +} diff --git a/x/evidence/types/genesis.pb.go b/x/evidence/types/genesis.pb.go new file mode 100644 index 000000000000..f842db337cde --- /dev/null +++ b/x/evidence/types/genesis.pb.go @@ -0,0 +1,332 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: cosmos/evidence/genesis.proto + +package types + +import ( + fmt "fmt" + types "github.com/cosmos/cosmos-sdk/codec/types" + proto "github.com/gogo/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// GenesisState defines the evidence module's genesis state. +type GenesisState struct { + Evidence []*types.Any `protobuf:"bytes,1,rep,name=evidence,proto3" json:"evidence,omitempty"` +} + +func (m *GenesisState) Reset() { *m = GenesisState{} } +func (m *GenesisState) String() string { return proto.CompactTextString(m) } +func (*GenesisState) ProtoMessage() {} +func (*GenesisState) Descriptor() ([]byte, []int) { + return fileDescriptor_948832e7dde7529c, []int{0} +} +func (m *GenesisState) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *GenesisState) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_GenesisState.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *GenesisState) XXX_Merge(src proto.Message) { + xxx_messageInfo_GenesisState.Merge(m, src) +} +func (m *GenesisState) XXX_Size() int { + return m.Size() +} +func (m *GenesisState) XXX_DiscardUnknown() { + xxx_messageInfo_GenesisState.DiscardUnknown(m) +} + +var xxx_messageInfo_GenesisState proto.InternalMessageInfo + +func (m *GenesisState) GetEvidence() []*types.Any { + if m != nil { + return m.Evidence + } + return nil +} + +func init() { + proto.RegisterType((*GenesisState)(nil), "cosmos.evidence.GenesisState") +} + +func init() { proto.RegisterFile("cosmos/evidence/genesis.proto", fileDescriptor_948832e7dde7529c) } + +var fileDescriptor_948832e7dde7529c = []byte{ + // 183 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4d, 0xce, 0x2f, 0xce, + 0xcd, 0x2f, 0xd6, 0x4f, 0x2d, 0xcb, 0x4c, 0x49, 0xcd, 0x4b, 0x4e, 0xd5, 0x4f, 0x4f, 0xcd, 0x4b, + 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x87, 0x48, 0xeb, 0xc1, 0xa4, + 0xa5, 0x24, 0xd3, 0xf3, 0xf3, 0xd3, 0x73, 0x52, 0xf5, 0xc1, 0xd2, 0x49, 0xa5, 0x69, 0xfa, 0x89, + 0x79, 0x95, 0x10, 0xb5, 0x4a, 0x0e, 0x5c, 0x3c, 0xee, 0x10, 0xcd, 0xc1, 0x25, 0x89, 0x25, 0xa9, + 0x42, 0x06, 0x5c, 0x1c, 0x30, 0x6d, 0x12, 0x8c, 0x0a, 0xcc, 0x1a, 0xdc, 0x46, 0x22, 0x7a, 0x10, + 0xdd, 0x7a, 0x30, 0xdd, 0x7a, 0x8e, 0x79, 0x95, 0x41, 0x70, 0x55, 0x4e, 0xee, 0x27, 0x1e, 0xc9, + 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0, 0x91, 0x1c, 0xe3, 0x84, 0xc7, 0x72, 0x0c, 0x17, 0x1e, + 0xcb, 0x31, 0xdc, 0x78, 0x2c, 0xc7, 0x10, 0xa5, 0x9b, 0x9e, 0x59, 0x92, 0x51, 0x9a, 0xa4, 0x97, + 0x9c, 0x9f, 0xab, 0x0f, 0x75, 0x31, 0x84, 0xd2, 0x2d, 0x4e, 0xc9, 0xd6, 0xaf, 0x40, 0x38, 0xbf, + 0xa4, 0xb2, 0x20, 0xb5, 0x38, 0x89, 0x0d, 0x6c, 0x81, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, 0x31, + 0xaa, 0x2f, 0xff, 0xde, 0x00, 0x00, 0x00, +} + +func (m *GenesisState) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *GenesisState) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Evidence) > 0 { + for iNdEx := len(m.Evidence) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Evidence[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + +func encodeVarintGenesis(dAtA []byte, offset int, v uint64) int { + offset -= sovGenesis(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *GenesisState) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.Evidence) > 0 { + for _, e := range m.Evidence { + l = e.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + } + return n +} + +func sovGenesis(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozGenesis(x uint64) (n int) { + return sovGenesis(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *GenesisState) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: GenesisState: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: GenesisState: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Evidence", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Evidence = append(m.Evidence, &types.Any{}) + if err := m.Evidence[len(m.Evidence)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipGenesis(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthGenesis + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthGenesis + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipGenesis(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowGenesis + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowGenesis + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowGenesis + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthGenesis + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupGenesis + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthGenesis + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthGenesis = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowGenesis = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupGenesis = fmt.Errorf("proto: unexpected end of group") +) diff --git a/x/evidence/types/genesis_test.go b/x/evidence/types/genesis_test.go index c905c6318267..7526fdd1833f 100644 --- a/x/evidence/types/genesis_test.go +++ b/x/evidence/types/genesis_test.go @@ -1,12 +1,16 @@ package types_test import ( + "fmt" "testing" "time" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" + tmbytes "github.com/tendermint/tendermint/libs/bytes" + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/x/evidence/exported" "github.com/cosmos/cosmos-sdk/x/evidence/types" ) @@ -17,36 +21,168 @@ func TestDefaultGenesisState(t *testing.T) { require.Len(t, gs.Evidence, 0) } -func TestGenesisStateValidate_Valid(t *testing.T) { - pk := ed25519.GenPrivKey() +func TestNewGenesisState(t *testing.T) { + var ( + evidence []exported.Evidence + ) - evidence := make([]exported.Evidence, 100) - for i := 0; i < 100; i++ { - evidence[i] = &types.Equivocation{ - Height: int64(i) + 1, - Power: 100, - Time: time.Now().UTC(), - ConsensusAddress: pk.PubKey().Address().Bytes(), - } + testCases := []struct { + msg string + malleate func() + expPass bool + }{ + { + "cannot proto marshal", + func() { + evidence = []exported.Evidence{&TestEvidence{}} + }, + false, + }, } - gs := types.NewGenesisState(evidence) - require.NoError(t, gs.Validate()) + for _, tc := range testCases { + t.Run(fmt.Sprintf("Case %s", tc.msg), func(t *testing.T) { + tc.malleate() + + if tc.expPass { + require.NotPanics(t, func() { + types.NewGenesisState(evidence) + }) + } else { + require.Panics(t, func() { + types.NewGenesisState(evidence) + }) + } + }) + } } -func TestGenesisStateValidate_Invalid(t *testing.T) { - pk := ed25519.GenPrivKey() +func TestGenesisStateValidate(t *testing.T) { + var ( + genesisState types.GenesisState + testEvidence []exported.Evidence + pk = ed25519.GenPrivKey() + ) + + testCases := []struct { + msg string + malleate func() + expPass bool + }{ + { + "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(), + ConsensusAddress: pk.PubKey().Address().Bytes(), + } + } + genesisState = types.NewGenesisState(testEvidence) + }, + true, + }, + { + "invalid", + func() { + testEvidence = make([]exported.Evidence, 100) + for i := 0; i < 100; i++ { + testEvidence[i] = &types.Equivocation{ + Height: int64(i), + Power: 100, + Time: time.Now().UTC(), + ConsensusAddress: pk.PubKey().Address().Bytes(), + } + } + genesisState = types.NewGenesisState(testEvidence) + }, + false, + }, + { + "expected evidence", + func() { + genesisState = types.GenesisState{ + Evidence: []*codectypes.Any{&codectypes.Any{}}, + } + }, + false, + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("Case %s", tc.msg), func(t *testing.T) { + tc.malleate() + + if tc.expPass { + require.NoError(t, genesisState.Validate()) + } else { + require.Error(t, genesisState.Validate()) + } + }) + } +} + +func TestUnpackInterfaces(t *testing.T) { + var gs = types.GenesisState{ + Evidence: []*codectypes.Any{&codectypes.Any{}}, + } + + testCases := []struct { + msg string + unpacker codectypes.AnyUnpacker + expPass bool + }{ + { + "success", + codectypes.NewInterfaceRegistry(), + true, + }, + { + "error", + codec.New(), + false, + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("Case %s", tc.msg), func(t *testing.T) { - evidence := make([]exported.Evidence, 100) - for i := 0; i < 100; i++ { - evidence[i] = &types.Equivocation{ - Height: int64(i), - Power: 100, - Time: time.Now().UTC(), - ConsensusAddress: pk.PubKey().Address().Bytes(), - } + if tc.expPass { + require.NoError(t, gs.UnpackInterfaces(tc.unpacker)) + } else { + require.Error(t, gs.UnpackInterfaces(tc.unpacker)) + } + }) } +} + +type TestEvidence struct{} + +var _ exported.Evidence = &TestEvidence{} + +func (*TestEvidence) Route() string { + return "test-route" +} + +func (*TestEvidence) Type() string { + return "test-type" +} + +func (*TestEvidence) String() string { + return "test-string" +} + +func (*TestEvidence) Hash() tmbytes.HexBytes { + return tmbytes.HexBytes([]byte("test-hash")) +} + +func (*TestEvidence) ValidateBasic() error { + return nil +} - gs := types.NewGenesisState(evidence) - require.Error(t, gs.Validate()) +func (*TestEvidence) GetHeight() int64 { + return 0 } diff --git a/x/staking/types/staking.pb.go b/x/staking/types/staking.pb.go index 93226ea42ae4..1d60e583f304 100644 --- a/x/staking/types/staking.pb.go +++ b/x/staking/types/staking.pb.go @@ -7,6 +7,12 @@ import ( bytes "bytes" compress_gzip "compress/gzip" fmt "fmt" + io "io" + io_ioutil "io/ioutil" + math "math" + math_bits "math/bits" + time "time" + github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types" types "github.com/cosmos/cosmos-sdk/types" _ "github.com/gogo/protobuf/gogoproto" @@ -17,11 +23,6 @@ import ( _ "github.com/golang/protobuf/ptypes/duration" _ "github.com/golang/protobuf/ptypes/timestamp" types1 "github.com/tendermint/tendermint/abci/types" - io "io" - io_ioutil "io/ioutil" - math "math" - math_bits "math/bits" - time "time" ) // Reference imports to suppress errors if they are not otherwise used.