Have serai-cosign index finalized blocks' numbers

This commit is contained in:
Luke Parker
2025-01-09 06:57:26 -05:00
parent 893a24a1cc
commit 9b0b5fd1e2
2 changed files with 18 additions and 8 deletions

View File

@@ -78,7 +78,7 @@ impl<D: Db> ContinuallyRan for CosignIntendTask<D> {
// Check we are indexing a linear chain // Check we are indexing a linear chain
if (block_number > 1) && if (block_number > 1) &&
(<[u8; 32]>::from(block.header.parent_hash) != (<[u8; 32]>::from(block.header.parent_hash) !=
SubstrateBlocks::get(&txn, block_number - 1) SubstrateBlockHash::get(&txn, block_number - 1)
.expect("indexing a block but haven't indexed its parent")) .expect("indexing a block but haven't indexed its parent"))
{ {
Err(format!( Err(format!(
@@ -86,14 +86,16 @@ impl<D: Db> ContinuallyRan for CosignIntendTask<D> {
block_number - 1 block_number - 1
))?; ))?;
} }
SubstrateBlocks::set(&mut txn, block_number, &block.hash()); let block_hash = block.hash();
SubstrateBlockHash::set(&mut txn, block_number, &block_hash);
SubstrateBlockNumber::set(&mut txn, block_hash, &block_number);
let global_session_for_this_block = LatestGlobalSessionIntended::get(&txn); let global_session_for_this_block = LatestGlobalSessionIntended::get(&txn);
// If this is notable, it creates a new global session, which we index into the database // If this is notable, it creates a new global session, which we index into the database
// now // now
if has_events == HasEvents::Notable { if has_events == HasEvents::Notable {
let serai = self.serai.as_of(block.hash()); let serai = self.serai.as_of(block_hash);
let sets_and_keys = cosigning_sets(&serai).await?; let sets_and_keys = cosigning_sets(&serai).await?;
let global_session = let global_session =
GlobalSession::id(sets_and_keys.iter().map(|(set, _key)| *set).collect()); GlobalSession::id(sets_and_keys.iter().map(|(set, _key)| *set).collect());
@@ -159,7 +161,7 @@ impl<D: Db> ContinuallyRan for CosignIntendTask<D> {
&CosignIntent { &CosignIntent {
global_session: global_session_for_this_block, global_session: global_session_for_this_block,
block_number, block_number,
block_hash: block.hash(), block_hash,
notable: has_events == HasEvents::Notable, notable: has_events == HasEvents::Notable,
}, },
); );

View File

@@ -127,7 +127,8 @@ create_db! {
// The following are populated by the intend task and used throughout the library // The following are populated by the intend task and used throughout the library
// An index of Substrate blocks // An index of Substrate blocks
SubstrateBlocks: (block_number: u64) -> [u8; 32], SubstrateBlockHash: (block_number: u64) -> [u8; 32],
SubstrateBlockNumber: (block_hash: [u8; 32]) -> u64,
// A mapping from a global session's ID to its relevant information. // A mapping from a global session's ID to its relevant information.
GlobalSessions: (global_session: [u8; 32]) -> GlobalSession, GlobalSessions: (global_session: [u8; 32]) -> GlobalSession,
// The last block to be cosigned by a global session. // The last block to be cosigned by a global session.
@@ -270,17 +271,24 @@ impl<D: Db> Cosigning<D> {
Ok(LatestCosignedBlockNumber::get(getter).unwrap_or(0)) Ok(LatestCosignedBlockNumber::get(getter).unwrap_or(0))
} }
/// Fetch an cosigned Substrate block by its block number. /// Fetch a cosigned Substrate block's hash by its block number.
pub fn cosigned_block(getter: &impl Get, block_number: u64) -> Result<Option<[u8; 32]>, Faulted> { pub fn cosigned_block(getter: &impl Get, block_number: u64) -> Result<Option<[u8; 32]>, Faulted> {
if block_number > Self::latest_cosigned_block_number(getter)? { if block_number > Self::latest_cosigned_block_number(getter)? {
return Ok(None); return Ok(None);
} }
Ok(Some( Ok(Some(
SubstrateBlocks::get(getter, block_number).expect("cosigned block but didn't index it"), SubstrateBlockHash::get(getter, block_number).expect("cosigned block but didn't index it"),
)) ))
} }
/// Fetch a finalized block's number by its hash.
///
/// This block is not guaranteed to be cosigned.
pub fn finalized_block_number(getter: &impl Get, block_hash: [u8; 32]) -> Option<u64> {
SubstrateBlockNumber::get(getter, block_hash)
}
/// Fetch the notable cosigns for a global session in order to respond to requests. /// Fetch the notable cosigns for a global session in order to respond to requests.
/// ///
/// If this global session hasn't produced any notable cosigns, this will return the latest /// If this global session hasn't produced any notable cosigns, this will return the latest
@@ -345,7 +353,7 @@ impl<D: Db> Cosigning<D> {
let network = cosign.cosigner; let network = cosign.cosigner;
// Check our indexed blockchain includes a block with this block number // Check our indexed blockchain includes a block with this block number
let Some(our_block_hash) = SubstrateBlocks::get(&self.db, cosign.block_number) else { let Some(our_block_hash) = SubstrateBlockHash::get(&self.db, cosign.block_number) else {
return Ok(true); return Ok(true);
}; };
let faulty = cosign.block_hash != our_block_hash; let faulty = cosign.block_hash != our_block_hash;