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/Cargo.lock b/Cargo.lock index 59cc052d9..0159e58b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6124,6 +6124,7 @@ dependencies = [ "hex-literal 0.3.4", "liquidity", "log", + "orderbook-primitives", "orml-vesting", "pallet-asset-conversion", "pallet-asset-conversion-tx-payment", @@ -7184,12 +7185,10 @@ dependencies = [ "hash-db", "jsonrpsee", "orderbook-primitives", - "pallet-ocex-lmp", "pallet-ocex-runtime-api", "parity-scale-codec", "parking_lot 0.12.1", "polkadex-primitives", - "rust_decimal", "sc-rpc", "sc-rpc-api", "serde", @@ -7200,7 +7199,6 @@ dependencies = [ "sp-offchain", "sp-rpc", "sp-runtime", - "sp-std", "sp-trie", "tokio", "trie-db", diff --git a/pallets/ocex/rpc/Cargo.toml b/pallets/ocex/rpc/Cargo.toml index 5f3b317fe..e2c76182c 100644 --- a/pallets/ocex/rpc/Cargo.toml +++ b/pallets/ocex/rpc/Cargo.toml @@ -19,11 +19,8 @@ serde_json = { workspace = true, default-features = true } polkadex-primitives = { workspace = true } sc-rpc-api = { workspace = true, default-features = true } sc-rpc = { workspace = true, default-features = true } -sp-std = { workspace = true, default-features = false } sp-offchain = { workspace = true, default-features = true } parking_lot = { workspace = true } hash-db = { workspace = true } trie-db = { workspace = true } sp-trie = { workspace = true } -rust_decimal = { workspace = true, features = ["scale-codec"], default-features = false } -pallet-ocex-lmp = { path = "../../ocex" } diff --git a/pallets/ocex/rpc/runtime-api/src/lib.rs b/pallets/ocex/rpc/runtime-api/src/lib.rs index ff63b3501..42b51b6d9 100644 --- a/pallets/ocex/rpc/runtime-api/src/lib.rs +++ b/pallets/ocex/rpc/runtime-api/src/lib.rs @@ -18,16 +18,20 @@ #![cfg_attr(not(feature = "std"), no_std)] -use parity_scale_codec::{Codec, Decode}; +use orderbook_primitives::ObCheckpointRaw; +use parity_scale_codec::Codec; use polkadex_primitives::AssetId; use rust_decimal::Decimal; use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; sp_api::decl_runtime_apis! { - pub trait PolkadexOcexRuntimeApi where AccountId: Codec, Hash : Codec, BTreeMap>: Decode { - // Returns all on-chain registered main accounts and it's proxies - fn get_main_accounts() -> BTreeMap>; + pub trait PolkadexOcexRuntimeApi where AccountId: Codec, Hash : Codec { + fn get_ob_recover_state() -> Result, sp_runtime::DispatchError>; + // gets balance from given account of given asset + fn get_balance(from: AccountId, of: AssetId) -> Result; + // gets the latest checkpoint from the offchain State + fn fetch_checkpoint() -> Result; // Returns the asset inventory deviation in the offchain State - fn calculate_inventory_deviation(offchain_inventory: BTreeMap, last_processed_block: u32) -> Result, sp_runtime::DispatchError>; + fn calculate_inventory_deviation() -> Result, sp_runtime::DispatchError>; } } diff --git a/pallets/ocex/rpc/src/lib.rs b/pallets/ocex/rpc/src/lib.rs index 775c28a8d..debee635a 100644 --- a/pallets/ocex/rpc/src/lib.rs +++ b/pallets/ocex/rpc/src/lib.rs @@ -27,34 +27,32 @@ use jsonrpsee::{ tracing::log, types::error::{CallError, ErrorObject}, }; -use orderbook_primitives::{ - recovery::{DeviationMap, ObCheckpoint, ObRecoveryState}, - types::AccountAsset, - ObCheckpointRaw, -}; -use pallet_ocex_lmp::{snapshot::StateInfo, validator::STATE_INFO}; +use orderbook_primitives::recovery::{DeviationMap, ObCheckpoint, ObRecoveryState}; pub use pallet_ocex_runtime_api::PolkadexOcexRuntimeApi; -use parity_scale_codec::{Codec, Decode, Encode}; -use parking_lot::RwLock; -use polkadex_primitives::{AccountId, AssetId}; -use rust_decimal::Decimal; +use parity_scale_codec::{Codec, Decode}; +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, ByteArray}; +use sp_core::offchain::{storage::OffchainDb, OffchainDbExt, OffchainStorage}; use sp_runtime::traits::Block as BlockT; -use std::{collections::BTreeMap, sync::Arc}; +use std::sync::Arc; const RUNTIME_ERROR: i32 = 1; const RETRIES: u8 = 3; #[rpc(client, server)] -pub trait PolkadexOcexRpcApi { +pub trait PolkadexOcexRpcApi { #[method(name = "ob_getRecoverState")] - async fn get_ob_recover_state(&self, at: Option) -> RpcResult; + fn get_ob_recover_state(&self, at: Option) -> RpcResult; #[method(name = "ob_getBalance")] - async fn get_balance(&self, account_id: AccountId, of: AssetId) -> RpcResult; + fn get_balance( + &self, + account_id: AccountId, + of: AssetId, + at: Option, + ) -> RpcResult; #[method(name = "ob_inventoryDeviation")] async fn calculate_inventory_deviation(&self, at: Option) -> RpcResult; @@ -75,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 @@ -87,115 +85,65 @@ 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(), } } - - pub fn get_offchain_balances( - &self, - state: &mut pallet_ocex_lmp::storage::OffchainState, - account: &AccountId, - ) -> Result, &'static str> { - match state.get(&account.to_raw_vec())? { - None => Ok(BTreeMap::new()), - Some(encoded) => BTreeMap::decode(&mut &encoded[..]) - .map_err(|_| "Unable to decode balances for account"), - } - } - - /// Loads the state info from the offchain state - pub fn load_state_info( - &self, - state: &mut pallet_ocex_lmp::storage::OffchainState, - ) -> Result { - match state.get(&STATE_INFO.to_vec())? { - Some(data) => Ok(StateInfo::decode(&mut &data[..]).unwrap_or_default()), - None => Ok(StateInfo::default()), - } - } } #[async_trait] -impl PolkadexOcexRpcApiServer<::Hash, Hash> +impl + PolkadexOcexRpcApiServer<::Hash, AccountId, Hash> for PolkadexOcexRpc where Block: BlockT, Client: Send + Sync + 'static + ProvideRuntimeApi + HeaderBackend, Client::Api: PolkadexOcexRuntimeApi, + AccountId: Codec, Hash: Codec, T: OffchainStorage + 'static, { - async fn get_ob_recover_state( + fn get_ob_recover_state( &self, at: Option<::Hash>, ) -> RpcResult { - // Acquire offchain storage lock - let offchain_storage = offchain::OffchainStorageAdapter::new(self.storage.clone()); - if !offchain_storage.acquire_offchain_lock(3).await { - return Err(runtime_error_into_rpc_err("Failed to acquire offchain lock")) - } - - // 1. Load Offchain State - let mut root = pallet_ocex_lmp::storage::load_trie_root(); - log::info!(target:"ocex-rpc","state_root {:?}", root); - let mut storage = pallet_ocex_lmp::storage::State; - let mut state = pallet_ocex_lmp::storage::OffchainState::load(&mut storage, &mut root); - - 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, }; - - log::debug!(target:"ocex", "fetch_ob_recovery called"); - let main_accounts: sp_std::collections::btree_map::BTreeMap< - AccountId, - sp_std::vec::Vec, - > = api.get_main_accounts(at).map_err(runtime_error_into_rpc_err)?; - - let mut balances: BTreeMap = BTreeMap::new(); - // all offchain balances for main accounts - for main in main_accounts.keys() { - let b = self - .get_offchain_balances(&mut state, main) - .map_err(runtime_error_into_rpc_err)?; - for (asset, balance) in b.into_iter() { - balances.insert(AccountAsset { main: main.clone(), asset }, balance); - } - } - let state_info = self.load_state_info(&mut state).map_err(runtime_error_into_rpc_err)?; - let last_processed_block_number = state_info.last_block; - let snapshot_id = state_info.snapshot_id; - let state_change_id = state_info.stid; - log::debug!(target:"ocex", "fetch_ob_recovery returning"); - Ok(ObRecoveryState { - snapshot_id, - account_ids: main_accounts, - balances, - last_processed_block_number, - state_change_id, - worker_nonce: 0, - }) + 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 + Decode::decode( + &mut api + .get_ob_recover_state(at) + .map_err(runtime_error_into_rpc_err)? + .map_err(runtime_error_into_rpc_err)? + .as_ref(), + ) + .map_err(runtime_error_into_rpc_err) } - async fn get_balance(&self, account_id: AccountId, of: AssetId) -> RpcResult { - // Acquire offchain storage lock - let offchain_storage = offchain::OffchainStorageAdapter::new(self.storage.clone()); - if !offchain_storage.acquire_offchain_lock(3).await { - return Err(runtime_error_into_rpc_err("Failed to acquire offchain lock")) - } + fn get_balance( + &self, + account_id: AccountId, + of: AssetId, + at: Option<::Hash>, + ) -> RpcResult { + let mut api = self.client.runtime_api(); + let at = match at { + Some(at) => at, + None => self.client.info().best_hash, + }; - let mut root = pallet_ocex_lmp::storage::load_trie_root(); - log::info!(target:"ocex-rpc","state_root {:?}", root); - let mut storage = pallet_ocex_lmp::storage::State; - let mut state = pallet_ocex_lmp::storage::OffchainState::load(&mut storage, &mut root); - let balances = self - .get_offchain_balances(&mut state, &account_id) - .map_err(runtime_error_into_rpc_err)?; - let balance = balances.get(&of).copied().unwrap_or_default(); - let json = serde_json::to_string(&balance).map_err(runtime_error_into_rpc_err)?; + 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 = + serde_json::to_string(&runtime_api_result).map_err(runtime_error_into_rpc_err)?; Ok(json) } @@ -204,57 +152,28 @@ 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")) } - - log::info!(target:"ocex-rpc","calculating the inventory deviation.."); - // 1. Load last processed blk - let mut root = pallet_ocex_lmp::storage::load_trie_root(); - log::info!(target:"ocex-rpc","state_root {:?}", root); - let mut storage = pallet_ocex_lmp::storage::State; - let mut state = pallet_ocex_lmp::storage::OffchainState::load(&mut storage, &mut root); - let state_info = self.load_state_info(&mut state).map_err(runtime_error_into_rpc_err)?; - let last_processed_blk = state_info.last_block; - // 2. Load all main accounts and registered assets from on-chain - let mut offchain_inventory = BTreeMap::new(); - let main_accounts = api.get_main_accounts(at).map_err(runtime_error_into_rpc_err)?; - for main in main_accounts { - // 3. Compute sum of all balances of all assets - let balances: BTreeMap = self - .get_offchain_balances( - &mut state, - &Decode::decode(&mut &main.encode()[..]).unwrap(), - ) - .map_err(runtime_error_into_rpc_err)?; - for (asset, balance) in balances { - offchain_inventory - .entry(asset) - .and_modify(|total: &mut Decimal| { - *total = (*total).saturating_add(balance); - }) - .or_insert(balance); - } - } - - let deviation = match api - .calculate_inventory_deviation(at, offchain_inventory, last_processed_blk) - .map_err(runtime_error_into_rpc_err)? - { - Err(err) => { - log::error!(target:"ocex","Error calling calculate_inventory_deviation: {:?}",err); - return Err(runtime_error_into_rpc_err( - "Error calling calculate_inventory_deviation ", - )) - }, - Ok(deviation_map) => DeviationMap::new(deviation_map), - }; + log::info!(target:"ocex","calculating the inventory deviation.."); + let deviation = + match api.calculate_inventory_deviation(at).map_err(runtime_error_into_rpc_err)? { + Err(err) => { + log::error!(target:"ocex","Error calling calculate_inventory_deviation: {:?}",err); + return Err(runtime_error_into_rpc_err( + "Error calling calculate_inventory_deviation ", + )) + }, + Ok(deviation_map) => DeviationMap::new(deviation_map), + }; log::info!(target:"ocex","serializing the deviation map.."); let json = serde_json::to_string(&deviation).map_err(runtime_error_into_rpc_err)?; Ok(json) @@ -265,49 +184,21 @@ 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")) } - - // 1. Load Offchain State - let mut root = pallet_ocex_lmp::storage::load_trie_root(); - log::info!(target:"ocex-rpc","state_root {:?}", root); - let mut storage = pallet_ocex_lmp::storage::State; - let mut state = pallet_ocex_lmp::storage::OffchainState::load(&mut storage, &mut root); - - log::debug!(target:"ocex", "fetch_checkpoint called"); - let main_accounts: sp_std::collections::btree_map::BTreeMap< - AccountId, - sp_std::vec::Vec, - > = api.get_main_accounts(at).map_err(runtime_error_into_rpc_err)?; - - let mut balances: BTreeMap = BTreeMap::new(); - // all offchain balances for main accounts - for main in main_accounts.keys() { - let b = self - .get_offchain_balances(&mut state, main) - .map_err(runtime_error_into_rpc_err)?; - for (asset, balance) in b.into_iter() { - balances.insert(AccountAsset { main: main.clone(), asset }, balance); - } - } - let state_info = self.load_state_info(&mut state).map_err(runtime_error_into_rpc_err)?; - let last_processed_block_number = state_info.last_block; - let snapshot_id = state_info.snapshot_id; - let state_change_id = state_info.stid; - log::debug!(target:"ocex", "fetch_checkpoint returning"); - let ob_checkpoint_raw = ObCheckpointRaw::new( - snapshot_id, - balances, - last_processed_block_number, - state_change_id, - ); + let ob_checkpoint_raw = api + .fetch_checkpoint(at) + .map_err(runtime_error_into_rpc_err)? + .map_err(runtime_error_into_rpc_err)?; let ob_checkpoint = ob_checkpoint_raw.to_checkpoint(); Ok(ob_checkpoint) } 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/pallets/ocex/src/lib.rs b/pallets/ocex/src/lib.rs index 3a48f07cc..cc2fe03ea 100644 --- a/pallets/ocex/src/lib.rs +++ b/pallets/ocex/src/lib.rs @@ -40,7 +40,7 @@ use frame_support::{ use frame_system::ensure_signed; use pallet_timestamp as timestamp; use parity_scale_codec::Encode; -use polkadex_primitives::{assets::AssetId, UNIT_BALANCE}; +use polkadex_primitives::{assets::AssetId, AccountId, UNIT_BALANCE}; use rust_decimal::Decimal; use sp_application_crypto::RuntimeAppPublic; use sp_core::crypto::KeyTypeId; @@ -52,7 +52,8 @@ use sp_std::{ops::Div, prelude::*}; // Re-export pallet items so that they can be accessed from the crate namespace. use frame_system::pallet_prelude::BlockNumberFor; use orderbook_primitives::{ - types::TradingPair, SnapshotSummary, ValidatorSet, GENESIS_AUTHORITY_SET_ID, + types::{AccountAsset, TradingPair}, + SnapshotSummary, ValidatorSet, GENESIS_AUTHORITY_SET_ID, }; pub use pallet::*; use polkadex_primitives::ocex::TradingPairConfig; @@ -60,9 +61,6 @@ use polkadex_primitives::ocex::TradingPairConfig; use sp_runtime::traits::One; use sp_std::vec::Vec; -use orderbook_primitives::types::AccountAsset; -use polkadex_primitives::AccountId; - #[cfg(test)] mod mock; #[cfg(test)] @@ -96,7 +94,7 @@ pub mod aggregator; mod benchmarking; pub mod rpc; mod settlement; -pub mod snapshot; +mod snapshot; pub mod storage; pub mod validator; diff --git a/pallets/ocex/src/rpc.rs b/pallets/ocex/src/rpc.rs index 622d1e35d..864855b67 100644 --- a/pallets/ocex/src/rpc.rs +++ b/pallets/ocex/src/rpc.rs @@ -18,18 +18,21 @@ use crate::{ pallet::{Accounts, AllowlistedToken, IngressMessages}, + storage::OffchainState, validator::WORKER_STATUS, Config, Pallet, }; use frame_system::pallet_prelude::BlockNumberFor; -use polkadex_primitives::AssetId; +use parity_scale_codec::{Decode, Encode}; +use polkadex_primitives::{AccountId, AssetId}; use rust_decimal::Decimal; +use sp_application_crypto::ByteArray; use sp_runtime::{ offchain::storage::{StorageRetrievalError, StorageValueRef}, traits::BlockNumberProvider, DispatchError, SaturatedConversion, }; -use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; +use sp_std::collections::btree_map::BTreeMap; impl Pallet { /// Try to acquire the offchain storage lock ( tries for 3 times ) @@ -67,22 +70,43 @@ impl Pallet { s_info.set(&false); // Set WORKER_STATUS to true } - /// Returns all registered main accounts - pub fn get_all_main_accounts() -> BTreeMap> { - let mut main_accounts = BTreeMap::new(); - for (main, info) in >::iter() { - main_accounts.insert(main, info.proxies.to_vec().clone()); + pub fn get_balances( + state: &mut OffchainState, + account: &AccountId, + ) -> Result, &'static str> { + match state.get(&account.to_raw_vec())? { + None => Ok(BTreeMap::new()), + Some(encoded) => BTreeMap::decode(&mut &encoded[..]) + .map_err(|_| "Unable to decode balances for account"), } - main_accounts } /// Calculates the deviation of all assets with Offchain and On-chain data. /// - /// Returns the deviation ( On-chain - Off-chain ) - pub fn calculate_inventory_deviation( - last_processed_blk: u32, - offchain_inventory: BTreeMap, - ) -> Result, DispatchError> { + /// This is a blocking call for offchain worker. + pub fn calculate_inventory_deviation() -> Result, DispatchError> { + // 1. Load last processed blk + let mut root = crate::storage::load_trie_root(); + log::info!(target:"ocex-rpc","state_root {:?}", root); + let mut storage = crate::storage::State; + let mut state = OffchainState::load(&mut storage, &mut root); + let state_info = Self::load_state_info(&mut state)?; + let last_processed_blk = state_info.last_block; + // 2. Load all main accounts and registered assets from on-chain + let mut offchain_inventory = BTreeMap::new(); + for (main, _) in >::iter() { + // 3. Compute sum of all balances of all assets + let balances: BTreeMap = + Self::get_balances(&mut state, &Decode::decode(&mut &main.encode()[..]).unwrap())?; + for (asset, balance) in balances { + offchain_inventory + .entry(asset) + .and_modify(|total: &mut Decimal| { + *total = (*total).saturating_add(balance); + }) + .or_insert(balance); + } + } // 4. Load assets pallet balances of registered assets let assets = >::get(); let mut onchain_inventory = BTreeMap::new(); diff --git a/pallets/ocex/src/storage.rs b/pallets/ocex/src/storage.rs index 27429f7e2..a69d3089e 100644 --- a/pallets/ocex/src/storage.rs +++ b/pallets/ocex/src/storage.rs @@ -232,7 +232,7 @@ pub fn prefixed_key(key: &::Out, prefix: Prefix) -> Vec ::Out { +pub(crate) fn load_trie_root() -> ::Out { let root_ref = StorageValueRef::persistent(&TRIE_ROOT); match root_ref.get::<::Out>() { Ok(Some(root)) => root, diff --git a/pallets/ocex/src/tests.rs b/pallets/ocex/src/tests.rs index 51cc5c1ed..aeb7365b1 100644 --- a/pallets/ocex/src/tests.rs +++ b/pallets/ocex/src/tests.rs @@ -20,7 +20,6 @@ use crate::{storage::store_trie_root, *}; use frame_support::{assert_noop, assert_ok}; -use orderbook_primitives::types::AccountAsset; use polkadex_primitives::{assets::AssetId, withdrawal::Withdrawal, Signature, UNIT_BALANCE}; use rust_decimal::prelude::{FromPrimitive, ToPrimitive}; use sp_std::collections::btree_map::BTreeMap; diff --git a/pallets/ocex/src/validator.rs b/pallets/ocex/src/validator.rs index bcaf23c18..07c41795c 100644 --- a/pallets/ocex/src/validator.rs +++ b/pallets/ocex/src/validator.rs @@ -41,7 +41,7 @@ use trie_db::{TrieError, TrieMut}; /// Key of the storage that stores the status of an offchain worker pub const WORKER_STATUS: [u8; 28] = *b"offchain-ocex::worker_status"; -pub const STATE_INFO: [u8; 25] = *b"offchain-ocex::state_info"; +const STATE_INFO: [u8; 25] = *b"offchain-ocex::state_info"; pub const LAST_PROCESSED_SNAPSHOT: [u8; 26] = *b"offchain-ocex::snapshot_id"; /// Aggregator endpoint: Even though it is centralized for now, it is trustless /// as it verifies the signature and and relays them to destination. diff --git a/runtimes/mainnet/Cargo.toml b/runtimes/mainnet/Cargo.toml index b97ac99fa..68a16018c 100644 --- a/runtimes/mainnet/Cargo.toml +++ b/runtimes/mainnet/Cargo.toml @@ -98,6 +98,7 @@ orml-vesting = { workspace = true, default-features = false } # Local Dependecies polkadex-primitives = { workspace = true, default-features = false } +orderbook-primitives = { path = "../../primitives/orderbook", default-features = false } pdex-migration = { path = "../../pallets/pdex-migration", default-features = false } # Orderbook @@ -194,6 +195,7 @@ std = [ "thea-executor/std", "frame-try-runtime?/std", "thea-message-handler?/std", + "orderbook-primitives/std", "pallet-asset-conversion/std", "pallet-asset-conversion-tx-payment/std", "frame-system-benchmarking?/std", diff --git a/runtimes/mainnet/src/lib.rs b/runtimes/mainnet/src/lib.rs index 54c6a66c8..748118f5b 100644 --- a/runtimes/mainnet/src/lib.rs +++ b/runtimes/mainnet/src/lib.rs @@ -50,7 +50,6 @@ use frame_system::{ limits::{BlockLength, BlockWeights}, EnsureRoot, EnsureSigned, RawOrigin, }; -use sp_std::collections::btree_map::BTreeMap; #[cfg(any(feature = "std", test))] pub use pallet_balances::Call as BalancesCall; @@ -123,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, @@ -1606,6 +1605,7 @@ use crate::{ impls::CreditToBlockAuthor, sp_api_hidden_includes_construct_runtime::hidden_include::traits::fungible::Inspect, }; +use orderbook_primitives::ObCheckpointRaw; impl_runtime_apis! { impl sp_api::Core for Runtime { fn version() -> RuntimeVersion { @@ -1694,12 +1694,14 @@ impl_runtime_apis! { } impl pallet_ocex_runtime_api::PolkadexOcexRuntimeApi for Runtime { - fn get_main_accounts() -> BTreeMap> { - OCEX::get_all_main_accounts() + fn get_ob_recover_state() -> Result, DispatchError> { Ok(OCEX::get_ob_recover_state()?.encode()) } + fn get_balance(from: AccountId, of: AssetId) -> Result { OCEX::get_balance(from, of) } + fn fetch_checkpoint() -> Result { + OCEX::fetch_checkpoint() } - fn calculate_inventory_deviation(offchain_inventory: BTreeMap, last_processed_blk: u32) -> Result, + fn calculate_inventory_deviation() -> Result, DispatchError> { - OCEX::calculate_inventory_deviation(last_processed_blk,offchain_inventory) + OCEX::calculate_inventory_deviation() } } 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