lido 1.1.0-patch.1

Lido for Solana is a Lido DAO-governed liquid staking protocol for the Solana blockchain.
Documentation
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// SPDX-FileCopyrightText: 2021 Chorus One AG
// SPDX-License-Identifier: GPL-3.0

//! Logic for keeping the stake pool balanced.

use std::ops::Mul;

use crate::account_map::PubkeyAndEntry;
use crate::state::{Validator, Validators};
use crate::{
    error::LidoError,
    token,
    token::{Lamports, Rational},
};

/// Compute the ideal stake balance for each validator.
///
/// The validator order in the result is the same as in `current_balance`.
///
/// This function targets a uniform distribution over all active validators.
pub fn get_target_balance(
    undelegated_lamports: Lamports,
    validators: &Validators,
) -> Result<Vec<Lamports>, LidoError> {
    let total_delegated_lamports: token::Result<Lamports> = validators
        .iter_entries()
        .map(|v| v.stake_accounts_balance)
        .sum();

    let total_lamports = total_delegated_lamports.and_then(|t| t + undelegated_lamports)?;

    // We only want to target validators that are not in the process of being
    // removed.
    let num_active_validators = validators.iter_active().count() as u64;

    // No active validators.
    if num_active_validators == 0 {
        return Err(LidoError::NoActiveValidators);
    }

    let lamports_per_validator = total_lamports
        .mul(Rational {
            numerator: 1,
            denominator: num_active_validators,
        })
        .expect("Does not divide by zero because `num_active_validators != 0`");

    // Target an uniform distribution.
    let mut target_balance: Vec<Lamports> = validators
        .iter_entries()
        .map(|validator| {
            if validator.active {
                lamports_per_validator
            } else {
                Lamports(0)
            }
        })
        .collect();

    // The total lamports to distribute may be slightly larger than the total
    // lamports we distributed so far, because we round down.
    let total_lamports_distributed = target_balance
        .iter()
        .cloned()
        .sum::<token::Result<Lamports>>()
        .expect("Does not overflow, is at most total_lamports.");

    let mut remainder = (total_lamports - total_lamports_distributed)
        .expect("Does not underflow because we distribute at most total_lamports.");

    assert!(remainder.0 < num_active_validators);

    // Distribute the remainder among the first few active validators, give them
    // one Lamport each. This does mean that the validators early in the list
    // are in a more beneficial position because their stake target is one
    // Lamport higher, but to put that number into perspective, the transaction
    // fee per signature is 10k Lamports at the time of writing. Also, there is
    // a minimum amount we can stake, so in practice, validators will never be
    // as close to their target that the one Lamport matters anyway.
    for (target, validator) in target_balance.iter_mut().zip(validators.iter_entries()) {
        if remainder == Lamports(0) {
            break;
        }
        if validator.active {
            *target = (*target + Lamports(1)).expect(
                "Does not overflow because per-validator balance is at most total_lamports.",
            );
            remainder =
                (remainder - Lamports(1)).expect("Does not underflow due to loop condition.");
        }
    }

    // Sanity check: now we should have distributed all inputs.
    let total_lamports_distributed = target_balance
        .iter()
        .cloned()
        .sum::<token::Result<Lamports>>()
        .expect("Does not overflow, is at most total_lamports.");

    assert_eq!(total_lamports_distributed, total_lamports);

    Ok(target_balance)
}

/// Get the index of the validator to unstake from, if we need to unstake at all.

/// If any validator is more than threshold away from its target, this function
/// will try to unstake, and return the index of the validator where unstaking
/// will have the largest impact.
pub fn get_unstake_validator_index(
    validators: &Validators,
    target_balance: &[Lamports],
    threshold: Rational,
) -> Option<(usize, Lamports)> {
    // Check if we need to rebalance because a validator is too far away from
    // its target.
    let needs_unstake =
        validators
            .entries
            .iter()
            .zip(target_balance.iter())
            .any(|(validator, target)| {
                let target_difference = target
                    .0
                    .saturating_sub(validator.entry.effective_stake_balance().0);
                if target == &Lamports(0) {
                    return false;
                }
                Rational {
                    numerator: target_difference,
                    denominator: target.0,
                } >= threshold
            });

    let ((idx, validator), target) = validators
        .entries
        .iter()
        .enumerate()
        .zip(target_balance)
        .max_by_key(|((_idx, validator), target)| {
            validator
                .entry
                .effective_stake_balance()
                .0
                .saturating_sub(target.0)
        })?;

    let amount = validator
        .entry
        .effective_stake_balance()
        .0
        .saturating_sub(target.0);
    let ratio = Rational {
        numerator: amount,
        denominator: target.0,
    };
    if ratio >= threshold || needs_unstake {
        Some((idx, Lamports(amount)))
    } else {
        None
    }
}

/// Given a list of validators and their target balance, return the index of the
/// validator that has less stake, and the amount by which it is below its target.
///
/// This assumes that there is at least one active validator. Panics otherwise.
pub fn get_minimum_stake_validator_index_amount(
    validators: &Validators,
    target_balance: &[Lamports],
) -> (usize, Lamports) {
    assert_eq!(
        validators.len(),
        target_balance.len(),
        "Must have as many target balances as current balances."
    );

    // Our initial index, that will be returned when no validator is below its target,
    // is the first active validator.
    let mut index = validators
        .iter_entries()
        .position(|v| v.active)
        .expect("get_minimum_stake_validator_index_amount requires at least one active validator.");
    let mut lowest_balance = validators.entries[index].entry.effective_stake_balance();
    let mut amount = Lamports(
        target_balance[index]
            .0
            .saturating_sub(validators.entries[index].entry.effective_stake_balance().0),
    );

    for (i, (validator, target)) in validators.iter_entries().zip(target_balance).enumerate() {
        if validator.active && validator.effective_stake_balance() < lowest_balance {
            index = i;
            amount = Lamports(
                target
                    .0
                    .saturating_sub(validator.effective_stake_balance().0),
            );
            lowest_balance = validator.effective_stake_balance();
        }
    }

    (index, amount)
}

pub fn get_validator_to_withdraw(
    validators: &Validators,
) -> Result<&PubkeyAndEntry<Validator>, crate::error::LidoError> {
    validators
        .entries
        .iter()
        .max_by_key(|v| v.entry.effective_stake_balance())
        .ok_or(LidoError::NoActiveValidators)
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::state::Validators;
    use crate::token::Lamports;

    #[test]
    fn get_target_balance_works_for_single_validator() {
        // 100 Lamports delegated + 50 undelegated => 150 per validator target.
        let mut validators = Validators::new_fill_default(1);
        validators.entries[0].entry.stake_accounts_balance = Lamports(100);
        let undelegated_stake = Lamports(50);
        let targets = get_target_balance(undelegated_stake, &validators).unwrap();
        assert_eq!(targets[0], Lamports(150));

        // With only one validator, that one is the least balanced. It is
        // missing the 50 undelegated Lamports.
        assert_eq!(
            get_minimum_stake_validator_index_amount(&validators, &targets[..]),
            (0, Lamports(50))
        );
    }

    #[test]
    fn get_target_balance_works_for_integer_multiple() {
        // 200 Lamports delegated + 50 undelegated => 125 per validator target.
        let mut validators = Validators::new_fill_default(2);
        validators.entries[0].entry.stake_accounts_balance = Lamports(101);
        validators.entries[1].entry.stake_accounts_balance = Lamports(99);

        let undelegated_stake = Lamports(50);
        let targets = get_target_balance(undelegated_stake, &validators).unwrap();
        assert_eq!(targets, [Lamports(125), Lamports(125)]);

        // The second validator is further away from its target.
        assert_eq!(
            get_minimum_stake_validator_index_amount(&validators, &targets[..]),
            (1, Lamports(26))
        );
    }

    #[test]
    fn get_target_balance_works_for_non_integer_multiple() {
        // 200 Lamports delegated + 51 undelegated => 125 per validator target,
        // and one validator gets 1 more.
        let mut validators = Validators::new_fill_default(2);
        validators.entries[0].entry.stake_accounts_balance = Lamports(101);
        validators.entries[1].entry.stake_accounts_balance = Lamports(99);

        let undelegated_stake = Lamports(51);
        let targets = get_target_balance(undelegated_stake, &validators).unwrap();
        assert_eq!(targets, [Lamports(126), Lamports(125)]);

        // The second validator is further from its target, by one Lamport.
        assert_eq!(
            get_minimum_stake_validator_index_amount(&validators, &targets[..]),
            (1, Lamports(26))
        );
    }

    #[test]
    fn get_target_balance_already_balanced() {
        let mut validators = Validators::new_fill_default(2);
        validators.entries[0].entry.stake_accounts_balance = Lamports(50);
        validators.entries[1].entry.stake_accounts_balance = Lamports(50);

        let undelegated_stake = Lamports(0);
        let targets = get_target_balance(undelegated_stake, &validators).unwrap();
        assert_eq!(targets, [Lamports(50), Lamports(50)]);

        assert_eq!(
            get_minimum_stake_validator_index_amount(&validators, &targets[..]),
            (0, Lamports(0))
        );
    }
    #[test]
    fn get_target_balance_works_with_inactive_for_non_integer_multiple() {
        let mut validators = Validators::new_fill_default(3);
        validators.entries[0].entry.stake_accounts_balance = Lamports(101);
        validators.entries[1].entry.stake_accounts_balance = Lamports(0);
        validators.entries[1].entry.active = false;
        validators.entries[2].entry.stake_accounts_balance = Lamports(99);

        let undelegated_stake = Lamports(51);
        let targets = get_target_balance(undelegated_stake, &validators).unwrap();
        assert_eq!(targets, [Lamports(126), Lamports(0), Lamports(125)]);

        assert_eq!(
            get_minimum_stake_validator_index_amount(&validators, &targets[..]),
            (2, Lamports(26))
        );
    }

    #[test]
    fn get_target_balance_works_with_inactive_for_integer_multiple() {
        // 500 Lamports delegated, but only two active validators out of three.
        // All target should be divided equally within the active validators.
        let mut validators = Validators::new_fill_default(3);
        validators.entries[0].entry.stake_accounts_balance = Lamports(100);
        validators.entries[1].entry.stake_accounts_balance = Lamports(100);
        validators.entries[1].entry.active = false;
        validators.entries[2].entry.stake_accounts_balance = Lamports(300);

        let undelegated_stake = Lamports(0);
        let targets = get_target_balance(undelegated_stake, &validators).unwrap();
        assert_eq!(targets, [Lamports(250), Lamports(0), Lamports(250)]);

        assert_eq!(
            get_minimum_stake_validator_index_amount(&validators, &targets[..]),
            (0, Lamports(150))
        );
    }

    #[test]
    fn get_target_balance_all_inactive() {
        // No active validators exist.
        let mut validators = Validators::new_fill_default(3);
        validators.entries[0].entry.stake_accounts_balance = Lamports(1);
        validators.entries[1].entry.stake_accounts_balance = Lamports(2);
        validators.entries[2].entry.stake_accounts_balance = Lamports(3);
        validators.entries[0].entry.active = false;
        validators.entries[1].entry.active = false;
        validators.entries[2].entry.active = false;

        let undelegated_stake = Lamports(0);
        let result = get_target_balance(undelegated_stake, &validators);
        assert!(result.is_err());
    }

    #[test]
    fn get_target_balance_no_preference_but_some_inactive() {
        // Every validator is exactly at its target, no validator is below.
        // But the validator furthest below target should still be an active one,
        // not the inactive one.
        let mut validators = Validators::new_fill_default(2);
        validators.entries[0].entry.stake_accounts_balance = Lamports(0);
        validators.entries[1].entry.stake_accounts_balance = Lamports(10);
        validators.entries[0].entry.active = false;

        let undelegated_stake = Lamports(0);
        let targets = get_target_balance(undelegated_stake, &validators).unwrap();
        assert_eq!(
            get_minimum_stake_validator_index_amount(&validators, &targets[..]),
            (1, Lamports(0)),
        );
    }

    #[test]
    fn get_target_balance_works_for_minimum_staked_validator() {
        let mut validators = Validators::new_fill_default(3);
        validators.entries[0].entry.stake_accounts_balance = Lamports(101);
        validators.entries[1].entry.stake_accounts_balance = Lamports(101);
        validators.entries[2].entry.stake_accounts_balance = Lamports(100);

        let undelegated_stake = Lamports(200);
        let targets = get_target_balance(undelegated_stake, &validators).unwrap();
        assert_eq!(targets, [Lamports(168), Lamports(167), Lamports(167)]);

        assert_eq!(
            get_minimum_stake_validator_index_amount(&validators, &targets[..]),
            (2, Lamports(67))
        );
    }

    #[test]
    fn get_unstake_from_active_validator_above_or_equal_threshold() {
        let mut validators = Validators::new_fill_default(3);
        validators.entries[0].entry.stake_accounts_balance = Lamports(10);
        validators.entries[1].entry.stake_accounts_balance = Lamports(16);
        validators.entries[2].entry.stake_accounts_balance = Lamports(10);

        let targets = get_target_balance(Lamports(0), &validators).unwrap();

        let minimum_unstake = get_unstake_validator_index(
            &validators,
            &targets,
            Rational {
                numerator: 1,
                denominator: 4,
            },
        );
        assert_eq!(minimum_unstake, Some((1, Lamports(4))));
        let minimum_unstake = get_unstake_validator_index(
            &validators,
            &targets,
            Rational {
                numerator: 1,
                denominator: 5,
            },
        );
        assert_eq!(minimum_unstake, Some((1, Lamports(4))));
    }

    #[test]
    fn get_unstake_from_active_validator_below_threshold() {
        let mut validators = Validators::new_fill_default(3);
        validators.entries[0].entry.stake_accounts_balance = Lamports(10);
        validators.entries[1].entry.stake_accounts_balance = Lamports(16);
        validators.entries[2].entry.stake_accounts_balance = Lamports(10);

        let targets = get_target_balance(Lamports(0), &validators).unwrap();

        // Test below the threshold.
        let minimum_unstake = get_unstake_validator_index(
            &validators,
            &targets,
            Rational {
                numerator: 1,
                denominator: 2,
            },
        );
        assert_eq!(minimum_unstake, None);
    }

    #[test]
    fn get_unstake_from_active_validator_because_another_needs_stake() {
        let mut validators = Validators::new_fill_default(3);
        validators.entries[0].entry.stake_accounts_balance = Lamports(17);
        validators.entries[1].entry.stake_accounts_balance = Lamports(15);
        validators.entries[2].entry.stake_accounts_balance = Lamports(0);

        let targets = get_target_balance(Lamports(0), &validators).unwrap();

        // Test get the unstake index even if the validator is not below the threshold but some other is.
        let minimum_unstake = get_unstake_validator_index(
            &validators,
            &targets,
            Rational {
                numerator: 1,
                denominator: 1,
            },
        );
        assert_eq!(minimum_unstake, Some((0, Lamports(6))))
    }
}