Skip to content

Commit 5cd43e2

Browse files
committed
Tests & minor fixes
1 parent ea68a06 commit 5cd43e2

File tree

3 files changed

+324
-26
lines changed

3 files changed

+324
-26
lines changed

pallets/price-aggregator/src/lib.rs

+46-19
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,28 @@ impl ProcessBlockValues for AverageBlockValue {
7373
}
7474
}
7575

76-
const LOG_TARGET: &str = "price-aggregator";
76+
/// Used to calculate the median of the accumulated values.
77+
pub struct MedianBlockValue;
78+
impl ProcessBlockValues for MedianBlockValue {
79+
fn process(values: &[CurrencyAmount]) -> Result<CurrencyAmount, &'static str> {
80+
if values.is_empty() {
81+
return Err("No values exist for the current block.");
82+
}
83+
84+
let mut sorted_values = values.to_vec();
85+
sorted_values.sort_unstable();
86+
87+
let mid = sorted_values.len() / 2;
88+
89+
if sorted_values.len() % 2 == 0 {
90+
Ok(sorted_values[mid.saturating_sub(1)]
91+
.saturating_add(sorted_values[mid])
92+
.saturating_mul(CurrencyAmount::from_rational(1, 2)))
93+
} else {
94+
Ok(sorted_values[mid])
95+
}
96+
}
97+
}
7798

7899
/// Used to aggregate the accumulated values over some time period.
79100
///
@@ -199,6 +220,8 @@ impl<L: Get<u32>> CircularBuffer<L> {
199220
}
200221
}
201222

223+
const LOG_TARGET: &str = "price-aggregator";
224+
202225
#[frame_support::pallet]
203226
pub mod pallet {
204227
use super::*;
@@ -274,12 +297,16 @@ pub mod pallet {
274297
Self::process_block_aggregated_values();
275298

276299
// 2. Check if we need to push the average aggregated value to the storage.
277-
if IntermediateValueAggregator::<T>::get().limit_block >= now.saturated_into() {
300+
if IntermediateValueAggregator::<T>::get().limit_block <= now.saturated_into() {
278301
Self::process_intermediate_aggregated_values(now);
279302
}
280303
}
281304

282-
// TODO: integration tests!
305+
fn integrity_test() {
306+
assert!(T::MaxValuesPerBlock::get() > 0);
307+
assert!(T::CircularBufferLength::get() > 0);
308+
assert!(!T::AggregationDuration::get().is_zero());
309+
}
283310
}
284311

285312
impl<T: Config> Pallet<T> {
@@ -297,7 +324,7 @@ pub mod pallet {
297324
) {
298325
Ok(value) => value,
299326
Err(message) => {
300-
log::error!(
327+
log::trace!(
301328
target: LOG_TARGET,
302329
"Failed to process the accumulated native currency values in the current block. \
303330
Reason: {:?}",
@@ -309,22 +336,22 @@ pub mod pallet {
309336
}
310337
};
311338

312-
// TODO: is it ok to ignore this? A bit confused what happens actually in the closure.
313339
// 3. Attempt to store the processed value.
314-
let _ignore = IntermediateValueAggregator::<T>::try_mutate(
315-
|aggregator| match aggregator.try_add(processed_value) {
316-
Ok(new_aggregator) => Ok(new_aggregator),
317-
Err(message) => {
318-
log::error!(
319-
target: LOG_TARGET,
320-
"Failed to add the processed native currency value to the intermediate storage. \
321-
Reason: {:?}",
322-
message
323-
);
324-
Err(())
325-
}
326-
},
327-
);
340+
// This operation is practically infallible, but we check the results for the additional safety.
341+
let intermediate_value = IntermediateValueAggregator::<T>::get();
342+
match intermediate_value.try_add(processed_value) {
343+
Ok(new_aggregator) => {
344+
IntermediateValueAggregator::<T>::put(new_aggregator);
345+
}
346+
Err(message) => {
347+
log::error!(
348+
target: LOG_TARGET,
349+
"Failed to add the processed native currency value to the intermediate storage. \
350+
Reason: {:?}",
351+
message
352+
);
353+
}
354+
}
328355
}
329356

330357
/// Used to process the intermediate aggregated values, and push them to the moving average storage.

pallets/price-aggregator/src/mock.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222

2323
use frame_support::{
2424
construct_runtime, parameter_types,
25-
traits::{ConstU128, ConstU32},
25+
traits::{ConstU128, ConstU32, Hooks},
2626
weights::Weight,
2727
};
2828
use sp_core::H256;
@@ -117,12 +117,16 @@ impl ExtBuilder {
117117

118118
let mut ext = TestExternalities::from(storage);
119119
ext.execute_with(|| {
120-
System::set_block_number(1);
121-
120+
// 1. Set the initial limit block for the intermediate value aggregator
122121
IntermediateValueAggregator::<Test>::mutate(|v| {
123122
v.limit_block =
124123
<Test as pallet_price_aggregator::Config>::AggregationDuration::get() + 1
125124
});
125+
126+
// 2. Init block setting
127+
let init_block_number = 1;
128+
System::set_block_number(init_block_number);
129+
PriceAggregator::on_initialize(init_block_number);
126130
});
127131

128132
ext

0 commit comments

Comments
 (0)