|
| 1 | +// Copyright (c) 2025 The VeChainThor developers |
| 2 | +// |
| 3 | +// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying |
| 4 | +// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> |
| 5 | + |
| 6 | +package delegation |
| 7 | + |
| 8 | +import ( |
| 9 | + "math/big" |
| 10 | + |
| 11 | + "github.com/pkg/errors" |
| 12 | + |
| 13 | + "github.com/vechain/thor/v2/builtin/solidity" |
| 14 | + "github.com/vechain/thor/v2/builtin/staker/validation" |
| 15 | + "github.com/vechain/thor/v2/thor" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + slotDelegations = thor.BytesToBytes32([]byte(("delegations"))) |
| 20 | + slotDelegationsCounter = thor.BytesToBytes32([]byte(("delegations-counter"))) |
| 21 | +) |
| 22 | + |
| 23 | +type Service struct { |
| 24 | + delegations *solidity.Mapping[thor.Bytes32, *Delegation] |
| 25 | + idCounter *solidity.Uint256 |
| 26 | +} |
| 27 | + |
| 28 | +func New(sctx *solidity.Context) *Service { |
| 29 | + return &Service{ |
| 30 | + delegations: solidity.NewMapping[thor.Bytes32, *Delegation](sctx, slotDelegations), |
| 31 | + idCounter: solidity.NewUint256(sctx, slotDelegationsCounter), |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func (s *Service) GetDelegation(delegationID thor.Bytes32) (*Delegation, error) { |
| 36 | + d, err := s.delegations.Get(delegationID) |
| 37 | + if err != nil { |
| 38 | + return nil, errors.Wrap(err, "failed to get delegation") |
| 39 | + } |
| 40 | + return d, nil |
| 41 | +} |
| 42 | + |
| 43 | +func (s *Service) SetDelegation(delegationID thor.Bytes32, entry *Delegation, isNew bool) error { |
| 44 | + if err := s.delegations.Set(delegationID, entry, isNew); err != nil { |
| 45 | + return errors.Wrap(err, "failed to set delegation") |
| 46 | + } |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func (s *Service) Add( |
| 51 | + validationID thor.Address, |
| 52 | + firstIteration uint32, |
| 53 | + stake *big.Int, |
| 54 | + multiplier uint8, |
| 55 | +) (thor.Bytes32, error) { |
| 56 | + // ensure input is sane |
| 57 | + if multiplier == 0 { |
| 58 | + return thor.Bytes32{}, errors.New("multiplier cannot be 0") |
| 59 | + } |
| 60 | + if stake.Cmp(big.NewInt(0)) <= 0 { |
| 61 | + return thor.Bytes32{}, errors.New("stake must be greater than 0") |
| 62 | + } |
| 63 | + |
| 64 | + // update the global delegation counter |
| 65 | + // todo: should this counter be per validation ? |
| 66 | + id, err := s.idCounter.Get() |
| 67 | + if err != nil { |
| 68 | + return thor.Bytes32{}, err |
| 69 | + } |
| 70 | + |
| 71 | + id = id.Add(id, big.NewInt(1)) |
| 72 | + if err := s.idCounter.Set(id); err != nil { |
| 73 | + return thor.Bytes32{}, errors.Wrap(err, "failed to increment delegation ID counter") |
| 74 | + } |
| 75 | + |
| 76 | + delegationID := thor.BytesToBytes32(id.Bytes()) |
| 77 | + delegation := &Delegation{ |
| 78 | + Multiplier: multiplier, |
| 79 | + Stake: stake, |
| 80 | + ValidationID: validationID, |
| 81 | + FirstIteration: firstIteration, |
| 82 | + } |
| 83 | + |
| 84 | + if err := s.delegations.Set(delegationID, delegation, false); err != nil { |
| 85 | + return thor.Bytes32{}, errors.Wrap(err, "failed to set delegation") |
| 86 | + } |
| 87 | + |
| 88 | + return delegationID, nil |
| 89 | +} |
| 90 | + |
| 91 | +// todo uncouple this |
| 92 | +func (s *Service) SignalExit(delegationID thor.Bytes32, val *validation.Validation) error { |
| 93 | + delegation, err := s.GetDelegation(delegationID) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + if delegation.LastIteration != nil { |
| 99 | + return errors.New("delegation is already disabled for auto-renew") |
| 100 | + } |
| 101 | + if delegation.Stake.Sign() == 0 { |
| 102 | + return errors.New("delegation is not active") |
| 103 | + } |
| 104 | + if !delegation.Started(val) { |
| 105 | + return errors.New("delegation has not started yet, funds can be withdrawn") |
| 106 | + } |
| 107 | + if delegation.Ended(val) { |
| 108 | + return errors.New("delegation has ended, funds can be withdrawn") |
| 109 | + } |
| 110 | + |
| 111 | + last := val.CurrentIteration() |
| 112 | + delegation.LastIteration = &last |
| 113 | + |
| 114 | + return s.SetDelegation(delegationID, delegation, false) |
| 115 | +} |
| 116 | + |
| 117 | +func (s *Service) Withdraw(delegationID thor.Bytes32) (*big.Int, *big.Int, error) { |
| 118 | + del, err := s.GetDelegation(delegationID) |
| 119 | + if err != nil { |
| 120 | + return nil, nil, err |
| 121 | + } |
| 122 | + |
| 123 | + // ensure the pointers are copied, not referenced |
| 124 | + withdrawableStake := new(big.Int).Set(del.Stake) |
| 125 | + withdrawableStakeWeight := del.CalcWeight() |
| 126 | + |
| 127 | + del.Stake = big.NewInt(0) |
| 128 | + if err := s.SetDelegation(delegationID, del, false); err != nil { |
| 129 | + return nil, nil, err |
| 130 | + } |
| 131 | + |
| 132 | + return withdrawableStake, withdrawableStakeWeight, nil |
| 133 | +} |
0 commit comments