@@ -201,7 +201,7 @@ func (s *Service) Add(
201201 return err
202202 }
203203
204- return s .SetValidation (validator , entry , true )
204+ return s .repo . SetValidation (validator , entry , true )
205205}
206206
207207func (s * Service ) SignalExit (validator thor.Address , endorser thor.Address ) error {
@@ -226,6 +226,24 @@ func (s *Service) SignalExit(validator thor.Address, endorser thor.Address) erro
226226 return s .repo .SetValidation (validator , validation , false )
227227}
228228
229+ func (s * Service ) Evict (validator thor.Address , currentBlock uint32 ) error {
230+ validation , err := s .GetExistingValidation (validator )
231+ if err != nil {
232+ return err
233+ }
234+
235+ exitBlock , err := s .SetExitBlock (validator , currentBlock + s .epochLength )
236+ if err != nil {
237+ return err
238+ }
239+ if validation .ExitBlock != nil && * validation .ExitBlock < exitBlock {
240+ exitBlock = * validation .ExitBlock
241+ }
242+ validation .ExitBlock = & exitBlock
243+
244+ return s .repo .SetValidation (validator , validation , false )
245+ }
246+
229247func (s * Service ) IncreaseStake (validator thor.Address , endorser thor.Address , amount * big.Int ) error {
230248 entry , err := s .GetExistingValidation (validator )
231249 if err != nil {
@@ -243,7 +261,7 @@ func (s *Service) IncreaseStake(validator thor.Address, endorser thor.Address, a
243261
244262 entry .QueuedVET = big .NewInt (0 ).Add (amount , entry .QueuedVET )
245263
246- return s .SetValidation (validator , entry , false )
264+ return s .repo . SetValidation (validator , entry , false )
247265}
248266
249267func (s * Service ) SetBeneficiary (validator , endorser , beneficiary thor.Address ) error {
@@ -262,7 +280,7 @@ func (s *Service) SetBeneficiary(validator, endorser, beneficiary thor.Address)
262280 } else {
263281 entry .Beneficiary = & beneficiary
264282 }
265- if err = s .SetValidation (validator , entry , false ); err != nil {
283+ if err = s .repo . SetValidation (validator , entry , false ); err != nil {
266284 return errors .Wrap (err , "failed to set beneficiary" )
267285 }
268286 return nil
@@ -305,19 +323,22 @@ func (s *Service) DecreaseStake(validator thor.Address, endorser thor.Address, a
305323 entry .WithdrawableVET = big .NewInt (0 ).Add (entry .WithdrawableVET , amount )
306324 }
307325
308- return entry .Status == StatusQueued , s .SetValidation (validator , entry , false )
326+ return entry .Status == StatusQueued , s .repo . SetValidation (validator , entry , false )
309327}
310328
311329// WithdrawStake allows validations to withdraw any withdrawable stake.
312330// It also verifies the endorser and updates the validator totals.
313331func (s * Service ) WithdrawStake (
314- val * Validation ,
315332 validator thor.Address ,
316333 endorser thor.Address ,
317334 currentBlock uint32 ,
318- ) (* big.Int , error ) {
335+ ) (* big.Int , * big.Int , error ) {
336+ val , err := s .GetExistingValidation (validator )
337+ if err != nil {
338+ return nil , nil , err
339+ }
319340 if val .Endorser != endorser {
320- return big .NewInt (0 ), reverts .New ("invalid endorser" )
341+ return big .NewInt (0 ), big . NewInt ( 0 ), reverts .New ("invalid endorser" )
321342 }
322343
323344 // calculate currently available VET to withdraw
@@ -333,21 +354,22 @@ func (s *Service) WithdrawStake(
333354 val .QueuedVET = big .NewInt (0 )
334355 val .Status = StatusExit
335356 if err := s .validatorQueue .Remove (validator ); err != nil {
336- return nil , err
357+ return nil , nil , err
337358 }
338359 }
360+ queuedVET := big .NewInt (0 ).Set (val .QueuedVET )
339361 // remove any que
340362 if val .QueuedVET .Sign () > 0 {
341363 val .QueuedVET = big .NewInt (0 )
342364 }
343365
344366 // no more withdraw after this
345367 val .WithdrawableVET = big .NewInt (0 )
346- if err := s .SetValidation (validator , val , false ); err != nil {
347- return nil , err
368+ if err := s .repo . SetValidation (validator , val , false ); err != nil {
369+ return nil , nil , err
348370 }
349371
350- return withdrawable , nil
372+ return withdrawable , queuedVET , nil
351373}
352374
353375func (s * Service ) NextToActivate (maxLeaderGroupSize * big.Int ) (* thor.Address , error ) {
@@ -388,12 +410,12 @@ func (s *Service) ExitValidator(validator thor.Address) (*delta.Exit, error) {
388410 if entry .IsEmpty () {
389411 return nil , nil
390412 }
391- exit := entry .Exit ()
413+ exit := entry .exit ()
392414 if err = s .leaderGroup .Remove (validator ); err != nil {
393415 return nil , err
394416 }
395417
396- if err = s .SetValidation (validator , entry , false ); err != nil {
418+ if err = s .repo . SetValidation (validator , entry , false ); err != nil {
397419 return nil , err
398420 }
399421
@@ -477,7 +499,7 @@ func (s *Service) ActivateValidator(
477499 }
478500
479501 // Persist the updated validation state
480- if err = s .SetValidation (validationID , val , false ); err != nil {
502+ if err = s .repo . SetValidation (validationID , val , false ); err != nil {
481503 return nil , err
482504 }
483505
@@ -504,7 +526,20 @@ func (s *Service) UpdateOfflineBlock(validator thor.Address, block uint32, onlin
504526 validation .OfflineBlock = & block
505527 }
506528
507- return s .SetValidation (validator , validation , false )
529+ return s .repo .SetValidation (validator , validation , false )
530+ }
531+
532+ func (s * Service ) Renew (validator thor.Address , aggRenew * delta.Renewal ) (* delta.Renewal , error ) {
533+ validation , err := s .GetExistingValidation (validator )
534+ if err != nil {
535+ return nil , err
536+ }
537+ delta := validation .renew (aggRenew )
538+ if err = s .repo .SetValidation (validator , validation , false ); err != nil {
539+ return nil , errors .Wrap (err , "failed to renew validator" )
540+ }
541+
542+ return delta , nil
508543}
509544
510545//
@@ -526,10 +561,6 @@ func (s *Service) GetExistingValidation(validator thor.Address) (*Validation, er
526561 return v , nil
527562}
528563
529- func (s * Service ) SetValidation (validator thor.Address , entry * Validation , isNew bool ) error {
530- return s .repo .SetValidation (validator , entry , isNew )
531- }
532-
533564func (s * Service ) LeaderGroupNext (prev thor.Address ) (thor.Address , error ) {
534565 return s .leaderGroup .Next (prev )
535566}
0 commit comments