diff --git a/polkadot/node/core/av-store/src/lib.rs b/polkadot/node/core/av-store/src/lib.rs index 0160e87c43..09a3a28fed 100644 --- a/polkadot/node/core/av-store/src/lib.rs +++ b/polkadot/node/core/av-store/src/lib.rs @@ -48,7 +48,7 @@ use polkadot_node_subsystem_util::{ metrics::{self, prometheus}, }; use polkadot_subsystem::messages::{ - AvailabilityStoreMessage, ChainApiMessage, RuntimeApiMessage, RuntimeApiRequest, + AvailabilityStoreMessage, ChainApiMessage, }; use bitvec::{vec::BitVec, order::Lsb0 as BitOrderLsb0}; @@ -655,9 +655,11 @@ where subsystem.finalized_number.unwrap_or(block_number.saturating_sub(1)), ).await?; - let mut tx = DBTransaction::new(); // determine_new_blocks is descending in block height for (hash, header) in new_blocks.into_iter().rev() { + // it's important to commit the db transactions for a head before the next one is processed + // alternatively, we could utilize the OverlayBackend from approval-voting + let mut tx = DBTransaction::new(); process_new_head( ctx, &subsystem.db, @@ -669,8 +671,8 @@ where header, ).await?; subsystem.known_blocks.insert(hash, block_number); + subsystem.db.write(tx)?; } - subsystem.db.write(tx)?; Ok(()) } @@ -690,25 +692,17 @@ where Context: overseer::SubsystemContext, { - let candidate_events = { - let (tx, rx) = oneshot::channel(); - ctx.send_message( - RuntimeApiMessage::Request(hash, RuntimeApiRequest::CandidateEvents(tx)) - ).await; - - rx.await?? - }; + let candidate_events = util::request_candidate_events( + hash, + ctx.sender(), + ).await.await??; // We need to request the number of validators based on the parent state, // as that is the number of validators used to create this block. - let n_validators = { - let (tx, rx) = oneshot::channel(); - ctx.send_message( - RuntimeApiMessage::Request(header.parent_hash, RuntimeApiRequest::Validators(tx)) - ).await; - - rx.await??.len() - }; + let n_validators = util::request_validators( + header.parent_hash, + ctx.sender(), + ).await.await??.len(); for event in candidate_events { match event { diff --git a/polkadot/node/core/av-store/src/tests.rs b/polkadot/node/core/av-store/src/tests.rs index e923545a78..38c05eb151 100644 --- a/polkadot/node/core/av-store/src/tests.rs +++ b/polkadot/node/core/av-store/src/tests.rs @@ -32,8 +32,8 @@ use polkadot_primitives::v1::{ use polkadot_node_primitives::{AvailableData, BlockData, PoV}; use polkadot_node_subsystem_util::TimeoutExt; use polkadot_subsystem::{ - ActiveLeavesUpdate, errors::RuntimeApiError, jaeger, messages::AllMessages, ActivatedLeaf, - LeafStatus, + ActiveLeavesUpdate, errors::RuntimeApiError, jaeger, ActivatedLeaf, + LeafStatus, messages::{AllMessages, RuntimeApiMessage, RuntimeApiRequest}, }; use polkadot_node_subsystem_test_helpers as test_helpers; use sp_keyring::Sr25519Keyring; diff --git a/polkadot/node/core/runtime-api/src/cache.rs b/polkadot/node/core/runtime-api/src/cache.rs index 97d392863d..0077ac0763 100644 --- a/polkadot/node/core/runtime-api/src/cache.rs +++ b/polkadot/node/core/runtime-api/src/cache.rs @@ -77,10 +77,10 @@ pub(crate) struct RequestResultCache { check_validation_outputs: MemoryLruCache<(Hash, ParaId, CandidateCommitments), ResidentSizeOf>, session_index_for_child: MemoryLruCache>, validation_code: MemoryLruCache<(Hash, ParaId, OccupiedCoreAssumption), ResidentSizeOf>>, - validation_code_by_hash: MemoryLruCache<(Hash, ValidationCodeHash), ResidentSizeOf>>, + validation_code_by_hash: MemoryLruCache>>, candidate_pending_availability: MemoryLruCache<(Hash, ParaId), ResidentSizeOf>>, candidate_events: MemoryLruCache>>, - session_info: MemoryLruCache<(Hash, SessionIndex), ResidentSizeOf>>, + session_info: MemoryLruCache>>, dmq_contents: MemoryLruCache<(Hash, ParaId), ResidentSizeOf>>>, inbound_hrmp_channels_contents: MemoryLruCache<(Hash, ParaId), ResidentSizeOf>>>>, current_babe_epoch: MemoryLruCache>, @@ -173,11 +173,13 @@ impl RequestResultCache { self.validation_code.insert(key, ResidentSizeOf(value)); } + // the actual key is `ValidationCodeHash` (`Hash` is ignored), + // but we keep the interface that way to keep the macro simple pub(crate) fn validation_code_by_hash(&mut self, key: (Hash, ValidationCodeHash)) -> Option<&Option> { - self.validation_code_by_hash.get(&key).map(|v| &v.0) + self.validation_code_by_hash.get(&key.1).map(|v| &v.0) } - pub(crate) fn cache_validation_code_by_hash(&mut self, key: (Hash, ValidationCodeHash), value: Option) { + pub(crate) fn cache_validation_code_by_hash(&mut self, key: ValidationCodeHash, value: Option) { self.validation_code_by_hash.insert(key, ResidentSizeOf(value)); } @@ -198,10 +200,10 @@ impl RequestResultCache { } pub(crate) fn session_info(&mut self, key: (Hash, SessionIndex)) -> Option<&Option> { - self.session_info.get(&key).map(|v| &v.0) + self.session_info.get(&key.1).map(|v| &v.0) } - pub(crate) fn cache_session_info(&mut self, key: (Hash, SessionIndex), value: Option) { + pub(crate) fn cache_session_info(&mut self, key: SessionIndex, value: Option) { self.session_info.insert(key, ResidentSizeOf(value)); } diff --git a/polkadot/node/core/runtime-api/src/lib.rs b/polkadot/node/core/runtime-api/src/lib.rs index 88c85a0372..5cba9bbb76 100644 --- a/polkadot/node/core/runtime-api/src/lib.rs +++ b/polkadot/node/core/runtime-api/src/lib.rs @@ -125,14 +125,14 @@ impl RuntimeApiSubsystem where self.requests_cache.cache_session_index_for_child(relay_parent, session_index), ValidationCode(relay_parent, para_id, assumption, code) => self.requests_cache.cache_validation_code((relay_parent, para_id, assumption), code), - ValidationCodeByHash(relay_parent, validation_code_hash, code) => - self.requests_cache.cache_validation_code_by_hash((relay_parent, validation_code_hash), code), + ValidationCodeByHash(_relay_parent, validation_code_hash, code) => + self.requests_cache.cache_validation_code_by_hash(validation_code_hash, code), CandidatePendingAvailability(relay_parent, para_id, candidate) => self.requests_cache.cache_candidate_pending_availability((relay_parent, para_id), candidate), CandidateEvents(relay_parent, events) => self.requests_cache.cache_candidate_events(relay_parent, events), - SessionInfo(relay_parent, session_index, info) => - self.requests_cache.cache_session_info((relay_parent, session_index), info), + SessionInfo(_relay_parent, session_index, info) => + self.requests_cache.cache_session_info(session_index, info), DmqContents(relay_parent, para_id, messages) => self.requests_cache.cache_dmq_contents((relay_parent, para_id), messages), InboundHrmpChannelsContents(relay_parent, para_id, contents) =>