Skip to content

Commit

Permalink
Move ticket balance updates to tmp state
Browse files Browse the repository at this point in the history
  • Loading branch information
matsakiv committed Aug 20, 2023
1 parent 9dceae9 commit aea7a2c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
24 changes: 19 additions & 5 deletions layered_store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct LayeredStore<Backend: StoreBackend> {
backend: Backend,
pending_state: HashMap<String, Option<(DynStoreType, StoreTypeSer)>>,
modified_keys: HashSet<String>,
//tmp_state: HashMap<String, Option<(DynStoreType, StoreTypeSer)>>,
tmp_state: HashMap<String, Option<(DynStoreType, StoreTypeSer)>>,
}

impl<Backend: StoreBackend> LayeredStore<Backend> {
Expand All @@ -51,7 +51,7 @@ impl<Backend: StoreBackend> LayeredStore<Backend> {
backend,
pending_state: HashMap::new(),
modified_keys: HashSet::new(),
//tmp_state: Hash
tmp_state: HashMap::new(),
}
}

Expand Down Expand Up @@ -111,14 +111,26 @@ impl<Backend: StoreBackend> LayeredStore<Backend> {
pub fn set_tmp<T: StoreType>(&mut self, key: String, val: Option<T>) -> Result<()> {
match val {
Some(value) => self
.pending_state
.tmp_state
.insert(key.clone(), Some((Box::new(value), Box::new(T::serialize)))),
None => self.pending_state.insert(key.clone(), None),
None => self.tmp_state.insert(key.clone(), None),
};
Ok(())
}

//pub fn iter_pending(&mut self, key_prefix: String, )
pub fn pop_tmp<T: StoreType>(&mut self) -> Result<Vec<T>> {
let values: Vec<T> = self
.tmp_state
.drain()
.filter(|v| v.1.is_some())
.map(|v| {
let (dyn_value, _) = v.1.expect("Value must be not None");
T::downcast_ref(&dyn_value).unwrap().clone()
})
.collect();

Ok(values)
}

pub fn commit(&mut self) -> Result<()> {
let modified_keys: Vec<String> = self.modified_keys.drain().collect();
Expand All @@ -145,12 +157,14 @@ impl<Backend: StoreBackend> LayeredStore<Backend> {
for key in self.modified_keys.drain().into_iter() {
self.pending_state.remove(&key);
}
//self.tmp_state.clear();
}

pub fn clear(&mut self) {
self.pending_state.clear();
self.modified_keys.clear();
self.backend.clear();
self.tmp_state.clear();
}
}

Expand Down
2 changes: 1 addition & 1 deletion michelson_vm/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub trait InterpreterContext {
owner: &Address,
value: IBig,
) -> Result<()>;
fn aggregate_ticket_updates(&self) -> Vec<TicketBalanceDiff>;
fn aggregate_ticket_updates(&mut self) -> Vec<TicketBalanceDiff>;
}

pub struct OperationScope {
Expand Down
2 changes: 1 addition & 1 deletion michelson_vm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl InterpreterContext for MockContext {
todo!()
}

fn aggregate_ticket_updates(&self) -> Vec<TicketBalanceDiff> {
fn aggregate_ticket_updates(&mut self) -> Vec<TicketBalanceDiff> {
todo!()
}
}
8 changes: 6 additions & 2 deletions michelson_vm/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ impl<Backend: StoreBackend> InterpreterContext for LayeredStore<Backend> {
Ok(())
}

fn aggregate_ticket_updates(&self) -> Vec<TicketBalanceDiff> {
todo!()
fn aggregate_ticket_updates(&mut self) -> Vec<TicketBalanceDiff> {
self.pop_tmp::<Micheline>()
.unwrap()
.iter()
.map(|v| TicketBalanceDiff::from_micheline(v))
.collect()
}
}
5 changes: 5 additions & 0 deletions michelson_vm/src/types/ticket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,12 @@ impl TicketBalanceDiff {
value,
}
}

pub fn into_micheline(&self) -> Micheline {
todo!()
}

pub fn from_micheline(micheline: &Micheline) -> Self {
todo!()
}
}

0 comments on commit aea7a2c

Please sign in to comment.