Skip to content

Commit

Permalink
Merge pull request #300 from lavanet/CNS-268-implement-package-module
Browse files Browse the repository at this point in the history
CNS-268: Implement Plans module
  • Loading branch information
Yaroms committed Mar 2, 2023
2 parents e81f52f + 3e67ec4 commit 7d0c406
Show file tree
Hide file tree
Showing 65 changed files with 6,317 additions and 63 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/consensus_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@ jobs:
- name: lava conflict unit Tests
run: go test ./x/conflict/ ./x/conflict/keeper ./x/conflict/types -v

- name: lava plans unit Tests
run: go test ./x/plans/ ./x/plans/keeper ./x/plans/types -v

24 changes: 24 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ import (
pairingmodule "github.com/lavanet/lava/x/pairing"
pairingmodulekeeper "github.com/lavanet/lava/x/pairing/keeper"
pairingmoduletypes "github.com/lavanet/lava/x/pairing/types"
plans "github.com/lavanet/lava/x/plans"
plansmoduleclient "github.com/lavanet/lava/x/plans/client"
plansmodulekeeper "github.com/lavanet/lava/x/plans/keeper"
plansmoduletypes "github.com/lavanet/lava/x/plans/types"
"github.com/lavanet/lava/x/spec"
specmoduleclient "github.com/lavanet/lava/x/spec/client"
specmodulekeeper "github.com/lavanet/lava/x/spec/keeper"
Expand Down Expand Up @@ -146,6 +150,7 @@ func getGovProposalHandlers() []govclient.ProposalHandler {
ibcclientclient.UpdateClientProposalHandler,
ibcclientclient.UpgradeProposalHandler,
specmoduleclient.SpecAddProposalHandler,
plansmoduleclient.PlansAddProposalHandler,
// this line is used by starport scaffolding # stargate/app/govProposalHandler
)

Expand Down Expand Up @@ -181,6 +186,7 @@ var (
epochstoragemodule.AppModuleBasic{},
pairingmodule.AppModuleBasic{},
conflictmodule.AppModuleBasic{},
plans.AppModuleBasic{},
// this line is used by starport scaffolding # stargate/app/moduleBasic
)

Expand Down Expand Up @@ -277,6 +283,7 @@ func New(
epochstoragemoduletypes.StoreKey,
pairingmoduletypes.StoreKey,
conflictmoduletypes.StoreKey,
plansmoduletypes.StoreKey,
// this line is used by starport scaffolding # stargate/app/storeKey
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
Expand Down Expand Up @@ -361,6 +368,15 @@ func New(
)
specModule := spec.NewAppModule(appCodec, app.SpecKeeper, app.AccountKeeper, app.BankKeeper)

// Initialize PackagesKeeper prior to govRouter (order is critical)
app.PlansKeeper = *plansmodulekeeper.NewKeeper(
appCodec,
keys[plansmoduletypes.StoreKey],
keys[plansmoduletypes.MemStoreKey],
app.GetSubspace(plansmoduletypes.ModuleName),
)
plansModule := plans.NewAppModule(appCodec, app.PlansKeeper)

// register the proposal types
govRouter := govtypes.NewRouter()
govRouter.AddRoute(govtypes.RouterKey, govtypes.ProposalHandler).
Expand All @@ -369,6 +385,8 @@ func New(
AddRoute(specmoduletypes.ProposalsRouterKey, spec.NewSpecProposalsHandler(app.SpecKeeper)).
// copied the code from param and changed the handler to enable functionality
AddRoute(paramproposal.RouterKey, spec.NewParamChangeProposalHandler(app.ParamsKeeper)).
// user defined
AddRoute(plansmoduletypes.ProposalsRouterKey, plans.NewPlansProposalsHandler(app.PlansKeeper)).

//
// default
Expand Down Expand Up @@ -479,6 +497,7 @@ func New(
epochstorageModule,
pairingModule,
conflictModule,
plansModule,
// this line is used by starport scaffolding # stargate/app/appModule
)

Expand All @@ -504,6 +523,7 @@ func New(
epochstoragemoduletypes.ModuleName,
conflictmoduletypes.ModuleName, // conflict needs to change state before pairing changes stakes
pairingmoduletypes.ModuleName,
plansmoduletypes.ModuleName,
vestingtypes.ModuleName,
upgradetypes.ModuleName,
feegrant.ModuleName,
Expand All @@ -527,6 +547,7 @@ func New(
epochstoragemoduletypes.ModuleName,
conflictmoduletypes.ModuleName,
pairingmoduletypes.ModuleName,
plansmoduletypes.ModuleName,
vestingtypes.ModuleName,
upgradetypes.ModuleName,
feegrant.ModuleName,
Expand Down Expand Up @@ -554,6 +575,7 @@ func New(
specmoduletypes.ModuleName,
epochstoragemoduletypes.ModuleName, // epochStyorage end block must come before pairing for proper epoch handling
pairingmoduletypes.ModuleName,
plansmoduletypes.ModuleName,
vestingtypes.ModuleName,
upgradetypes.ModuleName,
feegrant.ModuleName,
Expand Down Expand Up @@ -589,6 +611,7 @@ func New(
epochstorageModule,
pairingModule,
conflictModule,
plansModule,
// this line is used by starport scaffolding # stargate/app/appModule
)
app.sm.RegisterStoreDecoders()
Expand Down Expand Up @@ -807,6 +830,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(epochstoragemoduletypes.ModuleName)
paramsKeeper.Subspace(pairingmoduletypes.ModuleName)
paramsKeeper.Subspace(conflictmoduletypes.ModuleName)
paramsKeeper.Subspace(plansmoduletypes.ModuleName)
// this line is used by starport scaffolding # stargate/app/paramSubspace

return paramsKeeper
Expand Down
2 changes: 2 additions & 0 deletions app/keepers/lavaKeepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
conflictmodulekeeper "github.com/lavanet/lava/x/conflict/keeper"
epochstoragemodulekeeper "github.com/lavanet/lava/x/epochstorage/keeper"
pairingmodulekeeper "github.com/lavanet/lava/x/pairing/keeper"
plansmodulekeeper "github.com/lavanet/lava/x/plans/keeper"
specmodulekeeper "github.com/lavanet/lava/x/spec/keeper"
// this line is used by starport scaffolding # stargate/app/moduleImport
)
Expand Down Expand Up @@ -50,4 +51,5 @@ type LavaKeepers struct {
EpochstorageKeeper epochstoragemodulekeeper.Keeper
PairingKeeper pairingmodulekeeper.Keeper
ConflictKeeper conflictmodulekeeper.Keeper
PlansKeeper plansmodulekeeper.Keeper
}
44 changes: 18 additions & 26 deletions common/fixation_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (fs *FixationStore) AppendEntry(ctx sdk.Context, index string, block uint64

// get the relevant store
store := prefix.NewStore(ctx.KVStore(fs.storeKey), types.KeyPrefix(fs.createStoreKey(index)))
byteKey := types.KeyPrefix(createEntryKey(block))
byteKey := types.EncodeKey(block)

// set the new entry to the store
store.Set(byteKey, marshaledEntry)
Expand Down Expand Up @@ -134,7 +134,7 @@ func (fs *FixationStore) deleteStaleEntries(ctx sdk.Context, index string) {
func (fs *FixationStore) ModifyEntry(ctx sdk.Context, index string, block uint64, entryData codec.ProtoMarshaler) error {
// get the relevant store
store := prefix.NewStore(ctx.KVStore(fs.storeKey), types.KeyPrefix(fs.createStoreKey(index)))
byteKey := types.KeyPrefix(createEntryKey(block))
byteKey := types.EncodeKey(block)

// marshal the new entry data
marshaledEntryData := fs.cdc.MustMarshal(entryData)
Expand Down Expand Up @@ -200,99 +200,91 @@ func (fs *FixationStore) getUnmarshaledEntryForBlock(ctx sdk.Context, index stri
}

// get entry with index and block without influencing the entry's refs
func (fs *FixationStore) FindEntry(ctx sdk.Context, index string, block uint64, entryData codec.ProtoMarshaler) error {
func (fs *FixationStore) FindEntry(ctx sdk.Context, index string, block uint64, entryData codec.ProtoMarshaler) (error, bool) {
// get the unmarshaled entry for block
entry, found := fs.getUnmarshaledEntryForBlock(ctx, index, block)
if !found {
return types.ErrEntryNotFound
return types.ErrEntryNotFound, false
}

// unmarshal the entry's data
err := fs.cdc.Unmarshal(entry.GetData(), entryData)
if err != nil {
return utils.LavaError(ctx, ctx.Logger(), "GetEntry_cant_unmarshal", map[string]string{"err": err.Error()}, "can't unmarshal entry data")
return utils.LavaError(ctx, ctx.Logger(), "GetEntry_cant_unmarshal", map[string]string{"err": err.Error()}, "can't unmarshal entry data"), false
}

return nil
return nil, true
}

// get entry with index and block with ref increase
func (fs *FixationStore) GetEntry(ctx sdk.Context, index string, block uint64, entryData codec.ProtoMarshaler) error {
func (fs *FixationStore) GetEntry(ctx sdk.Context, index string, block uint64, entryData codec.ProtoMarshaler) (error, bool) {
// get the unmarshaled entry for block
entry, found := fs.getUnmarshaledEntryForBlock(ctx, index, block)
if !found {
return types.ErrEntryNotFound
return types.ErrEntryNotFound, false
}

// unmarshal the entry's data
err := fs.cdc.Unmarshal(entry.GetData(), entryData)
if err != nil {
return utils.LavaError(ctx, ctx.Logger(), "GetEntry_cant_unmarshal", map[string]string{"err": err.Error()}, "can't unmarshal entry data")
return utils.LavaError(ctx, ctx.Logger(), "GetEntry_cant_unmarshal", map[string]string{"err": err.Error()}, "can't unmarshal entry data"), false
}

entry.Refcount += 1

// get the relevant byte
store := prefix.NewStore(ctx.KVStore(fs.storeKey), types.KeyPrefix(fs.createStoreKey(index)))
byteKey := types.KeyPrefix(createEntryKey(entry.GetBlock()))
byteKey := types.EncodeKey(block)

// marshal the entry
marshaledEntry := fs.cdc.MustMarshal(&entry)

// set the entry
store.Set(byteKey, marshaledEntry)

return nil
return nil, true
}

// get entry with index and block with ref decrease
func (fs *FixationStore) PutEntry(ctx sdk.Context, index string, block uint64, entryData codec.ProtoMarshaler) error {
func (fs *FixationStore) PutEntry(ctx sdk.Context, index string, block uint64, entryData codec.ProtoMarshaler) (error, bool) {
// get the unmarshaled entry for block
entry, found := fs.getUnmarshaledEntryForBlock(ctx, index, block)
if !found {
return types.ErrEntryNotFound
return types.ErrEntryNotFound, false
}

// unmarshal the entry's data
err := fs.cdc.Unmarshal(entry.GetData(), entryData)
if err != nil {
return utils.LavaError(ctx, ctx.Logger(), "GetEntry_cant_unmarshal", map[string]string{"err": err.Error()}, "can't unmarshal entry data")
return utils.LavaError(ctx, ctx.Logger(), "GetEntry_cant_unmarshal", map[string]string{"err": err.Error()}, "can't unmarshal entry data"), false
}

if entry.GetRefcount() > 0 {
entry.Refcount -= 1
} else {
return utils.LavaError(ctx, ctx.Logger(), "handleRefAction_sub_ref_from_non_positive_count", map[string]string{"refCount": strconv.FormatUint(entry.GetRefcount(), 10)}, "refCount is not larger than zero. Can't subtract refcount")
return utils.LavaError(ctx, ctx.Logger(), "handleRefAction_sub_ref_from_non_positive_count", map[string]string{"refCount": strconv.FormatUint(entry.GetRefcount(), 10)}, "refCount is not larger than zero. Can't subtract refcount"), false
}

// get the relevant byte
store := prefix.NewStore(ctx.KVStore(fs.storeKey), types.KeyPrefix(fs.createStoreKey(index)))
byteKey := types.KeyPrefix(createEntryKey(entry.GetBlock()))
byteKey := types.EncodeKey(block)

// marshal the entry
marshaledEntry := fs.cdc.MustMarshal(&entry)

// set the entry
store.Set(byteKey, marshaledEntry)

return nil
return nil, true
}

// RemoveEntry removes an entry from the store
func (fs *FixationStore) removeEntry(ctx sdk.Context, index string, block uint64) {
// get the relevant store
store := prefix.NewStore(ctx.KVStore(fs.storeKey), types.KeyPrefix(fs.createStoreKey(index)))

// create entry's key
entryKey := createEntryKey(block)

// delete the entry
store.Delete(types.KeyPrefix(entryKey))
}

// createEntryKey creates an entry key for the KVStore
func createEntryKey(block uint64) string {
return strconv.FormatUint(block, 10)
store.Delete(types.EncodeKey(block))
}

func (fs *FixationStore) createStoreKey(index string) string {
Expand Down
Loading

0 comments on commit 7d0c406

Please sign in to comment.