From 36cdf6d4bf5ae36a6b5cf5041f572de4609a3cd2 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sun, 16 Apr 2023 02:57:19 -0400 Subject: [PATCH] Have InInstructions track the latest block for a network in storage --- substrate/client/src/serai/in_instructions.rs | 15 ++++++++++++++- substrate/client/tests/batch.rs | 1 + substrate/in-instructions/pallet/src/lib.rs | 11 +++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/substrate/client/src/serai/in_instructions.rs b/substrate/client/src/serai/in_instructions.rs index aa0cfd78..fadd3fe0 100644 --- a/substrate/client/src/serai/in_instructions.rs +++ b/substrate/client/src/serai/in_instructions.rs @@ -4,11 +4,24 @@ use primitives::SignedBatch; use subxt::utils::Encoded; -use crate::{Serai, SeraiError}; +use crate::{ + primitives::{BlockHash, NetworkId}, + SeraiError, Serai, scale_value, +}; pub type InInstructionsEvent = in_instructions::Event; +const PALLET: &str = "InInstructions"; + impl Serai { + pub async fn get_latest_block_for_network( + &self, + hash: [u8; 32], + network: NetworkId, + ) -> Result, SeraiError> { + self.storage(PALLET, "LatestBlock", Some(vec![scale_value(network)]), hash).await + } + pub async fn get_batch_events( &self, block: [u8; 32], diff --git a/substrate/client/tests/batch.rs b/substrate/client/tests/batch.rs index 66bd4688..31f669dc 100644 --- a/substrate/client/tests/batch.rs +++ b/substrate/client/tests/batch.rs @@ -41,6 +41,7 @@ serai_test!( let block = provide_batch(batch).await; let serai = serai().await; + assert_eq!(serai.get_latest_block_for_network(block, network).await.unwrap(), Some(block_hash)); let batches = serai.get_batch_events(block).await.unwrap(); assert_eq!(batches, vec![InInstructionsEvent::Batch { network, id, block: block_hash }]); diff --git a/substrate/in-instructions/pallet/src/lib.rs b/substrate/in-instructions/pallet/src/lib.rs index 17ea1abe..31038901 100644 --- a/substrate/in-instructions/pallet/src/lib.rs +++ b/substrate/in-instructions/pallet/src/lib.rs @@ -52,11 +52,17 @@ pub mod pallet { #[pallet::pallet] pub struct Pallet(PhantomData); - // Latest block number agreed upon for a coin + // The amount of batches a network has issued, which is also the ID to use for the next batch #[pallet::storage] - #[pallet::getter(fn batch)] + #[pallet::getter(fn batches)] pub(crate) type Batches = StorageMap<_, Blake2_256, NetworkId, u32, OptionQuery>; + // The latest block a network has acknowledged as finalized + #[pallet::storage] + #[pallet::getter(fn last_block)] + pub(crate) type LatestBlock = + StorageMap<_, Blake2_256, NetworkId, BlockHash, OptionQuery>; + impl Pallet { fn execute(instruction: InInstructionWithBalance) -> Result<(), ()> { match instruction.instruction { @@ -77,6 +83,7 @@ pub mod pallet { let mut batch = batch.batch; Batches::::insert(batch.network, batch.id); + LatestBlock::::insert(batch.network, batch.block); Self::deposit_event(Event::Batch { network: batch.network, id: batch.id,