Skip to content

Commit

Permalink
Upgrade ssz_rs crate. (#880)
Browse files Browse the repository at this point in the history
* Upgrade ssz_rs crate.

* Upgrade ssz_rs crate.

---------

Co-authored-by: claravanstaden <Cats 4 life!>
  • Loading branch information
claravanstaden authored Jul 17, 2023
1 parent b726f59 commit a9d3df4
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 46 deletions.
25 changes: 13 additions & 12 deletions parachain/Cargo.lock

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

6 changes: 3 additions & 3 deletions parachain/pallets/ethereum-beacon-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 = [
Expand Down
6 changes: 6 additions & 0 deletions parachain/pallets/outbound-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ impl Into<Token> for Message {
}
}

impl From<u32> 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<T> =
<<T as Config>::MessageQueue as EnqueueMessage<AggregateMessageOrigin>>::MaxMessageLen;
Expand Down
6 changes: 3 additions & 3 deletions parachain/primitives/beacon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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",
]
57 changes: 37 additions & 20 deletions parachain/primitives/beacon/src/ssz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -49,16 +49,23 @@ impl<const COMMITTEE_SIZE: usize> From<SyncCommittee<COMMITTEE_SIZE>>
let mut pubkeys_vec = Vec::new();

for pubkey in sync_committee.pubkeys.iter() {
let conv_pubkey = Vector::<u8, PUBKEY_SIZE>::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::<u8, PUBKEY_SIZE>::try_from(pubkey.0.to_vec())
.expect("checked statically; qed");

pubkeys_vec.push(conv_pubkey);
}

let pubkeys =
Vector::<Vector<u8, PUBKEY_SIZE>, { COMMITTEE_SIZE }>::from_iter(pubkeys_vec.clone());
let pubkeys = Vector::<Vector<u8, PUBKEY_SIZE>, { COMMITTEE_SIZE }>::try_from(pubkeys_vec)
.expect("checked statically; qed");

let aggregate_pubkey =
Vector::<u8, PUBKEY_SIZE>::from_iter(sync_committee.aggregate_pubkey.0);
Vector::<u8, PUBKEY_SIZE>::try_from(sync_committee.aggregate_pubkey.0.to_vec())
.expect("checked statically; qed");

SSZSyncCommittee { pubkeys, aggregate_pubkey }
}
Expand All @@ -79,9 +86,10 @@ impl<const COMMITTEE_SIZE: usize, const COMMITTEE_BITS_SIZE: usize>
&sync_aggregate.sync_committee_bits,
)
.expect("checked statically; qed"),
sync_committee_signature: Vector::<u8, SIGNATURE_SIZE>::from_iter(
sync_aggregate.sync_committee_signature.0,
),
sync_committee_signature: Vector::<u8, SIGNATURE_SIZE>::try_from(
sync_aggregate.sync_committee_signature.0.to_vec(),
)
.expect("checked statically; qed"),
}
}
}
Expand Down Expand Up @@ -135,22 +143,31 @@ pub struct SSZExecutionPayloadHeader {
pub withdrawals_root: [u8; 32],
}

impl From<ExecutionPayloadHeader> for SSZExecutionPayloadHeader {
fn from(payload: ExecutionPayloadHeader) -> Self {
SSZExecutionPayloadHeader {
impl TryFrom<ExecutionPayloadHeader> for SSZExecutionPayloadHeader {
type Error = SimpleSerializeError;

fn try_from(payload: ExecutionPayloadHeader) -> Result<Self, Self::Error> {
Ok(SSZExecutionPayloadHeader {
parent_hash: payload.parent_hash.to_fixed_bytes(),
fee_recipient: Vector::<u8, FEE_RECIPIENT_SIZE>::from_iter(
payload.fee_recipient.to_fixed_bytes(),
),
fee_recipient: Vector::<u8, FEE_RECIPIENT_SIZE>::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::<u8, 256>::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::<u8, LOGS_BLOOM_SIZE>::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::<u8, EXTRA_DATA_SIZE>::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::<u8, EXTRA_DATA_SIZE>::try_from(payload.extra_data)
.map_err(|(_, err)| err)?,
base_fee_per_gas: U256::from_bytes_le(
payload
.base_fee_per_gas
Expand All @@ -161,17 +178,17 @@ impl From<ExecutionPayloadHeader> 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<T: SimpleSerialize>(mut object: T) -> Result<H256, MerkleizationError> {
pub fn hash_tree_root<T: SimpleSerialize>(mut object: T) -> Result<H256, SimpleSerializeError> {
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()),
}
}
16 changes: 8 additions & 8 deletions parachain/primitives/beacon/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -131,7 +131,7 @@ pub struct ForkData {
}

impl ForkData {
pub fn hash_tree_root(&self) -> Result<H256, MerkleizationError> {
pub fn hash_tree_root(&self) -> Result<H256, SimpleSerializeError> {
hash_tree_root::<SSZForkData>(self.clone().into())
}
}
Expand All @@ -143,7 +143,7 @@ pub struct SigningData {
}

impl SigningData {
pub fn hash_tree_root(&self) -> Result<H256, MerkleizationError> {
pub fn hash_tree_root(&self) -> Result<H256, SimpleSerializeError> {
hash_tree_root::<SSZSigningData>(self.clone().into())
}
}
Expand Down Expand Up @@ -174,7 +174,7 @@ impl<const COMMITTEE_SIZE: usize> Default for SyncCommittee<COMMITTEE_SIZE> {
}

impl<const COMMITTEE_SIZE: usize> SyncCommittee<COMMITTEE_SIZE> {
pub fn hash_tree_root(&self) -> Result<H256, MerkleizationError> {
pub fn hash_tree_root(&self) -> Result<H256, SimpleSerializeError> {
hash_tree_root::<SSZSyncCommittee<COMMITTEE_SIZE>>(self.clone().into())
}
}
Expand Down Expand Up @@ -235,7 +235,7 @@ pub struct BeaconHeader {
}

impl BeaconHeader {
pub fn hash_tree_root(&self) -> Result<H256, MerkleizationError> {
pub fn hash_tree_root(&self) -> Result<H256, SimpleSerializeError> {
hash_tree_root::<SSZBeaconBlockHeader>((*self).into())
}
}
Expand Down Expand Up @@ -271,7 +271,7 @@ impl<const COMMITTEE_SIZE: usize, const COMMITTEE_BITS_SIZE: usize> Default
impl<const COMMITTEE_SIZE: usize, const COMMITTEE_BITS_SIZE: usize>
SyncAggregate<COMMITTEE_SIZE, COMMITTEE_BITS_SIZE>
{
pub fn hash_tree_root(&self) -> Result<H256, MerkleizationError> {
pub fn hash_tree_root(&self) -> Result<H256, SimpleSerializeError> {
hash_tree_root::<SSZSyncAggregate<COMMITTEE_SIZE>>(self.clone().into())
}
}
Expand Down Expand Up @@ -335,8 +335,8 @@ pub struct ExecutionPayloadHeader {
}

impl ExecutionPayloadHeader {
pub fn hash_tree_root(&self) -> Result<H256, MerkleizationError> {
hash_tree_root::<SSZExecutionPayloadHeader>(self.clone().into())
pub fn hash_tree_root(&self) -> Result<H256, SimpleSerializeError> {
hash_tree_root::<SSZExecutionPayloadHeader>(self.clone().try_into()?)
}
}

Expand Down

0 comments on commit a9d3df4

Please sign in to comment.