2024-12-22 06:41:55 -05:00
|
|
|
use core::future::Future;
|
2025-01-10 01:20:26 -05:00
|
|
|
use std::{sync::Arc, collections::HashMap};
|
2024-12-22 06:41:55 -05:00
|
|
|
|
2024-12-25 21:19:04 -05:00
|
|
|
use serai_client::{
|
|
|
|
|
primitives::{SeraiAddress, Amount},
|
|
|
|
|
validator_sets::primitives::ValidatorSet,
|
|
|
|
|
Serai,
|
|
|
|
|
};
|
2024-12-22 06:41:55 -05:00
|
|
|
|
|
|
|
|
use serai_db::*;
|
|
|
|
|
use serai_task::ContinuallyRan;
|
|
|
|
|
|
|
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
|
|
create_db!(
|
|
|
|
|
CosignIntend {
|
|
|
|
|
ScanCosignFrom: () -> u64,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-12-25 01:45:37 -05:00
|
|
|
#[derive(Debug, BorshSerialize, BorshDeserialize)]
|
|
|
|
|
pub(crate) struct BlockEventData {
|
|
|
|
|
pub(crate) block_number: u64,
|
|
|
|
|
pub(crate) has_events: HasEvents,
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-22 06:41:55 -05:00
|
|
|
db_channel! {
|
|
|
|
|
CosignIntendChannels {
|
2024-12-26 00:15:49 -05:00
|
|
|
GlobalSessionsChannel: () -> ([u8; 32], GlobalSession),
|
2024-12-25 01:45:37 -05:00
|
|
|
BlockEvents: () -> BlockEventData,
|
2024-12-22 06:41:55 -05:00
|
|
|
IntendedCosigns: (set: ValidatorSet) -> CosignIntent,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn block_has_events_justifying_a_cosign(
|
|
|
|
|
serai: &Serai,
|
2024-12-25 01:45:37 -05:00
|
|
|
block_number: u64,
|
2024-12-25 02:11:05 -05:00
|
|
|
) -> Result<(Block, HasEvents), String> {
|
2024-12-25 01:45:37 -05:00
|
|
|
let block = serai
|
|
|
|
|
.finalized_block_by_number(block_number)
|
2024-12-25 02:11:05 -05:00
|
|
|
.await
|
|
|
|
|
.map_err(|e| format!("{e:?}"))?
|
|
|
|
|
.ok_or_else(|| "couldn't get block which should've been finalized".to_string())?;
|
2024-12-25 01:45:37 -05:00
|
|
|
let serai = serai.as_of(block.hash());
|
2024-12-22 06:41:55 -05:00
|
|
|
|
2024-12-25 02:11:05 -05:00
|
|
|
if !serai.validator_sets().key_gen_events().await.map_err(|e| format!("{e:?}"))?.is_empty() {
|
2024-12-25 01:45:37 -05:00
|
|
|
return Ok((block, HasEvents::Notable));
|
2024-12-22 06:41:55 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-25 02:11:05 -05:00
|
|
|
if !serai.coins().burn_with_instruction_events().await.map_err(|e| format!("{e:?}"))?.is_empty() {
|
2024-12-25 01:45:37 -05:00
|
|
|
return Ok((block, HasEvents::NonNotable));
|
2024-12-22 06:41:55 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-25 01:45:37 -05:00
|
|
|
Ok((block, HasEvents::No))
|
2024-12-22 06:41:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A task to determine which blocks we should intend to cosign.
|
|
|
|
|
pub(crate) struct CosignIntendTask<D: Db> {
|
|
|
|
|
pub(crate) db: D,
|
2025-01-10 01:20:26 -05:00
|
|
|
pub(crate) serai: Arc<Serai>,
|
2024-12-22 06:41:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<D: Db> ContinuallyRan for CosignIntendTask<D> {
|
|
|
|
|
fn run_iteration(&mut self) -> impl Send + Future<Output = Result<bool, String>> {
|
|
|
|
|
async move {
|
|
|
|
|
let start_block_number = ScanCosignFrom::get(&self.db).unwrap_or(1);
|
|
|
|
|
let latest_block_number =
|
|
|
|
|
self.serai.latest_finalized_block().await.map_err(|e| format!("{e:?}"))?.number();
|
|
|
|
|
|
|
|
|
|
for block_number in start_block_number ..= latest_block_number {
|
|
|
|
|
let mut txn = self.db.txn();
|
|
|
|
|
|
2024-12-25 01:45:37 -05:00
|
|
|
let (block, mut has_events) =
|
|
|
|
|
block_has_events_justifying_a_cosign(&self.serai, block_number)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| format!("{e:?}"))?;
|
2024-12-22 06:41:55 -05:00
|
|
|
|
2024-12-25 21:19:04 -05:00
|
|
|
// Check we are indexing a linear chain
|
|
|
|
|
if (block_number > 1) &&
|
|
|
|
|
(<[u8; 32]>::from(block.header.parent_hash) !=
|
2025-01-09 06:57:26 -05:00
|
|
|
SubstrateBlockHash::get(&txn, block_number - 1)
|
2024-12-25 21:19:04 -05:00
|
|
|
.expect("indexing a block but haven't indexed its parent"))
|
|
|
|
|
{
|
|
|
|
|
Err(format!(
|
|
|
|
|
"node's block #{block_number} doesn't build upon the block #{} prior indexed",
|
|
|
|
|
block_number - 1
|
|
|
|
|
))?;
|
|
|
|
|
}
|
2025-01-09 06:57:26 -05:00
|
|
|
let block_hash = block.hash();
|
|
|
|
|
SubstrateBlockHash::set(&mut txn, block_number, &block_hash);
|
2024-12-25 21:19:04 -05:00
|
|
|
|
2024-12-25 23:21:25 -05:00
|
|
|
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 {
|
2025-01-09 06:57:26 -05:00
|
|
|
let serai = self.serai.as_of(block_hash);
|
2024-12-25 23:21:25 -05:00
|
|
|
let sets_and_keys = cosigning_sets(&serai).await?;
|
|
|
|
|
let global_session =
|
|
|
|
|
GlobalSession::id(sets_and_keys.iter().map(|(set, _key)| *set).collect());
|
|
|
|
|
|
|
|
|
|
let mut sets = Vec::with_capacity(sets_and_keys.len());
|
|
|
|
|
let mut keys = HashMap::with_capacity(sets_and_keys.len());
|
|
|
|
|
let mut stakes = HashMap::with_capacity(sets_and_keys.len());
|
|
|
|
|
let mut total_stake = 0;
|
|
|
|
|
for (set, key) in &sets_and_keys {
|
|
|
|
|
sets.push(*set);
|
|
|
|
|
keys.insert(set.network, SeraiAddress::from(*key));
|
|
|
|
|
let stake = serai
|
|
|
|
|
.validator_sets()
|
|
|
|
|
.total_allocated_stake(set.network)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| format!("{e:?}"))?
|
|
|
|
|
.unwrap_or(Amount(0))
|
|
|
|
|
.0;
|
|
|
|
|
stakes.insert(set.network, stake);
|
|
|
|
|
total_stake += stake;
|
|
|
|
|
}
|
|
|
|
|
if total_stake == 0 {
|
|
|
|
|
Err(format!("cosigning sets for block #{block_number} had 0 stake in total"))?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let global_session_info = GlobalSession {
|
|
|
|
|
// This session starts cosigning after this block, as this block must be cosigned by
|
|
|
|
|
// the existing validators
|
|
|
|
|
start_block_number: block_number + 1,
|
|
|
|
|
sets,
|
|
|
|
|
keys,
|
|
|
|
|
stakes,
|
|
|
|
|
total_stake,
|
|
|
|
|
};
|
2024-12-26 00:15:49 -05:00
|
|
|
GlobalSessions::set(&mut txn, global_session, &global_session_info);
|
|
|
|
|
if let Some(ending_global_session) = global_session_for_this_block {
|
2024-12-25 23:21:25 -05:00
|
|
|
GlobalSessionsLastBlock::set(&mut txn, ending_global_session, &block_number);
|
|
|
|
|
}
|
2024-12-26 00:15:49 -05:00
|
|
|
LatestGlobalSessionIntended::set(&mut txn, &global_session);
|
|
|
|
|
GlobalSessionsChannel::send(&mut txn, &(global_session, global_session_info));
|
2024-12-25 23:21:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there isn't anyone available to cosign this block, meaning it'll never be cosigned,
|
|
|
|
|
// we flag it as not having any events requiring cosigning so we don't attempt to
|
|
|
|
|
// sign/require a cosign for it
|
|
|
|
|
if global_session_for_this_block.is_none() {
|
|
|
|
|
has_events = HasEvents::No;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-22 06:41:55 -05:00
|
|
|
match has_events {
|
|
|
|
|
HasEvents::Notable | HasEvents::NonNotable => {
|
2024-12-26 00:15:49 -05:00
|
|
|
let global_session_for_this_block = global_session_for_this_block
|
|
|
|
|
.expect("global session for this block was None but still attempting to cosign it");
|
|
|
|
|
let global_session_info = GlobalSessions::get(&txn, global_session_for_this_block)
|
|
|
|
|
.expect("last global session intended wasn't saved to the database");
|
2024-12-25 23:21:25 -05:00
|
|
|
|
|
|
|
|
// Tell each set of their expectation to cosign this block
|
|
|
|
|
for set in global_session_info.sets {
|
|
|
|
|
log::debug!("{:?} will be cosigning block #{block_number}", set);
|
|
|
|
|
IntendedCosigns::send(
|
2024-12-25 21:19:04 -05:00
|
|
|
&mut txn,
|
2024-12-25 23:21:25 -05:00
|
|
|
set,
|
|
|
|
|
&CosignIntent {
|
|
|
|
|
global_session: global_session_for_this_block,
|
|
|
|
|
block_number,
|
2025-01-09 06:57:26 -05:00
|
|
|
block_hash,
|
2024-12-25 23:21:25 -05:00
|
|
|
notable: has_events == HasEvents::Notable,
|
|
|
|
|
},
|
2024-12-25 21:19:04 -05:00
|
|
|
);
|
2024-12-22 06:41:55 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
HasEvents::No => {}
|
|
|
|
|
}
|
2024-12-25 23:21:25 -05:00
|
|
|
|
2024-12-22 06:41:55 -05:00
|
|
|
// Populate a singular feed with every block's status for the evluator to work off of
|
2024-12-25 23:21:25 -05:00
|
|
|
BlockEvents::send(&mut txn, &(BlockEventData { block_number, has_events }));
|
2024-12-22 06:41:55 -05:00
|
|
|
// Mark this block as handled, meaning we should scan from the next block moving on
|
|
|
|
|
ScanCosignFrom::set(&mut txn, &(block_number + 1));
|
|
|
|
|
txn.commit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(start_block_number <= latest_block_number)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|