mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 10:01:17 +00:00
Custom RPC for Merkle Mountain Range pallet (#8137)
* Add MMR custom RPC.
* Change RuntimeApi to avoid hardcoding leaf type.
* Properly implement the new RuntimeAPI and wire up RPC.
* Extract Offchain DB as separate execution extension.
* Enable offchain DB access for offchain calls.
* Fix offchain_election tests.
* Skip block initialisation for proof generation.
* Fix integration test setup.
* Fix offchain tests. Not sure how I missed them earlier 🤷.
* Fix long line.
* One more test missing.
* Update mock for multi-phase.
* Address review grumbbles.
* Address review grumbles.
* Fix line width of a comment
This commit is contained in:
@@ -119,9 +119,10 @@ impl ExecutionContext {
|
||||
match self {
|
||||
Importing | Syncing | BlockConstruction =>
|
||||
offchain::Capabilities::none(),
|
||||
// Enable keystore and transaction pool by default for offchain calls.
|
||||
// Enable keystore, transaction pool and Offchain DB reads by default for offchain calls.
|
||||
OffchainCall(None) => [
|
||||
offchain::Capability::Keystore,
|
||||
offchain::Capability::OffchainDbRead,
|
||||
offchain::Capability::TransactionPool,
|
||||
][..].into(),
|
||||
OffchainCall(Some((_, capabilities))) => *capabilities,
|
||||
|
||||
@@ -29,10 +29,10 @@ pub mod storage;
|
||||
#[cfg(feature = "std")]
|
||||
pub mod testing;
|
||||
|
||||
/// Local storage prefix used by the Offchain Worker API to
|
||||
/// Persistent storage prefix used by the Offchain Worker API when creating a DB key.
|
||||
pub const STORAGE_PREFIX : &[u8] = b"storage";
|
||||
|
||||
/// Offchain workers local storage.
|
||||
/// Offchain DB persistent (non-fork-aware) storage.
|
||||
pub trait OffchainStorage: Clone + Send + Sync {
|
||||
/// Persist a value in storage under given key and prefix.
|
||||
fn set(&mut self, prefix: &[u8], key: &[u8], value: &[u8]);
|
||||
@@ -263,9 +263,9 @@ pub enum Capability {
|
||||
/// Access to opaque network state.
|
||||
NetworkState = 16,
|
||||
/// Access to offchain worker DB (read only).
|
||||
OffchainWorkerDbRead = 32,
|
||||
OffchainDbRead = 32,
|
||||
/// Access to offchain worker DB (writes).
|
||||
OffchainWorkerDbWrite = 64,
|
||||
OffchainDbWrite = 64,
|
||||
/// Manage the authorized nodes
|
||||
NodeAuthorization = 128,
|
||||
}
|
||||
@@ -293,7 +293,7 @@ impl Capabilities {
|
||||
[
|
||||
Capability::TransactionPool,
|
||||
Capability::Keystore,
|
||||
Capability::OffchainWorkerDbRead,
|
||||
Capability::OffchainDbRead,
|
||||
][..].into()
|
||||
}
|
||||
|
||||
@@ -337,42 +337,6 @@ pub trait Externalities: Send {
|
||||
/// Obviously fine in the off-chain worker context.
|
||||
fn random_seed(&mut self) -> [u8; 32];
|
||||
|
||||
/// Sets a value in the local storage.
|
||||
///
|
||||
/// Note this storage is not part of the consensus, it's only accessible by
|
||||
/// offchain worker tasks running on the same machine. It _is_ persisted between runs.
|
||||
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]);
|
||||
|
||||
/// Removes a value in the local storage.
|
||||
///
|
||||
/// Note this storage is not part of the consensus, it's only accessible by
|
||||
/// offchain worker tasks running on the same machine. It _is_ persisted between runs.
|
||||
fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8]);
|
||||
|
||||
/// Sets a value in the local storage if it matches current value.
|
||||
///
|
||||
/// Since multiple offchain workers may be running concurrently, to prevent
|
||||
/// data races use CAS to coordinate between them.
|
||||
///
|
||||
/// Returns `true` if the value has been set, `false` otherwise.
|
||||
///
|
||||
/// Note this storage is not part of the consensus, it's only accessible by
|
||||
/// offchain worker tasks running on the same machine. It _is_ persisted between runs.
|
||||
fn local_storage_compare_and_set(
|
||||
&mut self,
|
||||
kind: StorageKind,
|
||||
key: &[u8],
|
||||
old_value: Option<&[u8]>,
|
||||
new_value: &[u8],
|
||||
) -> bool;
|
||||
|
||||
/// Gets a value from the local storage.
|
||||
///
|
||||
/// If the value does not exist in the storage `None` will be returned.
|
||||
/// Note this storage is not part of the consensus, it's only accessible by
|
||||
/// offchain worker tasks running on the same machine. It _is_ persisted between runs.
|
||||
fn local_storage_get(&mut self, kind: StorageKind, key: &[u8]) -> Option<Vec<u8>>;
|
||||
|
||||
/// Initiates a http request given HTTP verb and the URL.
|
||||
///
|
||||
/// Meta is a future-reserved field containing additional, parity-scale-codec encoded parameters.
|
||||
@@ -521,28 +485,6 @@ impl<T: Externalities + ?Sized> Externalities for Box<T> {
|
||||
(&mut **self).random_seed()
|
||||
}
|
||||
|
||||
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]) {
|
||||
(&mut **self).local_storage_set(kind, key, value)
|
||||
}
|
||||
|
||||
fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8]) {
|
||||
(&mut **self).local_storage_clear(kind, key)
|
||||
}
|
||||
|
||||
fn local_storage_compare_and_set(
|
||||
&mut self,
|
||||
kind: StorageKind,
|
||||
key: &[u8],
|
||||
old_value: Option<&[u8]>,
|
||||
new_value: &[u8],
|
||||
) -> bool {
|
||||
(&mut **self).local_storage_compare_and_set(kind, key, old_value, new_value)
|
||||
}
|
||||
|
||||
fn local_storage_get(&mut self, kind: StorageKind, key: &[u8]) -> Option<Vec<u8>> {
|
||||
(&mut **self).local_storage_get(kind, key)
|
||||
}
|
||||
|
||||
fn http_request_start(&mut self, method: &str, uri: &str, meta: &[u8]) -> Result<HttpRequestId, ()> {
|
||||
(&mut **self).http_request_start(method, uri, meta)
|
||||
}
|
||||
@@ -582,7 +524,7 @@ impl<T: Externalities + ?Sized> Externalities for Box<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// An `OffchainExternalities` implementation with limited capabilities.
|
||||
/// An `*Externalities` implementation with limited capabilities.
|
||||
pub struct LimitedExternalities<T> {
|
||||
capabilities: Capabilities,
|
||||
externalities: T,
|
||||
@@ -633,32 +575,6 @@ impl<T: Externalities> Externalities for LimitedExternalities<T> {
|
||||
self.externalities.random_seed()
|
||||
}
|
||||
|
||||
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]) {
|
||||
self.check(Capability::OffchainWorkerDbWrite, "local_storage_set");
|
||||
self.externalities.local_storage_set(kind, key, value)
|
||||
}
|
||||
|
||||
fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8]) {
|
||||
self.check(Capability::OffchainWorkerDbWrite, "local_storage_clear");
|
||||
self.externalities.local_storage_clear(kind, key)
|
||||
}
|
||||
|
||||
fn local_storage_compare_and_set(
|
||||
&mut self,
|
||||
kind: StorageKind,
|
||||
key: &[u8],
|
||||
old_value: Option<&[u8]>,
|
||||
new_value: &[u8],
|
||||
) -> bool {
|
||||
self.check(Capability::OffchainWorkerDbWrite, "local_storage_compare_and_set");
|
||||
self.externalities.local_storage_compare_and_set(kind, key, old_value, new_value)
|
||||
}
|
||||
|
||||
fn local_storage_get(&mut self, kind: StorageKind, key: &[u8]) -> Option<Vec<u8>> {
|
||||
self.check(Capability::OffchainWorkerDbRead, "local_storage_get");
|
||||
self.externalities.local_storage_get(kind, key)
|
||||
}
|
||||
|
||||
fn http_request_start(&mut self, method: &str, uri: &str, meta: &[u8]) -> Result<HttpRequestId, ()> {
|
||||
self.check(Capability::Http, "http_request_start");
|
||||
self.externalities.http_request_start(method, uri, meta)
|
||||
@@ -707,18 +623,123 @@ impl<T: Externalities> Externalities for LimitedExternalities<T> {
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
sp_externalities::decl_extension! {
|
||||
/// The offchain extension that will be registered at the Substrate externalities.
|
||||
pub struct OffchainExt(Box<dyn Externalities>);
|
||||
/// The offchain worker extension that will be registered at the Substrate externalities.
|
||||
pub struct OffchainWorkerExt(Box<dyn Externalities>);
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl OffchainExt {
|
||||
impl OffchainWorkerExt {
|
||||
/// Create a new instance of `Self`.
|
||||
pub fn new<O: Externalities + 'static>(offchain: O) -> Self {
|
||||
Self(Box::new(offchain))
|
||||
}
|
||||
}
|
||||
|
||||
/// A externalities extension for accessing the Offchain DB.
|
||||
pub trait DbExternalities: Send {
|
||||
/// Sets a value in the local storage.
|
||||
///
|
||||
/// Note this storage is not part of the consensus, it's only accessible by
|
||||
/// offchain worker tasks running on the same machine. It _is_ persisted between runs.
|
||||
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]);
|
||||
|
||||
/// Removes a value in the local storage.
|
||||
///
|
||||
/// Note this storage is not part of the consensus, it's only accessible by
|
||||
/// offchain worker tasks running on the same machine. It _is_ persisted between runs.
|
||||
fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8]);
|
||||
|
||||
/// Sets a value in the local storage if it matches current value.
|
||||
///
|
||||
/// Since multiple offchain workers may be running concurrently, to prevent
|
||||
/// data races use CAS to coordinate between them.
|
||||
///
|
||||
/// Returns `true` if the value has been set, `false` otherwise.
|
||||
///
|
||||
/// Note this storage is not part of the consensus, it's only accessible by
|
||||
/// offchain worker tasks running on the same machine. It _is_ persisted between runs.
|
||||
fn local_storage_compare_and_set(
|
||||
&mut self,
|
||||
kind: StorageKind,
|
||||
key: &[u8],
|
||||
old_value: Option<&[u8]>,
|
||||
new_value: &[u8],
|
||||
) -> bool;
|
||||
|
||||
/// Gets a value from the local storage.
|
||||
///
|
||||
/// If the value does not exist in the storage `None` will be returned.
|
||||
/// Note this storage is not part of the consensus, it's only accessible by
|
||||
/// offchain worker tasks running on the same machine. It _is_ persisted between runs.
|
||||
fn local_storage_get(&mut self, kind: StorageKind, key: &[u8]) -> Option<Vec<u8>>;
|
||||
}
|
||||
|
||||
impl<T: DbExternalities + ?Sized> DbExternalities for Box<T> {
|
||||
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]) {
|
||||
(&mut **self).local_storage_set(kind, key, value)
|
||||
}
|
||||
|
||||
fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8]) {
|
||||
(&mut **self).local_storage_clear(kind, key)
|
||||
}
|
||||
|
||||
fn local_storage_compare_and_set(
|
||||
&mut self,
|
||||
kind: StorageKind,
|
||||
key: &[u8],
|
||||
old_value: Option<&[u8]>,
|
||||
new_value: &[u8],
|
||||
) -> bool {
|
||||
(&mut **self).local_storage_compare_and_set(kind, key, old_value, new_value)
|
||||
}
|
||||
|
||||
fn local_storage_get(&mut self, kind: StorageKind, key: &[u8]) -> Option<Vec<u8>> {
|
||||
(&mut **self).local_storage_get(kind, key)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: DbExternalities> DbExternalities for LimitedExternalities<T> {
|
||||
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]) {
|
||||
self.check(Capability::OffchainDbWrite, "local_storage_set");
|
||||
self.externalities.local_storage_set(kind, key, value)
|
||||
}
|
||||
|
||||
fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8]) {
|
||||
self.check(Capability::OffchainDbWrite, "local_storage_clear");
|
||||
self.externalities.local_storage_clear(kind, key)
|
||||
}
|
||||
|
||||
fn local_storage_compare_and_set(
|
||||
&mut self,
|
||||
kind: StorageKind,
|
||||
key: &[u8],
|
||||
old_value: Option<&[u8]>,
|
||||
new_value: &[u8],
|
||||
) -> bool {
|
||||
self.check(Capability::OffchainDbWrite, "local_storage_compare_and_set");
|
||||
self.externalities.local_storage_compare_and_set(kind, key, old_value, new_value)
|
||||
}
|
||||
|
||||
fn local_storage_get(&mut self, kind: StorageKind, key: &[u8]) -> Option<Vec<u8>> {
|
||||
self.check(Capability::OffchainDbRead, "local_storage_get");
|
||||
self.externalities.local_storage_get(kind, key)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
sp_externalities::decl_extension! {
|
||||
/// The offchain database extension that will be registered at the Substrate externalities.
|
||||
pub struct OffchainDbExt(Box<dyn DbExternalities>);
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl OffchainDbExt {
|
||||
/// Create a new instance of `OffchainDbExt`.
|
||||
pub fn new<O: DbExternalities + 'static>(offchain: O) -> Self {
|
||||
Self(Box::new(offchain))
|
||||
}
|
||||
}
|
||||
|
||||
/// Abstraction over transaction pool.
|
||||
///
|
||||
/// This trait is currently used within the `ExternalitiesExtension`
|
||||
|
||||
@@ -244,44 +244,6 @@ impl offchain::Externalities for TestOffchainExt {
|
||||
self.0.read().seed
|
||||
}
|
||||
|
||||
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]) {
|
||||
let mut state = self.0.write();
|
||||
match kind {
|
||||
StorageKind::LOCAL => state.local_storage.set(b"", key, value),
|
||||
StorageKind::PERSISTENT => state.persistent_storage.set(b"", key, value),
|
||||
};
|
||||
}
|
||||
|
||||
fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8]) {
|
||||
let mut state = self.0.write();
|
||||
match kind {
|
||||
StorageKind::LOCAL => state.local_storage.remove(b"", key),
|
||||
StorageKind::PERSISTENT => state.persistent_storage.remove(b"", key),
|
||||
};
|
||||
}
|
||||
|
||||
fn local_storage_compare_and_set(
|
||||
&mut self,
|
||||
kind: StorageKind,
|
||||
key: &[u8],
|
||||
old_value: Option<&[u8]>,
|
||||
new_value: &[u8]
|
||||
) -> bool {
|
||||
let mut state = self.0.write();
|
||||
match kind {
|
||||
StorageKind::LOCAL => state.local_storage.compare_and_set(b"", key, old_value, new_value),
|
||||
StorageKind::PERSISTENT => state.persistent_storage.compare_and_set(b"", key, old_value, new_value),
|
||||
}
|
||||
}
|
||||
|
||||
fn local_storage_get(&mut self, kind: StorageKind, key: &[u8]) -> Option<Vec<u8>> {
|
||||
let state = self.0.read();
|
||||
match kind {
|
||||
StorageKind::LOCAL => state.local_storage.get(TestPersistentOffchainDB::PREFIX, key),
|
||||
StorageKind::PERSISTENT => state.persistent_storage.get(key),
|
||||
}
|
||||
}
|
||||
|
||||
fn http_request_start(&mut self, method: &str, uri: &str, meta: &[u8]) -> Result<RequestId, ()> {
|
||||
let mut state = self.0.write();
|
||||
let id = RequestId(state.requests.len() as u16);
|
||||
@@ -393,6 +355,48 @@ impl offchain::Externalities for TestOffchainExt {
|
||||
}
|
||||
}
|
||||
|
||||
impl offchain::DbExternalities for TestOffchainExt {
|
||||
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]) {
|
||||
let mut state = self.0.write();
|
||||
match kind {
|
||||
StorageKind::LOCAL => state.local_storage.set(b"", key, value),
|
||||
StorageKind::PERSISTENT => state.persistent_storage.set(b"", key, value),
|
||||
};
|
||||
}
|
||||
|
||||
fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8]) {
|
||||
let mut state = self.0.write();
|
||||
match kind {
|
||||
StorageKind::LOCAL => state.local_storage.remove(b"", key),
|
||||
StorageKind::PERSISTENT => state.persistent_storage.remove(b"", key),
|
||||
};
|
||||
}
|
||||
|
||||
fn local_storage_compare_and_set(
|
||||
&mut self,
|
||||
kind: StorageKind,
|
||||
key: &[u8],
|
||||
old_value: Option<&[u8]>,
|
||||
new_value: &[u8]
|
||||
) -> bool {
|
||||
let mut state = self.0.write();
|
||||
match kind {
|
||||
StorageKind::LOCAL => state.local_storage
|
||||
.compare_and_set(b"", key, old_value, new_value),
|
||||
StorageKind::PERSISTENT => state.persistent_storage
|
||||
.compare_and_set(b"", key, old_value, new_value),
|
||||
}
|
||||
}
|
||||
|
||||
fn local_storage_get(&mut self, kind: StorageKind, key: &[u8]) -> Option<Vec<u8>> {
|
||||
let state = self.0.read();
|
||||
match kind {
|
||||
StorageKind::LOCAL => state.local_storage.get(TestPersistentOffchainDB::PREFIX, key),
|
||||
StorageKind::PERSISTENT => state.persistent_storage.get(key),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The internal state of the fake transaction pool.
|
||||
#[derive(Default)]
|
||||
pub struct PoolState {
|
||||
|
||||
@@ -39,7 +39,7 @@ use tracing;
|
||||
use sp_core::{
|
||||
crypto::Pair,
|
||||
traits::{CallInWasmExt, TaskExecutorExt, RuntimeSpawnExt},
|
||||
offchain::{OffchainExt, TransactionPoolExt},
|
||||
offchain::{OffchainDbExt, OffchainWorkerExt, TransactionPoolExt},
|
||||
hexdisplay::HexDisplay,
|
||||
storage::ChildInfo,
|
||||
};
|
||||
@@ -843,7 +843,7 @@ pub trait Offchain {
|
||||
/// Even if this function returns `true`, it does not mean that any keys are configured
|
||||
/// and that the validator is registered in the chain.
|
||||
fn is_validator(&mut self) -> bool {
|
||||
self.extension::<OffchainExt>()
|
||||
self.extension::<OffchainWorkerExt>()
|
||||
.expect("is_validator can be called only in the offchain worker context")
|
||||
.is_validator()
|
||||
}
|
||||
@@ -860,21 +860,21 @@ pub trait Offchain {
|
||||
|
||||
/// Returns information about the local node's network state.
|
||||
fn network_state(&mut self) -> Result<OpaqueNetworkState, ()> {
|
||||
self.extension::<OffchainExt>()
|
||||
self.extension::<OffchainWorkerExt>()
|
||||
.expect("network_state can be called only in the offchain worker context")
|
||||
.network_state()
|
||||
}
|
||||
|
||||
/// Returns current UNIX timestamp (in millis)
|
||||
fn timestamp(&mut self) -> Timestamp {
|
||||
self.extension::<OffchainExt>()
|
||||
self.extension::<OffchainWorkerExt>()
|
||||
.expect("timestamp can be called only in the offchain worker context")
|
||||
.timestamp()
|
||||
}
|
||||
|
||||
/// Pause the execution until `deadline` is reached.
|
||||
fn sleep_until(&mut self, deadline: Timestamp) {
|
||||
self.extension::<OffchainExt>()
|
||||
self.extension::<OffchainWorkerExt>()
|
||||
.expect("sleep_until can be called only in the offchain worker context")
|
||||
.sleep_until(deadline)
|
||||
}
|
||||
@@ -884,7 +884,7 @@ pub trait Offchain {
|
||||
/// This is a truly random, non-deterministic seed generated by host environment.
|
||||
/// Obviously fine in the off-chain worker context.
|
||||
fn random_seed(&mut self) -> [u8; 32] {
|
||||
self.extension::<OffchainExt>()
|
||||
self.extension::<OffchainWorkerExt>()
|
||||
.expect("random_seed can be called only in the offchain worker context")
|
||||
.random_seed()
|
||||
}
|
||||
@@ -894,8 +894,9 @@ pub trait Offchain {
|
||||
/// Note this storage is not part of the consensus, it's only accessible by
|
||||
/// offchain worker tasks running on the same machine. It IS persisted between runs.
|
||||
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]) {
|
||||
self.extension::<OffchainExt>()
|
||||
.expect("local_storage_set can be called only in the offchain worker context")
|
||||
self.extension::<OffchainDbExt>()
|
||||
.expect("local_storage_set can be called only in the offchain call context with
|
||||
OffchainDb extension")
|
||||
.local_storage_set(kind, key, value)
|
||||
}
|
||||
|
||||
@@ -904,8 +905,9 @@ pub trait Offchain {
|
||||
/// Note this storage is not part of the consensus, it's only accessible by
|
||||
/// offchain worker tasks running on the same machine. It IS persisted between runs.
|
||||
fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8]) {
|
||||
self.extension::<OffchainExt>()
|
||||
.expect("local_storage_clear can be called only in the offchain worker context")
|
||||
self.extension::<OffchainDbExt>()
|
||||
.expect("local_storage_clear can be called only in the offchain call context with
|
||||
OffchainDb extension")
|
||||
.local_storage_clear(kind, key)
|
||||
}
|
||||
|
||||
@@ -925,9 +927,15 @@ pub trait Offchain {
|
||||
old_value: Option<Vec<u8>>,
|
||||
new_value: &[u8],
|
||||
) -> bool {
|
||||
self.extension::<OffchainExt>()
|
||||
.expect("local_storage_compare_and_set can be called only in the offchain worker context")
|
||||
.local_storage_compare_and_set(kind, key, old_value.as_ref().map(|v| v.deref()), new_value)
|
||||
self.extension::<OffchainDbExt>()
|
||||
.expect("local_storage_compare_and_set can be called only in the offchain call context
|
||||
with OffchainDb extension")
|
||||
.local_storage_compare_and_set(
|
||||
kind,
|
||||
key,
|
||||
old_value.as_ref().map(|v| v.deref()),
|
||||
new_value,
|
||||
)
|
||||
}
|
||||
|
||||
/// Gets a value from the local storage.
|
||||
@@ -936,8 +944,9 @@ pub trait Offchain {
|
||||
/// Note this storage is not part of the consensus, it's only accessible by
|
||||
/// offchain worker tasks running on the same machine. It IS persisted between runs.
|
||||
fn local_storage_get(&mut self, kind: StorageKind, key: &[u8]) -> Option<Vec<u8>> {
|
||||
self.extension::<OffchainExt>()
|
||||
.expect("local_storage_get can be called only in the offchain worker context")
|
||||
self.extension::<OffchainDbExt>()
|
||||
.expect("local_storage_get can be called only in the offchain call context with
|
||||
OffchainDb extension")
|
||||
.local_storage_get(kind, key)
|
||||
}
|
||||
|
||||
@@ -951,7 +960,7 @@ pub trait Offchain {
|
||||
uri: &str,
|
||||
meta: &[u8],
|
||||
) -> Result<HttpRequestId, ()> {
|
||||
self.extension::<OffchainExt>()
|
||||
self.extension::<OffchainWorkerExt>()
|
||||
.expect("http_request_start can be called only in the offchain worker context")
|
||||
.http_request_start(method, uri, meta)
|
||||
}
|
||||
@@ -963,7 +972,7 @@ pub trait Offchain {
|
||||
name: &str,
|
||||
value: &str,
|
||||
) -> Result<(), ()> {
|
||||
self.extension::<OffchainExt>()
|
||||
self.extension::<OffchainWorkerExt>()
|
||||
.expect("http_request_add_header can be called only in the offchain worker context")
|
||||
.http_request_add_header(request_id, name, value)
|
||||
}
|
||||
@@ -980,7 +989,7 @@ pub trait Offchain {
|
||||
chunk: &[u8],
|
||||
deadline: Option<Timestamp>,
|
||||
) -> Result<(), HttpError> {
|
||||
self.extension::<OffchainExt>()
|
||||
self.extension::<OffchainWorkerExt>()
|
||||
.expect("http_request_write_body can be called only in the offchain worker context")
|
||||
.http_request_write_body(request_id, chunk, deadline)
|
||||
}
|
||||
@@ -997,7 +1006,7 @@ pub trait Offchain {
|
||||
ids: &[HttpRequestId],
|
||||
deadline: Option<Timestamp>,
|
||||
) -> Vec<HttpRequestStatus> {
|
||||
self.extension::<OffchainExt>()
|
||||
self.extension::<OffchainWorkerExt>()
|
||||
.expect("http_response_wait can be called only in the offchain worker context")
|
||||
.http_response_wait(ids, deadline)
|
||||
}
|
||||
@@ -1007,7 +1016,7 @@ pub trait Offchain {
|
||||
/// Returns a vector of pairs `(HeaderKey, HeaderValue)`.
|
||||
/// NOTE response headers have to be read before response body.
|
||||
fn http_response_headers(&mut self, request_id: HttpRequestId) -> Vec<(Vec<u8>, Vec<u8>)> {
|
||||
self.extension::<OffchainExt>()
|
||||
self.extension::<OffchainWorkerExt>()
|
||||
.expect("http_response_headers can be called only in the offchain worker context")
|
||||
.http_response_headers(request_id)
|
||||
}
|
||||
@@ -1026,7 +1035,7 @@ pub trait Offchain {
|
||||
buffer: &mut [u8],
|
||||
deadline: Option<Timestamp>,
|
||||
) -> Result<u32, HttpError> {
|
||||
self.extension::<OffchainExt>()
|
||||
self.extension::<OffchainWorkerExt>()
|
||||
.expect("http_response_read_body can be called only in the offchain worker context")
|
||||
.http_response_read_body(request_id, buffer, deadline)
|
||||
.map(|r| r as u32)
|
||||
@@ -1034,7 +1043,7 @@ pub trait Offchain {
|
||||
|
||||
/// Set the authorized nodes and authorized_only flag.
|
||||
fn set_authorized_nodes(&mut self, nodes: Vec<OpaquePeerId>, authorized_only: bool) {
|
||||
self.extension::<OffchainExt>()
|
||||
self.extension::<OffchainWorkerExt>()
|
||||
.expect("set_authorized_nodes can be called only in the offchain worker context")
|
||||
.set_authorized_nodes(nodes, authorized_only)
|
||||
}
|
||||
|
||||
@@ -518,7 +518,7 @@ mod tests {
|
||||
use super::*;
|
||||
use sp_io::TestExternalities;
|
||||
use sp_core::offchain::{
|
||||
OffchainExt,
|
||||
OffchainWorkerExt,
|
||||
testing,
|
||||
};
|
||||
|
||||
@@ -526,7 +526,7 @@ mod tests {
|
||||
fn should_send_a_basic_request_and_get_response() {
|
||||
let (offchain, state) = testing::TestOffchainExt::new();
|
||||
let mut t = TestExternalities::default();
|
||||
t.register_extension(OffchainExt::new(offchain));
|
||||
t.register_extension(OffchainWorkerExt::new(offchain));
|
||||
|
||||
t.execute_with(|| {
|
||||
let request: Request = Request::get("http://localhost:1234");
|
||||
@@ -567,7 +567,7 @@ mod tests {
|
||||
fn should_send_a_post_request() {
|
||||
let (offchain, state) = testing::TestOffchainExt::new();
|
||||
let mut t = TestExternalities::default();
|
||||
t.register_extension(OffchainExt::new(offchain));
|
||||
t.register_extension(OffchainWorkerExt::new(offchain));
|
||||
|
||||
t.execute_with(|| {
|
||||
let pending = Request::default()
|
||||
|
||||
@@ -104,7 +104,7 @@ mod tests {
|
||||
use super::*;
|
||||
use sp_io::TestExternalities;
|
||||
use sp_core::offchain::{
|
||||
OffchainExt,
|
||||
OffchainDbExt,
|
||||
testing,
|
||||
};
|
||||
|
||||
@@ -112,7 +112,7 @@ mod tests {
|
||||
fn should_set_and_get() {
|
||||
let (offchain, state) = testing::TestOffchainExt::new();
|
||||
let mut t = TestExternalities::default();
|
||||
t.register_extension(OffchainExt::new(offchain));
|
||||
t.register_extension(OffchainDbExt::new(offchain));
|
||||
|
||||
t.execute_with(|| {
|
||||
let val = StorageValue::persistent(b"testval");
|
||||
@@ -134,7 +134,7 @@ mod tests {
|
||||
fn should_mutate() {
|
||||
let (offchain, state) = testing::TestOffchainExt::new();
|
||||
let mut t = TestExternalities::default();
|
||||
t.register_extension(OffchainExt::new(offchain));
|
||||
t.register_extension(OffchainDbExt::new(offchain));
|
||||
|
||||
t.execute_with(|| {
|
||||
let val = StorageValue::persistent(b"testval");
|
||||
|
||||
@@ -453,7 +453,7 @@ pub trait BlockNumberProvider {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sp_core::offchain::{testing, OffchainExt};
|
||||
use sp_core::offchain::{testing, OffchainWorkerExt, OffchainDbExt};
|
||||
use sp_io::TestExternalities;
|
||||
|
||||
const VAL_1: u32 = 0u32;
|
||||
@@ -463,7 +463,8 @@ mod tests {
|
||||
fn storage_lock_write_unlock_lock_read_unlock() {
|
||||
let (offchain, state) = testing::TestOffchainExt::new();
|
||||
let mut t = TestExternalities::default();
|
||||
t.register_extension(OffchainExt::new(offchain));
|
||||
t.register_extension(OffchainDbExt::new(offchain.clone()));
|
||||
t.register_extension(OffchainWorkerExt::new(offchain));
|
||||
|
||||
t.execute_with(|| {
|
||||
let mut lock = StorageLock::<'_, Time>::new(b"lock_1");
|
||||
@@ -493,7 +494,8 @@ mod tests {
|
||||
fn storage_lock_and_forget() {
|
||||
let (offchain, state) = testing::TestOffchainExt::new();
|
||||
let mut t = TestExternalities::default();
|
||||
t.register_extension(OffchainExt::new(offchain));
|
||||
t.register_extension(OffchainDbExt::new(offchain.clone()));
|
||||
t.register_extension(OffchainWorkerExt::new(offchain));
|
||||
|
||||
t.execute_with(|| {
|
||||
let mut lock = StorageLock::<'_, Time>::new(b"lock_2");
|
||||
@@ -517,7 +519,8 @@ mod tests {
|
||||
fn storage_lock_and_let_expire_and_lock_again() {
|
||||
let (offchain, state) = testing::TestOffchainExt::new();
|
||||
let mut t = TestExternalities::default();
|
||||
t.register_extension(OffchainExt::new(offchain));
|
||||
t.register_extension(OffchainDbExt::new(offchain.clone()));
|
||||
t.register_extension(OffchainWorkerExt::new(offchain));
|
||||
|
||||
t.execute_with(|| {
|
||||
let sleep_until = offchain::timestamp().add(Duration::from_millis(500));
|
||||
@@ -549,7 +552,8 @@ mod tests {
|
||||
fn extend_active_lock() {
|
||||
let (offchain, state) = testing::TestOffchainExt::new();
|
||||
let mut t = TestExternalities::default();
|
||||
t.register_extension(OffchainExt::new(offchain));
|
||||
t.register_extension(OffchainDbExt::new(offchain.clone()));
|
||||
t.register_extension(OffchainWorkerExt::new(offchain));
|
||||
|
||||
t.execute_with(|| {
|
||||
let lock_expiration = Duration::from_millis(300);
|
||||
|
||||
Reference in New Issue
Block a user