From 23b077529edf5d9beaac05db96e9fee3132501e1 Mon Sep 17 00:00:00 2001 From: Jun Kimura Date: Wed, 7 Feb 2024 10:20:21 +0900 Subject: [PATCH] fix serialization for sync committee bits Signed-off-by: Jun Kimura --- crates/ibc/src/errors.rs | 14 ++++++-------- crates/ibc/src/types.rs | 16 ++++++++++------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/crates/ibc/src/errors.rs b/crates/ibc/src/errors.rs index d9eee09..1297540 100644 --- a/crates/ibc/src/errors.rs +++ b/crates/ibc/src/errors.rs @@ -76,8 +76,6 @@ pub enum Error { EthereumConsensusError(ethereum_consensus::errors::Error), /// decode error: `{0}` Decode(prost::DecodeError), - /// ssz deserialize error: `{0}` - SSZDeserialize(ssz_rs::DeserializeError), /// ics02 error: `{0}` ICS02(ClientError), /// ics24 error: `{0}` @@ -88,6 +86,12 @@ pub enum Error { TimestampOverflowError(TimestampOverflowError), /// parse timestamp error: `{0}` ParseTimestampError(ParseTimestampError), + /// deserialize sync committee bits error: `{parent}` sync_committee_size={sync_committee_size} sync_committee_bits={sync_committee_bits:?} + DeserializeSyncCommitteeBitsError { + parent: ssz_rs::DeserializeError, + sync_committee_size: usize, + sync_committee_bits: Vec, + }, } impl From for ClientError { @@ -134,12 +138,6 @@ impl From for Error { } } -impl From for Error { - fn from(value: ssz_rs::DeserializeError) -> Self { - Self::SSZDeserialize(value) - } -} - impl From for Error { fn from(value: ContextError) -> Self { Self::ContextError(value) diff --git a/crates/ibc/src/types.rs b/crates/ibc/src/types.rs index 6825949..8fa9433 100644 --- a/crates/ibc/src/types.rs +++ b/crates/ibc/src/types.rs @@ -178,15 +178,14 @@ pub(crate) fn convert_execution_update_to_proto( } } +/// CONTRACT: `SYNC_COMMITTEE_SIZE` must be greater than 0 pub(crate) fn convert_sync_aggregate_to_proto( sync_aggregate: SyncAggregate, ) -> ProtoSyncAggregate { + let sync_committee_bits = ssz_rs::serialize(&sync_aggregate.sync_committee_bits) + .expect("failed to serialize sync_committee_bits: this should never happen unless `SYNC_COMMITTEE_SIZE` is 0"); ProtoSyncAggregate { - sync_committee_bits: sync_aggregate - .sync_committee_bits - .iter() - .map(|b| if b == true { 1 } else { 0 }) - .collect(), + sync_committee_bits, sync_committee_signature: sync_aggregate.sync_committee_signature.0.to_vec(), } } @@ -197,7 +196,12 @@ pub(crate) fn convert_proto_sync_aggregate( Ok(SyncAggregate { sync_committee_bits: Bitvector::::deserialize( sync_aggregate.sync_committee_bits.as_slice(), - )?, + ) + .map_err(|e| Error::DeserializeSyncCommitteeBitsError { + parent: e, + sync_committee_size: SYNC_COMMITTEE_SIZE, + sync_committee_bits: sync_aggregate.sync_committee_bits, + })?, sync_committee_signature: Signature::try_from(sync_aggregate.sync_committee_signature)?, }) }