Add versioning (#8348)

automerge
This commit is contained in:
carllin
2020-02-25 17:12:01 -08:00
committed by GitHub
parent 6b99ab3a57
commit d821fd29d6
11 changed files with 318 additions and 102 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,60 @@
use super::*;
const MAX_ITEMS: usize = 32;
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct VoteState0_23_5 {
/// the node that votes in this account
pub node_pubkey: Pubkey,
/// the signer for vote transactions
pub authorized_voter: Pubkey,
/// when the authorized voter was set/initialized
pub authorized_voter_epoch: Epoch,
/// history of prior authorized voters and the epoch ranges for which
/// they were set
pub prior_voters: CircBuf<(Pubkey, Epoch, Epoch, Slot)>,
/// the signer for withdrawals
pub authorized_withdrawer: Pubkey,
/// percentage (0-100) that represents what part of a rewards
/// payout should be given to this VoteAccount
pub commission: u8,
pub votes: VecDeque<Lockout>,
pub root_slot: Option<u64>,
/// history of how many credits earned by the end of each epoch
/// each tuple is (Epoch, credits, prev_credits)
pub epoch_credits: Vec<(Epoch, u64, u64)>,
/// most recent timestamp submitted with a vote
pub last_timestamp: BlockTimestamp,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct CircBuf<I> {
pub buf: [I; MAX_ITEMS],
/// next pointer
pub idx: usize,
}
impl<I: Default + Copy> Default for CircBuf<I> {
fn default() -> Self {
Self {
buf: [I::default(); MAX_ITEMS],
idx: MAX_ITEMS - 1,
}
}
}
impl<I> CircBuf<I> {
pub fn append(&mut self, item: I) {
// remember prior delegate and when we switched, to support later slashing
self.idx += 1;
self.idx %= MAX_ITEMS;
self.buf[self.idx] = item;
}
}

View File

@@ -0,0 +1,60 @@
use super::*;
use crate::vote_state::vote_state_0_23_5::VoteState0_23_5;
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub enum VoteStateVersions {
V0_23_5(Box<VoteState0_23_5>),
Current(Box<VoteState>),
}
impl VoteStateVersions {
pub fn convert_to_current(self) -> VoteState {
match self {
VoteStateVersions::V0_23_5(state) => {
let authorized_voters =
AuthorizedVoters::new(state.authorized_voter_epoch, state.authorized_voter);
VoteState {
node_pubkey: state.node_pubkey,
/// the signer for withdrawals
authorized_withdrawer: state.authorized_withdrawer,
/// percentage (0-100) that represents what part of a rewards
/// payout should be given to this VoteAccount
commission: state.commission,
votes: state.votes.clone(),
root_slot: state.root_slot,
/// the signer for vote transactions
authorized_voters,
/// history of prior authorized voters and the epochs for which
/// they were set, the bottom end of the range is inclusive,
/// the top of the range is exclusive
prior_voters: CircBuf::default(),
/// history of how many credits earned by the end of each epoch
/// each tuple is (Epoch, credits, prev_credits)
epoch_credits: state.epoch_credits.clone(),
/// most recent timestamp submitted with a vote
last_timestamp: state.last_timestamp.clone(),
}
}
VoteStateVersions::Current(state) => *state,
}
}
pub fn is_uninitialized(&self) -> bool {
match self {
VoteStateVersions::V0_23_5(vote_state) => {
vote_state.authorized_voter == Pubkey::default()
}
VoteStateVersions::Current(vote_state) => vote_state.authorized_voters.is_empty(),
}
}
}