Skip to content

Commit

Permalink
Merge pull request #1854 from radixdlt/feature/sbor-flatten-enum-variant
Browse files Browse the repository at this point in the history
feature: Add `#[sbor(flatten)]` for enum variants
  • Loading branch information
dhedey authored Aug 7, 2024
2 parents be92477 + ee74439 commit 3dda808
Show file tree
Hide file tree
Showing 17 changed files with 867 additions and 539 deletions.
91 changes: 46 additions & 45 deletions radix-clis/src/rtmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,54 +61,55 @@ pub fn run() -> Result<(), String> {
None => NetworkDefinition::simulator(),
};

let (manifest_instructions, blobs) =
match manifest_decode::<TransactionManifestV1>(&content).map_err(Error::DecodeError) {
Ok(manifest) => {
let blobs: Vec<Vec<u8>> = manifest
.blobs
.values()
.into_iter()
.map(|item| item.to_owned())
.collect();
(manifest.instructions, blobs)
}
Err(e) => {
// try to decode versioned transaction
match manifest_decode::<VersionedTransactionPayload>(&content) {
Ok(manifest) => {
let (manifest_instructions, blobs) = match manifest {
VersionedTransactionPayload::IntentV1 {
instructions,
blobs,
..
} => (instructions.0, blobs.blobs),
VersionedTransactionPayload::SignedIntentV1 { intent, .. } => {
(intent.instructions.0, intent.blobs.blobs)
}
VersionedTransactionPayload::NotarizedTransactionV1 {
signed_intent,
..
} => (
signed_intent.intent.instructions.0,
signed_intent.intent.blobs.blobs,
),
VersionedTransactionPayload::SystemTransactionV1 {
instructions,
blobs,
..
} => (instructions.0, blobs.blobs),
};
let (manifest_instructions, blobs) = match manifest_decode::<TransactionManifestV1>(&content)
.map_err(Error::DecodeError)
{
Ok(manifest) => {
let blobs: Vec<Vec<u8>> = manifest
.blobs
.values()
.into_iter()
.map(|item| item.to_owned())
.collect();
(manifest.instructions, blobs)
}
Err(e) => {
// try to decode versioned transaction
match manifest_decode::<VersionedTransactionPayload>(&content) {
Ok(manifest) => {
let (manifest_instructions, blobs) = match manifest {
VersionedTransactionPayload::IntentV1(IntentV1 {
instructions,
blobs,
..
}) => (instructions.0, blobs.blobs),
VersionedTransactionPayload::SignedIntentV1(SignedIntentV1 {
intent,
..
}) => (intent.instructions.0, intent.blobs.blobs),
VersionedTransactionPayload::NotarizedTransactionV1(
NotarizedTransactionV1 { signed_intent, .. },
) => (
signed_intent.intent.instructions.0,
signed_intent.intent.blobs.blobs,
),
VersionedTransactionPayload::SystemTransactionV1(SystemTransactionV1 {
instructions,
blobs,
..
}) => (instructions.0, blobs.blobs),
};

let blobs: Vec<Vec<u8>> = blobs.into_iter().map(|item| item.0).collect();
(manifest_instructions, blobs)
}
Err(_) => {
// return original error
return Err(e.into());
}
let blobs: Vec<Vec<u8>> = blobs.into_iter().map(|item| item.0).collect();
(manifest_instructions, blobs)
}
Err(_) => {
// return original error
return Err(e.into());
}
}
};
}
};

validate_call_arguments_to_native_components(&manifest_instructions)
.map_err(Error::InstructionSchemaValidationError)?;
Expand Down
6 changes: 3 additions & 3 deletions radix-sbor-derive/src/manifest_categorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ mod tests {

fn get_length(&self) -> usize {
match self {
Self::A { .. } => 1,
Self::B(_) => 1,
Self::C => 0,
Self::A { .. } => 1usize,
Self::B(_) => 1usize,
Self::C => 0usize,
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions radix-sbor-derive/src/manifest_decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod tests {
) -> Result<Self, sbor::DecodeError> {
use sbor::{self, Decode};
decoder.check_preloaded_value_kind(value_kind, sbor::ValueKind::Tuple)?;
decoder.read_and_check_size(0)?;
decoder.read_and_check_size(0usize)?;
Ok(Self {})
}
}
Expand Down Expand Up @@ -72,17 +72,17 @@ mod tests {
#[deny(unreachable_patterns)]
match discriminator {
0u8 => {
decoder.read_and_check_size(1)?;
decoder.read_and_check_size(1usize)?;
Ok(Self::A {
named: decoder.decode::<T>()?,
})
},
1u8 => {
decoder.read_and_check_size(1)?;
decoder.read_and_check_size(1usize)?;
Ok(Self::B(decoder.decode::<String>()?,))
},
2u8 => {
decoder.read_and_check_size(0)?;
decoder.read_and_check_size(0usize)?;
Ok(Self::C)
},
_ => Err(sbor::DecodeError::UnknownDiscriminator(discriminator))
Expand Down
8 changes: 4 additions & 4 deletions radix-sbor-derive/src/manifest_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod tests {
#[inline]
fn encode_body(&self, encoder: &mut E) -> Result<(), sbor::EncodeError> {
use sbor::{self, Encode};
encoder.write_size(0)?;
encoder.write_size(0usize)?;
Ok(())
}
}
Expand Down Expand Up @@ -71,17 +71,17 @@ mod tests {
match self {
Self::A { named, .. } => {
encoder.write_discriminator(0u8)?;
encoder.write_size(1)?;
encoder.write_size(1usize)?;
encoder.encode(named)?;
}
Self::B(a0) => {
encoder.write_discriminator(1u8)?;
encoder.write_size(1)?;
encoder.write_size(1usize)?;
encoder.encode(a0)?;
}
Self::C => {
encoder.write_discriminator(2u8)?;
encoder.write_size(0)?;
encoder.write_size(0usize)?;
}
}
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions radix-sbor-derive/src/scrypto_categorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ mod tests {

fn get_length(&self) -> usize {
match self {
Self::A { .. } => 1,
Self::B(_) => 1,
Self::C => 0,
Self::A { .. } => 1usize,
Self::B(_) => 1usize,
Self::C => 0usize,
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions radix-sbor-derive/src/scrypto_decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod tests {
) -> Result<Self, sbor::DecodeError> {
use sbor::{self, Decode};
decoder.check_preloaded_value_kind(value_kind, sbor::ValueKind::Tuple)?;
decoder.read_and_check_size(0)?;
decoder.read_and_check_size(0usize)?;
Ok(Self {})
}
}
Expand Down Expand Up @@ -72,17 +72,17 @@ mod tests {
#[deny(unreachable_patterns)]
match discriminator {
0u8 => {
decoder.read_and_check_size(1)?;
decoder.read_and_check_size(1usize)?;
Ok(Self::A {
named: decoder.decode::<T>()?,
})
},
1u8 => {
decoder.read_and_check_size(1)?;
decoder.read_and_check_size(1usize)?;
Ok(Self::B(decoder.decode::<String>()?,))
},
2u8 => {
decoder.read_and_check_size(0)?;
decoder.read_and_check_size(0usize)?;
Ok(Self::C)
},
_ => Err(sbor::DecodeError::UnknownDiscriminator(discriminator))
Expand Down
6 changes: 3 additions & 3 deletions radix-sbor-derive/src/scrypto_describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ mod tests {
sbor::TypeData::enum_variants(
"MyEnum",
sbor :: rust :: prelude :: indexmap ! [
0u8 => sbor :: TypeData :: struct_with_named_fields ("A", sbor :: rust :: vec ! [("named", < T as sbor :: Describe < radix_common::data::scrypto::ScryptoCustomTypeKind >> :: TYPE_ID) ,] ,) ,
1u8 => sbor :: TypeData :: struct_with_unnamed_fields ("B", sbor :: rust :: vec ! [< String as sbor :: Describe < radix_common::data::scrypto::ScryptoCustomTypeKind >> :: TYPE_ID ,] ,) ,
2u8 => sbor :: TypeData :: struct_with_unit_fields ("C") ,
0u8 => { sbor :: TypeData :: struct_with_named_fields ("A", sbor :: rust :: vec ! [("named", < T as sbor :: Describe < radix_common::data::scrypto::ScryptoCustomTypeKind >> :: TYPE_ID) ,] ,) },
1u8 => { sbor :: TypeData :: struct_with_unnamed_fields ("B", sbor :: rust :: vec ! [< String as sbor :: Describe < radix_common::data::scrypto::ScryptoCustomTypeKind >> :: TYPE_ID ,] ,) },
2u8 => { sbor :: TypeData :: struct_with_unit_fields ("C") },
],
)
}
Expand Down
8 changes: 4 additions & 4 deletions radix-sbor-derive/src/scrypto_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod tests {
#[inline]
fn encode_body(&self, encoder: &mut E) -> Result<(), sbor::EncodeError> {
use sbor::{self, Encode};
encoder.write_size(0)?;
encoder.write_size(0usize)?;
Ok(())
}
}
Expand Down Expand Up @@ -71,17 +71,17 @@ mod tests {
match self {
Self::A { named, .. } => {
encoder.write_discriminator(0u8)?;
encoder.write_size(1)?;
encoder.write_size(1usize)?;
encoder.encode(named)?;
}
Self::B(a0) => {
encoder.write_discriminator(1u8)?;
encoder.write_size(1)?;
encoder.write_size(1usize)?;
encoder.encode(a0)?;
}
Self::C => {
encoder.write_discriminator(2u8)?;
encoder.write_size(0)?;
encoder.write_size(0usize)?;
}
}
Ok(())
Expand Down
59 changes: 13 additions & 46 deletions radix-transactions/src/model/versioned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,19 @@ const V1_PREVIEW_TRANSACTION: u8 = 6;
const V1_LEDGER_TRANSACTION: u8 = 7;
const V1_FLASH_TRANSACTION: u8 = 8;

// TODO - change this to use #[flatten] when REP-84 is out
/// An enum of a variety of different transaction payload types
/// An enum of a variety of different transaction payload types.
/// This might see use in (eg) the Node's transaction parse API.
/// These represent the different transaction types.
#[derive(Clone, Debug, Eq, PartialEq, ManifestSbor)]
pub enum VersionedTransactionPayload {
#[sbor(discriminator(V1_INTENT))]
IntentV1 {
header: TransactionHeaderV1,
instructions: InstructionsV1,
blobs: BlobsV1,
message: MessageV1,
},
#[sbor(discriminator(V1_SIGNED_INTENT))]
SignedIntentV1 {
intent: IntentV1,
intent_signatures: IntentSignaturesV1,
},
#[sbor(discriminator(V1_NOTARIZED_TRANSACTION))]
NotarizedTransactionV1 {
signed_intent: SignedIntentV1,
notary_signature: NotarySignatureV1,
},
#[sbor(discriminator(V1_SYSTEM_TRANSACTION))]
SystemTransactionV1 {
instructions: InstructionsV1,
blobs: BlobsV1,
pre_allocated_addresses: Vec<PreAllocatedAddress>,
hash_for_execution: Hash,
},
#[sbor(flatten, discriminator(V1_INTENT))]
IntentV1(IntentV1),
#[sbor(flatten, discriminator(V1_SIGNED_INTENT))]
SignedIntentV1(SignedIntentV1),
#[sbor(flatten, discriminator(V1_NOTARIZED_TRANSACTION))]
NotarizedTransactionV1(NotarizedTransactionV1),
#[sbor(flatten, discriminator(V1_SYSTEM_TRANSACTION))]
SystemTransactionV1(SystemTransactionV1),
}

#[cfg(test)]
Expand Down Expand Up @@ -153,12 +136,7 @@ mod tests {
manifest_decode::<VersionedTransactionPayload>(&intent_payload_bytes).unwrap();
assert_eq!(
intent_as_versioned,
VersionedTransactionPayload::IntentV1 {
header: header_v1,
instructions: instructions_v1,
blobs: blobs_v1,
message: message_v1,
}
VersionedTransactionPayload::IntentV1(intent_v1.clone())
);

let prepared_intent =
Expand Down Expand Up @@ -212,10 +190,7 @@ mod tests {
manifest_decode::<VersionedTransactionPayload>(&signed_intent_payload_bytes).unwrap();
assert_eq!(
signed_intent_as_versioned,
VersionedTransactionPayload::SignedIntentV1 {
intent: intent_v1,
intent_signatures: intent_signatures_v1,
}
VersionedTransactionPayload::SignedIntentV1(signed_intent_v1.clone())
);

let prepared_signed_intent =
Expand Down Expand Up @@ -272,10 +247,7 @@ mod tests {
.unwrap();
assert_eq!(
notarized_transaction_as_versioned,
VersionedTransactionPayload::NotarizedTransactionV1 {
signed_intent: signed_intent_v1,
notary_signature: notary_signature_v1,
}
VersionedTransactionPayload::NotarizedTransactionV1(notarized_transaction_v1)
);

let prepared_notarized_transaction = PreparedNotarizedTransactionV1::prepare_from_payload(
Expand Down Expand Up @@ -364,12 +336,7 @@ mod tests {
.unwrap();
assert_eq!(
system_transaction_as_versioned,
VersionedTransactionPayload::SystemTransactionV1 {
instructions: instructions_v1,
blobs: blobs_v1,
pre_allocated_addresses: pre_allocated_addresses_v1,
hash_for_execution
}
VersionedTransactionPayload::SystemTransactionV1(system_transaction_v1)
);

let prepared_system_transaction =
Expand Down
Loading

0 comments on commit 3dda808

Please sign in to comment.