Skip to content

Commit

Permalink
types: fold EffectsObjectChange into ChangedObject struct
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Dec 19, 2024
1 parent 5f49887 commit aad2365
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 75 deletions.
1 change: 0 additions & 1 deletion crates/sui-sdk-types/src/types/effects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pub use v1::ModifiedAtVersion;
pub use v1::ObjectReferenceWithOwner;
pub use v1::TransactionEffectsV1;
pub use v2::ChangedObject;
pub use v2::EffectsObjectChange;
pub use v2::IdOperation;
pub use v2::ObjectIn;
pub use v2::ObjectOut;
Expand Down
83 changes: 13 additions & 70 deletions crates/sui-sdk-types/src/types/effects/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,24 @@ pub struct TransactionEffectsV2 {
pub auxiliary_data_digest: Option<EffectsAuxiliaryDataDigest>,
}

//XXX Do we maybe want to just fold "EffectsObjectChange" into this struct?
#[derive(Eq, PartialEq, Clone, Debug)]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct ChangedObject {
pub object_id: ObjectId,
#[cfg_attr(feature = "schemars", schemars(flatten))]
pub change: EffectsObjectChange,
/// State of the object in the store prior to this transaction.
pub input_state: ObjectIn,
/// State of the object in the store after this transaction.
pub output_state: ObjectOut,

/// Whether this object ID is created or deleted in this transaction.
/// This information isn't required by the protocol but is useful for providing more detailed
/// semantics on object changes.
pub id_operation: IdOperation,
}

#[derive(Eq, PartialEq, Clone, Debug)]
Expand Down Expand Up @@ -108,27 +118,6 @@ pub enum UnchangedSharedKind {
PerEpochConfig,
}

#[derive(Eq, PartialEq, Clone, Debug)]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct EffectsObjectChange {
// input_state and output_state are the core fields that's required by
// the protocol as it tells how an object changes on-chain.
/// State of the object in the store prior to this transaction.
pub input_state: ObjectIn,
/// State of the object in the store after this transaction.
pub output_state: ObjectOut,

/// Whether this object ID is created or deleted in this transaction.
/// This information isn't required by the protocol but is useful for providing more detailed
/// semantics on object changes.
pub id_operation: IdOperation,
}

/// If an object exists (at root-level) in the store prior to this transaction,
/// it should be Exist, otherwise it's NonExist, e.g. wrapped objects should be
/// NonExist.
Expand Down Expand Up @@ -393,52 +382,6 @@ mod serialization {
}
}

#[derive(serde_derive::Serialize, serde_derive::Deserialize)]
struct ReadableChangedObject {
object_id: ObjectId,
#[serde(flatten)]
change: EffectsObjectChange,
}

#[derive(serde_derive::Serialize, serde_derive::Deserialize)]
struct BinaryChangedObject {
object_id: ObjectId,
change: EffectsObjectChange,
}

impl Serialize for ChangedObject {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let Self { object_id, change } = self.clone();
if serializer.is_human_readable() {
let readable = ReadableChangedObject { object_id, change };
readable.serialize(serializer)
} else {
let binary = BinaryChangedObject { object_id, change };
binary.serialize(serializer)
}
}
}

impl<'de> Deserialize<'de> for ChangedObject {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
let ReadableChangedObject { object_id, change } =
Deserialize::deserialize(deserializer)?;
Ok(Self { object_id, change })
} else {
let BinaryChangedObject { object_id, change } =
Deserialize::deserialize(deserializer)?;
Ok(Self { object_id, change })
}
}
}

#[derive(serde_derive::Serialize, serde_derive::Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
enum ReadableUnchangedSharedKind {
Expand Down
1 change: 0 additions & 1 deletion crates/sui-sdk-types/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ pub use digest::TransactionDigest;
pub use digest::TransactionEffectsDigest;
pub use digest::TransactionEventsDigest;
pub use effects::ChangedObject;
pub use effects::EffectsObjectChange;
pub use effects::IdOperation;
pub use effects::ModifiedAtVersion;
pub use effects::ObjectIn;
Expand Down
1 change: 0 additions & 1 deletion crates/sui-sdk-types/src/types/serialization_proptests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ serialization_test!(TransactionDigest);
serialization_test!(TransactionEffectsDigest);
serialization_test!(TransactionEventsDigest);
serialization_test!(ChangedObject);
serialization_test!(EffectsObjectChange);
serialization_test!(IdOperation);
serialization_test!(ModifiedAtVersion);
serialization_test!(ObjectIn);
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-transaction-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,8 @@ mod tests {
match effects {
TransactionEffects::V2(e) => {
for obj in e.changed_objects.clone() {
if obj.change.id_operation == IdOperation::Created {
let change = obj.change.output_state;
if obj.id_operation == IdOperation::Created {
let change = obj.output_state;
match change {
sui_types::types::ObjectOut::PackageWrite { .. } => {
package_id = Some(obj.object_id);
Expand Down

0 comments on commit aad2365

Please sign in to comment.