Skip to content

Commit

Permalink
feat(x): decode modules genesis state for testnetify command (#1812)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian committed May 3, 2023
1 parent 52829d7 commit 0b17c6c
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 3 deletions.
15 changes: 15 additions & 0 deletions x/audit/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package audit

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"

Expand Down Expand Up @@ -29,3 +32,15 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
func DefaultGenesisState() *types.GenesisState {
return &types.GenesisState{}
}

// GetGenesisStateFromAppState returns x/audit GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *types.GenesisState {
var genesisState types.GenesisState

if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}

return &genesisState
}
17 changes: 16 additions & 1 deletion x/deployment/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package deployment

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"

Expand All @@ -11,7 +14,7 @@ import (
"github.com/akash-network/node/x/deployment/keeper"
)

// ValidateGenesis does validation check of the Genesis and return error incase of failure
// ValidateGenesis does validation check of the Genesis and return error in case of failure
func ValidateGenesis(data *types.GenesisState) error {
for _, record := range data.Deployments {
if err := record.Deployment.ID().Validate(); err != nil {
Expand Down Expand Up @@ -58,3 +61,15 @@ func ExportGenesis(ctx sdk.Context, k keeper.IKeeper) *types.GenesisState {
Params: params,
}
}

// GetGenesisStateFromAppState returns x/deployment GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *types.GenesisState {
var genesisState types.GenesisState

if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}

return &genesisState
}
15 changes: 15 additions & 0 deletions x/gov/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package provider

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"

Expand Down Expand Up @@ -37,3 +40,15 @@ func ExportGenesis(ctx sdk.Context, k keeper.IKeeper) *types.GenesisState {
DepositParams: k.GetDepositParams(ctx),
}
}

// GetGenesisStateFromAppState returns x/gov GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *types.GenesisState {
var genesisState types.GenesisState

if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}

return &genesisState
}
15 changes: 15 additions & 0 deletions x/inflation/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package inflation

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

abci "github.com/tendermint/tendermint/abci/types"
Expand Down Expand Up @@ -36,3 +39,15 @@ func ExportGenesis(ctx sdk.Context, k keeper.IKeeper) *types.GenesisState {
Params: params,
}
}

// GetGenesisStateFromAppState returns x/inflation GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *types.GenesisState {
var genesisState types.GenesisState

if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}

return &genesisState
}
44 changes: 43 additions & 1 deletion x/market/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package market

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"

Expand All @@ -11,7 +14,11 @@ import (

// ValidateGenesis does validation check of the Genesis
func ValidateGenesis(data *types.GenesisState) error {
return data.Params.Validate()
if err := data.Params.Validate(); err != nil {
return err
}

return nil
}

// DefaultGenesisState returns default genesis state as raw bytes for the market
Expand All @@ -31,7 +38,42 @@ func InitGenesis(ctx sdk.Context, keeper keeper.IKeeper, data *types.GenesisStat
// ExportGenesis returns genesis state as raw bytes for the market module
func ExportGenesis(ctx sdk.Context, k keeper.IKeeper) *types.GenesisState {
params := k.GetParams(ctx)

var bids []types.Bid
var leases []types.Lease
var orders []types.Order

k.WithLeases(ctx, func(lease types.Lease) bool {
leases = append(leases, lease)
return false
})

k.WithOrders(ctx, func(order types.Order) bool {
orders = append(orders, order)
return false
})

k.WithBids(ctx, func(bid types.Bid) bool {
bids = append(bids, bid)
return false
})

return &types.GenesisState{
Params: params,
Orders: orders,
Leases: leases,
Bids: bids,
}
}

// GetGenesisStateFromAppState returns x/market GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *types.GenesisState {
var genesisState types.GenesisState

if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}

return &genesisState
}
26 changes: 25 additions & 1 deletion x/provider/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package provider

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"

Expand All @@ -21,11 +24,32 @@ func InitGenesis(ctx sdk.Context, keeper keeper.IKeeper, data *types.GenesisStat

// ExportGenesis returns genesis state as raw bytes for the provider module
func ExportGenesis(ctx sdk.Context, k keeper.IKeeper) *types.GenesisState {
return &types.GenesisState{}
var providers []types.Provider

k.WithProviders(ctx, func(provider types.Provider) bool {
providers = append(providers, provider)
return true
})

return &types.GenesisState{
Providers: providers,
}
}

// DefaultGenesisState returns default genesis state as raw bytes for the provider
// module.
func DefaultGenesisState() *types.GenesisState {
return &types.GenesisState{}
}

// GetGenesisStateFromAppState returns x/provider GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *types.GenesisState {
var genesisState types.GenesisState

if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}

return &genesisState
}

0 comments on commit 0b17c6c

Please sign in to comment.