Merge ExternalKeyForSessionToSignBatch into InfoForBatch

This commit is contained in:
Luke Parker
2024-12-30 05:33:53 -05:00
parent e67e301fc2
commit 5b74fc8ac1
3 changed files with 28 additions and 55 deletions

View File

@@ -10,12 +10,14 @@ use serai_db::{Get, DbTxn, create_db};
use serai_primitives::Balance; use serai_primitives::Balance;
use serai_validator_sets_primitives::Session; use serai_validator_sets_primitives::Session;
use primitives::EncodableG;
use crate::{ScannerFeed, KeyFor, AddressFor}; use crate::{ScannerFeed, KeyFor, AddressFor};
#[derive(BorshSerialize, BorshDeserialize)] #[derive(BorshSerialize, BorshDeserialize)]
pub(crate) struct BatchInfo { pub(crate) struct BatchInfo<K: BorshSerialize> {
pub(crate) block_number: u64, 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], pub(crate) in_instructions_hash: [u8; 32],
} }
@@ -27,11 +29,7 @@ create_db!(
NextBatchId: () -> u32, NextBatchId: () -> u32,
// The information needed to verify a batch // The information needed to verify a batch
InfoForBatch: (batch: u32) -> BatchInfo, InfoForBatch: <G: GroupEncoding>(batch: u32) -> BatchInfo<EncodableG<G>>,
// The external key for the session which should sign a batch
// TODO: Merge this with InfoForBatch
ExternalKeyForSessionToSignBatch: (batch: u32) -> Vec<u8>,
// The return addresses for the InInstructions within a Batch // The return addresses for the InInstructions within a Batch
SerializedReturnAddresses: (batch: u32) -> Vec<u8>, SerializedReturnAddresses: (batch: u32) -> Vec<u8>,
@@ -65,37 +63,27 @@ impl<S: ScannerFeed> ReportDb<S> {
txn: &mut impl DbTxn, txn: &mut impl DbTxn,
id: u32, id: u32,
block_number: u64, block_number: u64,
publisher: Session, session_to_sign_batch: Session,
external_key_for_session_to_sign_batch: KeyFor<S>,
in_instructions_hash: [u8; 32], in_instructions_hash: [u8; 32],
) { ) {
InfoForBatch::set(txn, id, &BatchInfo { block_number, publisher, in_instructions_hash }); InfoForBatch::set(
}
pub(crate) fn take_info_for_batch(txn: &mut impl DbTxn, id: u32) -> Option<BatchInfo> {
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<S>,
) {
ExternalKeyForSessionToSignBatch::set(
txn, txn,
id, 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, txn: &mut impl DbTxn,
id: u32, id: u32,
) -> Option<KeyFor<S>> { ) -> Option<BatchInfo<EncodableG<KeyFor<S>>>> {
ExternalKeyForSessionToSignBatch::get(txn, id).map(|key_vec| { InfoForBatch::take(txn, id)
let mut key = <KeyFor<S> as GroupEncoding>::Repr::default();
key.as_mut().copy_from_slice(&key_vec);
KeyFor::<S>::from_bytes(&key).unwrap()
})
} }
pub(crate) fn save_return_information( pub(crate) fn save_return_information(

View File

@@ -7,7 +7,7 @@ use serai_db::{DbTxn, Db};
use serai_in_instructions_primitives::{MAX_BATCH_SIZE, Batch}; use serai_in_instructions_primitives::{MAX_BATCH_SIZE, Batch};
use primitives::task::ContinuallyRan; use primitives::{EncodableG, task::ContinuallyRan};
use crate::{ use crate::{
db::{Returnable, ScannerGlobalDb, InInstructionData, ScanToReportDb, Batches, BatchesToSign}, db::{Returnable, ScannerGlobalDb, InInstructionData, ScanToReportDb, Batches, BatchesToSign},
scan::next_to_scan_for_outputs_block, scan::next_to_scan_for_outputs_block,
@@ -21,17 +21,10 @@ use db::ReportDb;
pub(crate) fn take_info_for_batch<S: ScannerFeed>( pub(crate) fn take_info_for_batch<S: ScannerFeed>(
txn: &mut impl DbTxn, txn: &mut impl DbTxn,
id: u32, id: u32,
) -> Option<BatchInfo> { ) -> Option<BatchInfo<EncodableG<KeyFor<S>>>> {
ReportDb::<S>::take_info_for_batch(txn, id) ReportDb::<S>::take_info_for_batch(txn, id)
} }
pub(crate) fn take_external_key_for_session_to_sign_batch<S: ScannerFeed>(
txn: &mut impl DbTxn,
id: u32,
) -> Option<KeyFor<S>> {
ReportDb::<S>::take_external_key_for_session_to_sign_batch(txn, id)
}
pub(crate) fn take_return_information<S: ScannerFeed>( pub(crate) fn take_return_information<S: ScannerFeed>(
txn: &mut impl DbTxn, txn: &mut impl DbTxn,
id: u32, id: u32,
@@ -151,13 +144,9 @@ impl<D: Db, S: ScannerFeed> ContinuallyRan for ReportTask<D, S> {
batch.id, batch.id,
block_number, block_number,
session_to_sign_batch, session_to_sign_batch,
external_key_for_session_to_sign_batch,
Blake2b::<U32>::digest(batch.instructions.encode()).into(), Blake2b::<U32>::digest(batch.instructions.encode()).into(),
); );
ReportDb::<S>::save_external_key_for_session_to_sign_batch(
&mut txn,
batch.id,
&external_key_for_session_to_sign_batch,
);
ReportDb::<S>::save_return_information(&mut txn, batch.id, return_information); ReportDb::<S>::save_return_information(&mut txn, batch.id, return_information);
} }

View File

@@ -81,7 +81,8 @@ impl<D: Db, S: ScannerFeed> ContinuallyRan for SubstrateTask<D, S> {
// Check if we have the information for this batch // Check if we have the information for this batch
let Some(report::BatchInfo { let Some(report::BatchInfo {
block_number, block_number,
publisher: expected_publisher, session_to_sign_batch,
external_key_for_session_to_sign_batch,
in_instructions_hash: expected_in_instructions_hash, in_instructions_hash: expected_in_instructions_hash,
}) = report::take_info_for_batch::<S>(&mut txn, batch_id) }) = report::take_info_for_batch::<S>(&mut txn, batch_id)
else { else {
@@ -90,7 +91,7 @@ impl<D: Db, S: ScannerFeed> ContinuallyRan for SubstrateTask<D, S> {
return Ok(made_progress); return Ok(made_progress);
}; };
assert_eq!( assert_eq!(
publisher, expected_publisher, publisher, session_to_sign_batch,
"batch acknowledged on-chain was acknowledged by an unexpected publisher" "batch acknowledged on-chain was acknowledged by an unexpected publisher"
); );
assert_eq!( assert_eq!(
@@ -98,16 +99,11 @@ impl<D: Db, S: ScannerFeed> ContinuallyRan for SubstrateTask<D, S> {
"batch acknowledged on-chain was distinct" "batch acknowledged on-chain was distinct"
); );
{
let external_key_for_session_to_sign_batch =
report::take_external_key_for_session_to_sign_batch::<S>(&mut txn, batch_id)
.unwrap();
AcknowledgedBatches::send( AcknowledgedBatches::send(
&mut txn, &mut txn,
&external_key_for_session_to_sign_batch, &external_key_for_session_to_sign_batch.0,
batch_id, batch_id,
); );
}
// Mark we made progress and handle this // Mark we made progress and handle this
made_progress = true; made_progress = true;