2019-11-06 13:08:02 -08:00
|
|
|
package evidence
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-08-06 01:32:19 -07:00
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
|
2020-07-29 11:22:51 -07:00
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2019-11-06 13:08:02 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-07-29 11:22:51 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
|
2020-06-14 02:58:48 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/evidence/keeper"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/evidence/types"
|
2019-11-06 13:08:02 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// InitGenesis initializes the evidence module's state from a provided genesis
|
|
|
|
// state.
|
2020-08-11 07:22:30 -07:00
|
|
|
func InitGenesis(ctx sdk.Context, k keeper.Keeper, gs *types.GenesisState) {
|
2019-11-06 13:08:02 -08:00
|
|
|
if err := gs.Validate(); err != nil {
|
2020-06-14 02:58:48 -07:00
|
|
|
panic(fmt.Sprintf("failed to validate %s genesis state: %s", types.ModuleName, err))
|
2019-11-06 13:08:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range gs.Evidence {
|
2020-07-29 11:22:51 -07:00
|
|
|
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()))
|
2019-11-06 13:08:02 -08:00
|
|
|
}
|
|
|
|
|
2020-07-29 11:22:51 -07:00
|
|
|
k.SetEvidence(ctx, evi)
|
2019-11-06 13:08:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportGenesis returns the evidence module's exported genesis.
|
2020-08-11 07:22:30 -07:00
|
|
|
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
|
2020-07-29 11:22:51 -07:00
|
|
|
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
|
|
|
|
}
|
2020-08-11 07:22:30 -07:00
|
|
|
return &types.GenesisState{
|
2020-07-29 11:22:51 -07:00
|
|
|
Evidence: evidence,
|
2019-11-06 13:08:02 -08:00
|
|
|
}
|
|
|
|
}
|