Skip to content

Commit

Permalink
custom Deserialize for MoveOSTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
pause125 committed Jul 30, 2024
1 parent cc2a77f commit 121b1b4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Binary file modified crates/rooch-genesis/released/test
Binary file not shown.
45 changes: 43 additions & 2 deletions moveos/moveos-types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use move_core_types::{
language_storage::{ModuleId, TypeTag},
vm_status::KeptVMStatus,
};
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};
use std::fmt::Display;

#[cfg(any(test, feature = "fuzzing"))]
Expand Down Expand Up @@ -238,7 +238,7 @@ impl Display for VerifiedMoveAction {
}
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub struct MoveOSTransaction {
pub root: ObjectMeta,
pub ctx: TxContext,
Expand Down Expand Up @@ -269,6 +269,47 @@ impl MoveOSTransaction {
}
}

/// Custom deserialization logic for MoveOSTransaction
/// `MoveOSTransaction` has been changed from a struct with 5 fields to a struct with 3 fields.
/// The old one was defined:
/// ```rust
/// pub struct MoveOSTransaction {
/// pub root: ObjectMeta,
/// pub ctx: TxContext,
/// pub action: MoveAction,
/// pub pre_execute_functions: Option<Vec<FunctionCall>>,
/// pub post_execute_functions: Option<Vec<FunctionCall>>,
/// }
/// ```
/// Some old transactions are still stored in the database or genesis file,
/// so we need to support deserializing the old format.
impl<'de> Deserialize<'de> for MoveOSTransaction {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(::serde::Deserialize)]
struct OldValue(
(
ObjectMeta,
TxContext,
MoveAction,
Option<Vec<FunctionCall>>,
Option<Vec<FunctionCall>>,
),
);

// FIXME: This is a hack to deserialize the old MoveOSTransaction format.
// We need compatible deserialization logic to support both old and new formats.
let value = OldValue::deserialize(deserializer)?;
Ok(MoveOSTransaction {
root: value.0 .0,
ctx: value.0 .1,
action: value.0 .2,
})
}
}

#[derive(Debug, Clone)]
pub struct GasStatement {
pub execution_gas_used: InternalGas,
Expand Down

0 comments on commit 121b1b4

Please sign in to comment.