Skip to content

Commit

Permalink
Gas cost is only calculated in ondemand mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sulijia committed Mar 29, 2024
1 parent 6a7edc8 commit 031fb06
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
7 changes: 5 additions & 2 deletions pallets/liquidation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,11 @@ pub mod pallet {
let base_account_balance = <T as pallet::Config>::Currency::free_balance(&base_account);

let (collator, real_gas_cost) = match T::OrderGasCost::gas_cost(n) {
Some((collator, real_gas_cost)) => (collator, real_gas_cost),
None => {
Ok(cost) => match cost {
Some((collator, real_gas_cost)) => (collator, real_gas_cost),
None => return,
},
Err(_) => {
Self::deposit_event(Event::Error(Error::<T>::FailedToFetchRealGasCost.into()));
return;
},
Expand Down
6 changes: 4 additions & 2 deletions pallets/liquidation/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ where
T: pallet_order::Config,
T::AccountId: From<[u8; 32]>,
{
fn gas_cost(_block_number: BlockNumberFor<T>) -> Option<(T::AccountId, Balance)> {
fn gas_cost(
_block_number: BlockNumberFor<T>,
) -> Result<Option<(T::AccountId, Balance)>, sp_runtime::DispatchError> {
let account = T::AccountId::try_from(COLLATOR_BYTES).unwrap();
Some((account, 10000000 as u128))
Ok(Some((account, 10000000 as u128)))
}
}

Expand Down
4 changes: 3 additions & 1 deletion pallets/order/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,5 +407,7 @@ impl<T: Config> Pallet<T> {
}

pub trait OrderGasCost<T: frame_system::Config> {
fn gas_cost(block_number: BlockNumberFor<T>) -> Option<(T::AccountId, Balance)>;
fn gas_cost(
block_number: BlockNumberFor<T>,
) -> Result<Option<(T::AccountId, Balance)>, DispatchError>;
}
21 changes: 15 additions & 6 deletions pallets/order/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,22 @@ where
T: crate::Config,
T::AccountId: From<[u8; 32]>,
{
fn gas_cost(block_number: BlockNumberFor<T>) -> Option<(T::AccountId, Balance)> {
let sequece_number = <crate::Pallet<T>>::block_2_sequence(block_number)?;
let order = <crate::Pallet<T>>::order_map(sequece_number)?;
fn gas_cost(
block_number: BlockNumberFor<T>,
) -> Result<Option<(T::AccountId, Balance)>, sp_runtime::DispatchError> {
let sequece_number = <crate::Pallet<T>>::block_2_sequence(block_number);
if sequece_number.is_none() {
return Ok(None);
}
let order = <crate::Pallet<T>>::order_map(
sequece_number.ok_or(sp_runtime::DispatchError::Other("sequece_number is none"))?,
)
.ok_or(sp_runtime::DispatchError::Other("Not exist order"))?;
let mut r = [0u8; 32];
r.copy_from_slice(order.orderer.encode().as_slice());
let account = T::AccountId::try_from(r).ok()?;
Some((account, order.price))
let account = T::AccountId::try_from(r)
.map_err(|_| sp_runtime::DispatchError::Other("Account error"))?;
Ok(Some((account, order.price)))
}
}

Expand All @@ -142,7 +151,7 @@ pub mod mock_pallet {

impl<T: Config> Pallet<T> {
pub fn get_gas_cost(block_number: BlockNumberFor<T>) -> Option<(T::AccountId, Balance)> {
T::OrderGasCost::gas_cost(block_number)
T::OrderGasCost::gas_cost(block_number).unwrap()
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,13 +696,22 @@ where
T: pallet_order::Config,
T::AccountId: From<[u8; 32]>,
{
fn gas_cost(block_number: BlockNumberFor<T>) -> Option<(T::AccountId, Balance)> {
let sequece_number = <pallet_order::Pallet<T>>::block_2_sequence(block_number)?;
let order = <pallet_order::Pallet<T>>::order_map(sequece_number)?;
fn gas_cost(
block_number: BlockNumberFor<T>,
) -> Result<Option<(T::AccountId, Balance)>, sp_runtime::DispatchError> {
let sequece_number = <pallet_order::Pallet<T>>::block_2_sequence(block_number);
if sequece_number.is_none() {
return Ok(None);
}
let order = <pallet_order::Pallet<T>>::order_map(
sequece_number.ok_or(sp_runtime::DispatchError::Other("sequece_number is none"))?,
)
.ok_or(sp_runtime::DispatchError::Other("Not exist order"))?;
let mut r = [0u8; 32];
r.copy_from_slice(order.orderer.encode().as_slice());
let account = T::AccountId::try_from(r).ok()?;
Some((account, order.price))
let account = T::AccountId::try_from(r)
.map_err(|_| sp_runtime::DispatchError::Other("Account error"))?;
Ok(Some((account, order.price)))
}
}

Expand Down

0 comments on commit 031fb06

Please sign in to comment.