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

Remove BlockHeight arithmetic #596

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed

#### Breaking

- [#596](https://github.com/FuelLabs/fuel-vm/pull/596): Remove `core::ops::{Add, Sub}` impls from `BlockHeight`. Use `succ` and `pred` to access adjacent blocks, or perform arithmetic directly on the wrapped integer instead.

## [Version 0.38.0]

### Added
Expand Down
2 changes: 1 addition & 1 deletion fuel-tx/src/tests/valid_cases/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ fn mint() {
.add_output(Output::coin(rng.gen(), rng.next_u64(), AssetId::BASE))
.add_output(Output::coin(rng.gen(), rng.next_u64(), AssetId::BASE))
.finalize()
.check(block_height + 1.into(), &test_params())
.check(block_height.succ().unwrap(), &test_params())
.expect_err("Expected erroneous transaction");

assert_eq!(err, CheckError::TransactionMintIncorrectBlockHeight);
Expand Down
32 changes: 12 additions & 20 deletions fuel-types/src/numeric_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ use core::{
convert::TryFrom,
fmt,
ops::{
Add,
Deref,
DerefMut,
Sub,
},
str,
};
Expand Down Expand Up @@ -246,27 +244,21 @@ macro_rules! key_methods {
Ok(ret.into())
}
}

impl Add for $i {
type Output = $i;

#[inline(always)]
fn add(self, rhs: $i) -> $i {
$i(self.0.wrapping_add(rhs.0))
}
}

impl Sub for $i {
type Output = $i;

#[inline(always)]
fn sub(self, rhs: $i) -> $i {
$i(self.0.wrapping_sub(rhs.0))
}
}
};
};
}

key!(BlockHeight, u32);
key!(ChainId, u64);

impl BlockHeight {
/// Successor, i.e. next block after this
pub fn succ(self) -> Option<BlockHeight> {
Some(Self(self.0.checked_add(1)?))
}

/// Predecessor, i.e. previous block before this
pub fn pred(self) -> Option<BlockHeight> {
Some(Self(self.0.checked_sub(1)?))
}
}