Skip to content

Commit

Permalink
Inside publish (for a Serai transaction from the coordinator), use Re…
Browse files Browse the repository at this point in the history
…tiredDb over latest session

Not only is this more performant, the definition of retired won't be if a newer
session is active. It will be if the session has posted a slash report or the
stake for that session has unlocked.

Initial commit towards implementing SlashReports.
  • Loading branch information
kayabaNerve committed Jan 6, 2024
1 parent 1cff9b4 commit f3429ec
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
3 changes: 2 additions & 1 deletion coordinator/src/tests/tributary/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serai_client::{

use tokio::time::sleep;

use serai_db::{Db, MemDb, DbTxn};
use serai_db::{Get, DbTxn, Db, MemDb};

use processor_messages::{
key_gen::{self, KeyGenId},
Expand Down Expand Up @@ -349,6 +349,7 @@ async fn dkg_test() {
impl PublishSeraiTransaction for CheckPublishSetKeys {
async fn publish_set_keys(
&self,
_db: &(impl Sync + Get),
set: ValidatorSet,
removed: Vec<SeraiAddress>,
key_pair: KeyPair,
Expand Down
1 change: 1 addition & 0 deletions coordinator/src/tests/tributary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod sync;
impl PublishSeraiTransaction for () {
async fn publish_set_keys(
&self,
_db: &(impl Sync + serai_db::Get),
_set: ValidatorSet,
_removed: Vec<SeraiAddress>,
_key_pair: KeyPair,
Expand Down
4 changes: 3 additions & 1 deletion coordinator/src/tributary/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ fn unflatten(
}

impl<
D: Db,
T: DbTxn,
Pro: Processors,
PST: PublishSeraiTransaction,
PTT: PTTTrait,
RID: RIDTrait,
P: P2p,
> TributaryBlockHandler<'_, T, Pro, PST, PTT, RID, P>
> TributaryBlockHandler<'_, D, T, Pro, PST, PTT, RID, P>
{
fn accumulate(
&mut self,
Expand Down Expand Up @@ -548,6 +549,7 @@ impl<
self
.publish_serai_tx
.publish_set_keys(
self.db,
self.spec.set(),
removed.into_iter().map(|key| key.to_bytes().into()).collect(),
key_pair,
Expand Down
44 changes: 24 additions & 20 deletions coordinator/src/tributary/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl<
pub trait PublishSeraiTransaction {
async fn publish_set_keys(
&self,
db: &(impl Sync + Get),
set: ValidatorSet,
removed: Vec<SeraiAddress>,
key_pair: KeyPair,
Expand All @@ -84,6 +85,7 @@ mod impl_pst_for_serai {
($Meta: ty, $check: ident) => {
async fn publish(
serai: &Serai,
db: &impl Get,
set: ValidatorSet,
tx: serai_client::Transaction,
meta: $Meta,
Expand All @@ -95,29 +97,25 @@ mod impl_pst_for_serai {
// creation
// TODO2: Differentiate connection errors from invariants
Err(e) => {
// The following block is irrelevant, and can/likely will fail, if we're publishing
// a TX for an old session
// If we're on a newer session, move on
if crate::RetiredTributaryDb::get(db, set).is_some() {
log::warn!("trying to publish a TX relevant to set {set:?} which isn't the latest");
return false;
}

if let Ok(serai) = serai.as_of_latest_finalized_block().await {
let serai = serai.validator_sets();

// The following block is irrelevant, and can/likely will fail, if we're publishing
// a TX for an old session
// If we're on a newer session, move on
if let Ok(Some(current_session)) = serai.session(set.network).await {
if current_session.0 > set.session.0 {
log::warn!(
"trying to publish a TX relevant to set {set:?} which isn't the latest"
);
return false;
}
}

// Check if someone else published the TX in question
if $check(serai, set, meta).await {
return false;
}

log::error!("couldn't connect to Serai node to publish TX: {e:?}");
tokio::time::sleep(core::time::Duration::from_secs(5)).await;
}

log::error!("couldn't connect to Serai node to publish TX: {e:?}");
tokio::time::sleep(core::time::Duration::from_secs(5)).await;
}
}
}
Expand All @@ -129,6 +127,7 @@ mod impl_pst_for_serai {
impl PublishSeraiTransaction for Serai {
async fn publish_set_keys(
&self,
db: &(impl Sync + Get),
set: ValidatorSet,
removed: Vec<SeraiAddress>,
key_pair: KeyPair,
Expand All @@ -143,7 +142,7 @@ mod impl_pst_for_serai {
false
}
common_pst!((), check);
if publish(self, set, tx, ()).await {
if publish(self, db, set, tx, ()).await {
log::info!("published set keys for {set:?}");
}
}
Expand All @@ -163,13 +162,15 @@ impl<FPtt: Send + Future<Output = ()>, F: Sync + Fn(Transaction) -> FPtt> PTTTra

pub struct TributaryBlockHandler<
'a,
D: Db,
T: DbTxn,
Pro: Processors,
PST: PublishSeraiTransaction,
PTT: PTTTrait,
RID: RIDTrait,
P: P2p,
> {
pub db: &'a D,
pub txn: &'a mut T,
pub our_key: &'a Zeroizing<<Ristretto as Ciphersuite>::F>,
pub recognized_id: &'a RID,
Expand All @@ -183,13 +184,14 @@ pub struct TributaryBlockHandler<
}

impl<
D: Db,
T: DbTxn,
Pro: Processors,
PST: PublishSeraiTransaction,
PTT: PTTTrait,
RID: RIDTrait,
P: P2p,
> TributaryBlockHandler<'_, T, Pro, PST, PTT, RID, P>
> TributaryBlockHandler<'_, D, T, Pro, PST, PTT, RID, P>
{
pub fn fatal_slash(&mut self, slashing: [u8; 32], reason: &str) {
let genesis = self.spec.genesis();
Expand All @@ -204,7 +206,7 @@ impl<
// Tributary post-DKG
// https://github.com/serai-dex/serai/issues/426

async fn handle<D: Db>(mut self) {
async fn handle(mut self) {
log::info!("found block for Tributary {:?}", self.spec.set());

let transactions = self.block.transactions.clone();
Expand Down Expand Up @@ -581,9 +583,11 @@ pub(crate) async fn handle_new_blocks<
}
}

let mut txn = db.txn();
let mut db_clone = db.clone();
let mut txn = db_clone.txn();
TributaryBlockNumber::set(&mut txn, next, &block_number);
(TributaryBlockHandler {
db,
txn: &mut txn,
spec,
our_key: key,
Expand All @@ -595,7 +599,7 @@ pub(crate) async fn handle_new_blocks<
block_number,
_p2p: PhantomData::<P>,
})
.handle::<D>()
.handle()
.await;
last_block = next;
LastHandledBlock::set(&mut txn, genesis, &next);
Expand Down

0 comments on commit f3429ec

Please sign in to comment.