Skip to content
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

unreachable to ord for amount #228

Merged
merged 5 commits into from
Jun 27, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/interface/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::cmp::Ordering;
use std::collections::HashMap;

use amplify::confinement::{SmallOrdSet, SmallVec};
Expand Down Expand Up @@ -154,21 +155,23 @@
*self = match self {
AmountChange::Dec(neg) => AmountChange::Dec(*neg + sub),
AmountChange::Zero => AmountChange::Dec(sub),
AmountChange::Inc(pos) if *pos > sub => AmountChange::Inc(*pos - sub),
AmountChange::Inc(pos) if *pos == sub => AmountChange::Zero,
AmountChange::Inc(pos) if *pos < sub => AmountChange::Dec(sub - *pos),
AmountChange::Inc(_) => unreachable!(),
AmountChange::Inc(pos) => match sub.cmp(pos) {
Ordering::Less => AmountChange::Inc(*pos - sub),
Ordering::Equal => AmountChange::Zero,
Ordering::Greater => AmountChange::Dec(sub - *pos),

Check warning on line 161 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L158-L161

Added lines #L158 - L161 were not covered by tests
},
};
}

fn merge_received(&mut self, add: Self::State) {
*self = match self {
AmountChange::Inc(pos) => AmountChange::Inc(*pos + add),
AmountChange::Zero => AmountChange::Inc(add),
AmountChange::Dec(neg) if *neg > add => AmountChange::Dec(*neg - add),
AmountChange::Dec(neg) if *neg == add => AmountChange::Zero,
AmountChange::Dec(neg) if *neg < add => AmountChange::Inc(add - *neg),
AmountChange::Dec(_) => unreachable!(),
AmountChange::Dec(neg) => match add.cmp(neg) {
Ordering::Greater => AmountChange::Dec(*neg - add),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be less, not greater, since we reverse the order neg and add are compared

Suggested change
Ordering::Greater => AmountChange::Dec(*neg - add),
Ordering::Less => AmountChange::Dec(*neg - add),

Ordering::Equal => AmountChange::Zero,
Ordering::Less => AmountChange::Inc(add - *neg),

Check warning on line 173 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L170-L173

Added lines #L170 - L173 were not covered by tests
},
};
}
}
Expand Down
Loading