Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dex: move SwapExecutions to NV storage #4107

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions crates/core/component/dex/src/component/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub trait StateReadExt: StateRead {
height: u64,
trading_pair: DirectedTradingPair,
) -> Result<Option<SwapExecution>> {
self.get(&state_key::swap_execution(height, trading_pair))
self.nonverifiable_get(state_key::swap_execution(height, trading_pair).as_bytes())
.await
}

Expand Down Expand Up @@ -240,17 +240,11 @@ pub trait StateWriteExt: StateWrite + StateReadExt {
// Store the swap executions for both directions in the state as well.
if let Some(swap_execution) = swap_execution_1_for_2.clone() {
let tp_1_for_2 = DirectedTradingPair::new(trading_pair.asset_1, trading_pair.asset_2);
self.put(
state_key::swap_execution(height, tp_1_for_2),
swap_execution,
);
self.put_swap_execution_at_height(height, tp_1_for_2, swap_execution);
}
if let Some(swap_execution) = swap_execution_2_for_1.clone() {
let tp_2_for_1 = DirectedTradingPair::new(trading_pair.asset_2, trading_pair.asset_1);
self.put(
state_key::swap_execution(height, tp_2_for_1),
swap_execution,
);
self.put_swap_execution_at_height(height, tp_2_for_1, swap_execution);
}

// ... and also add it to the set in the compact block to be pushed out to clients.
Expand Down Expand Up @@ -299,6 +293,16 @@ pub trait StateWriteExt: StateWrite + StateReadExt {

Ok(())
}

fn put_swap_execution_at_height(
&mut self,
height: u64,
pair: DirectedTradingPair,
swap_execution: SwapExecution,
) {
let path = state_key::swap_execution(height, pair);
self.nonverifiable_put(path.as_bytes().to_vec(), swap_execution);
}
}

impl<T: StateWrite> StateWriteExt for T {}
9 changes: 9 additions & 0 deletions crates/proto/src/state/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ pub trait StateWriteProto: StateWrite + Send + Sync {
self.put_raw(key, value.encode_to_vec());
}

/// Puts a domain type into the nonverifiable key-value store with the given key
fn nonverifiable_put<D>(&mut self, key: Vec<u8>, value: D)
where
D: DomainType,
anyhow::Error: From<<D as TryFrom<D::Proto>>::Error>,
{
self.nonverifiable_put_raw(key, value.encode_to_vec());
}

/// Records a Protobuf message as a typed ABCI event.
fn record_proto<E>(&mut self, proto_event: E)
where
Expand Down
Loading