Skip to content

Commit

Permalink
aligning
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Zajkowski committed Nov 23, 2024
1 parent 843b19e commit ec76ccb
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 144 deletions.
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.clone(),
}
}

/// 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
13 changes: 5 additions & 8 deletions resources/test/sse_data_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@
"$ref": "#/definitions/TransactionV1Hash"
},
"payload": {
"$ref": "#/definitions/TransactionV1Payload"
"$ref": "#/definitions/TransactionV1PayloadJson"
},
"approvals": {
"type": "array",
Expand All @@ -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.",
"TransactionV1PayloadJson": {
"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
29 changes: 4 additions & 25 deletions types/src/cl_value.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use alloc::string::String;
use alloc::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};
#[cfg(feature = "json-schema")]
Expand Down Expand Up @@ -219,20 +217,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 +240,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 +258,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
Loading

0 comments on commit ec76ccb

Please sign in to comment.