Unverified Commit 343283bd authored by antstalepresh's avatar antstalepresh Committed by GitHub
Browse files

lockup module implement private functions (#39)

* lockup module implement private functions
parent c991b0e7
Showing with 42 additions and 24 deletions
+42 -24
......@@ -27,7 +27,7 @@ func (ak AdminKeeper) Relock(ctx sdk.Context, lockID uint64, newCoins sdk.Coins)
// reset lock record inside store
store := ctx.KVStore(ak.storeKey)
store.Set(LockStoreKey(lockID), ak.cdc.MustMarshalJSON(lock))
store.Set(lockStoreKey(lockID), ak.cdc.MustMarshalJSON(lock))
return nil
}
......@@ -44,11 +44,11 @@ func (ak AdminKeeper) BreakLock(ctx sdk.Context, lockID uint64) error {
}
store := ctx.KVStore(ak.storeKey)
store.Delete(LockStoreKey(lockID)) // remove lock from store
store.Delete(lockStoreKey(lockID)) // remove lock from store
refKeys := lockRefKeys(*lock)
for _, refKey := range refKeys {
ak.DeleteLockRefByKey(ctx, refKey, lockID)
ak.deleteLockRefByKey(ctx, refKey, lockID)
}
return nil
}
package keeper
import (
"github.com/c-osmosis/osmosis/x/lockup/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (k Keeper) AddLockRefByKey(ctx sdk.Context, key []byte, lockID uint64) error {
return k.addLockRefByKey(ctx, key, lockID)
}
func (k Keeper) DeleteLockRefByKey(ctx sdk.Context, key []byte, lockID uint64) {
k.deleteLockRefByKey(ctx, key, lockID)
}
func (k Keeper) GetLockRefs(ctx sdk.Context, key []byte) types.LockIDs {
return k.getLockRefs(ctx, key)
}
......@@ -107,7 +107,7 @@ func (k Keeper) GetAccountLockedLongerThanDurationDenom(ctx sdk.Context, addr sd
func (k Keeper) GetLockByID(ctx sdk.Context, lockID uint64) (*types.PeriodLock, error) {
lock := types.PeriodLock{}
store := ctx.KVStore(k.storeKey)
lockKey := LockStoreKey(lockID)
lockKey := lockStoreKey(lockID)
if !store.Has(lockKey) {
return nil, fmt.Errorf("lock with ID %d does not exist", lockID)
}
......@@ -144,7 +144,7 @@ func (k Keeper) UnlockPeriodLockByID(ctx sdk.Context, LockID uint64) (*types.Per
// LockTokens lock tokens from an account for specified duration
func (k Keeper) LockTokens(ctx sdk.Context, owner sdk.AccAddress, coins sdk.Coins, duration time.Duration) (types.PeriodLock, error) {
ID := k.GetLastLockID(ctx) + 1
ID := k.getLastLockID(ctx) + 1
lock := types.NewPeriodLock(ID, owner, duration, ctx.BlockTime().Add(duration), coins)
return lock, k.Lock(ctx, lock)
}
......@@ -157,12 +157,12 @@ func (k Keeper) Lock(ctx sdk.Context, lock types.PeriodLock) error {
lockID := lock.ID
store := ctx.KVStore(k.storeKey)
store.Set(LockStoreKey(lockID), k.cdc.MustMarshalJSON(&lock))
k.SetLastLockID(ctx, lockID)
store.Set(lockStoreKey(lockID), k.cdc.MustMarshalJSON(&lock))
k.setLastLockID(ctx, lockID)
refKeys := lockRefKeys(lock)
for _, refKey := range refKeys {
if err := k.AddLockRefByKey(ctx, refKey, lockID); err != nil {
if err := k.addLockRefByKey(ctx, refKey, lockID); err != nil {
return err
}
}
......@@ -184,11 +184,11 @@ func (k Keeper) Unlock(ctx sdk.Context, lock types.PeriodLock) error {
lockID := lock.ID
store := ctx.KVStore(k.storeKey)
store.Delete(LockStoreKey(lockID)) // remove lock from store
store.Delete(lockStoreKey(lockID)) // remove lock from store
refKeys := lockRefKeys(lock)
for _, refKey := range refKeys {
k.DeleteLockRefByKey(ctx, refKey, lockID)
k.deleteLockRefByKey(ctx, refKey, lockID)
}
return nil
}
......@@ -8,8 +8,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// GetLastLockID returns ID used last time
func (k Keeper) GetLastLockID(ctx sdk.Context) uint64 {
// getLastLockID returns ID used last time
func (k Keeper) getLastLockID(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyLastLockID)
......@@ -20,19 +20,19 @@ func (k Keeper) GetLastLockID(ctx sdk.Context) uint64 {
return sdk.BigEndianToUint64(bz)
}
// SetLastLockID save ID used by last lock
func (k Keeper) SetLastLockID(ctx sdk.Context, ID uint64) {
// setLastLockID save ID used by last lock
func (k Keeper) setLastLockID(ctx sdk.Context, ID uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyLastLockID, sdk.Uint64ToBigEndian(ID))
}
// LockStoreKey returns action store key from ID
func LockStoreKey(ID uint64) []byte {
// lockStoreKey returns action store key from ID
func lockStoreKey(ID uint64) []byte {
return combineKeys(types.KeyPrefixPeriodLock, sdk.Uint64ToBigEndian(ID))
}
// GetLockRefs get lock IDs specified on the prefix and timestamp key
func (k Keeper) GetLockRefs(ctx sdk.Context, key []byte) types.LockIDs {
// getLockRefs get lock IDs specified on the prefix and timestamp key
func (k Keeper) getLockRefs(ctx sdk.Context, key []byte) types.LockIDs {
store := ctx.KVStore(k.storeKey)
timeLock := types.LockIDs{}
if store.Has(key) {
......@@ -45,10 +45,10 @@ func (k Keeper) GetLockRefs(ctx sdk.Context, key []byte) types.LockIDs {
return timeLock
}
// AddLockRefByKey append lock ID into an array associated to provided key
func (k Keeper) AddLockRefByKey(ctx sdk.Context, key []byte, lockID uint64) error {
// addLockRefByKey append lock ID into an array associated to provided key
func (k Keeper) addLockRefByKey(ctx sdk.Context, key []byte, lockID uint64) error {
store := ctx.KVStore(k.storeKey)
timeLock := k.GetLockRefs(ctx, key)
timeLock := k.getLockRefs(ctx, key)
if findIndex(timeLock.IDs, lockID) > -1 {
return fmt.Errorf("lock with same ID exist: %d", lockID)
}
......@@ -61,11 +61,11 @@ func (k Keeper) AddLockRefByKey(ctx sdk.Context, key []byte, lockID uint64) erro
return nil
}
// DeleteLockRefByKey remove lock ID from an array associated to provided key
func (k Keeper) DeleteLockRefByKey(ctx sdk.Context, key []byte, lockID uint64) {
// deleteLockRefByKey removes lock ID from an array associated to provided key
func (k Keeper) deleteLockRefByKey(ctx sdk.Context, key []byte, lockID uint64) {
var index = -1
store := ctx.KVStore(k.storeKey)
timeLock := k.GetLockRefs(ctx, key)
timeLock := k.getLockRefs(ctx, key)
timeLock.IDs, index = removeValue(timeLock.IDs, lockID)
if index < 0 {
panic(fmt.Sprintf("specific lock with ID %d not found", lockID))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment