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 a8a5b46
Showing 1 changed file with 36 additions and 2 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.try_into()?);

// Store the correctly formatted new value:
delta.nonverifiable_put(key, 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

0 comments on commit a8a5b46

Please sign in to comment.