-
Notifications
You must be signed in to change notification settings - Fork 17
/
logic_unbonding.go
87 lines (72 loc) · 2.93 KB
/
logic_unbonding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package keeper
import (
"github.com/KYVENetwork/chain/util"
"github.com/KYVENetwork/chain/x/delegation/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// StartUnbondingDelegator creates a queue entry to schedule the unbonding.
// After the DelegationTime is reached the actual unbonding will be performed
// The actual unbonding is then performed by `func ProcessDelegatorUnbondingQueue(...)`
func (k Keeper) StartUnbondingDelegator(ctx sdk.Context, staker string, delegatorAddress string, amount uint64) {
// the queue is ordered by time
queueState := k.GetQueueState(ctx)
// Increase topIndex as a new entry is about to be appended
queueState.HighIndex += 1
k.SetQueueState(ctx, queueState)
// UnbondingEntry stores all the information which are needed to perform
// the undelegation at the end of the unbonding time
undelegationQueueEntry := types.UndelegationQueueEntry{
Delegator: delegatorAddress,
Index: queueState.HighIndex,
Staker: staker,
Amount: amount,
CreationTime: uint64(ctx.BlockTime().Unix()),
}
k.SetUndelegationQueueEntry(ctx, undelegationQueueEntry)
}
// ProcessDelegatorUnbondingQueue is called in the end block and
// checks the queue for entries that have surpassed the unbonding time.
// If the unbonding time is reached, the actual unbonding is performed
// and the entry is removed from the queue.
func (k Keeper) ProcessDelegatorUnbondingQueue(ctx sdk.Context) {
// Get Queue information
queueState := k.GetQueueState(ctx)
// flag for computing every entry at the end of the queue which is due.
// start processing the end of the queue
for continueProcessing := true; continueProcessing; {
continueProcessing = false
// Get end of queue
undelegationEntry, found := k.GetUndelegationQueueEntry(ctx, queueState.LowIndex+1)
if !found {
if queueState.LowIndex < queueState.HighIndex {
queueState.LowIndex += 1
continueProcessing = true
}
} else
// Check if unbonding time is over
if undelegationEntry.CreationTime+k.GetUnbondingDelegationTime(ctx) <= uint64(ctx.BlockTime().Unix()) {
// Perform undelegation and save undelegated amount to then transfer back to the user
undelegatedAmount := k.performUndelegation(ctx, undelegationEntry.Staker, undelegationEntry.Delegator, undelegationEntry.Amount)
// Transfer the money
if err := util.TransferFromModuleToAddress(
k.bankKeeper,
ctx,
types.ModuleName,
undelegationEntry.Delegator,
undelegatedAmount,
); err != nil {
util.PanicHalt(k.upgradeKeeper, ctx, "Not enough money in delegation module - logic_unbonding")
}
// Emit a delegation event.
_ = ctx.EventManager().EmitTypedEvent(&types.EventUndelegate{
Address: undelegationEntry.Delegator,
Staker: undelegationEntry.Staker,
Amount: undelegatedAmount,
})
k.RemoveUndelegationQueueEntry(ctx, &undelegationEntry)
continueProcessing = true
queueState.LowIndex += 1
}
}
k.SetQueueState(ctx, queueState)
}