mirror of
https://github.com/serai-dex/serai.git
synced 2024-12-22 19:49:22 +00:00
Bound cosign work and ensure it progress forward even when cosigns don't occur
Should resolve the DB load observed on testnet.
This commit is contained in:
parent
97f433c694
commit
2347bf5fd3
1 changed files with 31 additions and 2 deletions
|
@ -41,6 +41,7 @@ enum HasEvents {
|
||||||
|
|
||||||
create_db!(
|
create_db!(
|
||||||
SubstrateCosignDb {
|
SubstrateCosignDb {
|
||||||
|
ScanCosignFrom: () -> u64,
|
||||||
IntendedCosign: () -> (u64, Option<u64>),
|
IntendedCosign: () -> (u64, Option<u64>),
|
||||||
BlockHasEvents: (block: u64) -> HasEvents,
|
BlockHasEvents: (block: u64) -> HasEvents,
|
||||||
LatestCosignedBlock: () -> u64,
|
LatestCosignedBlock: () -> u64,
|
||||||
|
@ -178,7 +179,7 @@ async fn potentially_cosign_block(
|
||||||
which should be cosigned). Accordingly, it is necessary to call multiple times even if
|
which should be cosigned). Accordingly, it is necessary to call multiple times even if
|
||||||
`latest_number` doesn't change.
|
`latest_number` doesn't change.
|
||||||
*/
|
*/
|
||||||
pub async fn advance_cosign_protocol(
|
async fn advance_cosign_protocol_inner(
|
||||||
db: &mut impl Db,
|
db: &mut impl Db,
|
||||||
key: &Zeroizing<<Ristretto as Ciphersuite>::F>,
|
key: &Zeroizing<<Ristretto as Ciphersuite>::F>,
|
||||||
serai: &Serai,
|
serai: &Serai,
|
||||||
|
@ -227,7 +228,16 @@ pub async fn advance_cosign_protocol(
|
||||||
// A list of sets which are cosigning, along with a boolean of if we're in the set
|
// A list of sets which are cosigning, along with a boolean of if we're in the set
|
||||||
let mut cosigning = vec![];
|
let mut cosigning = vec![];
|
||||||
|
|
||||||
for block in (last_intended_to_cosign_block + 1) ..= latest_number {
|
// The consensus rules for this are `last_intended_to_cosign_block + 1`
|
||||||
|
let scan_start_block = last_intended_to_cosign_block + 1;
|
||||||
|
// As a practical optimization, we don't re-scan old blocks since old blocks are independent to
|
||||||
|
// new state
|
||||||
|
let scan_start_block = scan_start_block.max(ScanCosignFrom::get(&txn).unwrap_or(0));
|
||||||
|
for block in scan_start_block ..= latest_number {
|
||||||
|
// This TX is committed, always re-run this loop from immediately before this block
|
||||||
|
// That allows the below loop to break out on a block it wants to revisit later
|
||||||
|
ScanCosignFrom::set(&mut txn, &(scan_start_block - 1));
|
||||||
|
|
||||||
let actual_block = serai
|
let actual_block = serai
|
||||||
.finalized_block_by_number(block)
|
.finalized_block_by_number(block)
|
||||||
.await?
|
.await?
|
||||||
|
@ -297,3 +307,22 @@ pub async fn advance_cosign_protocol(
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn advance_cosign_protocol(
|
||||||
|
db: &mut impl Db,
|
||||||
|
key: &Zeroizing<<Ristretto as Ciphersuite>::F>,
|
||||||
|
serai: &Serai,
|
||||||
|
latest_number: u64,
|
||||||
|
) -> Result<(), SeraiError> {
|
||||||
|
loop {
|
||||||
|
let scan_from = ScanCosignFrom::get(db).unwrap_or(0);
|
||||||
|
// Only scan 1000 blocks at a time to limit a massive txn from forming
|
||||||
|
let scan_to = latest_number.min(scan_from + 1000);
|
||||||
|
advance_cosign_protocol_inner(db, key, serai, scan_to).await?;
|
||||||
|
// If we didn't limit the scan_to, break
|
||||||
|
if scan_to == latest_number {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue