diff --git a/.gitignore b/.gitignore index 3385bd1ba..55768798a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ /clients/orderbook/keystore-* /clients/thea/keystore-* - +/fullnode # Generated by Cargo # will have compiled files and executables **/target/ diff --git a/pallets/ocex/rpc/src/lib.rs b/pallets/ocex/rpc/src/lib.rs index 50d2b216a..debee635a 100644 --- a/pallets/ocex/rpc/src/lib.rs +++ b/pallets/ocex/rpc/src/lib.rs @@ -30,12 +30,11 @@ use jsonrpsee::{ use orderbook_primitives::recovery::{DeviationMap, ObCheckpoint, ObRecoveryState}; pub use pallet_ocex_runtime_api::PolkadexOcexRuntimeApi; use parity_scale_codec::{Codec, Decode}; -use parking_lot::RwLock; use polkadex_primitives::AssetId; use sc_rpc_api::DenyUnsafe; -use sp_api::ProvideRuntimeApi; +use sp_api::{ApiExt, ProvideRuntimeApi}; use sp_blockchain::HeaderBackend; -use sp_core::offchain::OffchainStorage; +use sp_core::offchain::{storage::OffchainDb, OffchainDbExt, OffchainStorage}; use sp_runtime::traits::Block as BlockT; use std::sync::Arc; @@ -74,7 +73,7 @@ pub struct PolkadexOcexRpc { client: Arc, /// Offchain storage - storage: Arc>, + offchain_db: OffchainDb, deny_unsafe: DenyUnsafe, /// A marker for the `Block` type parameter, used to ensure the struct @@ -86,7 +85,7 @@ impl PolkadexOcexRpc { pub fn new(client: Arc, storage: T, deny_unsafe: DenyUnsafe) -> Self { Self { client, - storage: Arc::new(RwLock::new(storage)), + offchain_db: OffchainDb::new(storage), deny_unsafe, _marker: Default::default(), } @@ -109,11 +108,12 @@ where &self, at: Option<::Hash>, ) -> RpcResult { - let api = self.client.runtime_api(); + let mut api = self.client.runtime_api(); let at = match at { Some(at) => at, None => self.client.info().best_hash, }; + api.register_extension(OffchainDbExt::new(self.offchain_db.clone())); // WARN: this is a hack on beating the boundry of runtime -> // polkadex-node with decoding tuple of underlying data into // solid std type @@ -133,11 +133,13 @@ where of: AssetId, at: Option<::Hash>, ) -> RpcResult { - let api = self.client.runtime_api(); + let mut api = self.client.runtime_api(); let at = match at { Some(at) => at, None => self.client.info().best_hash, }; + + api.register_extension(OffchainDbExt::new(self.offchain_db.clone())); let runtime_api_result = api.get_balance(at, account_id, of).map_err(runtime_error_into_rpc_err)?; let json = @@ -150,12 +152,14 @@ where at: Option<::Hash>, ) -> RpcResult { self.deny_unsafe.check_if_safe()?; - let api = self.client.runtime_api(); + let mut api = self.client.runtime_api(); let at = match at { Some(at) => at, None => self.client.info().best_hash, }; - let offchain_storage = offchain::OffchainStorageAdapter::new(self.storage.clone()); + + api.register_extension(OffchainDbExt::new(self.offchain_db.clone())); + let mut offchain_storage = offchain::OffchainStorageAdapter::new(self.offchain_db.clone()); if !offchain_storage.acquire_offchain_lock(3).await { return Err(runtime_error_into_rpc_err("Failed to acquire offchain lock")) } @@ -180,12 +184,14 @@ where at: Option<::Hash>, ) -> RpcResult { //self.deny_unsafe.check_if_safe()?; //As it is used by the aggregator, we need to allow it - let api = self.client.runtime_api(); + let mut api = self.client.runtime_api(); let at = match at { Some(at) => at, None => self.client.info().best_hash, }; - let offchain_storage = offchain::OffchainStorageAdapter::new(self.storage.clone()); + + api.register_extension(OffchainDbExt::new(self.offchain_db.clone())); + let mut offchain_storage = offchain::OffchainStorageAdapter::new(self.offchain_db.clone()); if !offchain_storage.acquire_offchain_lock(RETRIES).await { return Err(runtime_error_into_rpc_err("Failed to acquire offchain lock")) } diff --git a/pallets/ocex/rpc/src/offchain.rs b/pallets/ocex/rpc/src/offchain.rs index 625b911a7..c7a6a3938 100644 --- a/pallets/ocex/rpc/src/offchain.rs +++ b/pallets/ocex/rpc/src/offchain.rs @@ -21,15 +21,13 @@ //! This adapter is used by `function_handler` to access offchain storage. use parity_scale_codec::Encode; -use parking_lot::RwLock; -use sp_core::offchain::OffchainStorage; -use std::sync::Arc; +use sp_core::offchain::{storage::OffchainDb, DbExternalities, OffchainStorage, StorageKind}; pub const WORKER_STATUS: [u8; 28] = *b"offchain-ocex::worker_status"; /// Adapter to Access OCEX Offchain Storage pub struct OffchainStorageAdapter { - storage: Arc>, + storage: OffchainDb, } impl OffchainStorageAdapter { @@ -38,7 +36,7 @@ impl OffchainStorageAdapter { /// * `storage`: Offchain storage /// # Returns /// * `OffchainStorageAdapter`: A new `OffchainStorageAdapter` instance. - pub fn new(storage: Arc>) -> Self { + pub fn new(storage: OffchainDb) -> Self { Self { storage } } @@ -47,13 +45,12 @@ impl OffchainStorageAdapter { /// * `tries`: Number of tries to acquire lock /// # Returns /// * `bool`: True if lock is acquired else false - pub async fn acquire_offchain_lock(&self, tries: u8) -> bool { - let prefix = sp_offchain::STORAGE_PREFIX; + pub async fn acquire_offchain_lock(&mut self, tries: u8) -> bool { let old_value = Encode::encode(&false); let new_value = Encode::encode(&true); for _ in 0..tries { - if self.storage.write().compare_and_set( - prefix, + if self.storage.local_storage_compare_and_set( + StorageKind::PERSISTENT, &WORKER_STATUS, Some(&old_value), &new_value, @@ -69,8 +66,8 @@ impl OffchainStorageAdapter { impl Drop for OffchainStorageAdapter { fn drop(&mut self) { - let prefix = sp_offchain::STORAGE_PREFIX; let encoded_value = Encode::encode(&false); - self.storage.write().set(prefix, &WORKER_STATUS, &encoded_value); + self.storage + .local_storage_set(StorageKind::PERSISTENT, &WORKER_STATUS, &encoded_value); } } diff --git a/runtimes/mainnet/src/lib.rs b/runtimes/mainnet/src/lib.rs index 89b86fbf8..748118f5b 100644 --- a/runtimes/mainnet/src/lib.rs +++ b/runtimes/mainnet/src/lib.rs @@ -122,7 +122,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to 0. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 314, + spec_version: 315, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 3, diff --git a/scripts/fetch_checkpoint.sh b/scripts/fetch_checkpoint.sh new file mode 100644 index 000000000..a3ae5c933 --- /dev/null +++ b/scripts/fetch_checkpoint.sh @@ -0,0 +1,7 @@ +#!/bin/bash +curl -H "Content-Type: application/json" -d '{ + "jsonrpc":"2.0", + "id":1, + "method":"ob_fetchCheckpoint", + "params":[] +}' http://localhost:9944 diff --git a/scripts/fetch_obRecoverState.sh b/scripts/fetch_obRecoverState.sh new file mode 100644 index 000000000..9c2c3b01b --- /dev/null +++ b/scripts/fetch_obRecoverState.sh @@ -0,0 +1,7 @@ +#!/bin/bash +curl -H "Content-Type: application/json" -d '{ + "jsonrpc":"2.0", + "id":1, + "method":"ob_getRecoverState", + "params":[] +}' http://localhost:9944