Skip to content

Commit

Permalink
fix to always set next sync committee in consensus state
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Nov 3, 2024
1 parent 27719dd commit c50d370
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
44 changes: 16 additions & 28 deletions crates/ibc/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct ConsensusState {
/// aggregate public key of current sync committee
pub current_sync_committee: PublicKey,
/// aggregate public key of next sync committee
pub next_sync_committee: Option<PublicKey>,
pub next_sync_committee: PublicKey,
}

impl ConsensusState {
Expand Down Expand Up @@ -103,9 +103,11 @@ impl TryFrom<RawConsensusState> for ConsensusState {

fn try_from(value: RawConsensusState) -> Result<Self, Self::Error> {
let next_sync_committee = if value.next_sync_committee.is_empty() {
None
return Err(Self::Error::InvalidRawConsensusState {
reason: "next_sync_committee is empty".to_string(),
});
} else {
Some(PublicKey::try_from(value.next_sync_committee)?)
PublicKey::try_from(value.next_sync_committee)?
};
Ok(Self {
slot: value.slot.into(),
Expand All @@ -123,16 +125,12 @@ impl TryFrom<RawConsensusState> for ConsensusState {

impl From<ConsensusState> for RawConsensusState {
fn from(value: ConsensusState) -> Self {
let next_sync_committee = match value.next_sync_committee {
Some(next_sync_committee) => next_sync_committee.0.to_vec(),
None => Vec::new(),
};
Self {
slot: value.slot.into(),
storage_root: value.storage_root.into_vec(),
timestamp: Some(ibc_timestamp_to_proto_timestamp(value.timestamp)),
current_sync_committee: value.current_sync_committee.to_vec(),
next_sync_committee,
next_sync_committee: value.next_sync_committee.to_vec(),
}
}
}
Expand Down Expand Up @@ -203,31 +201,21 @@ impl<const SYNC_COMMITTEE_SIZE: usize> TrustedConsensusState<SYNC_COMMITTEE_SIZE
};
}

if let Some(next_sync_committee) = consensus_state.next_sync_committee.clone() {
if sync_committee.aggregate_pubkey == next_sync_committee {
Ok(Self {
state: consensus_state,
current_sync_committee: None,
next_sync_committee: Some(sync_committee),
})
} else {
Err(Error::InvalidNextSyncCommitteeKeys(
sync_committee.aggregate_pubkey,
next_sync_committee,
))
}
if sync_committee.aggregate_pubkey == consensus_state.next_sync_committee {
Ok(Self {
state: consensus_state,
current_sync_committee: None,
next_sync_committee: Some(sync_committee),
})
} else {
Err(Error::NoNextSyncCommitteeInConsensusState)
Err(Error::InvalidNextSyncCommitteeKeys(
sync_committee.aggregate_pubkey,
consensus_state.next_sync_committee,
))
}
}
}

impl<const SYNC_COMMITTEE_SIZE: usize> TrustedConsensusState<SYNC_COMMITTEE_SIZE> {
pub fn current_sync_committee_aggregate_key(&self) -> PublicKey {
self.state.current_sync_committee.clone()
}
}

impl<const SYNC_COMMITTEE_SIZE: usize> LightClientStoreReader<SYNC_COMMITTEE_SIZE>
for TrustedConsensusState<SYNC_COMMITTEE_SIZE>
{
Expand Down
4 changes: 2 additions & 2 deletions crates/ibc/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub enum Error {
String,
Vec<String>,
),
/// consensus state doesn't have next sync committee
NoNextSyncCommitteeInConsensusState,
/// consensus update doesn't have next sync committee: store_period={0} update_period={1}
NoNextSyncCommitteeInConsensusUpdate(u64, u64),
/// invalid current sync committee keys: expected={0:?} actual={1:?}
InvalidCurrentSyncCommitteeKeys(PublicKey, PublicKey),
/// invalid next sync committee keys: expected={0:?} actual={1:?}
Expand Down
22 changes: 13 additions & 9 deletions crates/ibc/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@ pub fn apply_updates<const SYNC_COMMITTEE_SIZE: usize, C: ChainContext>(
slot: update_slot,
storage_root: account_update.account_storage_root.0.to_vec().into(),
timestamp: wrap_compute_timestamp_at_slot(ctx, update_slot)?,
current_sync_committee: trusted_consensus_state.current_sync_committee_aggregate_key(),
next_sync_committee: consensus_update
.next_sync_committee
.as_ref()
.map(|c| c.0.aggregate_pubkey.clone()),
current_sync_committee: trusted_consensus_state.state.current_sync_committee.clone(),
next_sync_committee: trusted_consensus_state.state.next_sync_committee.clone(),
}
} else if store_period + 1 == update_period {
let next_sync_committee = consensus_update
.next_sync_committee
.as_ref()
.map(|c| c.0.aggregate_pubkey.clone());
if next_sync_committee.is_none() {
return Err(Error::NoNextSyncCommitteeInConsensusUpdate(
store_period.into(),
update_period.into(),
));
}
ConsensusState {
slot: update_slot,
storage_root: account_update.account_storage_root.0.to_vec().into(),
Expand All @@ -55,10 +62,7 @@ pub fn apply_updates<const SYNC_COMMITTEE_SIZE: usize, C: ChainContext>(
.unwrap()
.aggregate_pubkey
.clone(),
next_sync_committee: consensus_update
.next_sync_committee
.as_ref()
.map(|c| c.0.aggregate_pubkey.clone()),
next_sync_committee: next_sync_committee.unwrap(),
}
} else {
// store_period + 1 < update_period
Expand Down

0 comments on commit c50d370

Please sign in to comment.