Offchain Local Storage
Steps
fn offchain_worker(block_number: T::BlockNumber) { // Create a reference to Local Storage value. // Since the local storage is common for all offchain workers, it's a good practice // to prepend our entry with the pallet name. let storage = StorageValueRef::persistent(b"pallet::my-storage"); }fn offchain_worker(block_number: T::BlockNumber) { // ... let storage = StorageValueRef::persistent(b"pallet::my-storage"); if let Ok(Some(res)) = storage.get::<u64>() { log::info!("cached result: {:?}", res); return Ok(()); } }const LOCK_BLOCK_EXPIRATION: u32 = 3; // in block number const LOCK_TIMEOUT_EXPIRATION: u64 = 10000; // in milli-seconds fn offchain_worker(block_number: T::BlockNumber) { // ... let storage = StorageValueRef::persistent(b"pallet::my-storage"); if let Ok(Some(res)) = storage.get::<u64>() { log::info!("cached result: {:?}", res); return Ok(()); } // Very intensive computation here let val: u64 = 100 + 100; // Define the storage lock let mut lock = StorageLock::<BlockAndTime<Self>>::with_block_and_time_deadline( b"pallet::storage-lock", LOCK_BLOCK_EXPIRATION, Duration::from_millis(LOCK_TIMEOUT_EXPIRATION) ); }fn offchain_worker(block_number: T::BlockNumber) { // ... let mut lock = /* .... */; if let Ok(_guard) = lock.try_lock() { storage.set(&val); } }const LOCK_BLOCK_EXPIRATION: u32 = 3; // in block number const LOCK_TIMEOUT_EXPIRATION: u64 = 10000; // in milli-seconds fn offchain_worker(block_number: T::BlockNumber) { let storage = StorageValueRef::persistent(b"pallet::my-storage"); if let Ok(Some(res)) = storage.get::<u64>() { log::info!("cached result: {:?}", res); return Ok(()); } // Very intensive computation here let val: u64 = 100 + 100; // Define the storage lock let mut lock = StorageLock::<BlockAndTime<Self>>::with_block_and_time_deadline( b"pallet::storage-lock", LOCK_BLOCK_EXPIRATION, Duration::from_millis(LOCK_TIMEOUT_EXPIRATION) ); if let Ok(_guard) = lock.try_lock() { storage.set(&val); } Ok(()) }
Examples
Last updated