Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 249 additions & 0 deletions programs/validator-history/idl/validator_history.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@
}
]
},
{
"name": "copy_stake_info",
"discriminator": [
154,
206,
202,
123,
87,
125,
60,
240
],
"accounts": [
{
"name": "validator_history_account",
"writable": true
},
{
"name": "config"
},
{
"name": "validator_stake_buffer_account"
}
],
"args": []
},
{
"name": "copy_tip_distribution_account",
"discriminator": [
Expand Down Expand Up @@ -316,6 +342,34 @@
],
"args": []
},
{
"name": "initialize_validator_stake_buffer_account",
"discriminator": [
184,
250,
142,
48,
78,
36,
131,
111
],
"accounts": [
{
"name": "validator_stake_buffer_account",
"writable": true
},
{
"name": "system_program"
},
{
"name": "payer",
"writable": true,
"signer": true
}
],
"args": []
},
{
"name": "realloc_cluster_history_account",
"discriminator": [
Expand Down Expand Up @@ -410,6 +464,34 @@
],
"args": []
},
{
"name": "realloc_validator_stake_buffer_account",
"discriminator": [
56,
114,
33,
215,
109,
155,
244,
46
],
"accounts": [
{
"name": "validator_stake_buffer_account",
"writable": true
},
{
"name": "payer",
"writable": true,
"signer": true
},
{
"name": "system_program"
}
],
"args": []
},
{
"name": "set_new_admin",
"discriminator": [
Expand Down Expand Up @@ -596,6 +678,32 @@
}
]
},
{
"name": "update_stake_buffer",
"discriminator": [
43,
154,
133,
214,
246,
39,
27,
89
],
"accounts": [
{
"name": "validator_stake_buffer_account",
"writable": true
},
{
"name": "validator_history_account"
},
{
"name": "config"
}
],
"args": []
},
{
"name": "update_stake_history",
"discriminator": [
Expand Down Expand Up @@ -684,6 +792,19 @@
2,
146
]
},
{
"name": "ValidatorStakeBuffer",
"discriminator": [
53,
175,
63,
177,
129,
250,
119,
110
]
}
],
"errors": [
Expand Down Expand Up @@ -766,9 +887,55 @@
"code": 6015,
"name": "PriorityFeeDistributionAccountNotFinalized",
"msg": "Priority Fee Distribution Account cannot be copied during its own epoch"
},
{
"code": 6016,
"name": "StakeBufferFinalized",
"msg": "Stake buffer has been finalized"
},
{
"code": 6017,
"name": "StakeBufferNotFinalized",
"msg": "Stake buffer has not been finalized"
},
{
"code": 6018,
"name": "StakeBufferOutOfBounds",
"msg": "Stake buffer out of bounds"
},
{
"code": 6019,
"name": "StakeBufferEmpty",
"msg": "Stake buffer is empty"
},
{
"code": 6020,
"name": "StakeBufferDuplicate",
"msg": "Stake buffer already contains entry"
}
],
"types": [
{
"name": "BitMask",
"serialization": "bytemuck",
"repr": {
"kind": "c"
},
"type": {
"kind": "struct",
"fields": [
{
"name": "values",
"type": {
"array": [
"u64",
782
]
}
}
]
}
},
{
"name": "CircBuf",
"serialization": "bytemuck",
Expand Down Expand Up @@ -1240,6 +1407,88 @@
}
]
}
},
{
"name": "ValidatorStake",
"serialization": "bytemuck",
"repr": {
"kind": "c"
},
"type": {
"kind": "struct",
"fields": [
{
"name": "validator_index",
"type": "u32"
},
{
"name": "_padding0",
"type": "u32"
},
{
"name": "stake_amount",
"type": "u64"
}
]
}
},
{
"name": "ValidatorStakeBuffer",
"serialization": "bytemuck",
"repr": {
"kind": "c"
},
"type": {
"kind": "struct",
"fields": [
{
"name": "last_observed_epoch",
"type": "u64"
},
{
"name": "length",
"type": "u32"
},
{
"name": "finalized",
"type": "u8"
},
{
"name": "_padding0",
"type": {
"array": [
"u8",
131
]
}
},
{
"name": "total_stake",
"type": "u64"
},
{
"name": "inserted_validators",
"type": {
"defined": {
"name": "BitMask"
}
}
},
{
"name": "buffer",
"type": {
"array": [
{
"defined": {
"name": "ValidatorStake"
}
},
50000
]
}
}
]
}
}
]
}
54 changes: 54 additions & 0 deletions programs/validator-history/src/bitmask.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use anchor_lang::{prelude::*, zero_copy};
use borsh::BorshSerialize;

use crate::{constants::MAX_STAKE_BUFFER_VALIDATORS, errors::ValidatorHistoryError};

#[allow(clippy::manual_div_ceil)]
#[allow(clippy::identity_op)]
#[allow(clippy::integer_division)]
const BITMASK_SIZE: usize = (MAX_STAKE_BUFFER_VALIDATORS + 64 - 1) / 64;

#[derive(BorshSerialize, Debug, PartialEq)]
#[zero_copy]
pub struct BitMask {
pub values: [u64; BITMASK_SIZE],
}

impl Default for BitMask {
fn default() -> Self {
Self {
values: [0; BITMASK_SIZE],
}
}
}

impl BitMask {
#[allow(clippy::integer_division)]
pub fn set(&mut self, index: usize, value: bool) -> Result<()> {
if index >= MAX_STAKE_BUFFER_VALIDATORS {
return Err(ValidatorHistoryError::StakeBufferOutOfBounds.into());
}
let word = index / 64;
let bit = index % 64;
if value {
self.values[word] |= 1 << bit;
} else {
self.values[word] &= !(1 << bit);
}
Ok(())
}

#[allow(clippy::integer_division)]
pub fn get(&self, index: usize) -> Result<bool> {
if index >= MAX_STAKE_BUFFER_VALIDATORS {
return Err(ValidatorHistoryError::StakeBufferOutOfBounds.into());
}
let word = index / 64;
let bit = index % 64;
Ok((self.values[word] >> bit) & 1 == 1)
}

pub fn reset(&mut self) {
self.values = [0; BITMASK_SIZE];
}
}
3 changes: 2 additions & 1 deletion programs/validator-history/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub const MAX_ALLOC_BYTES: usize = 10240;
pub const MAX_ALLOC_BYTES: usize = 10_240;
pub const MAX_STAKE_BUFFER_VALIDATORS: usize = 50_000;
pub const MIN_VOTE_EPOCHS: usize = 5;
pub const TVC_MULTIPLIER: u32 = 16;
10 changes: 10 additions & 0 deletions programs/validator-history/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ pub enum ValidatorHistoryError {
PriorityFeeDistributionAccountAlreadyCopied,
#[msg("Priority Fee Distribution Account cannot be copied during its own epoch")]
PriorityFeeDistributionAccountNotFinalized,
#[msg("Stake buffer has been finalized")]
StakeBufferFinalized,
#[msg("Stake buffer has not been finalized")]
StakeBufferNotFinalized,
#[msg("Stake buffer out of bounds")]
StakeBufferOutOfBounds,
#[msg("Stake buffer is empty")]
StakeBufferEmpty,
#[msg("Stake buffer already contains entry")]
StakeBufferDuplicate,
}
Loading
Loading