Skip to content

Commit

Permalink
fix serialization for sync committee bits
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Feb 7, 2024
1 parent 91a290e commit 23b0775
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
14 changes: 6 additions & 8 deletions crates/ibc/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand All @@ -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<u8>,
},
}

impl From<Error> for ClientError {
Expand Down Expand Up @@ -134,12 +138,6 @@ impl From<ValidationError> for Error {
}
}

impl From<ssz_rs::DeserializeError> for Error {
fn from(value: ssz_rs::DeserializeError) -> Self {
Self::SSZDeserialize(value)
}
}

impl From<ContextError> for Error {
fn from(value: ContextError) -> Self {
Self::ContextError(value)
Expand Down
16 changes: 10 additions & 6 deletions crates/ibc/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<const SYNC_COMMITTEE_SIZE: usize>(
sync_aggregate: SyncAggregate<SYNC_COMMITTEE_SIZE>,
) -> 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(),
}
}
Expand All @@ -197,7 +196,12 @@ pub(crate) fn convert_proto_sync_aggregate<const SYNC_COMMITTEE_SIZE: usize>(
Ok(SyncAggregate {
sync_committee_bits: Bitvector::<SYNC_COMMITTEE_SIZE>::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)?,
})
}
Expand Down

0 comments on commit 23b0775

Please sign in to comment.