From 1f37062bdebcab1be7a5fd9593b5e27b26ae2520 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Thu, 13 Jul 2023 09:18:33 +0200 Subject: [PATCH 1/2] Upgrade ssz_rs crate. --- parachain/Cargo.lock | 27 +++++++++++++++++-- .../pallets/ethereum-beacon-client/Cargo.toml | 6 ++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/parachain/Cargo.lock b/parachain/Cargo.lock index 36e71f795e..7fbb17926f 100644 --- a/parachain/Cargo.lock +++ b/parachain/Cargo.lock @@ -2676,8 +2676,8 @@ dependencies = [ "sp-keyring", "sp-runtime", "sp-std", - "ssz-rs", - "ssz-rs-derive", + "ssz_rs", + "ssz_rs_derive", "static_assertions", ] @@ -3279,6 +3279,29 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "ssz_rs" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "057291e5631f280978fa9c8009390663ca4613359fc1318e36a8c24c392f6d1f" +dependencies = [ + "bitvec", + "num-bigint", + "sha2 0.9.9", + "ssz_rs_derive", +] + +[[package]] +name = "ssz_rs_derive" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f07d54c4d01a1713eb363b55ba51595da15f6f1211435b71466460da022aa140" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "stable_deref_trait" version = "1.2.0" diff --git a/parachain/pallets/ethereum-beacon-client/Cargo.toml b/parachain/pallets/ethereum-beacon-client/Cargo.toml index 5f8d7f2c07..8c85b489a4 100644 --- a/parachain/pallets/ethereum-beacon-client/Cargo.toml +++ b/parachain/pallets/ethereum-beacon-client/Cargo.toml @@ -14,8 +14,8 @@ targets = ["x86_64-unknown-linux-gnu"] serde = { version = "1.0.164", optional = true } codec = { version = "3.1.5", package = "parity-scale-codec", default-features = false, features = [ "derive" ] } scale-info = { version = "2.7.0", default-features = false, features = [ "derive" ] } -ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs", default-features = false, rev="d18af912abacbf84219be37ab3b42a9abcf10d2a" } -ssz-rs-derive = { git = "https://github.com/ralexstokes/ssz-rs", default-features = false, rev="d18af912abacbf84219be37ab3b42a9abcf10d2a" } +ssz_rs = { version="0.9.0", default-features = false } +ssz_rs_derive = { version="0.9.0", default-features = false } byte-slice-cast = { version = "1.2.1", default-features = false } rlp = { version = "0.5", default-features = false } hex-literal = { version = "0.4.1", optional = true } @@ -56,7 +56,7 @@ std = [ "snowbridge-core/std", "snowbridge-ethereum/std", "primitives/std", - "ssz-rs/std", + "ssz_rs/std", "byte-slice-cast/std", ] runtime-benchmarks = [ From da767bdce2bfe2d6c917e6b2ad946dda2b76915b Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Fri, 14 Jul 2023 12:15:48 +0200 Subject: [PATCH 2/2] Upgrade ssz_rs crate. --- parachain/Cargo.lock | 26 +--------- parachain/pallets/outbound-queue/src/lib.rs | 6 +++ parachain/primitives/beacon/Cargo.toml | 6 +-- parachain/primitives/beacon/src/ssz.rs | 57 +++++++++++++-------- parachain/primitives/beacon/src/types.rs | 16 +++--- 5 files changed, 56 insertions(+), 55 deletions(-) diff --git a/parachain/Cargo.lock b/parachain/Cargo.lock index 7fbb17926f..d3960ce11a 100644 --- a/parachain/Cargo.lock +++ b/parachain/Cargo.lock @@ -2585,8 +2585,8 @@ dependencies = [ "sp-io", "sp-runtime", "sp-std", - "ssz-rs", - "ssz-rs-derive", + "ssz_rs", + "ssz_rs_derive", "static_assertions", ] @@ -3257,28 +3257,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "ssz-rs" -version = "0.8.0" -source = "git+https://github.com/ralexstokes/ssz-rs?rev=d18af912abacbf84219be37ab3b42a9abcf10d2a#d18af912abacbf84219be37ab3b42a9abcf10d2a" -dependencies = [ - "bitvec", - "num-bigint", - "sha2 0.9.9", - "ssz-rs-derive", - "thiserror", -] - -[[package]] -name = "ssz-rs-derive" -version = "0.8.0" -source = "git+https://github.com/ralexstokes/ssz-rs?rev=d18af912abacbf84219be37ab3b42a9abcf10d2a#d18af912abacbf84219be37ab3b42a9abcf10d2a" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "ssz_rs" version = "0.9.0" diff --git a/parachain/pallets/outbound-queue/src/lib.rs b/parachain/pallets/outbound-queue/src/lib.rs index 68ca0edab8..a92340768b 100644 --- a/parachain/pallets/outbound-queue/src/lib.rs +++ b/parachain/pallets/outbound-queue/src/lib.rs @@ -79,6 +79,12 @@ impl Into for Message { } } +impl From for AggregateMessageOrigin { + fn from(value: u32) -> Self { + AggregateMessageOrigin::Parachain(value.into()) + } +} + /// The maximal length of an enqueued message, as determined by the MessageQueue pallet pub type MaxEnqueuedMessageSizeOf = <::MessageQueue as EnqueueMessage>::MaxMessageLen; diff --git a/parachain/primitives/beacon/Cargo.toml b/parachain/primitives/beacon/Cargo.toml index caace1c836..829641856a 100644 --- a/parachain/primitives/beacon/Cargo.toml +++ b/parachain/primitives/beacon/Cargo.toml @@ -18,8 +18,8 @@ sp-std = { git = "https://github.com/paritytech/substrate.git", branch = "master sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "master", default-features = false } sp-io = { git = "https://github.com/paritytech/substrate.git", branch = "master", default-features = false } -ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs", default-features = false, rev = "d18af912abacbf84219be37ab3b42a9abcf10d2a" } -ssz-rs-derive = { git = "https://github.com/ralexstokes/ssz-rs", default-features = false, rev = "d18af912abacbf84219be37ab3b42a9abcf10d2a" } +ssz_rs = { version="0.9.0", default-features = false } +ssz_rs_derive = { version="0.9.0", default-features = false } byte-slice-cast = { version = "1.2.1", default-features = false } snowbridge-ethereum = { path = "../../primitives/ethereum", default-features = false } @@ -43,7 +43,7 @@ std = [ "sp-io/std", "rlp/std", "snowbridge-ethereum/std", - "ssz-rs/std", + "ssz_rs/std", "byte-slice-cast/std", "milagro_bls/std", ] diff --git a/parachain/primitives/beacon/src/ssz.rs b/parachain/primitives/beacon/src/ssz.rs index 164cc13602..4f8b19ca88 100644 --- a/parachain/primitives/beacon/src/ssz.rs +++ b/parachain/primitives/beacon/src/ssz.rs @@ -11,7 +11,7 @@ use sp_core::H256; use sp_std::{vec, vec::Vec}; use ssz_rs::{ prelude::{List, Vector}, - Bitvector, Deserialize, MerkleizationError, SimpleSerialize, Sized, U256, + Bitvector, Deserialize, DeserializeError, SimpleSerialize, SimpleSerializeError, Sized, U256, }; use ssz_rs_derive::SimpleSerialize as SimpleSerializeDerive; @@ -49,16 +49,23 @@ impl From> let mut pubkeys_vec = Vec::new(); for pubkey in sync_committee.pubkeys.iter() { - let conv_pubkey = Vector::::from_iter(pubkey.0); + // The only thing that can go wrong in the conversion from vec to Vector (ssz type) is + // that the Vector size is 0, or that the given data to create the Vector from does not + // match the expected size N. Because these sizes are statically checked (i.e. + // PublicKey's size is 48, and const PUBKEY_SIZE is 48, it is impossible for "try_from" + // to return an error condition. + let conv_pubkey = Vector::::try_from(pubkey.0.to_vec()) + .expect("checked statically; qed"); pubkeys_vec.push(conv_pubkey); } - let pubkeys = - Vector::, { COMMITTEE_SIZE }>::from_iter(pubkeys_vec.clone()); + let pubkeys = Vector::, { COMMITTEE_SIZE }>::try_from(pubkeys_vec) + .expect("checked statically; qed"); let aggregate_pubkey = - Vector::::from_iter(sync_committee.aggregate_pubkey.0); + Vector::::try_from(sync_committee.aggregate_pubkey.0.to_vec()) + .expect("checked statically; qed"); SSZSyncCommittee { pubkeys, aggregate_pubkey } } @@ -79,9 +86,10 @@ impl &sync_aggregate.sync_committee_bits, ) .expect("checked statically; qed"), - sync_committee_signature: Vector::::from_iter( - sync_aggregate.sync_committee_signature.0, - ), + sync_committee_signature: Vector::::try_from( + sync_aggregate.sync_committee_signature.0.to_vec(), + ) + .expect("checked statically; qed"), } } } @@ -135,22 +143,31 @@ pub struct SSZExecutionPayloadHeader { pub withdrawals_root: [u8; 32], } -impl From for SSZExecutionPayloadHeader { - fn from(payload: ExecutionPayloadHeader) -> Self { - SSZExecutionPayloadHeader { +impl TryFrom for SSZExecutionPayloadHeader { + type Error = SimpleSerializeError; + + fn try_from(payload: ExecutionPayloadHeader) -> Result { + Ok(SSZExecutionPayloadHeader { parent_hash: payload.parent_hash.to_fixed_bytes(), - fee_recipient: Vector::::from_iter( - payload.fee_recipient.to_fixed_bytes(), - ), + fee_recipient: Vector::::try_from( + payload.fee_recipient.to_fixed_bytes().to_vec(), + ) + .expect("checked statically; qed"), state_root: payload.state_root.to_fixed_bytes(), receipts_root: payload.receipts_root.to_fixed_bytes(), - logs_bloom: Vector::::from_iter(payload.logs_bloom), + // Logs bloom bytes size is not constrained, so here we do need to check the try_from + // error + logs_bloom: Vector::::try_from(payload.logs_bloom) + .map_err(|(_, err)| err)?, prev_randao: payload.prev_randao.to_fixed_bytes(), block_number: payload.block_number, gas_limit: payload.gas_limit, gas_used: payload.gas_used, timestamp: payload.timestamp, - extra_data: List::::from_iter(payload.extra_data), + // Extra data bytes size is not constrained, so here we do need to check the try_from + // error + extra_data: List::::try_from(payload.extra_data) + .map_err(|(_, err)| err)?, base_fee_per_gas: U256::from_bytes_le( payload .base_fee_per_gas @@ -161,17 +178,17 @@ impl From for SSZExecutionPayloadHeader { block_hash: payload.block_hash.to_fixed_bytes(), transactions_root: payload.transactions_root.to_fixed_bytes(), withdrawals_root: payload.withdrawals_root.to_fixed_bytes(), - } + }) } } -pub fn hash_tree_root(mut object: T) -> Result { +pub fn hash_tree_root(mut object: T) -> Result { match object.hash_tree_root() { Ok(node) => { let fixed_bytes: [u8; 32] = - node.as_bytes().try_into().expect("Node is a newtype over [u8; 32]; qed"); + node.as_ref().try_into().expect("Node is a newtype over [u8; 32]; qed"); Ok(fixed_bytes.into()) }, - Err(err) => Err(err), + Err(err) => Err(err.into()), } } diff --git a/parachain/primitives/beacon/src/types.rs b/parachain/primitives/beacon/src/types.rs index 798cf71746..0a7a420eb5 100644 --- a/parachain/primitives/beacon/src/types.rs +++ b/parachain/primitives/beacon/src/types.rs @@ -19,7 +19,7 @@ use crate::ssz::{ hash_tree_root, SSZBeaconBlockHeader, SSZExecutionPayloadHeader, SSZForkData, SSZSigningData, SSZSyncAggregate, SSZSyncCommittee, }; -use ssz_rs::MerkleizationError; +use ssz_rs::SimpleSerializeError; pub use crate::bits::decompress_sync_committee_bits; @@ -131,7 +131,7 @@ pub struct ForkData { } impl ForkData { - pub fn hash_tree_root(&self) -> Result { + pub fn hash_tree_root(&self) -> Result { hash_tree_root::(self.clone().into()) } } @@ -143,7 +143,7 @@ pub struct SigningData { } impl SigningData { - pub fn hash_tree_root(&self) -> Result { + pub fn hash_tree_root(&self) -> Result { hash_tree_root::(self.clone().into()) } } @@ -174,7 +174,7 @@ impl Default for SyncCommittee { } impl SyncCommittee { - pub fn hash_tree_root(&self) -> Result { + pub fn hash_tree_root(&self) -> Result { hash_tree_root::>(self.clone().into()) } } @@ -235,7 +235,7 @@ pub struct BeaconHeader { } impl BeaconHeader { - pub fn hash_tree_root(&self) -> Result { + pub fn hash_tree_root(&self) -> Result { hash_tree_root::((*self).into()) } } @@ -271,7 +271,7 @@ impl Default impl SyncAggregate { - pub fn hash_tree_root(&self) -> Result { + pub fn hash_tree_root(&self) -> Result { hash_tree_root::>(self.clone().into()) } } @@ -335,8 +335,8 @@ pub struct ExecutionPayloadHeader { } impl ExecutionPayloadHeader { - pub fn hash_tree_root(&self) -> Result { - hash_tree_root::(self.clone().into()) + pub fn hash_tree_root(&self) -> Result { + hash_tree_root::(self.clone().try_into()?) } }