diff --git a/coordinator/cosign/src/intend.rs b/coordinator/cosign/src/intend.rs index 7466ae5a..db38c905 100644 --- a/coordinator/cosign/src/intend.rs +++ b/coordinator/cosign/src/intend.rs @@ -78,7 +78,7 @@ impl ContinuallyRan for CosignIntendTask { // Check we are indexing a linear chain if (block_number > 1) && (<[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")) { Err(format!( @@ -86,14 +86,16 @@ impl ContinuallyRan for CosignIntendTask { 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); // If this is notable, it creates a new global session, which we index into the database // now 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 global_session = GlobalSession::id(sets_and_keys.iter().map(|(set, _key)| *set).collect()); @@ -159,7 +161,7 @@ impl ContinuallyRan for CosignIntendTask { &CosignIntent { global_session: global_session_for_this_block, block_number, - block_hash: block.hash(), + block_hash, notable: has_events == HasEvents::Notable, }, ); diff --git a/coordinator/cosign/src/lib.rs b/coordinator/cosign/src/lib.rs index 29420d56..7d909712 100644 --- a/coordinator/cosign/src/lib.rs +++ b/coordinator/cosign/src/lib.rs @@ -127,7 +127,8 @@ create_db! { // The following are populated by the intend task and used throughout the library // 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. GlobalSessions: (global_session: [u8; 32]) -> GlobalSession, // The last block to be cosigned by a global session. @@ -270,17 +271,24 @@ impl Cosigning { 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, Faulted> { if block_number > Self::latest_cosigned_block_number(getter)? { return Ok(None); } 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 { + SubstrateBlockNumber::get(getter, block_hash) + } + /// 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 @@ -345,7 +353,7 @@ impl Cosigning { let network = cosign.cosigner; // 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); }; let faulty = cosign.block_hash != our_block_hash;