Skip to content

Commit

Permalink
A0-3256: Check for equivocations (#1457)
Browse files Browse the repository at this point in the history
# Description

Detecting Aura equivocations, `panic!` when self-equivocating

## Type of change

- New feature (non-breaking change which adds functionality)

# Checklist:

- I have added tests
- I have created new documentation

---------

Co-authored-by: Damian Leśniak <damian.lesniak@cardinals.cc>
  • Loading branch information
lesniak43 and Damian Leśniak authored Nov 13, 2023
1 parent dfef76a commit 8daf338
Show file tree
Hide file tree
Showing 13 changed files with 642 additions and 263 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions bin/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,10 +1017,10 @@ impl_runtime_apis! {
CommitteeManagement::predict_session_committee_for_session(session)
}

fn next_session_aura_authorities() -> Vec<AuraId> {
fn next_session_aura_authorities() -> Vec<(AccountId, AuraId)> {
let queued_keys = QueuedKeys::<Runtime>::get();

queued_keys.into_iter().filter_map(|(_, keys)| keys.get(AURA)).collect()
queued_keys.into_iter().filter_map(|(account_id, keys)| keys.get(AURA).map(|key| (account_id, key))).collect()
}

fn key_owner(key: AlephId) -> Option<AccountId> {
Expand Down
1 change: 1 addition & 0 deletions finality-aleph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ env_logger = { workspace = true }
futures = { workspace = true }
futures-timer = { workspace = true }
hash-db = { workspace = true }
hex = { workspace = true }
ip_network = { workspace = true }
log = { workspace = true }
lru = { workspace = true }
Expand Down
11 changes: 7 additions & 4 deletions finality-aleph/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use crate::{
pub trait RuntimeApi {
type Error: Display;
/// Returns aura authorities for the next session using state from block `at`
fn next_aura_authorities(&self, at: BlockHash) -> Result<Vec<AuraId>, Self::Error>;
fn next_aura_authorities(&self, at: BlockHash)
-> Result<Vec<(AccountId, AuraId)>, Self::Error>;
}

type QueuedKeys = Vec<(AccountId, SessionKeys)>;
Expand Down Expand Up @@ -95,16 +96,18 @@ where
{
type Error = ApiError;

fn next_aura_authorities(&self, at: BlockHash) -> Result<Vec<AuraId>, Self::Error> {
fn next_aura_authorities(
&self,
at: BlockHash,
) -> Result<Vec<(AccountId, AuraId)>, Self::Error> {
if let Ok(authorities) = self.client.runtime_api().next_session_aura_authorities(at) {
return Ok(authorities);
}

let queued_keys: QueuedKeys = self.read_storage("Session", "QueuedKeys", at)?;

Ok(queued_keys
.into_iter()
.filter_map(|(_, keys)| keys.get(AURA))
.filter_map(|(account_id, keys)| keys.get(AURA).map(|key| (account_id, key)))
.collect())
}
}
13 changes: 9 additions & 4 deletions finality-aleph/src/session_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use tokio::sync::{
};

use crate::{
aleph_primitives::{AlephSessionApi, AuraId, BlockHash, BlockNumber, SessionAuthorityData},
aleph_primitives::{
AccountId, AlephSessionApi, AuraId, BlockHash, BlockNumber, SessionAuthorityData,
},
runtime_api::RuntimeApi,
session::SessionBoundaryInfo,
ClientForAleph, SessionId, SessionPeriod,
Expand All @@ -30,7 +32,7 @@ pub trait AuthorityProvider {
/// returns list of Aura authorities for a given block number
fn aura_authorities(&self, block_number: BlockNumber) -> Option<Vec<AuraId>>;
/// returns list of next session Aura authorities for a given block number
fn next_aura_authorities(&self, block_number: BlockNumber) -> Option<Vec<AuraId>>;
fn next_aura_authorities(&self, block_number: BlockNumber) -> Option<Vec<(AccountId, AuraId)>>;
}

/// Default implementation of authority provider trait.
Expand Down Expand Up @@ -97,7 +99,7 @@ where
.ok()
}

fn next_aura_authorities(&self, block_number: BlockNumber) -> Option<Vec<AuraId>> {
fn next_aura_authorities(&self, block_number: BlockNumber) -> Option<Vec<(AccountId, AuraId)>> {
self.api
.next_aura_authorities(self.block_hash(block_number)?)
.ok()
Expand Down Expand Up @@ -457,7 +459,10 @@ mod tests {
None
}

fn next_aura_authorities(&self, _block_number: BlockNumber) -> Option<Vec<AuraId>> {
fn next_aura_authorities(
&self,
_block_number: BlockNumber,
) -> Option<Vec<(AccountId, AuraId)>> {
None
}
}
Expand Down
Loading

0 comments on commit 8daf338

Please sign in to comment.