@@ -115,14 +115,14 @@ func (d *delegations) DisableAutoRenew(delegationID thor.Bytes32) error {
115115 if delegation .Stake .Sign () == 0 {
116116 return errors .New ("delegation is not active" )
117117 }
118- if validation . Status == StatusExit {
119- return errors .New ("delegation is not active, withdraw is available " )
118+ if delegation . Ended ( validation ) {
119+ return errors .New ("delegation is not active" )
120120 }
121121
122122 weight := delegation .Weight ()
123123
124124 // the delegation's funds have already been locked, so we need to move them to non-recurring, but still locked
125- if delegation .IsLocked (validation ) {
125+ if delegation .Started (validation ) {
126126 // move the delegation's portion of locked to non-recurring.
127127 // this will make the funds available at the end of the current iteration
128128 aggregation .CurrentRecurringVET = big .NewInt (0 ).Sub (aggregation .CurrentRecurringVET , delegation .Stake )
@@ -141,8 +141,15 @@ func (d *delegations) DisableAutoRenew(delegationID thor.Bytes32) error {
141141 aggregation .PendingRecurringWeight = big .NewInt (0 ).Sub (aggregation .PendingRecurringWeight , weight )
142142 }
143143
144+ // TODO: In a future PR this won't be possible, so it will be removed. This is backwards compatible according to the unit tests.
145+ // - In future: delegations auto added as auto-renew, then will have to signal an exit to withdraw in the next staking period.
144146 // set the delegation's exit iteration
145- lastIteration := validation .CurrentIteration () + 1
147+ var lastIteration uint32
148+ if delegation .Started (validation ) {
149+ lastIteration = validation .CurrentIteration ()
150+ } else {
151+ lastIteration = delegation .FirstIteration
152+ }
146153 delegation .LastIteration = & lastIteration
147154 delegation .AutoRenew = false
148155
@@ -161,8 +168,8 @@ func (d *delegations) EnableAutoRenew(delegationID thor.Bytes32) error {
161168 if delegation .AutoRenew {
162169 return errors .New ("delegation is already autoRenew" )
163170 }
164- if validation . Status == StatusExit {
165- return errors .New ("delegation is not active, withdraw is available " )
171+ if delegation . Ended ( validation ) {
172+ return errors .New ("delegation is not active" )
166173 }
167174 weight := delegation .Weight ()
168175
@@ -173,7 +180,7 @@ func (d *delegations) EnableAutoRenew(delegationID thor.Bytes32) error {
173180 return errors .New ("validation's next period stake exceeds max stake" )
174181 }
175182
176- if delegation .IsLocked (validation ) {
183+ if delegation .Started (validation ) {
177184 // move the delegation's portion of non-recurring to locked.
178185 // this means the funds will not be available until the validator is inactive, or the delegation signals an exit
179186 // and completes the current staking period
@@ -205,19 +212,14 @@ func (d *delegations) Withdraw(delegationID thor.Bytes32) (*big.Int, error) {
205212 if err != nil {
206213 return nil , err
207214 }
208- if delegation .IsLocked (validation ) {
215+ started := delegation .Started (validation )
216+ finished := delegation .Ended (validation )
217+ if started && ! finished {
209218 return nil , errors .New ("delegation is not eligible for withdraw" )
210219 }
211220 weight := delegation .Weight ()
212221
213- delegationStarted := delegation .FirstIteration <= validation .CompleteIterations
214- if delegationStarted && validation .Status != StatusQueued {
215- // the stake has moved to withdrawable since we checked if the validation is locked above
216- if aggregation .WithdrawableVET .Cmp (delegation .Stake ) < 0 {
217- return nil , errors .New ("not enough withdraw VET" )
218- }
219- aggregation .WithdrawableVET = big .NewInt (0 ).Sub (aggregation .WithdrawableVET , delegation .Stake )
220- } else {
222+ if ! started {
221223 if delegation .AutoRenew { // delegation's stake is pending locked
222224 if aggregation .PendingRecurringVET .Cmp (delegation .Stake ) < 0 {
223225 return nil , errors .New ("not enough pending locked VET" )
@@ -239,6 +241,14 @@ func (d *delegations) Withdraw(delegationID thor.Bytes32) (*big.Int, error) {
239241 }
240242 }
241243
244+ if finished {
245+ // the stake has moved to withdrawable since we checked if the validation is locked above
246+ if aggregation .WithdrawableVET .Cmp (delegation .Stake ) < 0 {
247+ return nil , errors .New ("not enough withdraw VET" )
248+ }
249+ aggregation .WithdrawableVET = big .NewInt (0 ).Sub (aggregation .WithdrawableVET , delegation .Stake )
250+ }
251+
242252 stake := delegation .Stake
243253 delegation .Stake = big .NewInt (0 )
244254 // remove the delegation from the mapping after the withdraw
0 commit comments