// Copyright 2019-2020 Parity Technologies (UK) Ltd. // This file is part of Substrate. // Substrate is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Substrate is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Substrate. If not, see . //! # Off-chain Storage Lock //! //! A storage-based lock with a defined expiry time. //! //! The lock is using Local Storage and allows synchronizing access to critical //! section of your code for concurrently running Off-chain Workers. Usage of //! `PERSISTENT` variant of the storage persists the lock value across a full node //! restart or re-orgs. //! //! A use case for the lock is to make sure that a particular section of the //! code is only run by one Off-chain Worker at a time. This may include //! performing a side-effect (i.e. an HTTP call) or alteration of single or //! multiple Local Storage entries. //! //! One use case would be collective updates of multiple data items or append / //! remove of i.e. sets, vectors which are stored in the off-chain storage DB. //! //! ## Example: //! //! ```rust //! # use codec::{Decode, Encode, Codec}; //! // in your off-chain worker code //! use sp_runtime::offchain::{ //! storage::StorageValueRef, //! storage_lock::{StorageLock, Time}, //! }; //! //! fn append_to_in_storage_vec<'a, T>(key: &'a [u8], _: T) where T: Codec { //! // `access::lock` defines the storage entry which is used for //! // persisting the lock in the underlying database. //! // The entry name _must_ be unique and can be interpreted as a //! // unique mutex instance reference tag. //! let mut lock = StorageLock::