-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Correct Select Median Implementation #3934
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,44 @@ | ||
use crate::{BitcoinInstance, EthereumInstance, PolkadotInstance, Runtime, RuntimeCall}; | ||
use cf_chains::{btc::BitcoinFeeInfo, dot::PolkadotBalance}; | ||
use cf_primitives::EthAmount; | ||
use codec::{Decode, Encode}; | ||
use pallet_cf_witnesser::WitnessDataExtraction; | ||
use sp_std::{mem, prelude::*}; | ||
|
||
fn select_median<T: Encode + Decode>(data: &mut [Vec<u8>]) -> Option<T> { | ||
fn select_median<T: Ord + Copy>(mut data: Vec<T>) -> Option<T> { | ||
if data.is_empty() { | ||
return None | ||
} | ||
|
||
let len = data.len(); | ||
let median_index = if len % 2 == 0 { (len - 1) / 2 } else { len / 2 }; | ||
// Encoding is order-preserving so we can sort the raw encoded bytes and then decode | ||
// just the result. | ||
let (_, median_bytes, _) = data.select_nth_unstable(median_index); | ||
|
||
match Decode::decode(&mut &median_bytes[..]) { | ||
Ok(median) => Some(median), | ||
Err(e) => { | ||
log::error!("Failed to decode median priority fee: {:?}", e); | ||
None | ||
}, | ||
let median_index = (len - 1) / 2; | ||
let (_, median_value, _) = data.select_nth_unstable(median_index); | ||
|
||
Some(*median_value) | ||
} | ||
|
||
fn decode_many<T: Encode + Decode>(data: &mut [Vec<u8>]) -> Vec<T> { | ||
data.iter_mut() | ||
.map(|entry| T::decode(&mut entry.as_slice())) | ||
.filter_map(Result::ok) | ||
.collect() | ||
} | ||
|
||
fn select_median_btc_info(data: Vec<BitcoinFeeInfo>) -> Option<BitcoinFeeInfo> { | ||
if data.is_empty() { | ||
return None | ||
} | ||
|
||
Some(BitcoinFeeInfo { | ||
fee_per_input_utxo: select_median(data.iter().map(|x| x.fee_per_input_utxo).collect()) | ||
.expect("non-empty list"), | ||
fee_per_output_utxo: select_median(data.iter().map(|x| x.fee_per_output_utxo).collect()) | ||
.expect("non-empty list"), | ||
min_fee_required_per_tx: select_median( | ||
data.iter().map(|x| x.min_fee_required_per_tx).collect(), | ||
) | ||
.expect("non-empty list"), | ||
}) | ||
} | ||
|
||
impl WitnessDataExtraction for RuntimeCall { | ||
|
@@ -64,27 +82,32 @@ impl WitnessDataExtraction for RuntimeCall { | |
EthereumInstance, | ||
>::update_chain_state { | ||
new_chain_state, | ||
}) => | ||
if let Some(median) = select_median(data) { | ||
}) => { | ||
let fee_votes = decode_many::<EthAmount>(data); | ||
if let Some(median) = select_median(fee_votes) { | ||
new_chain_state.tracked_data.priority_fee = median; | ||
}, | ||
} | ||
}, | ||
RuntimeCall::BitcoinChainTracking(pallet_cf_chain_tracking::Call::< | ||
Runtime, | ||
BitcoinInstance, | ||
>::update_chain_state { | ||
new_chain_state, | ||
}) => { | ||
if let Some(median) = select_median(data) { | ||
let fee_infos = decode_many::<BitcoinFeeInfo>(data); | ||
|
||
if let Some(median) = select_median_btc_info(fee_infos) { | ||
new_chain_state.tracked_data.btc_fee_info = median; | ||
}; | ||
} | ||
}, | ||
RuntimeCall::PolkadotChainTracking(pallet_cf_chain_tracking::Call::< | ||
Runtime, | ||
PolkadotInstance, | ||
>::update_chain_state { | ||
new_chain_state, | ||
}) => { | ||
if let Some(median) = select_median(data) { | ||
let tip_votes = decode_many::<PolkadotBalance>(data); | ||
if let Some(median) = select_median(tip_votes) { | ||
new_chain_state.tracked_data.median_tip = median; | ||
}; | ||
}, | ||
|
@@ -240,4 +263,60 @@ mod tests { | |
); | ||
}) | ||
} | ||
|
||
// Selecting median from integers spanning multiple bytes wasn't | ||
// working correctly previously, so this serves as a regression test: | ||
#[test] | ||
fn select_median_multi_bytes_ints() { | ||
let values = vec![1_u16, 8, 32, 256, 768]; | ||
assert_eq!(select_median::<u16>(values).unwrap(), 32); | ||
} | ||
|
||
#[test] | ||
fn select_median_out_of_order() { | ||
let values = vec![4, 1, 8, 7, 100]; | ||
assert_eq!(select_median::<u16>(values).unwrap(), 7); | ||
} | ||
|
||
#[test] | ||
fn select_median_empty() { | ||
assert_eq!(select_median::<u16>(vec![]), None); | ||
} | ||
|
||
// For BTC, we witness multiple values, and median should be | ||
// selected for each value independently: | ||
#[test] | ||
fn select_median_btc_info_test() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should have a test for when the input is empty, that it returns None + doesn't panic |
||
let votes = vec![ | ||
BitcoinFeeInfo { | ||
fee_per_input_utxo: 10, | ||
fee_per_output_utxo: 55, | ||
min_fee_required_per_tx: 100, | ||
}, | ||
BitcoinFeeInfo { | ||
fee_per_input_utxo: 45, | ||
fee_per_output_utxo: 100, | ||
min_fee_required_per_tx: 10, | ||
}, | ||
BitcoinFeeInfo { | ||
fee_per_input_utxo: 100, | ||
fee_per_output_utxo: 10, | ||
min_fee_required_per_tx: 50, | ||
}, | ||
]; | ||
|
||
assert_eq!( | ||
select_median_btc_info(votes), | ||
Some(BitcoinFeeInfo { | ||
fee_per_input_utxo: 45, | ||
fee_per_output_utxo: 55, | ||
min_fee_required_per_tx: 50 | ||
}) | ||
); | ||
} | ||
|
||
#[test] | ||
fn select_median_btc_info_empty() { | ||
assert_eq!(select_median_btc_info(vec![]), None); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should also test when they're not in order
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also here, good practice to test base case of empty (especially when removing the empty check would result in a panic)