Skip to content

Commit

Permalink
Restore offchin worker state
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauthamastro committed Mar 20, 2024
1 parent 6f50723 commit 8f42e19
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 49 deletions.
25 changes: 15 additions & 10 deletions pallets/ocex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1931,47 +1931,52 @@ pub mod pallet {
for (account, _) in &account_ids {
let main = Self::transform_account(account.clone())?;
// Get Q scores
let (q_scores, uptime) =
crate::lmp::get_q_score_and_uptime(&mut state, epoch, &pair, &main)?;
let q_scores_map = crate::lmp::get_q_score_and_uptime_for_checkpoint(
&mut state, epoch, &pair, &main,
)?;

if !q_scores.is_zero() || !uptime.is_zero() {
q_scores_uptime_map.insert((epoch,pair,main.clone()),(q_scores,uptime));
if !q_scores_map.is_empty() {
q_scores_uptime_map.insert((epoch, pair, main.clone()), q_scores_map);
}

let fees_paid = crate::lmp::get_fees_paid_by_main_account_in_quote(
&mut state, epoch, &pair, &main,
)?;

fees_paid_map.insert((epoch,pair,main.clone()),fees_paid);
fees_paid_map.insert((epoch, pair, main.clone()), fees_paid);

let maker_volume = crate::lmp::get_maker_volume_by_main_account(
&mut state, epoch, &pair, &main,
)?;

maker_volume_map.insert((epoch,pair,main.clone()),maker_volume);
maker_volume_map.insert((epoch, pair, main.clone()), maker_volume);

let trade_volume = crate::lmp::get_trade_volume_by_main_account(
&mut state, epoch, &pair, &main,
)?;

taker_volume_map.insert((epoch, pair, main.clone()),trade_volume);
taker_volume_map.insert((epoch, pair, main.clone()), trade_volume);
}
let total_maker_volume =
crate::lmp::get_total_maker_volume(&mut state, epoch, &pair)?;
total_maker_volume_map.insert((epoch,pair), total_maker_volume);
total_maker_volume_map.insert((epoch, pair), total_maker_volume);
}
}

let config = crate::lmp::get_lmp_config(&mut state, current_epoch)?;


log::debug!(target:"ocex", "fetch_checkpoint returning");
Ok(ObCheckpointRaw::new(
snapshot_id,
balances,
last_processed_block_number,
state_change_id,
config
config,
q_scores_uptime_map,
maker_volume_map,
taker_volume_map,
fees_paid_map,
total_maker_volume_map,
))
}

Expand Down
69 changes: 35 additions & 34 deletions pallets/ocex/src/lmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@ pub mod keys {
pub fn update_trade_volume_by_main_account(
state: &mut OffchainState,
epoch: u16,
market: &TradingPairConfig,
trading_pair: TradingPair,
volume: Decimal,
main: &AccountId,
) -> Result<Decimal, &'static str> {
let trading_pair = TradingPair::from(market.quote_asset, market.base_asset);
let key = get_trade_volume_by_main_account_key(epoch, trading_pair, &main);
Ok(match state.get(&key)? {
None => {
Expand Down Expand Up @@ -156,11 +155,10 @@ pub fn get_maker_volume_by_main_account(
pub fn update_maker_volume_by_main_account(
state: &mut OffchainState,
epoch: u16,
market: &TradingPairConfig,
trading_pair: TradingPair,
volume: Decimal,
main: &AccountId,
) -> Result<Decimal, &'static str> {
let trading_pair = TradingPair::from(market.quote_asset, market.base_asset);
let key = get_maker_volume_by_main_account_key(epoch, trading_pair, &main);
Ok(match state.get(&key)? {
None => {
Expand Down Expand Up @@ -200,10 +198,9 @@ pub fn get_total_maker_volume(
pub fn update_total_maker_volume(
state: &mut OffchainState,
epoch: u16,
market: &TradingPairConfig,
trading_pair: TradingPair,
volume: Decimal,
) -> Result<Decimal, &'static str> {
let trading_pair = TradingPair::from(market.quote_asset, market.base_asset);
let key = get_total_maker_volume_key(epoch, trading_pair);
Ok(match state.get(&key)? {
None => {
Expand All @@ -224,11 +221,10 @@ pub fn update_total_maker_volume(
pub fn store_fees_paid_by_main_account_in_quote(
state: &mut OffchainState,
epoch: u16,
market: &TradingPairConfig,
trading_pair: TradingPair,
fees_in_quote_terms: Decimal,
main: &AccountId,
) -> Result<Decimal, &'static str> {
let trading_pair = TradingPair::from(market.quote_asset, market.base_asset);
let key = get_fees_paid_by_main_account(epoch, trading_pair, main);
Ok(match state.get(&key)? {
None => {
Expand Down Expand Up @@ -340,6 +336,28 @@ pub fn get_q_score_and_uptime(
}
}

/// Returns the individial Q score and uptime indexe
pub fn get_q_score_and_uptime_for_checkpoint(
state: &mut OffchainState,
epoch: u16,
trading_pair: &TradingPair,
main: &AccountId,
) -> Result<BTreeMap<u16, Decimal>, &'static str> {
let key = get_q_score_uptime_by_main_account(epoch, *trading_pair, main);
match state.get(&key)? {
None => {
log::warn!(target:"ocex","q_score&uptime not found for: main: {:?}, market: {:?}",main.to_ss58check_with_version(Ss58AddressFormat::from(POLKADEX_MAINNET_SS58)), trading_pair.to_string());
// If the q_score is not found, zero will be returned.
Ok(Default::default())
},
Some(encoded_q_scores_map) => {
let map = BTreeMap::<u16, Decimal>::decode(&mut &encoded_q_scores_map[..])
.map_err(|_| "Unable to decode decimal")?;
Ok(map)
},
}
}

impl<T: Config> Pallet<T> {
/// Updates the respective offchain DB trie keys for LMP metrics from given trade
pub fn update_lmp_storage_from_trade(
Expand All @@ -350,35 +368,18 @@ impl<T: Config> Pallet<T> {
taker_fees: Decimal,
) -> Result<(), &'static str> {
let epoch: u16 = <LMPEpoch<T>>::get();
let pair = TradingPair::from(config.quote_asset, config.base_asset);

// Store trade.price * trade.volume as maker volume for this epoch
let volume = trade.price.saturating_mul(trade.amount);
// Update the trade volume generated to maker account
update_trade_volume_by_main_account(
state,
epoch,
&config,
volume,
&trade.maker.main_account,
)?;
update_trade_volume_by_main_account(state, epoch, pair, volume, &trade.maker.main_account)?;
// Update the trade volume generated to taker account
update_trade_volume_by_main_account(
state,
epoch,
&config,
volume,
&trade.taker.main_account,
)?;
update_trade_volume_by_main_account(state, epoch, pair, volume, &trade.taker.main_account)?;
// Update the maker volume generated to account
update_maker_volume_by_main_account(
state,
epoch,
&config,
volume,
&trade.maker.main_account,
)?;
update_maker_volume_by_main_account(state, epoch, pair, volume, &trade.maker.main_account)?;
// Update the total maker volume generated
update_total_maker_volume(state, epoch, &config, volume)?;
update_total_maker_volume(state, epoch, pair, volume)?;

// Store maker_fees and taker_fees for the corresponding main account for this epoch
match trade.maker.side {
Expand All @@ -387,7 +388,7 @@ impl<T: Config> Pallet<T> {
store_fees_paid_by_main_account_in_quote(
state,
epoch,
&config,
pair,
fees,
&trade.maker.main_account,
)?;
Expand All @@ -397,7 +398,7 @@ impl<T: Config> Pallet<T> {
store_fees_paid_by_main_account_in_quote(
state,
epoch,
&config,
pair,
fees,
&trade.taker.main_account,
)?;
Expand All @@ -408,7 +409,7 @@ impl<T: Config> Pallet<T> {
store_fees_paid_by_main_account_in_quote(
state,
epoch,
&config,
pair,
fees,
&trade.maker.main_account,
)?;
Expand All @@ -418,7 +419,7 @@ impl<T: Config> Pallet<T> {
store_fees_paid_by_main_account_in_quote(
state,
epoch,
&config,
pair,
fees,
&trade.taker.main_account,
)?;
Expand Down
41 changes: 41 additions & 0 deletions pallets/ocex/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,47 @@ impl<T: Config> Pallet<T> {
value.insert(account_asset.asset, *balance);
state.insert(key, value.encode());
}
// Store LMP Config
crate::lmp::store_lmp_config(state, checkpoint.config);
// Restore Q scores and Uptime map
for ((epoch, pair, main), map) in &checkpoint.q_scores_uptime_map {
for (index, q_score) in map {
crate::lmp::store_q_score_and_uptime(state, *epoch, *index, *q_score, pair, main)?;
}
}
// Restore maker volume
for ((epoch, pair, main), maker_volume) in &checkpoint.maker_volume_map {
crate::lmp::update_maker_volume_by_main_account(
state,
*epoch,
*pair,
*maker_volume,
main,
)?;
}

// Restore taker volume
for ((epoch, pair, main), maker_volume) in &checkpoint.taker_volume_map {
crate::lmp::update_trade_volume_by_main_account(
state,
*epoch,
*pair,
*maker_volume,
main,
)?;
}

// Restore taker volume
for ((epoch, pair, main), fees_paid) in &checkpoint.fees_paid_map {
crate::lmp::store_fees_paid_by_main_account_in_quote(
state, *epoch, *pair, *fees_paid, main,
)?;
}

for ((epoch, pair), total_maker_volume) in &checkpoint.total_maker_volume_map {
crate::lmp::update_total_maker_volume(state, *epoch, *pair, *total_maker_volume)?;
}

Ok(())
}

Expand Down
33 changes: 30 additions & 3 deletions primitives/orderbook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use rust_decimal::prelude::ToPrimitive;
use rust_decimal::Decimal;
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sp_core::crypto::AccountId32;
use sp_core::H256;
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};
use crate::lmp::LMPConfig;
Expand Down Expand Up @@ -170,7 +171,12 @@ pub struct ObCheckpointRaw {
/// State change id
pub state_change_id: u64,
/// LMPConfig
pub config: LMPConfig
pub config: LMPConfig,
pub q_scores_uptime_map: BTreeMap<(u16, TradingPair, AccountId32), BTreeMap<u16, Decimal>>,
pub maker_volume_map: BTreeMap<(u16, TradingPair, AccountId32), Decimal>,
pub taker_volume_map: BTreeMap<(u16, TradingPair, AccountId32), Decimal>,
pub fees_paid_map: BTreeMap<(u16, TradingPair, AccountId32), Decimal>,
pub total_maker_volume_map: BTreeMap<(u16, TradingPair), Decimal>,
}

impl ObCheckpointRaw {
Expand All @@ -187,9 +193,25 @@ impl ObCheckpointRaw {
balances: BTreeMap<AccountAsset, Decimal>,
last_processed_block_number: BlockNumber,
state_change_id: u64,
config: LMPConfig
config: LMPConfig,
q_scores_uptime_map: BTreeMap<(u16, TradingPair, AccountId32), BTreeMap<u16, Decimal>>,
maker_volume_map: BTreeMap<(u16, TradingPair, AccountId32), Decimal>,
taker_volume_map: BTreeMap<(u16, TradingPair, AccountId32), Decimal>,
fees_paid_map: BTreeMap<(u16, TradingPair, AccountId32), Decimal>,
total_maker_volume_map: BTreeMap<(u16, TradingPair), Decimal>,
) -> Self {
Self { snapshot_id, balances, last_processed_block_number, state_change_id, config }
Self {
snapshot_id,
balances,
last_processed_block_number,
state_change_id,
config,
q_scores_uptime_map,
maker_volume_map,
taker_volume_map,
fees_paid_map,
total_maker_volume_map
}
}

/// Convert `ObCheckpointRaw` to `ObCheckpoint`.
Expand All @@ -203,6 +225,11 @@ impl ObCheckpointRaw {
last_processed_block_number: self.last_processed_block_number,
state_change_id: self.state_change_id,
config: self.config,
q_scores_uptime_map: self.q_scores_uptime_map,
maker_volume_map: self.maker_volume_map,
taker_volume_map: self.taker_volume_map,
fees_paid_map: self.fees_paid_map,
total_maker_volume_map: self.total_maker_volume_map
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions primitives/orderbook/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use rust_decimal::Decimal;
use scale_info::TypeInfo;
use serde_with::{json::JsonString, serde_as};
use std::collections::BTreeMap;
use sp_core::crypto::AccountId32;
use crate::lmp::LMPConfig;
use crate::types::TradingPair;

/// A struct representing the recovery state of an Order Book.
#[serde_as]
Expand Down Expand Up @@ -58,7 +60,17 @@ pub struct ObCheckpoint {
/// State change id
pub state_change_id: u64,
/// LMP COnfig
pub config: LMPConfig
pub config: LMPConfig,
#[serde_as(as = "JsonString<Vec<(JsonString, _)>>")]
pub q_scores_uptime_map: BTreeMap<(u16, TradingPair, AccountId32), BTreeMap<u16, Decimal>>,
#[serde_as(as = "JsonString<Vec<(JsonString, _)>>")]
pub maker_volume_map: BTreeMap<(u16, TradingPair, AccountId32), Decimal>,
#[serde_as(as = "JsonString<Vec<(JsonString, _)>>")]
pub taker_volume_map: BTreeMap<(u16, TradingPair, AccountId32), Decimal>,
#[serde_as(as = "JsonString<Vec<(JsonString, _)>>")]
pub fees_paid_map: BTreeMap<(u16, TradingPair, AccountId32), Decimal>,
#[serde_as(as = "JsonString<Vec<(JsonString, _)>>")]
pub total_maker_volume_map: BTreeMap<(u16, TradingPair), Decimal>,
}

impl ObCheckpoint {
Expand All @@ -69,7 +81,12 @@ impl ObCheckpoint {
balances: self.balances.clone(),
last_processed_block_number: self.last_processed_block_number,
state_change_id: self.state_change_id,
config: self.config
config: self.config,
q_scores_uptime_map: self.q_scores_uptime_map.clone(),
maker_volume_map: self.maker_volume_map.clone(),
taker_volume_map: self.taker_volume_map.clone(),
fees_paid_map: self.fees_paid_map.clone(),
total_maker_volume_map: self.total_maker_volume_map.clone(),
}
}
}
Expand Down

0 comments on commit 8f42e19

Please sign in to comment.