-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
jwk #4: jwk update quorum certification #11857
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
68e2f24
jwk types update
zjma 777450d
update
zjma ef680a9
update
zjma d0957db
jwk txn and execution
zjma 3a46733
consensus ensure jwk txns are expected
zjma 6452075
update
zjma 873ab98
jwk consensus network type defs
zjma 54c5c9a
update cargo.toml
zjma f7ef043
update
zjma 20ccbc6
update
zjma 27b7d80
update
zjma 99cc957
lint
zjma b019753
jwk update quorum certification
zjma 51f750c
update
zjma bd675c0
update
zjma 652b9f4
update
zjma a0d1c56
update
zjma 744ae24
Merge remote-tracking branch 'origin/main' into zjma/jwk_update_qc
zjma 2f5556b
update
zjma 9817a00
update
zjma 0def8a9
update
zjma 21e47cd
update
zjma 99de43b
update
zjma b27aabd
update
zjma dd03efb
update
zjma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
crates/aptos-jwk-consensus/src/observation_aggregation/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright © Aptos Foundation | ||
|
||
use crate::types::{ | ||
JWKConsensusMsg, ObservedUpdate, ObservedUpdateRequest, ObservedUpdateResponse, | ||
}; | ||
use anyhow::{anyhow, ensure}; | ||
use aptos_consensus_types::common::Author; | ||
use aptos_infallible::Mutex; | ||
use aptos_reliable_broadcast::BroadcastStatus; | ||
use aptos_types::{ | ||
aggregate_signature::PartialSignatures, | ||
epoch_state::EpochState, | ||
jwks::{ProviderJWKs, QuorumCertifiedUpdate}, | ||
}; | ||
use move_core_types::account_address::AccountAddress; | ||
use std::{collections::BTreeSet, sync::Arc}; | ||
|
||
/// The aggregation state of reliable broadcast where a validator broadcast JWK observation requests | ||
/// and produce quorum-certified JWK updates. | ||
pub struct ObservationAggregationState { | ||
epoch_state: Arc<EpochState>, | ||
local_view: ProviderJWKs, | ||
inner_state: Mutex<PartialSignatures>, | ||
} | ||
|
||
impl ObservationAggregationState { | ||
pub fn new(epoch_state: Arc<EpochState>, local_view: ProviderJWKs) -> Self { | ||
Self { | ||
epoch_state, | ||
local_view, | ||
inner_state: Mutex::new(PartialSignatures::empty()), | ||
} | ||
} | ||
} | ||
|
||
impl BroadcastStatus<JWKConsensusMsg> for Arc<ObservationAggregationState> { | ||
type Aggregated = QuorumCertifiedUpdate; | ||
type Message = ObservedUpdateRequest; | ||
type Response = ObservedUpdateResponse; | ||
|
||
fn add( | ||
&self, | ||
sender: Author, | ||
response: Self::Response, | ||
) -> anyhow::Result<Option<Self::Aggregated>> { | ||
let ObservedUpdateResponse { epoch, update } = response; | ||
let ObservedUpdate { | ||
author, | ||
observed: peer_view, | ||
signature, | ||
} = update; | ||
ensure!( | ||
epoch == self.epoch_state.epoch, | ||
"adding peer observation failed with invalid epoch", | ||
); | ||
ensure!( | ||
author == sender, | ||
"adding peer observation failed with mismatched author", | ||
); | ||
|
||
let mut partial_sigs = self.inner_state.lock(); | ||
if partial_sigs.contains_voter(&sender) { | ||
return Ok(None); | ||
} | ||
|
||
ensure!( | ||
self.local_view == peer_view, | ||
"adding peer observation failed with mismatched view" | ||
); | ||
|
||
// Verify peer signature. | ||
self.epoch_state | ||
.verifier | ||
.verify(sender, &peer_view, &signature)?; | ||
|
||
// All checks passed. Aggregating. | ||
partial_sigs.add_signature(sender, signature); | ||
let voters: BTreeSet<AccountAddress> = partial_sigs.signatures().keys().copied().collect(); | ||
if self | ||
.epoch_state | ||
.verifier | ||
.check_voting_power(voters.iter(), true) | ||
.is_err() | ||
{ | ||
return Ok(None); | ||
} | ||
let multi_sig = self.epoch_state.verifier.aggregate_signatures(&partial_sigs).map_err(|e|anyhow!("adding peer observation failed with partial-to-aggregated conversion error: {e}"))?; | ||
|
||
Ok(Some(QuorumCertifiedUpdate { | ||
update: peer_view, | ||
multi_sig, | ||
})) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
132 changes: 132 additions & 0 deletions
132
crates/aptos-jwk-consensus/src/observation_aggregation/tests.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright © Aptos Foundation | ||
|
||
use crate::{ | ||
observation_aggregation::ObservationAggregationState, | ||
types::{ObservedUpdate, ObservedUpdateResponse}, | ||
}; | ||
use aptos_crypto::{bls12381, SigningKey, Uniform}; | ||
use aptos_reliable_broadcast::BroadcastStatus; | ||
use aptos_types::{ | ||
epoch_state::EpochState, | ||
jwks::{ | ||
jwk::{JWKMoveStruct, JWK}, | ||
unsupported::UnsupportedJWK, | ||
ProviderJWKs, QuorumCertifiedUpdate, | ||
}, | ||
validator_verifier::{ValidatorConsensusInfo, ValidatorVerifier}, | ||
}; | ||
use move_core_types::account_address::AccountAddress; | ||
use std::sync::Arc; | ||
|
||
#[test] | ||
fn test_observation_aggregation_state() { | ||
let num_validators = 5; | ||
let epoch = 999; | ||
let addrs: Vec<AccountAddress> = (0..num_validators) | ||
.map(|_| AccountAddress::random()) | ||
.collect(); | ||
let private_keys: Vec<bls12381::PrivateKey> = (0..num_validators) | ||
.map(|_| bls12381::PrivateKey::generate_for_testing()) | ||
.collect(); | ||
let public_keys: Vec<bls12381::PublicKey> = (0..num_validators) | ||
.map(|i| bls12381::PublicKey::from(&private_keys[i])) | ||
.collect(); | ||
let voting_powers = [1, 1, 1, 6, 6]; // total voting power: 15, default threshold: 11 | ||
let validator_infos: Vec<ValidatorConsensusInfo> = (0..num_validators) | ||
.map(|i| ValidatorConsensusInfo::new(addrs[i], public_keys[i].clone(), voting_powers[i])) | ||
.collect(); | ||
let verifier = ValidatorVerifier::new(validator_infos); | ||
let epoch_state = Arc::new(EpochState { epoch, verifier }); | ||
let view_0 = ProviderJWKs { | ||
issuer: b"https::/alice.com".to_vec(), | ||
version: 123, | ||
jwks: vec![JWKMoveStruct::from(JWK::Unsupported( | ||
UnsupportedJWK::new_for_testing("id1", "payload1"), | ||
))], | ||
}; | ||
let view_1 = ProviderJWKs { | ||
issuer: b"https::/alice.com".to_vec(), | ||
version: 123, | ||
jwks: vec![JWKMoveStruct::from(JWK::Unsupported( | ||
UnsupportedJWK::new_for_testing("id2", "payload2"), | ||
))], | ||
}; | ||
let ob_agg_state = Arc::new(ObservationAggregationState::new( | ||
epoch_state.clone(), | ||
view_0.clone(), | ||
)); | ||
|
||
// `ObservedUpdate` with incorrect epoch should be rejected. | ||
let result = ob_agg_state.add(addrs[0], ObservedUpdateResponse { | ||
epoch: 998, | ||
update: ObservedUpdate { | ||
author: addrs[0], | ||
observed: view_0.clone(), | ||
signature: private_keys[0].sign(&view_0).unwrap(), | ||
}, | ||
}); | ||
assert!(result.is_err()); | ||
|
||
// `ObservedUpdate` authored by X but sent by Y should be rejected. | ||
let result = ob_agg_state.add(addrs[1], ObservedUpdateResponse { | ||
epoch: 999, | ||
update: ObservedUpdate { | ||
author: addrs[0], | ||
observed: view_0.clone(), | ||
signature: private_keys[0].sign(&view_0).unwrap(), | ||
}, | ||
}); | ||
assert!(result.is_err()); | ||
|
||
// `ObservedUpdate` that cannot be verified should be rejected. | ||
let result = ob_agg_state.add(addrs[2], ObservedUpdateResponse { | ||
epoch: 999, | ||
update: ObservedUpdate { | ||
author: addrs[2], | ||
observed: view_0.clone(), | ||
signature: private_keys[2].sign(&view_1).unwrap(), | ||
}, | ||
}); | ||
assert!(result.is_err()); | ||
|
||
// Good `ObservedUpdate` should be accepted. | ||
let result = ob_agg_state.add(addrs[3], ObservedUpdateResponse { | ||
epoch: 999, | ||
update: ObservedUpdate { | ||
author: addrs[3], | ||
observed: view_0.clone(), | ||
signature: private_keys[3].sign(&view_0).unwrap(), | ||
}, | ||
}); | ||
assert!(matches!(result, Ok(None))); | ||
|
||
// `ObservedUpdate` from contributed author should be ignored. | ||
let result = ob_agg_state.add(addrs[3], ObservedUpdateResponse { | ||
epoch: 999, | ||
update: ObservedUpdate { | ||
author: addrs[3], | ||
observed: view_0.clone(), | ||
signature: private_keys[3].sign(&view_0).unwrap(), | ||
}, | ||
}); | ||
assert!(matches!(result, Ok(None))); | ||
|
||
// Quorum-certified update should be returned if after adding an `ObservedUpdate`, the threshold is exceeded. | ||
let result = ob_agg_state.add(addrs[4], ObservedUpdateResponse { | ||
epoch: 999, | ||
update: ObservedUpdate { | ||
author: addrs[4], | ||
observed: view_0.clone(), | ||
signature: private_keys[4].sign(&view_0).unwrap(), | ||
}, | ||
}); | ||
let QuorumCertifiedUpdate { | ||
update: observed, | ||
multi_sig, | ||
} = result.unwrap().unwrap(); | ||
assert_eq!(view_0, observed); | ||
assert!(epoch_state | ||
.verifier | ||
.verify_multi_signatures(&observed, &multi_sig) | ||
.is_ok()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright © Aptos Foundation | ||
|
||
use crate::{ | ||
observation_aggregation::ObservationAggregationState, | ||
types::{JWKConsensusMsg, ObservedUpdateRequest}, | ||
}; | ||
use aptos_channels::aptos_channel; | ||
use aptos_reliable_broadcast::ReliableBroadcast; | ||
use aptos_types::{ | ||
epoch_state::EpochState, | ||
jwks::{ProviderJWKs, QuorumCertifiedUpdate}, | ||
}; | ||
use futures_util::future::{AbortHandle, Abortable}; | ||
use std::sync::Arc; | ||
use tokio_retry::strategy::ExponentialBackoff; | ||
|
||
/// A sub-process of the whole JWK consensus process. | ||
/// Once invoked by `JWKConsensusManager` to `start_produce`, | ||
/// it starts producing a `QuorumCertifiedUpdate` and returns an abort handle. | ||
/// Once an `QuorumCertifiedUpdate` is available, it is sent back via a channel given earlier. | ||
pub trait TUpdateCertifier: Send + Sync { | ||
fn start_produce( | ||
&self, | ||
epoch_state: Arc<EpochState>, | ||
payload: ProviderJWKs, | ||
qc_update_tx: aptos_channel::Sender<(), QuorumCertifiedUpdate>, | ||
) -> AbortHandle; | ||
} | ||
|
||
pub struct CertifiedUpdateProducer { | ||
reliable_broadcast: Arc<ReliableBroadcast<JWKConsensusMsg, ExponentialBackoff>>, | ||
} | ||
|
||
impl CertifiedUpdateProducer { | ||
pub fn new(reliable_broadcast: ReliableBroadcast<JWKConsensusMsg, ExponentialBackoff>) -> Self { | ||
Self { | ||
reliable_broadcast: Arc::new(reliable_broadcast), | ||
} | ||
} | ||
} | ||
|
||
impl TUpdateCertifier for CertifiedUpdateProducer { | ||
fn start_produce( | ||
&self, | ||
epoch_state: Arc<EpochState>, | ||
payload: ProviderJWKs, | ||
qc_update_tx: aptos_channel::Sender<(), QuorumCertifiedUpdate>, | ||
) -> AbortHandle { | ||
let rb = self.reliable_broadcast.clone(); | ||
let req = ObservedUpdateRequest { | ||
epoch: epoch_state.epoch, | ||
issuer: payload.issuer.clone(), | ||
}; | ||
let agg_state = Arc::new(ObservationAggregationState::new(epoch_state, payload)); | ||
let task = async move { | ||
let qc_update = rb.broadcast(req, agg_state).await; | ||
let _ = qc_update_tx.push((), qc_update); | ||
}; | ||
let (abort_handle, abort_registration) = AbortHandle::new_pair(); | ||
tokio::spawn(Abortable::new(task, abort_registration)); | ||
abort_handle | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the reliable broadcast may panic, if a validator has a different observation than anyone else? If the rb receives all response but does not aggregate it will panic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For fixes, either rb keeps fetching if the response is different than mine, or allow aggregation to fail and return None. In the later case the validator needs to retry rb to fetch again. Let's discuss what is better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NVM, I saw you already did the first one.