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
Conversation
PRO-805 [BUG] Median value is computed incorrectly in witnessing
For example, given values [1, 8, 32, 256, 768] it returns 1 (the smallest value in this case):
I noticed that we have For multi-field values (e.g. BitcoinFeeInfo used in bitcoin witnessing) this obviously doesn't work either since sorting will essentially be done on the field encoded first, and the other two will effectively be selected arbitrarily. (This means for example, that you can keep submitting rubbish for the other fields and we will eventually accept them). daniel kylezs |
Codecov Report
@@ Coverage Diff @@
## main #3934 +/- ##
======================================
+ Coverage 71% 73% +1%
======================================
Files 366 367 +1
Lines 56773 56037 -736
Branches 56773 56037 -736
======================================
+ Hits 40554 40634 +80
+ Misses 14210 13345 -865
- Partials 2009 2058 +49
... and 77 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
6b3d5f9
to
b2f29a6
Compare
// 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]; |
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)
// 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 comment
The 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
* origin/main: Fix: Correct Select Median Implementation (#3934) fix: independent witnessing startup (#3913) 🍒 cherry-pick: changes in release for CI and chainspec (#3933) refactor: Re-arrange Chains traits for better composability (#3912) fix: log error when we try to transfer *more* than we have fetched (#3930) chore: add checks and increase timeout (#3928) Add `bind_redeem_address` to CLI (#3908) 🍒 cherry-pick: add missing prod dockerfiles (#3926) chore: skip localnet specific tests in devnet 🤫 (#3919) fix: broadcast success should be witnessable after a rotation (#3921) # Conflicts: # state-chain/chains/src/eth/api.rs # state-chain/runtime/src/chainflip.rs
…on-integration * origin/main: Added CFE setting for logging span lifecycles (#3936) fix: only burn flip if non zero (#3932) Fix: Correct Select Median Implementation (#3934) fix: independent witnessing startup (#3913) 🍒 cherry-pick: changes in release for CI and chainspec (#3933) refactor: Re-arrange Chains traits for better composability (#3912) fix: log error when we try to transfer *more* than we have fetched (#3930) chore: add checks and increase timeout (#3928) Add `bind_redeem_address` to CLI (#3908) 🍒 cherry-pick: add missing prod dockerfiles (#3926) chore: skip localnet specific tests in devnet 🤫 (#3919) fix: broadcast success should be witnessable after a rotation (#3921) # Conflicts: # state-chain/cf-integration-tests/src/network.rs
* fix: correct select median implementation * test: more select median tests
* fix: correct select median implementation * test: more select median tests
Pull Request
Closes: PRO-805
Checklist
Please conduct a thorough self-review before opening the PR.
Summary
As proposed in PRO-805, I changed the code to decode values before
select_median
(which now requiresOrd
rather thanEncode/Decode
), so now the comparison is done on integers correctly. For BTC, the median is selected for each field independently.