diff --git a/common/timer.go b/common/timer.go index 34f165bc34..be8da8ded8 100644 --- a/common/timer.go +++ b/common/timer.go @@ -77,12 +77,33 @@ type TimerStore struct { callbacks [2]TimerCallback // as per TimerType } +func TimerVersion() uint64 { + return 1 +} + // NewTimerStore returns a new TimerStore object func NewTimerStore(storeKey sdk.StoreKey, cdc codec.BinaryCodec, prefix string) *TimerStore { tstore := TimerStore{storeKey: storeKey, cdc: cdc, prefix: prefix} return &tstore } +func (tstore *TimerStore) getVersion(ctx sdk.Context) uint64 { + store := prefix.NewStore(ctx.KVStore(tstore.storeKey), types.KeyPrefix(tstore.prefix)) + + b := store.Get(types.KeyPrefix(types.TimerVersionKey)) + if b == nil { + return 1 + } + + return types.DecodeKey(b) +} + +func (tstore *TimerStore) setVersion(ctx sdk.Context, val uint64) { + store := prefix.NewStore(ctx.KVStore(tstore.storeKey), types.KeyPrefix(tstore.prefix)) + b := types.EncodeKey(val) + store.Set(types.KeyPrefix(types.TimerVersionKey), b) +} + func (tstore *TimerStore) WithCallbackByBlockHeight(callback func(ctx sdk.Context, data string)) *TimerStore { tstoreNew := tstore tstoreNew.callbacks[types.BlockHeight] = callback diff --git a/common/timer_migrate.go b/common/timer_migrate.go new file mode 100644 index 0000000000..bbf66d62aa --- /dev/null +++ b/common/timer_migrate.go @@ -0,0 +1,37 @@ +package common + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/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" +} + +func (tstore *TimerStore) MigrateVersion(ctx sdk.Context) (err error) { + from := tstore.getVersion(ctx) + to := TimerVersion() + + for from < to { + function, ok := timerMigrators[int(from)] + if !ok { + return fmt.Errorf("%s not available", prefixForErrors(from)) + } + + err = function(ctx, tstore) + if err != nil { + return err + } + + from += 1 + } + + tstore.setVersion(ctx, to) + return nil +} diff --git a/common/types/keys.go b/common/types/keys.go index f82e1fa159..5d10750431 100644 --- a/common/types/keys.go +++ b/common/types/keys.go @@ -24,6 +24,7 @@ const ( ) const ( + TimerVersionKey = "Entry_Version" NextTimerPrefix = "Timer_Next_" TimerPrefix = "Timer_Value_" )