Skip to content

Commit

Permalink
Refactored plan tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Jul 17, 2021
1 parent c7f58e0 commit c57779d
Show file tree
Hide file tree
Showing 11 changed files with 588 additions and 406 deletions.
30 changes: 23 additions & 7 deletions x/plan/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import (

func InitGenesis(ctx sdk.Context, k keeper.Keeper, state types.GenesisState) {
for _, item := range state {
k.SetPlan(ctx, item.Plan)
var (
provider = item.Plan.GetProvider()
)

k.SetPlan(ctx, item.Plan)
if item.Plan.Status.Equal(hubtypes.StatusActive) {
k.SetActivePlan(ctx, item.Plan.Id)
k.SetActivePlanForProvider(ctx, item.Plan.GetProvider(), item.Plan.Id)
k.SetActivePlanForProvider(ctx, provider, item.Plan.Id)
} else {
k.SetInactivePlan(ctx, item.Plan.Id)
k.SetInactivePlanForProvider(ctx, item.Plan.GetProvider(), item.Plan.Id)
k.SetInactivePlanForProvider(ctx, provider, item.Plan.Id)
}

for _, node := range item.Nodes {
Expand All @@ -27,20 +30,33 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, state types.GenesisState) {
}

k.SetNodeForPlan(ctx, item.Plan.Id, address)
k.IncreaseCountForNodeByProvider(ctx, provider, address)
}
}

var (
count uint64 = 0
)

for _, item := range state {
if item.Plan.Id > count {
count = item.Plan.Id
}
}

k.SetCount(ctx, uint64(len(state)))
k.SetCount(ctx, count)
}

func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
plans := k.GetPlans(ctx, 0, 0)
var (
plans = k.GetPlans(ctx, 0, 0)
items = make(types.GenesisPlans, 0, len(plans))
)

items := make(types.GenesisPlans, 0, len(plans))
for _, plan := range plans {
item := types.GenesisPlan{
Plan: plan,
Nodes: nil,
Nodes: []string{},
}

nodes := k.GetNodesForPlan(ctx, plan.Id, 0, 0)
Expand Down
6 changes: 1 addition & 5 deletions x/plan/keeper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

hubtypes "github.com/sentinel-official/hub/types"
nodetypes "github.com/sentinel-official/hub/x/node/types"
)

func (k *Keeper) GetAccount(ctx sdk.Context, address sdk.AccAddress) authtypes.AccountI {
return k.account.GetAccount(ctx, address)
}

func (k *Keeper) HasProvider(ctx sdk.Context, address hubtypes.ProvAddress) bool {
return k.provider.HasProvider(ctx, address)
}
Expand Down
5 changes: 0 additions & 5 deletions x/plan/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type Keeper struct {
key sdk.StoreKey
provider expected.ProviderKeeper
node expected.NodeKeeper
account expected.AccountKeeper
}

func NewKeeper(cdc codec.BinaryMarshaler, key sdk.StoreKey) Keeper {
Expand All @@ -27,10 +26,6 @@ func NewKeeper(cdc codec.BinaryMarshaler, key sdk.StoreKey) Keeper {
}
}

func (k *Keeper) WithAccountKeeper(keeper expected.AccountKeeper) {
k.account = keeper
}

func (k *Keeper) WithProviderKeeper(keeper expected.ProviderKeeper) {
k.provider = keeper
}
Expand Down
52 changes: 35 additions & 17 deletions x/plan/simulation/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,64 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/kv"
protobuf "github.com/gogo/protobuf/types"
protobuftypes "github.com/gogo/protobuf/types"

"github.com/sentinel-official/hub/x/plan/types"
)

func NewDecoderStore(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string {
decoderFn := func(kvA, kvB kv.Pair) string {
func NewStoreDecoder(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string {
return func(kvA, kvB kv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.CountKey):
var countA, countB protobuf.UInt64Value
var countA, countB protobuftypes.UInt64Value
cdc.MustUnmarshalBinaryBare(kvA.Value, &countA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &countB)
return fmt.Sprintf("%s\n%s", &countA, &countB)

return fmt.Sprintf("%v\n%v", &countA, &countB)
case bytes.Equal(kvA.Key[:1], types.PlanKeyPrefix):
var planA, planB types.Plan
cdc.MustUnmarshalBinaryBare(kvA.Value, &planA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &planB)
return fmt.Sprintf("%s\n%s", &planA, &planB)

return fmt.Sprintf("%v\n%v", &planA, &planB)
case bytes.Equal(kvA.Key[:1], types.ActivePlanKeyPrefix):
var acivePlanA, activePlanB protobuf.BoolValue
cdc.MustUnmarshalBinaryBare(kvA.Value, &acivePlanA)
var activePlanA, activePlanB protobuftypes.BoolValue
cdc.MustUnmarshalBinaryBare(kvA.Value, &activePlanA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &activePlanB)
return fmt.Sprintf("%s\n%s", &acivePlanA, &activePlanB)

return fmt.Sprintf("%v\n%v", &activePlanA, &activePlanB)
case bytes.Equal(kvA.Key[:1], types.InactivePlanKeyPrefix):
var inacivePlanA, inactivePlanB protobuf.BoolValue
cdc.MustUnmarshalBinaryBare(kvA.Value, &inacivePlanA)
var inactivePlanA, inactivePlanB protobuftypes.BoolValue
cdc.MustUnmarshalBinaryBare(kvA.Value, &inactivePlanA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &inactivePlanB)
return fmt.Sprintf("%s\n%s", &inacivePlanA, &inactivePlanB)

return fmt.Sprintf("%v\n%v", &inactivePlanA, &inactivePlanB)
case bytes.Equal(kvA.Key[:1], types.ActivePlanForProviderKeyPrefix):
var activePlanForProviderA, activePlanForProviderB protobuftypes.BoolValue
cdc.MustUnmarshalBinaryBare(kvA.Value, &activePlanForProviderA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &activePlanForProviderB)

return fmt.Sprintf("%v\n%v", &activePlanForProviderA, &activePlanForProviderB)
case bytes.Equal(kvA.Key[:1], types.InactivePlanForProviderKeyPrefix):
var inactivePlanForProviderA, inactivePlanForProviderB protobuftypes.BoolValue
cdc.MustUnmarshalBinaryBare(kvA.Value, &inactivePlanForProviderA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &inactivePlanForProviderB)

return fmt.Sprintf("%v\n%v", &inactivePlanForProviderA, &inactivePlanForProviderB)
case bytes.Equal(kvA.Key[:1], types.NodeForPlanKeyPrefix):
var nodeForPlanA, nodeForPlanB protobuftypes.BoolValue
cdc.MustUnmarshalBinaryBare(kvA.Value, &nodeForPlanA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &nodeForPlanB)

return fmt.Sprintf("%v\n%v", &nodeForPlanA, &nodeForPlanB)
case bytes.Equal(kvA.Key[:1], types.CountForNodeByProviderKeyPrefix):
var countForNodeByProviderA, countForNodeByProviderB protobuf.UInt64Value
var countForNodeByProviderA, countForNodeByProviderB protobuftypes.UInt64Value
cdc.MustUnmarshalBinaryBare(kvA.Value, &countForNodeByProviderA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &countForNodeByProviderB)
return fmt.Sprintf("%s\n%s", &countForNodeByProviderA, &countForNodeByProviderB)

return fmt.Sprintf("%v\n%v", &countForNodeByProviderA, &countForNodeByProviderB)
}

panic(fmt.Sprintf("invalid plan key prefix: %X", kvA.Key[:1]))
panic(fmt.Sprintf("invalid key prefix %X", kvA.Key[:1]))
}

return decoderFn
}
24 changes: 5 additions & 19 deletions x/plan/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@ package simulation

import (
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/sentinel-official/hub/x/plan/types"
)

const (
dayDurationInSeconds = 86400
weekDurationInSeconds = dayDurationInSeconds * 7
monthDurationInSeconds = dayDurationInSeconds * 30
gigabytes = 1024 << 20
terabytes = gigabytes * 1024
"github.com/sentinel-official/hub/x/plan/types"
)

func RandomizedGenState(simState *module.SimulationState) types.GenesisState {
var plans types.GenesisPlans

for _, plan := range getRandomPlans(simState.Rand) {
plans = append(plans, types.GenesisPlan{
Plan: plan,
Nodes: getRandomNodes(simState.Rand),
})
}

return types.NewGenesisState(plans)
func RandomizedGenesisState(state *module.SimulationState) types.GenesisState {
return types.NewGenesisState(
RandomGenesisPlans(state.Rand),
)
}
Loading

0 comments on commit c57779d

Please sign in to comment.