Skip to content

Commit

Permalink
Add migration for dex/ab/ values to store as proto-encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
zbuc committed May 7, 2024
1 parent 6c504c9 commit ba81c45
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
38 changes: 36 additions & 2 deletions crates/bin/pd/src/migrate/testnet74.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,40 @@ async fn fix_arb_execution_outputs(delta: &mut StateDelta<Snapshot>) -> anyhow::
Ok(())
}

/// Update base liquidity index values to be proto-encoded. Previously they were stored as big-endian
/// encoded amounts, but in https://github.com/penumbra-zone/penumbra/pull/4188 they were changed
/// to be proto-encoded.
///
/// This will rewrite all values under the `dex/ab/` prefix to be proto-encoded.
async fn rewrite_base_liquidity_indices(delta: &mut StateDelta<Snapshot>) -> anyhow::Result<()> {
let prefix_key = "dex/ab/".as_bytes();
tracing::trace!(prefix_key = ?EscapedByteSlice(&prefix_key), "updating base liquidity index values");
let mut liquidity_stream = delta.nonverifiable_prefix_raw(&prefix_key).boxed();

while let Some(r) = liquidity_stream.next().await {
let (key, raw_amount): (Vec<u8>, Vec<u8>) = r?;
tracing::info!(?key, raw_amount = ?EscapedByteSlice(&raw_amount), "migrating base liquidity index entry");

let amount = Amount::from_be_bytes(raw_amount.as_slice().try_into()?);

// Store the correctly formatted new value:
delta.nonverifiable_put(key.clone(), amount);
tracing::info!(
key = ?EscapedByteSlice(&key),
raw_amount = ?EscapedByteSlice(&raw_amount),
?amount,
"updated base liquidity index"
);
}

Ok(())
}

/// Update the ordering of liquidity position indices to return in descending order (see #4189)
///
/// Lookups for liquidity positions based on starting asset were ordered backwards
/// and returning the positions with the least liquidity first. This migration
/// needs to modify the keys stored under the JMT `dex/ra/` prefix key to reverse
/// needs to modify the keys stored under the nonverifiable `dex/ra/` prefix key to reverse
/// the ordering of the existing data.
async fn update_lp_index_order(delta: &mut StateDelta<Snapshot>) -> anyhow::Result<()> {
let prefix_key = "dex/ra/".as_bytes();
Expand Down Expand Up @@ -81,11 +110,13 @@ async fn update_lp_index_order(delta: &mut StateDelta<Snapshot>) -> anyhow::Resu
/// This migration script is responsible for:
///
/// - Updating the ordering of liquidity position indices to return in descending order (see #4189)
/// * JMT: `dex/ra/`
/// * nonverifiable: `dex/ra/`
/// - Updating arb execution output amounts to include the input amount instead of reporting only profit (see #3790)
/// * JMT: `dex/arb_execution/`
/// - Add `AuctionParameters` to the chain state
/// * JMT: `dex/auction_parameters`
/// - Update the base liquidity index values to be proto-encoded (see #4188)
/// * nonverifiable: `dex/ab/`
pub async fn migrate(
path_to_export: PathBuf,
genesis_start: Option<tendermint::time::Time>,
Expand Down Expand Up @@ -117,6 +148,9 @@ pub async fn migrate(
// Write auction parameters
write_auction_parameters(&mut delta).await?;

// Rewrite base liquidity indices as proto-encoded
rewrite_base_liquidity_indices(&mut delta).await?;

delta.put_block_height(0u64);
let post_upgrade_root_hash = storage.commit_in_place(delta).await?;
tracing::info!(?post_upgrade_root_hash, "post-upgrade root hash");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ pub trait PositionManager: StateWrite + PositionRead {
context: DirectedTradingPair,
) -> Result<Position> {
let position_id = new_state.id();
tracing::debug!(?position_id, "executing against position");
let prev_state = self
.position_by_id(&position_id)
.await?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ pub(crate) trait AssetByLiquidityIndex: StateWrite {
new_state: &Position,
id: &position::Id,
) -> Result<()> {
tracing::debug!("here");
// We need to reconstruct the position's previous contribution and compute
// its new contribution to the index. We do this for each asset in the pair
// and short-circuit if all contributions are zero.
Expand Down Expand Up @@ -154,11 +153,6 @@ trait Inner: StateWrite {
) -> Result<()> {
let aggregate_key = &engine::routable_assets::lookup_base_liquidity_by_pair(&pair);

let value = self.nonverifiable_get_raw(aggregate_key).await?;
let value_hex = value.as_ref().map(|v| hex::encode(v));
let key_hex = hex::encode(aggregate_key);
tracing::debug!(key_hex, value_hex, "k/v");

let prev_tally: Amount = self
.nonverifiable_get(aggregate_key)
.await?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub(crate) trait PositionCounter: StateWrite {
new_state: &Position,
_id: &position::Id,
) -> Result<()> {
tracing::debug!("updating trading pair position counter");
use position::State::*;
let trading_pair = new_state.phi.pair;
match (prev_state.as_ref().map(|p| p.state), new_state.state) {
Expand Down

0 comments on commit ba81c45

Please sign in to comment.