Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
breathx committed Oct 17, 2024
1 parent 4da1c4d commit b9d2e3b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 48 deletions.
10 changes: 8 additions & 2 deletions ethexe/processor/src/handling/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ impl Processor {
self.handle_reply_queueing(state_hash, replied_to, source, payload, value)?
{
in_block_transitions
.modify_state_with(actor_id, new_state_hash, 0, vec![value_claim], vec![])
.modify_transition(actor_id, |state_hash, transition| {
*state_hash = new_state_hash;
transition.claims.push(value_claim);
})
.ok_or_else(|| anyhow!("failed to modify state of recognized program"))?;

in_block_transitions.remove_task(
Expand All @@ -135,7 +138,10 @@ impl Processor {
self.handle_value_claiming(state_hash, claimed_id, source)?
{
in_block_transitions
.modify_state_with(actor_id, new_state_hash, 0, vec![value_claim], vec![])
.modify_transition(actor_id, |state_hash, transition| {
*state_hash = new_state_hash;
transition.claims.push(value_claim);
})
.ok_or_else(|| anyhow!("failed to modify state of recognized program"))?;

in_block_transitions.remove_task(
Expand Down
23 changes: 9 additions & 14 deletions ethexe/runtime/common/src/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,12 @@ impl<S: Storage> JournalHandler for Handler<'_, S> {
let source = dispatch.source();
let message = dispatch.into_parts().1;

let source_state_hash = self
.in_block_transitions
.state_of(&source)
self.in_block_transitions
.modify_transition(source, |_state_hash, transition| {
transition.messages.push(OutgoingMessage::from(message))
})
.expect("must exist");

self.in_block_transitions.modify_state_with(
source,
source_state_hash,
0,
vec![],
vec![OutgoingMessage::from(message)],
);

return;
}

Expand Down Expand Up @@ -400,14 +393,16 @@ impl<S: Storage> JournalHandler for Handler<'_, S> {
return;
}

let state_hash = self.update_state(to, |state| {
self.update_state(to, |state| {
state.balance += value;
Ok(())
});

self.in_block_transitions
.modify_state_with(to, state_hash, value, vec![], vec![])
.expect("queried above; infallible");
.modify_transition(to, |_state_hash, transition| {
transition.value_to_receive += value
})
.expect("must exist");
}
}

Expand Down
35 changes: 3 additions & 32 deletions ethexe/runtime/common/src/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,38 +129,9 @@ impl InBlockTransitions {
}

pub fn modify_state(&mut self, actor_id: ActorId, new_state_hash: H256) -> Option<()> {
self.modify_state_with(
actor_id,
new_state_hash,
0,
Default::default(),
Default::default(),
)
}

pub fn modify_state_with(
&mut self,
actor_id: ActorId,
new_state_hash: H256,
extra_value_to_receive: u128,
extra_claims: Vec<ValueClaim>,
extra_messages: Vec<OutgoingMessage>,
) -> Option<()> {
let initial_state = self.states.insert(actor_id, new_state_hash)?;

let transition = self
.modifications
.entry(actor_id)
.or_insert(NonFinalTransition {
initial_state,
..Default::default()
});

transition.value_to_receive += extra_value_to_receive;
transition.claims.extend(extra_claims);
transition.messages.extend(extra_messages);

Some(())
self.modify_transition(actor_id, |state_hash, _transition| {
*state_hash = new_state_hash
})
}

pub fn finalize(self) -> (Vec<StateTransition>, BTreeMap<ActorId, H256>, Schedule) {
Expand Down

0 comments on commit b9d2e3b

Please sign in to comment.