rewrite vote credits redemption to eat from rewards_pools on an epoch-sensitive basis (#4775)

* move redemption to rewards pools

* rewrite redemption, touch a few other things

* re-establish test coverage
This commit is contained in:
Rob Walker
2019-06-21 20:43:24 -07:00
committed by GitHub
parent f39e74f0d7
commit a49f5378e2
12 changed files with 570 additions and 553 deletions

View File

@ -4,6 +4,8 @@ use crate::account::Account;
use crate::syscall;
use bincode::serialized_size;
pub use crate::timing::{Epoch, Slot};
crate::solana_name_id!(ID, "Sysca11Current11111111111111111111111111111");
const ID: [u8; 32] = [
@ -14,9 +16,9 @@ const ID: [u8; 32] = [
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct Current {
pub slot: u64,
pub epoch: u64,
pub stakers_epoch: u64,
pub slot: Slot,
pub epoch: Epoch,
pub stakers_epoch: Epoch,
}
impl Current {
@ -32,7 +34,7 @@ impl Current {
}
}
pub fn create_account(lamports: u64, slot: u64, epoch: u64, stakers_epoch: u64) -> Account {
pub fn create_account(lamports: u64, slot: Slot, epoch: Epoch, stakers_epoch: Epoch) -> Account {
Account::new_data(
lamports,
&Current {
@ -45,6 +47,15 @@ pub fn create_account(lamports: u64, slot: u64, epoch: u64, stakers_epoch: u64)
.unwrap()
}
use crate::account::KeyedAccount;
use crate::instruction::InstructionError;
pub fn from_keyed_account(account: &KeyedAccount) -> Result<Current, InstructionError> {
if !check_id(account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
Current::from(account.account).ok_or(InstructionError::InvalidArgument)
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -47,6 +47,16 @@ pub fn create_account(
.unwrap()
}
use crate::account::KeyedAccount;
use crate::instruction::InstructionError;
pub fn from_keyed_account(account: &KeyedAccount) -> Result<Rewards, InstructionError> {
if !check_id(account.unsigned_key()) {
dbg!(account.unsigned_key());
return Err(InstructionError::InvalidArgument);
}
Rewards::from(account.account).ok_or(InstructionError::InvalidAccountData)
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -9,6 +9,8 @@ use crate::syscall;
use bincode::serialized_size;
use std::ops::Deref;
pub use crate::timing::Slot;
/// "Sysca11SlotHashes11111111111111111111111111"
/// slot hashes account pubkey
const ID: [u8; 32] = [
@ -29,7 +31,7 @@ pub const MAX_SLOT_HASHES: usize = 512; // 512 slots to get your vote in
#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct SlotHashes {
// non-pub to keep control of size
inner: Vec<(u64, Hash)>,
inner: Vec<(Slot, Hash)>,
}
impl SlotHashes {
@ -46,10 +48,15 @@ impl SlotHashes {
})
.unwrap() as usize
}
pub fn add(&mut self, slot: u64, hash: Hash) {
pub fn add(&mut self, slot: Slot, hash: Hash) {
self.inner.insert(0, (slot, hash));
self.inner.truncate(MAX_SLOT_HASHES);
}
pub fn new(slot_hashes: &[(Slot, Hash)]) -> Self {
Self {
inner: slot_hashes.to_vec(),
}
}
}
impl Deref for SlotHashes {
@ -59,8 +66,19 @@ impl Deref for SlotHashes {
}
}
pub fn create_account(lamports: u64) -> Account {
Account::new(lamports, SlotHashes::size_of(), &syscall::id())
pub fn create_account(lamports: u64, slot_hashes: &[(Slot, Hash)]) -> Account {
let mut account = Account::new(lamports, SlotHashes::size_of(), &syscall::id());
SlotHashes::new(slot_hashes).to(&mut account).unwrap();
account
}
use crate::account::KeyedAccount;
use crate::instruction::InstructionError;
pub fn from_keyed_account(account: &KeyedAccount) -> Result<SlotHashes, InstructionError> {
if !check_id(account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
SlotHashes::from(account.account).ok_or(InstructionError::InvalidArgument)
}
#[cfg(test)]
@ -71,7 +89,8 @@ mod tests {
#[test]
fn test_create_account() {
let lamports = 42;
let account = create_account(lamports);
let account = create_account(lamports, &[]);
assert_eq!(account.data.len(), SlotHashes::size_of());
let slot_hashes = SlotHashes::from(&account);
assert_eq!(slot_hashes, Some(SlotHashes { inner: vec![] }));
let mut slot_hashes = slot_hashes.unwrap();

View File

@ -57,3 +57,11 @@ pub fn timestamp() -> u64 {
.expect("create timestamp in timing");
duration_as_ms(&now)
}
/// Slot is a unit of time given to a leader for encoding,
/// is some some number of Ticks long. Use a u64 to count them.
pub type Slot = u64;
/// Epoch is a unit of time a given leader schedule is honored,
/// some number of Slots. Use a u64 to count them.
pub type Epoch = u64;