Skip to content

Commit

Permalink
A0-3037: Sync information for Aura (#1416)
Browse files Browse the repository at this point in the history
# Description

Make Aura use the information from our sync to decide on block
production (whether we are in "major sync").

Some of the choices here might be controversial, so I would welcome
opinions about them. <_<

## Type of change

Please delete options that are not relevant.

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

# Checklist:

- I have created new documentation
  • Loading branch information
timorleph authored Sep 29, 2023
1 parent 4ebefbf commit 94ed410
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 58 deletions.
38 changes: 8 additions & 30 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions bin/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use aleph_runtime::{self, opaque::Block, RuntimeApi};
use finality_aleph::{
run_validator_node, AlephBlockImport, AlephConfig, BlockImporter, Justification,
JustificationTranslator, MillisecsPerBlock, Protocol, ProtocolNaming, RateLimiterConfig,
SessionPeriod, SubstrateChainStatus, TimingBlockMetrics, TracingBlockImport,
SessionPeriod, SubstrateChainStatus, SyncOracle, TimingBlockMetrics, TracingBlockImport,
};
use futures::channel::mpsc;
use log::warn;
Expand Down Expand Up @@ -354,6 +354,8 @@ pub fn new_authority(

let slot_duration = sc_consensus_aura::slot_duration(&*client)?;

let sync_oracle = SyncOracle::new();

let aura = sc_consensus_aura::start_aura::<AuraPair, _, _, _, _, _, _, _, _, _, _>(
StartAuraParams {
slot_duration,
Expand All @@ -375,7 +377,7 @@ pub fn new_authority(
force_authoring,
backoff_authoring_blocks,
keystore: keystore_container.keystore(),
sync_oracle: sync_network.clone(),
sync_oracle: sync_oracle.clone(),
justification_sync_link: sync_network.clone(),
block_proposal_slot_portion: SlotProportion::new(2f32 / 3f32),
max_block_proposal_slot_portion: None,
Expand Down Expand Up @@ -419,6 +421,7 @@ pub fn new_authority(
validator_port: aleph_config.validator_port(),
protocol_naming,
rate_limiter_config,
sync_oracle,
};

task_manager.spawn_essential_handle().spawn_blocking(
Expand Down
3 changes: 3 additions & 0 deletions finality-aleph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ mod party;
mod session;
mod session_map;
mod sync;
mod sync_oracle;
#[cfg(test)]
pub mod testing;

Expand All @@ -67,6 +68,7 @@ pub use crate::{
substrate::{BlockImporter, Justification},
JustificationTranslator, SubstrateChainStatus,
},
sync_oracle::SyncOracle,
};

/// Constant defining how often components of finality-aleph should report their state
Expand Down Expand Up @@ -291,4 +293,5 @@ pub struct AlephConfig<C, SC> {
pub validator_port: u16,
pub protocol_naming: ProtocolNaming,
pub rate_limiter_config: RateLimiterConfig,
pub sync_oracle: SyncOracle,
}
28 changes: 15 additions & 13 deletions finality-aleph/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ use crate::{
session::SessionBoundaryInfo,
session_map::{AuthorityProviderImpl, FinalityNotifierImpl, SessionMapUpdater},
sync::{
ChainStatus, DatabaseIO as SyncDatabaseIO, FinalizationStatus, Justification,
JustificationTranslator, OldSyncCompatibleRequestBlocks, Service as SyncService,
SubstrateChainStatusNotifier, SubstrateFinalizationInfo, VerifierCache,
ChainStatus, FinalizationStatus, Justification, JustificationTranslator,
OldSyncCompatibleRequestBlocks, Service as SyncService, SubstrateChainStatusNotifier,
SubstrateFinalizationInfo, VerifierCache, IO as SyncIO,
},
AlephConfig,
};
Expand Down Expand Up @@ -70,6 +70,7 @@ where
validator_port,
protocol_naming,
rate_limiter_config,
sync_oracle,
} = aleph_config;

// We generate the phrase manually to only save the key in RAM, we don't want to have these
Expand Down Expand Up @@ -149,19 +150,20 @@ where
);
let finalizer = AlephFinalizer::new(client.clone(), metrics.clone());
import_queue_handle.attach_metrics(metrics.clone());
let database_io = SyncDatabaseIO::new(chain_status.clone(), finalizer, import_queue_handle);
let (sync_service, justifications_for_sync, request_block) = match SyncService::new(
let sync_io = SyncIO::new(
chain_status.clone(),
finalizer,
import_queue_handle,
block_sync_network,
chain_events,
verifier,
database_io,
session_info.clone(),
sync_oracle,
justification_rx,
registry.clone(),
) {
Ok(x) => x,
Err(e) => panic!("Failed to initialize Sync service: {e}"),
};
);
let (sync_service, justifications_for_sync, request_block) =
match SyncService::new(verifier, session_info.clone(), sync_io, registry.clone()) {
Ok(x) => x,
Err(e) => panic!("Failed to initialize Sync service: {e}"),
};
let sync_task = async move { sync_service.run().await };

let (connection_manager_service, connection_manager) = ConnectionManager::new(
Expand Down
9 changes: 8 additions & 1 deletion finality-aleph/src/sync/forest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,13 +596,20 @@ where
}
}

/// How far behind in finalization are we.
pub fn behind_finalization(&self) -> u32 {
self.highest_justified
.number()
.saturating_sub(self.root_id.number())
}

/// Returns an extension request with the appropriate data if either:
/// 1. We know of a justified header for which we do not have a block, or
/// 2. We know of nodes which have children of our favourite block.
pub fn extension_request(&self) -> ExtensionRequest<I, J> {
use ExtensionRequest::*;
use VertexHandle::*;
if self.highest_justified.number() > self.root_id.number() {
if self.behind_finalization() > 0 {
// This should always happen, but if it doesn't falling back to other forms of extension requests is acceptable.
if let Some((know_most, branch_knowledge)) =
self.prepare_request_info(&self.highest_justified, true)
Expand Down
19 changes: 15 additions & 4 deletions finality-aleph/src/sync/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
Block, BlockIdFor, BlockImport, BlockStatus, ChainStatus, Finalizer, Header, Justification,
PeerId, UnverifiedJustification, Verifier,
},
BlockIdentifier, BlockNumber,
BlockIdentifier, BlockNumber, SyncOracle,
};

mod request_handler;
Expand Down Expand Up @@ -209,6 +209,7 @@ where
session_info: SessionBoundaryInfo,
block_importer: BI,
missed_import_data: MissedImportData,
sync_oracle: SyncOracle,
phantom: PhantomData<B>,
}

Expand Down Expand Up @@ -371,6 +372,7 @@ where
pub fn new(
database_io: DatabaseIO<B, J, CS, F, BI>,
verifier: V,
sync_oracle: SyncOracle,
session_info: SessionBoundaryInfo,
) -> Result<Self, <Self as HandlerTypes>::Error> {
let DatabaseIO {
Expand All @@ -388,6 +390,7 @@ where
session_info,
block_importer,
missed_import_data: MissedImportData::new(),
sync_oracle,
phantom: PhantomData,
})
}
Expand Down Expand Up @@ -513,6 +516,8 @@ where
.forest
.update_justification(justification, maybe_peer)?;
self.try_finalize()?;
self.sync_oracle
.update_behind(self.forest.behind_finalization());
Ok(new_highest)
}

Expand Down Expand Up @@ -735,7 +740,7 @@ mod tests {
ChainStatusNotification::*,
ChainStatusNotifier, Header, Justification,
},
BlockIdentifier, BlockNumber, SessionPeriod,
BlockIdentifier, BlockNumber, SessionPeriod, SyncOracle,
};

type TestHandler =
Expand All @@ -753,8 +758,13 @@ mod tests {
let (backend, notifier) = Backend::setup(SESSION_BOUNDARY_INFO);
let verifier = backend.clone();
let database_io = DatabaseIO::new(backend.clone(), backend.clone(), backend.clone());
let handler =
Handler::new(database_io, verifier, SESSION_BOUNDARY_INFO).expect("mock backend works");
let handler = Handler::new(
database_io,
verifier,
SyncOracle::new(),
SESSION_BOUNDARY_INFO,
)
.expect("mock backend works");
let genesis = backend.top_finalized().expect("genesis").header().id();
(handler, backend, notifier, genesis)
}
Expand Down Expand Up @@ -1575,6 +1585,7 @@ mod tests {
let mut handler = Handler::new(
database_io,
verifier,
SyncOracle::new(),
SessionBoundaryInfo::new(SessionPeriod(20)),
)
.expect("mock backend works");
Expand Down
2 changes: 1 addition & 1 deletion finality-aleph/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod tasks;
mod ticker;

pub use compatibility::OldSyncCompatibleRequestBlocks;
pub use service::{DatabaseIO, Service};
pub use service::{Service, IO};
pub use substrate::{
Justification as SubstrateJustification, JustificationTranslator, SessionVerifier,
SubstrateChainStatus, SubstrateChainStatusNotifier, SubstrateFinalizationInfo, VerifierCache,
Expand Down
Loading

0 comments on commit 94ed410

Please sign in to comment.