Skip to content

Commit

Permalink
Merge pull request #859 from RexMilce/ft/timerstore/module
Browse files Browse the repository at this point in the history
feat(all): create timerstore module and migrate all modules to use it.
  • Loading branch information
Yaroms authored Oct 4, 2023
2 parents 0c6c09e + 6b1d50e commit 0ec47fe
Show file tree
Hide file tree
Showing 23 changed files with 173 additions and 48 deletions.
14 changes: 13 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/runtime"
consensusparamkeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
"github.com/lavanet/lava/x/fixationstore"
"github.com/lavanet/lava/x/timerstore"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
Expand Down Expand Up @@ -178,6 +179,7 @@ var (
// non-dependant module elements, such as codec registration
// and genesis verification.
ModuleBasics = module.NewBasicManager(
timerstore.AppModuleBasic{},
fixationstore.AppModuleBasic{},
auth.AppModuleBasic{},
genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator),
Expand Down Expand Up @@ -399,8 +401,11 @@ func New(
appCodec, keys[ibcexported.StoreKey], app.GetSubspace(ibcexported.ModuleName), app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper,
)

// timerstore keeper
app.TimerStoreKeeper = timerstore.NewKeeper(appCodec)

// fixation store keeper
app.FixationStoreKeeper = fixationstore.NewKeeper(appCodec)
app.FixationStoreKeeper = fixationstore.NewKeeper(appCodec, app.TimerStoreKeeper)

// Initialize SpecKeeper prior to govRouter (order is critical)
app.SpecKeeper = *specmodulekeeper.NewKeeper(
Expand Down Expand Up @@ -457,6 +462,7 @@ func New(
app.ProjectsKeeper,
app.PlansKeeper,
app.FixationStoreKeeper,
app.TimerStoreKeeper,
)
subscriptionModule := subscriptionmodule.NewAppModule(appCodec, app.SubscriptionKeeper, app.AccountKeeper, app.BankKeeper)

Expand All @@ -471,6 +477,7 @@ func New(
app.EpochstorageKeeper,
app.SpecKeeper,
app.FixationStoreKeeper,
app.TimerStoreKeeper,
)
dualstakingModule := dualstakingmodule.NewAppModule(appCodec, app.DualstakingKeeper, app.AccountKeeper, app.BankKeeper)

Expand All @@ -494,6 +501,7 @@ func New(
app.DowntimeKeeper,
app.DualstakingKeeper,
app.FixationStoreKeeper,
app.TimerStoreKeeper,
)
pairingModule := pairingmodule.NewAppModule(appCodec, app.PairingKeeper, app.AccountKeeper, app.BankKeeper)

Expand Down Expand Up @@ -603,6 +611,7 @@ func New(
ibc.NewAppModule(app.IBCKeeper),
params.NewAppModule(app.ParamsKeeper),
transferModule,
timerstore.NewAppModule(app.TimerStoreKeeper),
fixationstore.NewAppModule(app.FixationStoreKeeper),
specModule,
epochstorageModule,
Expand All @@ -625,6 +634,7 @@ func New(
// NOTE: staking module is required if HistoricalEntries param > 0
app.mm.SetOrderBeginBlockers(
upgradetypes.ModuleName,
timerstore.ModuleName,
fixationstore.ModuleName,
capabilitytypes.ModuleName,
authtypes.ModuleName,
Expand Down Expand Up @@ -682,6 +692,7 @@ func New(
paramstypes.ModuleName,
downtimemoduletypes.ModuleName, // downtime has no end block but module manager requires it.
fixationstore.ModuleName, // fixation store has no end block but module manager requires it.
timerstore.ModuleName, // timer store has no end block but module manager requires it.
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -717,6 +728,7 @@ func New(
feegrant.ModuleName,
paramstypes.ModuleName,
fixationstore.ModuleName, // fixation store has no init genesis but module manager requires it.
timerstore.ModuleName, // timer store has no init genesis but module manager requires it.
conflictmoduletypes.ModuleName, // NOTICE: the last module to initgenesis needs to push fixation in epoch storage
// this line is used by starport scaffolding # stargate/app/initGenesis
)
Expand Down
2 changes: 2 additions & 0 deletions app/keepers/lavaKeepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
protocolmodulekeeper "github.com/lavanet/lava/x/protocol/keeper"
specmodulekeeper "github.com/lavanet/lava/x/spec/keeper"
subscriptionmodulekeeper "github.com/lavanet/lava/x/subscription/keeper"
"github.com/lavanet/lava/x/timerstore"
// this line is used by starport scaffolding # stargate/app/moduleImport
)

Expand All @@ -54,6 +55,7 @@ type LavaKeepers struct {
ScopedTransferKeeper capabilitykeeper.ScopedKeeper

// Special Keepers
TimerStoreKeeper *timerstore.Keeper
FixationStoreKeeper *fixationstore.Keeper
SpecKeeper specmodulekeeper.Keeper
SubscriptionKeeper subscriptionmodulekeeper.Keeper
Expand Down
10 changes: 2 additions & 8 deletions common/fixation_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
// - GetAllEntryIndices(): get all the entries indices (without versions)
// - GetAllEntryVersions(index): get all the versions of an entry (for testing)
// - GetEntryVersionsRange(index, block, delta): get range of entry versions (**)
// - AdvanceBlock(): notify of block progress (e.g. BeginBlock) for garbage collection
// Note:
// - methods marked with (*) expect an exact existing method, or otherwise will panic
// - methods marked with (**) will match an entry with the nearest-no-later block version
Expand Down Expand Up @@ -93,7 +92,6 @@ import (
// GetAllEntryVersions() and GetEntryVersionsRange() give the all -or some- versions (blocks)
// of an entry. GetAllEntryIndices() return all the entry indices (names).
//
// On every new block, AdvanceBlock() should be called.
//
// Entry names (index) must contain only visible ascii characters (ascii values 32-126).
// The ascii 'DEL' invisible character is used internally to terminate the index values
Expand Down Expand Up @@ -1011,10 +1009,6 @@ func (fs *FixationStore) createEntryStoreKey(index string) string {
return fs.prefix + types.EntryPrefix + index
}

func (fs *FixationStore) AdvanceBlock(ctx sdk.Context) {
fs.tstore.Tick(ctx)
}

func (fs *FixationStore) getVersion(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(fs.storeKey), types.KeyPrefix(fs.prefix))
b := store.Get(types.KeyPrefix(types.FixationVersionKey))
Expand Down Expand Up @@ -1088,14 +1082,14 @@ func (fs *FixationStore) Init(ctx sdk.Context, gs types.GenesisState) {
}

// NewFixationStore returns a new FixationStore object
func NewFixationStore(storeKey storetypes.StoreKey, cdc codec.BinaryCodec, prefix string) *FixationStore {
func NewFixationStore(storeKey storetypes.StoreKey, cdc codec.BinaryCodec, prefix string, tstore *TimerStore) *FixationStore {
fs := FixationStore{storeKey: storeKey, cdc: cdc, prefix: prefix}

callback := func(ctx sdk.Context, key, data []byte) {
fs.entryCallbackBeginBlock(ctx, key, data)
}
tstore.WithCallbackByBlockHeight(callback)

tstore := NewTimerStore(storeKey, cdc, prefix).WithCallbackByBlockHeight(callback)
fs.tstore = *tstore

return &fs
Expand Down
5 changes: 3 additions & 2 deletions common/fixation_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func initCtxAndFixationStores(t *testing.T, count int) (sdk.Context, []*Fixation
fs := make([]*FixationStore, count)
for i := 0; i < count; i++ {
fixationKey := "mock_fix_" + strconv.Itoa(i)
fs[i] = NewFixationStore(mockStoreKey, cdc, fixationKey)
ts := NewTimerStore(mockStoreKey, cdc, fixationKey)
fs[i] = NewFixationStore(mockStoreKey, cdc, fixationKey, ts)
}

return ctx, fs
Expand Down Expand Up @@ -117,7 +118,7 @@ func testWithFixationTemplate(t *testing.T, playbook []fixationTemplate, countOb
case "block":
for i := 0; i < int(play.count); i++ {
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
fs[play.store].AdvanceBlock(ctx)
fs[play.store].tstore.Tick(ctx)
}
case "getall":
indexList := fs[play.store].GetAllEntryIndices(ctx)
Expand Down
6 changes: 4 additions & 2 deletions common/fixation_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func TestMigrate1to2(t *testing.T) {

ctx, cdc := initCtx(t)

fs := NewFixationStore(mockStoreKey, cdc, mockPrefix)
ts := NewTimerStore(mockStoreKey, cdc, mockPrefix)
fs := NewFixationStore(mockStoreKey, cdc, mockPrefix, ts)
fs.Init(ctx, types.GenesisState{Version: 1})

coin := sdk.Coin{Denom: "utest", Amount: sdk.NewInt(1)}
Expand Down Expand Up @@ -139,7 +140,8 @@ func TestMigrate2to3(t *testing.T) {

ctx, cdc := initCtx(t)

fs := NewFixationStore(mockStoreKey, cdc, mockPrefix)
ts := NewTimerStore(mockStoreKey, cdc, mockPrefix)
fs := NewFixationStore(mockStoreKey, cdc, mockPrefix, ts)
fs.Init(ctx, types.GenesisState{Version: 2})

coin := sdk.Coin{Denom: "utest", Amount: sdk.NewInt(1)}
Expand Down
6 changes: 5 additions & 1 deletion testutil/keeper/dualstaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
epochstoragekeeper "github.com/lavanet/lava/x/epochstorage/keeper"
"github.com/lavanet/lava/x/fixationstore"
speckeeper "github.com/lavanet/lava/x/spec/keeper"
"github.com/lavanet/lava/x/timerstore"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -54,6 +55,8 @@ func DualstakingKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
"SpecParams",
)

tsKeeper := timerstore.NewKeeper(cdc)

k := keeper.NewKeeper(
cdc,
storeKey,
Expand All @@ -63,7 +66,8 @@ func DualstakingKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
&mockAccountKeeper{},
epochstoragekeeper.NewKeeper(cdc, nil, nil, paramsSubspaceEpochstorage, nil, nil, nil),
speckeeper.NewKeeper(cdc, nil, nil, paramsSubspaceSpec),
fixationstore.NewKeeper(cdc),
fixationstore.NewKeeper(cdc, tsKeeper),
tsKeeper,
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
11 changes: 7 additions & 4 deletions testutil/keeper/keepers_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
spectypes "github.com/lavanet/lava/x/spec/types"
subscriptionkeeper "github.com/lavanet/lava/x/subscription/keeper"
subscriptiontypes "github.com/lavanet/lava/x/subscription/types"
"github.com/lavanet/lava/x/timerstore"
"github.com/stretchr/testify/require"
)

Expand All @@ -60,6 +61,7 @@ var Randomizer *sigs.ZeroReader

// NOTE: the order of the keeper fields must follow that of calling app.mm.SetOrderBeginBlockers() in app/app.go
type Keepers struct {
TimerStoreKeeper *timerstore.Keeper
FixationStoreKeeper *fixationstore.Keeper
AccountKeeper mockAccountKeeper
BankKeeper mockBankKeeper
Expand Down Expand Up @@ -195,19 +197,20 @@ func InitAllKeepers(t testing.TB) (*Servers, *Keepers, context.Context) {
downtimeParamsSubspace, _ := paramsKeeper.GetSubspace(downtimemoduletypes.ModuleName)

ks := Keepers{}
ks.FixationStoreKeeper = fixationstore.NewKeeper(cdc)
ks.TimerStoreKeeper = timerstore.NewKeeper(cdc)
ks.FixationStoreKeeper = fixationstore.NewKeeper(cdc, ks.TimerStoreKeeper)
ks.AccountKeeper = mockAccountKeeper{}
ks.BankKeeper = mockBankKeeper{balance: make(map[string]sdk.Coins)}
ks.StakingKeeper = mockStakingKeeper{}
ks.Spec = *speckeeper.NewKeeper(cdc, specStoreKey, specMemStoreKey, specparamsSubspace)
ks.Epochstorage = *epochstoragekeeper.NewKeeper(cdc, epochStoreKey, epochMemStoreKey, epochparamsSubspace, &ks.BankKeeper, &ks.AccountKeeper, ks.Spec)
ks.Dualstaking = *dualstakingkeeper.NewKeeper(cdc, dualstakingStoreKey, dualstakingMemStoreKey, dualstakingparamsSubspace, &ks.BankKeeper, &ks.AccountKeeper, ks.Epochstorage, ks.Spec, ks.FixationStoreKeeper)
ks.Dualstaking = *dualstakingkeeper.NewKeeper(cdc, dualstakingStoreKey, dualstakingMemStoreKey, dualstakingparamsSubspace, &ks.BankKeeper, &ks.AccountKeeper, ks.Epochstorage, ks.Spec, ks.FixationStoreKeeper, ks.TimerStoreKeeper)
ks.Plans = *planskeeper.NewKeeper(cdc, plansStoreKey, plansMemStoreKey, plansparamsSubspace, ks.Epochstorage, ks.Spec, ks.FixationStoreKeeper)
ks.Projects = *projectskeeper.NewKeeper(cdc, projectsStoreKey, projectsMemStoreKey, projectsparamsSubspace, ks.Epochstorage, ks.FixationStoreKeeper)
ks.Protocol = *protocolkeeper.NewKeeper(cdc, protocolStoreKey, protocolMemStoreKey, protocolparamsSubspace)
ks.Subscription = *subscriptionkeeper.NewKeeper(cdc, subscriptionStoreKey, subscriptionMemStoreKey, subscriptionparamsSubspace, &ks.BankKeeper, &ks.AccountKeeper, &ks.Epochstorage, ks.Projects, ks.Plans, ks.FixationStoreKeeper)
ks.Subscription = *subscriptionkeeper.NewKeeper(cdc, subscriptionStoreKey, subscriptionMemStoreKey, subscriptionparamsSubspace, &ks.BankKeeper, &ks.AccountKeeper, &ks.Epochstorage, ks.Projects, ks.Plans, ks.FixationStoreKeeper, ks.TimerStoreKeeper)
ks.Downtime = downtimekeeper.NewKeeper(cdc, downtimeKey, downtimeParamsSubspace, ks.Epochstorage)
ks.Pairing = *pairingkeeper.NewKeeper(cdc, pairingStoreKey, pairingMemStoreKey, pairingparamsSubspace, &ks.BankKeeper, &ks.AccountKeeper, ks.Spec, &ks.Epochstorage, ks.Projects, ks.Subscription, ks.Plans, ks.Downtime, ks.Dualstaking, ks.FixationStoreKeeper)
ks.Pairing = *pairingkeeper.NewKeeper(cdc, pairingStoreKey, pairingMemStoreKey, pairingparamsSubspace, &ks.BankKeeper, &ks.AccountKeeper, ks.Spec, &ks.Epochstorage, ks.Projects, ks.Subscription, ks.Plans, ks.Downtime, ks.Dualstaking, ks.FixationStoreKeeper, ks.TimerStoreKeeper)
ks.ParamsKeeper = paramsKeeper
ks.Conflict = *conflictkeeper.NewKeeper(cdc, conflictStoreKey, conflictMemStoreKey, conflictparamsSubspace, &ks.BankKeeper, &ks.AccountKeeper, ks.Pairing, ks.Epochstorage, ks.Spec)
ks.BlockStore = MockBlockStore{height: 0, blockHistory: make(map[int64]*tenderminttypes.Block)}
Expand Down
6 changes: 5 additions & 1 deletion testutil/keeper/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/lavanet/lava/x/fixationstore"
"github.com/lavanet/lava/x/pairing/keeper"
"github.com/lavanet/lava/x/pairing/types"
"github.com/lavanet/lava/x/timerstore"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -46,6 +47,8 @@ func PairingKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
"EpochStorageParams",
)

tsKeeper := timerstore.NewKeeper(cdc)

k := keeper.NewKeeper(
cdc,
storeKey,
Expand All @@ -60,7 +63,8 @@ func PairingKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
nil,
nil,
nil,
fixationstore.NewKeeper(cdc),
fixationstore.NewKeeper(cdc, tsKeeper),
tsKeeper,
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
3 changes: 2 additions & 1 deletion testutil/keeper/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/lavanet/lava/x/plans/keeper"
"github.com/lavanet/lava/x/plans/types"
speckeeper "github.com/lavanet/lava/x/spec/keeper"
"github.com/lavanet/lava/x/timerstore"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -61,7 +62,7 @@ func PlanKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
paramsSubspace,
epochstoragekeeper.NewKeeper(cdc, nil, nil, paramsSubspaceEpochstorage, nil, nil, nil),
speckeeper.NewKeeper(cdc, nil, nil, paramsSubspaceSpec),
fixationstore.NewKeeper(cdc),
fixationstore.NewKeeper(cdc, timerstore.NewKeeper(cdc)),
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
3 changes: 2 additions & 1 deletion testutil/keeper/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/lavanet/lava/x/fixationstore"
"github.com/lavanet/lava/x/projects/keeper"
"github.com/lavanet/lava/x/projects/types"
"github.com/lavanet/lava/x/timerstore"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -52,7 +53,7 @@ func ProjectsKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
memStoreKey,
paramsSubspace,
epochstoragekeeper.NewKeeper(cdc, nil, nil, paramsSubspaceEpochstorage, nil, nil, nil),
fixationstore.NewKeeper(cdc),
fixationstore.NewKeeper(cdc, timerstore.NewKeeper(cdc)),
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
5 changes: 4 additions & 1 deletion testutil/keeper/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
projectskeeper "github.com/lavanet/lava/x/projects/keeper"
"github.com/lavanet/lava/x/subscription/keeper"
"github.com/lavanet/lava/x/subscription/types"
"github.com/lavanet/lava/x/timerstore"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -62,7 +63,8 @@ func SubscriptionKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
"PlansParams",
)

fsKeeper := fixationstore.NewKeeper(cdc)
tsKeeper := timerstore.NewKeeper(cdc)
fsKeeper := fixationstore.NewKeeper(cdc, tsKeeper)

k := keeper.NewKeeper(
cdc,
Expand All @@ -75,6 +77,7 @@ func SubscriptionKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
projectskeeper.NewKeeper(cdc, nil, nil, paramsSubspaceProjects, nil, fsKeeper),
planskeeper.NewKeeper(cdc, nil, nil, paramsSubspacePlans, nil, nil, fsKeeper),
fsKeeper,
tsKeeper,
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
7 changes: 2 additions & 5 deletions x/dualstaking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewKeeper(
epochstorageKeeper types.EpochstorageKeeper,
specKeeper types.SpecKeeper,
fixationStoreKeeper types.FixationStoreKeeper,
timerStoreKeeper types.TimerStoreKeeper,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
Expand Down Expand Up @@ -75,7 +76,7 @@ func NewKeeper(
keeper.finalizeUnbonding(ctx, key, data)
}

unbondingTS := *common.NewTimerStore(storeKey, cdc, types.UnbondingPrefix).
unbondingTS := *timerStoreKeeper.NewTimerStore(storeKey, types.UnbondingPrefix).
WithCallbackByBlockHeight(timerCallback)

keeper.delegationFS = delegationFS
Expand All @@ -85,10 +86,6 @@ func NewKeeper(
return keeper
}

func (k Keeper) BeginBlock(ctx sdk.Context) {
k.unbondingTS.Tick(ctx)
}

// ExportDelegations exports dualstaking delegations data (for genesis)
func (k Keeper) ExportDelegations(ctx sdk.Context) commontypes.GenesisState {
return k.delegationFS.Export(ctx)
Expand Down
4 changes: 4 additions & 0 deletions x/dualstaking/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ type StakingKeeper interface {
type FixationStoreKeeper interface {
NewFixationStore(storeKey storetypes.StoreKey, prefix string) *common.FixationStore
}

type TimerStoreKeeper interface {
NewTimerStore(storeKey storetypes.StoreKey, prefix string) *common.TimerStore
}
Loading

0 comments on commit 0ec47fe

Please sign in to comment.