SetValidators() upserts multiple Validators into the state and updates the supply tracker
(validators []*Validator, supply *Supply)
| 129 | |
| 130 | // SetValidators() upserts multiple Validators into the state and updates the supply tracker |
| 131 | func (s *StateMachine) SetValidators(validators []*Validator, supply *Supply) lib.ErrorI { |
| 132 | // for each validator in the list |
| 133 | for _, val := range validators { |
| 134 | // if the unstaking height or the max paused height is set |
| 135 | if val.UnstakingHeight != 0 { |
| 136 | // if the validator is unstaking - update it accordingly in state |
| 137 | if err := s.SetValidatorUnstaking(crypto.NewAddress(val.Address), val, val.UnstakingHeight); err != nil { |
| 138 | return err |
| 139 | } |
| 140 | } else if val.MaxPausedHeight != 0 { |
| 141 | // if validator is paused - update it accordingly |
| 142 | if err := s.SetValidatorPaused(crypto.NewAddress(val.Address), val, val.MaxPausedHeight); err != nil { |
| 143 | return err |
| 144 | } |
| 145 | } |
| 146 | // add to 'total supply' in the supply tracker |
| 147 | supply.Total += val.StakedAmount |
| 148 | // add to 'staked supply' in the supply tracker |
| 149 | supply.Staked += val.StakedAmount |
| 150 | // set the validator structure in state |
| 151 | if err := s.SetValidator(val); err != nil { |
| 152 | return err |
| 153 | } |
| 154 | // if the validator is a 'delegate' |
| 155 | if val.Delegate { |
| 156 | // add to the delegation supply |
| 157 | supply.DelegatedOnly += val.StakedAmount |
| 158 | // set the delegations in state |
| 159 | if err := s.SetDelegations(crypto.NewAddressFromBytes(val.Address), val.StakedAmount, val.Committees); err != nil { |
| 160 | return err |
| 161 | } |
| 162 | } else { |
| 163 | if err := s.SetCommittees(crypto.NewAddressFromBytes(val.Address), val.StakedAmount, val.Committees); err != nil { |
| 164 | return err |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | // |
| 169 | // get the 'supply tracker' from state to update the local 'supply tracker' with the automatically populated committee/delegate staked pool |
| 170 | supplyFromState, err := s.GetSupply() |
| 171 | if err != nil { |
| 172 | return err |
| 173 | } |
| 174 | // update the committee staked pool |
| 175 | supply.CommitteeStaked = supplyFromState.CommitteeStaked |
| 176 | // update the delegate staked pool |
| 177 | supply.CommitteeDelegatedOnly = supplyFromState.CommitteeDelegatedOnly |
| 178 | return nil |
| 179 | } |
| 180 | |
| 181 | // SetValidator() upserts a Validator object into the state |
| 182 | func (s *StateMachine) SetValidator(validator *Validator) (err lib.ErrorI) { |