Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing Transaction::V1 json serialization #4975

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 31 additions & 21 deletions node/src/types/transaction/meta_transaction/meta_transaction_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ const ARGS_MAP_KEY: u16 = 0;
const TARGET_MAP_KEY: u16 = 1;
const ENTRY_POINT_MAP_KEY: u16 = 2;
const SCHEDULING_MAP_KEY: u16 = 3;
const TRANSFERRED_VALUE_MAP_KEY: u16 = 4;
const SEED_MAP_KEY: u16 = 5;
const EXPECTED_NUMBER_OF_FIELDS: usize = 6;
const EXPECTED_NUMBER_OF_FIELDS: usize = 4;

#[derive(Clone, Debug, Serialize, DataSize)]
pub struct MetaTransactionV1 {
Expand All @@ -37,8 +35,6 @@ pub struct MetaTransactionV1 {
approvals: BTreeSet<Approval>,
serialized_length: usize,
payload_hash: Digest,
transferred_value: u64,
seed: Option<[u8; 32]>,
has_valid_hash: Result<(), InvalidTransactionV1>,
#[serde(skip)]
#[data_size(skip)]
Expand All @@ -64,14 +60,6 @@ impl MetaTransactionV1 {
v1.deserialize_field(SCHEDULING_MAP_KEY).map_err(|error| {
InvalidTransaction::V1(InvalidTransactionV1::CouldNotDeserializeField { error })
})?;
let transferred_value =
v1.deserialize_field(TRANSFERRED_VALUE_MAP_KEY)
.map_err(|error| {
InvalidTransaction::V1(InvalidTransactionV1::CouldNotDeserializeField { error })
})?;
let seed = v1.deserialize_field(SEED_MAP_KEY).map_err(|error| {
InvalidTransaction::V1(InvalidTransactionV1::CouldNotDeserializeField { error })
})?;

if v1.number_of_fields() != EXPECTED_NUMBER_OF_FIELDS {
return Err(InvalidTransaction::V1(
Expand Down Expand Up @@ -108,8 +96,6 @@ impl MetaTransactionV1 {
serialized_length,
payload_hash,
approvals,
transferred_value,
seed,
has_valid_hash,
))
}
Expand Down Expand Up @@ -149,8 +135,6 @@ impl MetaTransactionV1 {
serialized_length: usize,
payload_hash: Digest,
approvals: BTreeSet<Approval>,
transferred_value: u64,
seed: Option<[u8; 32]>,
has_valid_hash: Result<(), InvalidTransactionV1>,
) -> Self {
Self {
Expand All @@ -169,8 +153,6 @@ impl MetaTransactionV1 {
serialized_length,
payload_hash,
has_valid_hash,
transferred_value,
seed,
is_verified: OnceCell::new(),
}
}
Expand Down Expand Up @@ -654,12 +636,40 @@ impl MetaTransactionV1 {

/// Returns the seed of the transaction.
pub(crate) fn seed(&self) -> Option<[u8; 32]> {
self.seed
match &self.target {
TransactionTarget::Native => None,
TransactionTarget::Stored {
id: _,
runtime: _,
transferred_value: _,
} => None,
TransactionTarget::Session {
is_install_upgrade: _,
runtime: _,
module_bytes: _,
transferred_value: _,
seed,
} => *seed,
}
}

/// Returns the transferred value of the transaction.
pub fn transferred_value(&self) -> u64 {
self.transferred_value
match &self.target {
TransactionTarget::Native => 0,
TransactionTarget::Stored {
id: _,
runtime: _,
transferred_value,
} => *transferred_value,
TransactionTarget::Session {
is_install_upgrade: _,
runtime: _,
module_bytes: _,
transferred_value,
seed: _,
} => *transferred_value,
}
}
}

Expand Down
9 changes: 3 additions & 6 deletions resources/test/sse_data_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1577,11 +1577,10 @@
},
"uniqueItems": true
}
},
"additionalProperties": false
}
},
"TransactionV1Payload": {
"description": "A unit of work sent by a client to the network, which when executed can cause global state to be altered.",
"description": "Internal payload of the transaction. The actual data over which the signing is done.",
"type": "object",
"required": [
"chain_name",
Expand Down Expand Up @@ -1609,9 +1608,7 @@
},
"fields": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/Bytes"
}
"additionalProperties": true
}
},
"additionalProperties": false
Expand Down
2 changes: 1 addition & 1 deletion types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ proptest = { version = "1.0.0", optional = true }
proptest-derive = { version = "0.3.0", optional = true }
rand = { version = "0.8.3", default-features = false, features = ["small_rng"] }
rand_pcg = { version = "0.3.0", optional = true }
schemars = { version = "0.8.16", features = ["preserve_order"], optional = true }
schemars = { version = "0.8.21", features = ["preserve_order"], optional = true }
serde-map-to-array = "1.1.0"
serde = { version = "1", default-features = false, features = ["alloc", "derive"] }
serde_bytes = { version = "0.11.5", default-features = false, features = ["alloc"] }
Expand Down
33 changes: 5 additions & 28 deletions types/src/cl_value.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
use alloc::vec::Vec;
use alloc::{string::String, vec::Vec};
use core::fmt::{self, Display, Formatter};

#[cfg(feature = "json-schema")]
use crate::checksummed_hex;
use crate::{
bytesrepr::{self, Bytes, FromBytes, ToBytes, U32_SERIALIZED_LENGTH},
CLType, CLTyped,
checksummed_hex, CLType, CLTyped,
};
#[cfg(feature = "datasize")]
use datasize::DataSize;
#[cfg(feature = "json-schema")]
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
#[cfg(feature = "json-schema")]
use serde::de::Error as SerdeError;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{de::Error as SerdeError, Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "json-schema")]
use serde_json::Value;

Expand Down Expand Up @@ -219,20 +215,20 @@ impl JsonSchema for CLValue {
#[serde(deny_unknown_fields)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
#[cfg_attr(feature = "json-schema", schemars(rename = "CLValue"))]
#[cfg(feature = "json-schema")]
struct CLValueJson {
cl_type: CLType,
bytes: String,
#[cfg(feature = "json-schema")]
parsed: Option<Value>,
}

#[cfg(feature = "json-schema")]
impl Serialize for CLValue {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
CLValueJson {
cl_type: self.cl_type.clone(),
bytes: base16::encode_lower(&self.bytes),
#[cfg(feature = "json-schema")]
parsed: jsonrepr::cl_value_to_json(self),
}
.serialize(serializer)
Expand All @@ -242,14 +238,6 @@ impl Serialize for CLValue {
}
}

#[cfg(not(feature = "json-schema"))]
impl Serialize for CLValue {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
(&self.cl_type, &self.bytes).serialize(serializer)
}
}

#[cfg(feature = "json-schema")]
impl<'de> Deserialize<'de> for CLValue {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let (cl_type, bytes) = if deserializer.is_human_readable() {
Expand All @@ -268,17 +256,6 @@ impl<'de> Deserialize<'de> for CLValue {
}
}

#[cfg(not(feature = "json-schema"))]
impl<'de> Deserialize<'de> for CLValue {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let (cl_type, bytes) = <(CLType, Vec<u8>)>::deserialize(deserializer)?;
Ok(CLValue {
cl_type,
bytes: bytes.into(),
})
}
}

#[cfg(test)]
mod tests {
use alloc::string::ToString;
Expand Down
12 changes: 10 additions & 2 deletions types/src/gens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,14 @@ pub fn transaction_scheduling_arb() -> impl Strategy<Value = TransactionScheduli
]
}

pub fn json_compliant_transaction_scheduling_arb() -> impl Strategy<Value = TransactionScheduling> {
prop_oneof![
Just(TransactionScheduling::Standard),
era_id_arb().prop_map(TransactionScheduling::FutureEra),
timestamp_arb().prop_map(TransactionScheduling::FutureTimestamp),
]
}

pub fn transaction_invocation_target_arb() -> impl Strategy<Value = TransactionInvocationTarget> {
prop_oneof![
addressable_entity_hash_arb().prop_map(TransactionInvocationTarget::new_invocable_entity),
Expand Down Expand Up @@ -1171,7 +1179,7 @@ pub fn initiator_addr_arb() -> impl Strategy<Value = InitiatorAddr> {

pub fn timestamp_arb() -> impl Strategy<Value = Timestamp> {
//The weird u64 value is the max milliseconds that are bofeore year 10000. 5 digit years are
// not rfc3339 compliant and will cause an error
// not rfc3339 compliant and will cause an error when trying to serialize to json.
prop_oneof![Just(0_u64), Just(1_u64), Just(253_402_300_799_999_u64)].prop_map(Timestamp::from)
}

Expand All @@ -1183,7 +1191,7 @@ pub fn legal_v1_transaction_arb() -> impl Strategy<Value = TransactionV1> {
pricing_mode_arb(),
secret_key_arb_no_system(),
transaction_args_arb(),
transaction_scheduling_arb(),
json_compliant_transaction_scheduling_arb(),
legal_target_entry_point_calls_arb(),
)
.prop_map(
Expand Down
Loading
Loading