오프체인 로컬 스토리지
절차
fn offchain_worker(block_number: T::BlockNumber) { // 로컬 스토리지 값에 대한 참조를 생성합니다. // 로컬 스토리지는 모든 오프체인 워커에 공통이므로, // 팔렛 이름으로 항목을 먼저 추가하는 것이 좋습니다. 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; // 블록 번호로 지정 const LOCK_TIMEOUT_EXPIRATION: u64 = 10000; // 밀리초로 지정 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(()); } // 매우 고성능 계산이 여기에 있습니다. let val: u64 = 100 + 100; // 스토리지 락을 정의합니다. 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; // 블록 번호로 지정 const LOCK_TIMEOUT_EXPIRATION: u64 = 10000; // 밀리초로 지정 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(()); } // 매우 고성능 계산이 여기에 있습니다. let val: u64 = 100 + 100; // 스토리지 락을 정의합니다. 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(()) }
예제
Last updated