Skip to content

Commit 6e06b8c

Browse files
committed
pda
1 parent 2615c54 commit 6e06b8c

File tree

12 files changed

+167
-0
lines changed

12 files changed

+167
-0
lines changed

Cargo.lock

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ serde_json = { version = "^1", default-features = false }
2323
# group them together to ensure same version throughout
2424
# and to deal with dependency hell more easily
2525
solana-account-decoder-client-types = { version = "^2", default-features = false }
26+
solana-pubkey = { version = "^2", default-features = false }
2627

2728
# workspace members

core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ bs58 = { workspace = true }
1212
serde = { workspace = true, features = ["derive"] }
1313
serde_json = { workspace = true }
1414
solana-account-decoder-client-types = { workspace = true }
15+
solana-pubkey = { workspace = true, features = ["curve25519"] }

core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
mod internal_utils;
44
mod keys;
5+
mod pda;
56
mod state;
67
mod typedefs;
78

89
pub use keys::*;
10+
pub use pda::*;
911
pub use state::*;
1012
pub use typedefs::*;

core/src/pda.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use crate::LIDO_STATE_ADDR;
2+
3+
pub const VALIDATOR_STAKE_ACCOUNT_SEED: [u8; 23] = *b"validator_stake_account";
4+
5+
pub type ValidatorStakeSeeds<'a> = (&'a [u8; 32], &'a [u8; 32], &'a [u8; 23], [u8; 8]);
6+
7+
#[inline]
8+
pub const fn validator_stake_seeds(vote_acc_addr: &[u8; 32], seed: u64) -> ValidatorStakeSeeds<'_> {
9+
(
10+
&LIDO_STATE_ADDR,
11+
vote_acc_addr,
12+
&VALIDATOR_STAKE_ACCOUNT_SEED,
13+
seed.to_le_bytes(),
14+
)
15+
}

core/src/typedefs/validator/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ mod seed_range;
66

77
pub use seed_range::*;
88

9+
use crate::{validator_stake_seeds, ValidatorStakeSeeds};
10+
911
#[repr(C)]
1012
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, BorshDeserialize, BorshSerialize)]
1113
pub struct Validator {
@@ -18,6 +20,16 @@ pub struct Validator {
1820
active: bool,
1921
}
2022

23+
impl Validator {
24+
/// Returns the seeds to find the PDA of the validator
25+
/// stake account to service stsol withdrawals from
26+
#[inline]
27+
pub const fn withdraw_stake_acc_seeds(&self) -> ValidatorStakeSeeds<'_> {
28+
validator_stake_seeds(&self.vote_account_address, self.stake_seeds.begin())
29+
}
30+
}
31+
32+
/// Getters and setters
2133
impl Validator {
2234
impl_set_with_get_ref!(
2335
set_vote_account_address,

core/tests/common/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mod accounts;
22
mod paths;
3+
mod pda;
34

45
pub use accounts::*;
56
pub use paths::*;
7+
pub use pda::*;

core/tests/common/pda.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use solana_pubkey::Pubkey;
2+
use solido_legacy_core::{Validator, PROGRAM_ID};
3+
4+
pub fn find_withdraw_stake_acc(validator: &Validator) -> (Pubkey, u8) {
5+
let (s0, s1, s2, s3) = validator.withdraw_stake_acc_seeds();
6+
Pubkey::find_program_address(
7+
&[s0.as_slice(), s1.as_slice(), s2.as_slice(), s3.as_slice()],
8+
&Pubkey::new_from_array(PROGRAM_ID),
9+
)
10+
}

core/tests/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
mod pda;
12
mod state;

core/tests/tests/pda.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use solana_pubkey::Pubkey;
2+
use solido_legacy_core::ValidatorList;
3+
4+
use crate::common::{find_withdraw_stake_acc, KeyedUiAccount};
5+
6+
const LARGEST_VAL_VOTE: [u8; 32] =
7+
const_crypto::bs58::decode_pubkey("DdCNGDpP7qMgoAy6paFzhhak2EeyCZcgjH7ak5u5v28m");
8+
9+
#[test]
10+
fn withdraw_stake_acc_sanity() {
11+
let account = KeyedUiAccount::from_test_fixtures_file("validator-list");
12+
let data = account.account_data();
13+
let deser = ValidatorList::deserialize(data.as_slice()).unwrap();
14+
15+
let val = deser
16+
.entries
17+
.iter()
18+
.find(|v| *v.vote_account_address() == LARGEST_VAL_VOTE)
19+
.unwrap();
20+
21+
let (pda, _bump) = find_withdraw_stake_acc(val);
22+
23+
assert_eq!(
24+
pda,
25+
Pubkey::from_str_const("58EuDup5Gg9nEqDoBBGEDnZ5fMc1u7fQi6kHmEESSUkh")
26+
);
27+
}

0 commit comments

Comments
 (0)