Skip to content

Commit

Permalink
CNS-428: timerstore: add migration v1 to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
orenl-lava committed May 20, 2023
1 parent 2dccbc4 commit 76ce33d
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion common/timer_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/common/types"
)

func (tstore *TimerStore) prefixForErrors(from uint64) string {
return fmt.Sprintf("TimerStore: migration from version %d", from)
}

var timerMigrators = map[int]func(sdk.Context, *TimerStore) error{
// fill with map entrys like "1: timerMigrate1to2"
1: timerMigrate1to2,
}

func (tstore *TimerStore) MigrateVersion(ctx sdk.Context) (err error) {
Expand All @@ -35,3 +36,25 @@ func (tstore *TimerStore) MigrateVersion(ctx sdk.Context) (err error) {
tstore.setVersion(ctx, to)
return nil
}

// timerMigrate1to2: fix refcounts
// - convert entry keys from "<block>" to "<block>index (otherwise timer for an entry
// with some block will be overwritten by timer for another entry with same block.
func timerMigrate1to2(ctx sdk.Context, tstore *TimerStore) error {
for _, which := range []types.TimerType{types.BlockHeight, types.BlockTime} {
store := tstore.getStoreTimer(ctx, which)

iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
value, key := types.DecodeBlockAndKey(iterator.Key())
key_v1 := types.EncodeKey(value)
key_v2 := types.EncodeBlockAndKey(value, key)
store.Set(key_v2, iterator.Value())
store.Delete(key_v1)
}
}

return nil
}

0 comments on commit 76ce33d

Please sign in to comment.