From 9b8c8f8231d0beb28054109812e5c3ffbaf9dcc0 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Wed, 11 Sep 2024 09:12:00 -0400 Subject: [PATCH] Misc tidying of serai-db calls --- common/db/src/create_db.rs | 42 ++++++++++--------- processor/bitcoin/src/main.rs | 16 +++---- processor/scanner/src/db.rs | 19 ++++----- processor/scanner/src/report/db.rs | 7 +--- processor/scanner/src/substrate/db.rs | 7 ++-- .../utxo/transaction-chaining/src/db.rs | 4 +- 6 files changed, 46 insertions(+), 49 deletions(-) diff --git a/common/db/src/create_db.rs b/common/db/src/create_db.rs index 1fb52b1b..50fe51f7 100644 --- a/common/db/src/create_db.rs +++ b/common/db/src/create_db.rs @@ -47,9 +47,13 @@ macro_rules! create_db { }) => { $( #[derive(Clone, Debug)] - pub(crate) struct $field_name; - impl $field_name { - pub(crate) fn key$(<$($generic_name: $generic_type),+>)?($($arg: $arg_type),*) -> Vec { + pub(crate) struct $field_name$( + <$($generic_name: $generic_type),+> + )?$( + (core::marker::PhantomData<($($generic_name),+)>) + )?; + impl$(<$($generic_name: $generic_type),+>)? $field_name$(<$($generic_name),+>)? { + pub(crate) fn key($($arg: $arg_type),*) -> Vec { use scale::Encode; $crate::serai_db_key( stringify!($db_name).as_bytes(), @@ -57,38 +61,38 @@ macro_rules! create_db { ($($arg),*).encode() ) } - pub(crate) fn set$(<$($generic_name: $generic_type),+>)?( + pub(crate) fn set( txn: &mut impl DbTxn $(, $arg: $arg_type)*, data: &$field_type ) { - let key = $field_name::key$(::<$($generic_name),+>)?($($arg),*); + let key = Self::key($($arg),*); txn.put(&key, borsh::to_vec(data).unwrap()); } - pub(crate) fn get$(<$($generic_name: $generic_type),+>)?( + pub(crate) fn get( getter: &impl Get, $($arg: $arg_type),* ) -> Option<$field_type> { - getter.get($field_name::key$(::<$($generic_name),+>)?($($arg),*)).map(|data| { + getter.get(Self::key($($arg),*)).map(|data| { borsh::from_slice(data.as_ref()).unwrap() }) } // Returns a PhantomData of all generic types so if the generic was only used in the value, // not the keys, this doesn't have unused generic types #[allow(dead_code)] - pub(crate) fn del$(<$($generic_name: $generic_type),+>)?( + pub(crate) fn del( txn: &mut impl DbTxn $(, $arg: $arg_type)* ) -> core::marker::PhantomData<($($($generic_name),+)?)> { - txn.del(&$field_name::key$(::<$($generic_name),+>)?($($arg),*)); + txn.del(&Self::key($($arg),*)); core::marker::PhantomData } - pub(crate) fn take$(<$($generic_name: $generic_type),+>)?( + pub(crate) fn take( txn: &mut impl DbTxn $(, $arg: $arg_type)* ) -> Option<$field_type> { - let key = $field_name::key$(::<$($generic_name),+>)?($($arg),*); + let key = Self::key($($arg),*); let res = txn.get(&key).map(|data| borsh::from_slice(data.as_ref()).unwrap()); if res.is_some() { txn.del(key); @@ -119,14 +123,14 @@ macro_rules! db_channel { } } - impl $field_name { - pub(crate) fn send$(<$($generic_name: $generic_type),+>)?( + impl$(<$($generic_name: $generic_type),+>)? $field_name$(<$($generic_name),+>)? { + pub(crate) fn send( txn: &mut impl DbTxn $(, $arg: $arg_type)* , value: &$field_type ) { // Use index 0 to store the amount of messages - let messages_sent_key = $field_name::key$(::<$($generic_name),+>)?($($arg,)* 0); + let messages_sent_key = Self::key($($arg,)* 0); let messages_sent = txn.get(&messages_sent_key).map(|counter| { u32::from_le_bytes(counter.try_into().unwrap()) }).unwrap_or(0); @@ -137,22 +141,22 @@ macro_rules! db_channel { // at the same time let index_to_use = messages_sent + 2; - $field_name::set$(::<$($generic_name),+>)?(txn, $($arg,)* index_to_use, value); + Self::set(txn, $($arg,)* index_to_use, value); } - pub(crate) fn try_recv$(<$($generic_name: $generic_type),+>)?( + pub(crate) fn try_recv( txn: &mut impl DbTxn $(, $arg: $arg_type)* ) -> Option<$field_type> { - let messages_recvd_key = $field_name::key$(::<$($generic_name),+>)?($($arg,)* 1); + let messages_recvd_key = Self::key($($arg,)* 1); let messages_recvd = txn.get(&messages_recvd_key).map(|counter| { u32::from_le_bytes(counter.try_into().unwrap()) }).unwrap_or(0); let index_to_read = messages_recvd + 2; - let res = $field_name::get$(::<$($generic_name),+>)?(txn, $($arg,)* index_to_read); + let res = Self::get(txn, $($arg,)* index_to_read); if res.is_some() { - $field_name::del$(::<$($generic_name),+>)?(txn, $($arg,)* index_to_read); + Self::del(txn, $($arg,)* index_to_read); txn.put(&messages_recvd_key, (messages_recvd + 1).to_le_bytes()); } res diff --git a/processor/bitcoin/src/main.rs b/processor/bitcoin/src/main.rs index 136b89cb..f1f14082 100644 --- a/processor/bitcoin/src/main.rs +++ b/processor/bitcoin/src/main.rs @@ -111,14 +111,14 @@ async fn coordinator_loop( ); // Queue the key to be activated upon the next Batch - db::KeyToActivate::send::< + db::KeyToActivate::< <::ExternalNetworkCurve as Ciphersuite>::G, - >(txn, &key); + >::send(txn, &key); // Set the external key, as needed by the signers - db::ExternalKeyForSessionForSigners::set::< + db::ExternalKeyForSessionForSigners::< <::ExternalNetworkCurve as Ciphersuite>::G, - >(txn, session, &key); + >::set(txn, session, &key); // This isn't cheap yet only happens for the very first set of keys if scanner.is_none() { @@ -130,9 +130,9 @@ async fn coordinator_loop( // Since this session had its slashes reported, it has finished all its signature // protocols and has been fully retired. We retire it from the signers accordingly - let key = db::ExternalKeyForSessionForSigners::take::< + let key = db::ExternalKeyForSessionForSigners::< <::ExternalNetworkCurve as Ciphersuite>::G, - >(txn, session) + >::take(txn, session) .unwrap() .0; @@ -147,9 +147,9 @@ async fn coordinator_loop( } => { let mut txn = txn.take().unwrap(); let scanner = scanner.as_mut().unwrap(); - let key_to_activate = db::KeyToActivate::try_recv::< + let key_to_activate = db::KeyToActivate::< <::ExternalNetworkCurve as Ciphersuite>::G, - >(&mut txn) + >::try_recv(&mut txn) .map(|key| key.0); // This is a cheap call as it internally just queues this to be done later scanner.acknowledge_batch( diff --git a/processor/scanner/src/db.rs b/processor/scanner/src/db.rs index 5fcdc160..107616cc 100644 --- a/processor/scanner/src/db.rs +++ b/processor/scanner/src/db.rs @@ -107,7 +107,7 @@ create_db!( pub(crate) struct ScannerGlobalDb(PhantomData); impl ScannerGlobalDb { pub(crate) fn has_any_key_been_queued(getter: &impl Get) -> bool { - ActiveKeys::get::>>(getter).is_some() + ActiveKeys::>>::get(getter).is_some() } /// Queue a key. @@ -315,7 +315,7 @@ pub(crate) struct ReceiverScanData { db_channel! { ScannerScanEventuality { - ScannedBlock: (empty_key: ()) -> Vec, + ScannedBlock: () -> Vec, } } @@ -364,14 +364,14 @@ impl ScanToEventualityDb { for output in &data.returns { output.write(&mut buf).unwrap(); } - ScannedBlock::send(txn, (), &buf); + ScannedBlock::send(txn, &buf); } pub(crate) fn recv_scan_data( txn: &mut impl DbTxn, expected_block_number: u64, ) -> ReceiverScanData { let data = - ScannedBlock::try_recv(txn, ()).expect("receiving data for a scanned block not yet sent"); + ScannedBlock::try_recv(txn).expect("receiving data for a scanned block not yet sent"); let mut data = data.as_slice(); let block_number = { @@ -462,7 +462,7 @@ struct BlockBoundInInstructions { db_channel! { ScannerScanReport { - InInstructions: (empty_key: ()) -> BlockBoundInInstructions, + InInstructions: () -> BlockBoundInInstructions, } } @@ -484,7 +484,6 @@ impl ScanToReportDb { } InInstructions::send( txn, - (), &BlockBoundInInstructions { block_number, returnable_in_instructions: buf }, ); } @@ -493,7 +492,7 @@ impl ScanToReportDb { txn: &mut impl DbTxn, block_number: u64, ) -> InInstructionData { - let data = InInstructions::try_recv(txn, ()) + let data = InInstructions::try_recv(txn) .expect("receiving InInstructions for a scanned block not yet sent"); assert_eq!( block_number, data.block_number, @@ -556,7 +555,7 @@ mod _public_db { db_channel! { ScannerPublic { - Batches: (empty_key: ()) -> Batch, + Batches: () -> Batch, BatchesToSign: (key: &[u8]) -> Batch, AcknowledgedBatches: (key: &[u8]) -> u32, CompletedEventualities: (key: &[u8]) -> [u8; 32], @@ -570,12 +569,12 @@ mod _public_db { pub struct Batches; impl Batches { pub(crate) fn send(txn: &mut impl DbTxn, batch: &Batch) { - _public_db::Batches::send(txn, (), batch); + _public_db::Batches::send(txn, batch); } /// Receive a batch to publish. pub fn try_recv(txn: &mut impl DbTxn) -> Option { - _public_db::Batches::try_recv(txn, ()) + _public_db::Batches::try_recv(txn) } } diff --git a/processor/scanner/src/report/db.rs b/processor/scanner/src/report/db.rs index 10a3f6bb..186accac 100644 --- a/processor/scanner/src/report/db.rs +++ b/processor/scanner/src/report/db.rs @@ -54,9 +54,7 @@ impl ReportDb { } pub(crate) fn take_block_number_for_batch(txn: &mut impl DbTxn, id: u32) -> Option { - let block_number = BlockNumberForBatch::get(txn, id)?; - BlockNumberForBatch::del(txn, id); - Some(block_number) + BlockNumberForBatch::take(txn, id) } pub(crate) fn save_external_key_for_session_to_sign_batch( @@ -103,8 +101,7 @@ impl ReportDb { txn: &mut impl DbTxn, id: u32, ) -> Option>>> { - let buf = SerializedReturnAddresses::get(txn, id)?; - SerializedReturnAddresses::del(txn, id); + let buf = SerializedReturnAddresses::take(txn, id)?; let mut buf = buf.as_slice(); let mut res = Vec::with_capacity(buf.len() / (32 + 1 + 8)); diff --git a/processor/scanner/src/substrate/db.rs b/processor/scanner/src/substrate/db.rs index 697897c2..18435856 100644 --- a/processor/scanner/src/substrate/db.rs +++ b/processor/scanner/src/substrate/db.rs @@ -37,7 +37,7 @@ pub(crate) enum Action { db_channel!( ScannerSubstrate { - Actions: (empty_key: ()) -> ActionEncodable, + Actions: () -> ActionEncodable, } ); @@ -52,7 +52,6 @@ impl SubstrateDb { ) { Actions::send( txn, - (), &ActionEncodable::AcknowledgeBatch(AcknowledgeBatchEncodable { batch_id, in_instruction_succeededs, @@ -62,11 +61,11 @@ impl SubstrateDb { ); } pub(crate) fn queue_queue_burns(txn: &mut impl DbTxn, burns: Vec) { - Actions::send(txn, (), &ActionEncodable::QueueBurns(burns)); + Actions::send(txn, &ActionEncodable::QueueBurns(burns)); } pub(crate) fn next_action(txn: &mut impl DbTxn) -> Option> { - let action_encodable = Actions::try_recv(txn, ())?; + let action_encodable = Actions::try_recv(txn)?; Some(match action_encodable { ActionEncodable::AcknowledgeBatch(AcknowledgeBatchEncodable { batch_id, diff --git a/processor/scheduler/utxo/transaction-chaining/src/db.rs b/processor/scheduler/utxo/transaction-chaining/src/db.rs index 697f1009..11bcd78d 100644 --- a/processor/scheduler/utxo/transaction-chaining/src/db.rs +++ b/processor/scheduler/utxo/transaction-chaining/src/db.rs @@ -69,9 +69,7 @@ impl Db { txn: &mut impl DbTxn, output: & as ReceivedOutput, AddressFor>>::Id, ) -> bool { - let res = AlreadyAccumulatedOutput::get(txn, output.as_ref()).is_some(); - AlreadyAccumulatedOutput::del(txn, output.as_ref()); - res + AlreadyAccumulatedOutput::take(txn, output.as_ref()).is_some() } pub(crate) fn queued_payments(