Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Nov 21, 2022
1 parent 8cc15c0 commit 8b48db9
Show file tree
Hide file tree
Showing 13 changed files with 535 additions and 92 deletions.
2 changes: 1 addition & 1 deletion core/src/ledger/storage/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,4 +874,4 @@ mod test {
);
assert!(basetree_verification_res);
}
}
}
30 changes: 21 additions & 9 deletions core/src/types/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use arse_merkle_tree::traits::Value;
use arse_merkle_tree::{InternalKey, Key as TreeKey};
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use data_encoding::BASE32HEX_NOPAD;
use regex::Regex;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use regex::Regex;

use crate::bytes::ByteBuf;
use crate::types::address::{self, Address};
Expand Down Expand Up @@ -772,24 +772,36 @@ impl_int_key_seg!(u32, i32, 4);
impl_int_key_seg!(u64, i64, 8);
impl_int_key_seg!(u128, i128, 16);

impl KeySeg for (Epoch, Epoch) {
impl KeySeg for (Epoch, Option<Epoch>) {
fn parse(string: String) -> Result<Self>
where
Self: Sized,
{
let re = Regex::new(r"\((\d{1}),(\d{1})\)").unwrap();
let re = Regex::new(r"\((\d{1}),(-?)(\d?)\)").unwrap();
let caps = re.captures(string.as_str()).unwrap();
let first = caps.get(1).map(|m| m.as_str().to_owned()).unwrap();
let second = caps.get(2).map(|m| m.as_str().to_owned()).unwrap();

let first = u64::parse(first)?;
let second = u64::parse(second)?;
let second = caps.get(2).map_or(None, |m| Some(m.as_str().to_owned()));
let third = caps.get(3).map_or(None, |m| Some(m.as_str().to_owned()));

Ok((Epoch(first), Epoch(second)))
let first = u64::parse(first)?;
match second {
Some(epoch_str) => {
let third = u64::parse(epoch_str)?;
Ok((Epoch(first), Some(Epoch(third))))
}
None => Ok((Epoch(first), None)),
}
}

fn raw(&self) -> String {
format!("({},{})",self.0,self.1)
match self.1 {
Some(epoch) => {
format!("({},{})", self.0, epoch)
}
None => {
format!("({},-)", self.0)
}
}
}

fn to_db_key(&self) -> DbKeySeg {
Expand Down
37 changes: 24 additions & 13 deletions proof_of_stake/src/epoched_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ impl<Data, FutureEpochs, const NUM_PAST_EPOCHS: u64>
EpochedDelta<Data, FutureEpochs, NUM_PAST_EPOCHS>
where
FutureEpochs: EpochOffset,
Data: BorshSerialize + BorshDeserialize + ops::Add<Output = Data> + 'static + Debug,
Data: BorshSerialize
+ BorshDeserialize
+ ops::Add<Output = Data>
+ 'static
+ Debug,
{
/// Open the handle
pub fn open(key: storage::Key) -> Self {
Expand Down Expand Up @@ -315,7 +319,8 @@ where
None => return Ok(None),
Some(last_update) => {
let data_handler = self.get_data_handler();
let future_most_epoch = last_update + FutureEpochs::value(params);
let future_most_epoch =
last_update + FutureEpochs::value(params);
// Epoch can be a lot greater than the epoch where
// a value is recorded, we check the upper bound
// epoch of the LazyMap data
Expand All @@ -325,7 +330,7 @@ where
match (&mut sum, next) {
(Some(_), Ok((next_epoch, next_val))) => {
if next_epoch > epoch {
return Ok(sum)
return Ok(sum);
} else {
sum = sum.map(|cur_sum| cur_sum + next_val)
}
Expand All @@ -337,9 +342,7 @@ where
sum = Some(next_val)
}
}
(Some(_), Err(_)) => {
return Ok(sum)
}
(Some(_), Err(_)) => return Ok(sum),
// perhaps elaborate with an error
_ => return Ok(None),
};
Expand Down Expand Up @@ -383,9 +386,10 @@ where

/// TODO: maybe better description
/// Update the data associated with epochs, if needed. Any key-value with
/// epoch before the oldest stored epoch is added to the key-value with the oldest stored epoch that is kept. If the oldest
/// stored epoch is not already associated with some value, the latest
/// value from the dropped values, if any, is associated with it.
/// epoch before the oldest stored epoch is added to the key-value with the
/// oldest stored epoch that is kept. If the oldest stored epoch is not
/// already associated with some value, the latest value from the
/// dropped values, if any, is associated with it.
fn update_data<S>(
&self,
storage: &mut S,
Expand All @@ -404,18 +408,25 @@ where
let data_handler = self.get_data_handler();
let mut new_oldest_value: Option<Data> = None;
for offset in 1..diff + 1 {
let old = data_handler
.remove(storage, &Epoch(expected_oldest_epoch.0 - offset))?;
let old = data_handler.remove(
storage,
&Epoch(expected_oldest_epoch.0 - offset),
)?;
if old.is_some() {
match new_oldest_value {
Some(latest) => new_oldest_value = Some(latest + old.unwrap()),
Some(latest) => {
new_oldest_value = Some(latest + old.unwrap())
}
None => new_oldest_value = old,
}
}
}
if let Some(new_oldest_value) = new_oldest_value {
// TODO we can add `contains_key` to LazyMap
if data_handler.get(storage, &expected_oldest_epoch)?.is_none() {
if data_handler
.get(storage, &expected_oldest_epoch)?
.is_none()
{
data_handler.insert(
storage,
expected_oldest_epoch,
Expand Down
Loading

0 comments on commit 8b48db9

Please sign in to comment.