From 5b74fc8ac1dfd363c83e73df978fe18d3fcbe126 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Mon, 30 Dec 2024 05:33:53 -0500 Subject: [PATCH] Merge ExternalKeyForSessionToSignBatch into InfoForBatch --- processor/scanner/src/report/db.rs | 46 ++++++++++---------------- processor/scanner/src/report/mod.rs | 17 ++-------- processor/scanner/src/substrate/mod.rs | 20 +++++------ 3 files changed, 28 insertions(+), 55 deletions(-) diff --git a/processor/scanner/src/report/db.rs b/processor/scanner/src/report/db.rs index b6503e86..209150d6 100644 --- a/processor/scanner/src/report/db.rs +++ b/processor/scanner/src/report/db.rs @@ -10,12 +10,14 @@ use serai_db::{Get, DbTxn, create_db}; use serai_primitives::Balance; use serai_validator_sets_primitives::Session; +use primitives::EncodableG; use crate::{ScannerFeed, KeyFor, AddressFor}; #[derive(BorshSerialize, BorshDeserialize)] -pub(crate) struct BatchInfo { +pub(crate) struct BatchInfo { pub(crate) block_number: u64, - pub(crate) publisher: Session, + pub(crate) session_to_sign_batch: Session, + pub(crate) external_key_for_session_to_sign_batch: K, pub(crate) in_instructions_hash: [u8; 32], } @@ -27,11 +29,7 @@ create_db!( NextBatchId: () -> u32, // The information needed to verify a batch - InfoForBatch: (batch: u32) -> BatchInfo, - - // The external key for the session which should sign a batch - // TODO: Merge this with InfoForBatch - ExternalKeyForSessionToSignBatch: (batch: u32) -> Vec, + InfoForBatch: (batch: u32) -> BatchInfo>, // The return addresses for the InInstructions within a Batch SerializedReturnAddresses: (batch: u32) -> Vec, @@ -65,37 +63,27 @@ impl ReportDb { txn: &mut impl DbTxn, id: u32, block_number: u64, - publisher: Session, + session_to_sign_batch: Session, + external_key_for_session_to_sign_batch: KeyFor, in_instructions_hash: [u8; 32], ) { - InfoForBatch::set(txn, id, &BatchInfo { block_number, publisher, in_instructions_hash }); - } - - pub(crate) fn take_info_for_batch(txn: &mut impl DbTxn, id: u32) -> Option { - InfoForBatch::take(txn, id) - } - - pub(crate) fn save_external_key_for_session_to_sign_batch( - txn: &mut impl DbTxn, - id: u32, - external_key_for_session_to_sign_batch: &KeyFor, - ) { - ExternalKeyForSessionToSignBatch::set( + InfoForBatch::set( txn, id, - &external_key_for_session_to_sign_batch.to_bytes().as_ref().to_vec(), + &BatchInfo { + block_number, + session_to_sign_batch, + external_key_for_session_to_sign_batch: EncodableG(external_key_for_session_to_sign_batch), + in_instructions_hash, + }, ); } - pub(crate) fn take_external_key_for_session_to_sign_batch( + pub(crate) fn take_info_for_batch( txn: &mut impl DbTxn, id: u32, - ) -> Option> { - ExternalKeyForSessionToSignBatch::get(txn, id).map(|key_vec| { - let mut key = as GroupEncoding>::Repr::default(); - key.as_mut().copy_from_slice(&key_vec); - KeyFor::::from_bytes(&key).unwrap() - }) + ) -> Option>>> { + InfoForBatch::take(txn, id) } pub(crate) fn save_return_information( diff --git a/processor/scanner/src/report/mod.rs b/processor/scanner/src/report/mod.rs index f5208460..1747fde3 100644 --- a/processor/scanner/src/report/mod.rs +++ b/processor/scanner/src/report/mod.rs @@ -7,7 +7,7 @@ use serai_db::{DbTxn, Db}; use serai_in_instructions_primitives::{MAX_BATCH_SIZE, Batch}; -use primitives::task::ContinuallyRan; +use primitives::{EncodableG, task::ContinuallyRan}; use crate::{ db::{Returnable, ScannerGlobalDb, InInstructionData, ScanToReportDb, Batches, BatchesToSign}, scan::next_to_scan_for_outputs_block, @@ -21,17 +21,10 @@ use db::ReportDb; pub(crate) fn take_info_for_batch( txn: &mut impl DbTxn, id: u32, -) -> Option { +) -> Option>>> { ReportDb::::take_info_for_batch(txn, id) } -pub(crate) fn take_external_key_for_session_to_sign_batch( - txn: &mut impl DbTxn, - id: u32, -) -> Option> { - ReportDb::::take_external_key_for_session_to_sign_batch(txn, id) -} - pub(crate) fn take_return_information( txn: &mut impl DbTxn, id: u32, @@ -151,13 +144,9 @@ impl ContinuallyRan for ReportTask { batch.id, block_number, session_to_sign_batch, + external_key_for_session_to_sign_batch, Blake2b::::digest(batch.instructions.encode()).into(), ); - ReportDb::::save_external_key_for_session_to_sign_batch( - &mut txn, - batch.id, - &external_key_for_session_to_sign_batch, - ); ReportDb::::save_return_information(&mut txn, batch.id, return_information); } diff --git a/processor/scanner/src/substrate/mod.rs b/processor/scanner/src/substrate/mod.rs index aced7d53..106397b0 100644 --- a/processor/scanner/src/substrate/mod.rs +++ b/processor/scanner/src/substrate/mod.rs @@ -81,7 +81,8 @@ impl ContinuallyRan for SubstrateTask { // Check if we have the information for this batch let Some(report::BatchInfo { block_number, - publisher: expected_publisher, + session_to_sign_batch, + external_key_for_session_to_sign_batch, in_instructions_hash: expected_in_instructions_hash, }) = report::take_info_for_batch::(&mut txn, batch_id) else { @@ -90,7 +91,7 @@ impl ContinuallyRan for SubstrateTask { return Ok(made_progress); }; assert_eq!( - publisher, expected_publisher, + publisher, session_to_sign_batch, "batch acknowledged on-chain was acknowledged by an unexpected publisher" ); assert_eq!( @@ -98,16 +99,11 @@ impl ContinuallyRan for SubstrateTask { "batch acknowledged on-chain was distinct" ); - { - let external_key_for_session_to_sign_batch = - report::take_external_key_for_session_to_sign_batch::(&mut txn, batch_id) - .unwrap(); - AcknowledgedBatches::send( - &mut txn, - &external_key_for_session_to_sign_batch, - batch_id, - ); - } + AcknowledgedBatches::send( + &mut txn, + &external_key_for_session_to_sign_batch.0, + batch_id, + ); // Mark we made progress and handle this made_progress = true;