Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization for sync committee bits #13

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading