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

Bump Sway-Libs to Forc v0.47.0 and Sway-Standards v0.2.0 #202

Merged
merged 11 commits into from
Nov 13, 2023
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ env:
CARGO_TERM_COLOR: always
REGISTRY: ghcr.io
RUST_VERSION: 1.71.1
FORC_VERSION: 0.46.0
CORE_VERSION: 0.20.3
FORC_VERSION: 0.47.0
CORE_VERSION: 0.20.8
PATH_TO_SCRIPTS: .github/scripts

jobs:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<a href="https://github.com/FuelLabs/sway-libs/actions/workflows/ci.yml" alt="CI">
<img src="https://github.com/FuelLabs/sway-libs/actions/workflows/ci.yml/badge.svg" />
</a>
<a href="https://crates.io/crates/forc/0.46.0" alt="forc">
<img src="https://img.shields.io/badge/forc-v0.46.0-orange" />
<a href="https://crates.io/crates/forc/0.47.0" alt="forc">
<img src="https://img.shields.io/badge/forc-v0.47.0-orange" />
</a>
<a href="./LICENSE" alt="forc">
<img src="https://img.shields.io/github/license/FuelLabs/sway-libs" />
Expand Down Expand Up @@ -82,7 +82,7 @@ cargo test
Any instructions related to using a specific library should be found within the README.md of that library.

> **Note**
> All projects currently use `forc v0.46.0`, `fuels-rs v0.46.0` and `fuel-core 0.20.3`.
> All projects currently use `forc v0.47.0`, `fuels-rs v0.46.0` and `fuel-core 0.20.8`.

## Contributing

Expand Down
12 changes: 5 additions & 7 deletions libs/fixed_point/src/ifp128.sw
Original file line number Diff line number Diff line change
Expand Up @@ -492,20 +492,18 @@ impl Exponent for IFP128 {

impl Power for IFP128 {
/// Power function. x ^ exponent
fn pow(self, exponent: Self) -> Self {
fn pow(self, exponent: u32) -> Self {
let upf64_exponent = UFP64::from(exponent.as_u64());
bitzoic marked this conversation as resolved.
Show resolved Hide resolved
let non_negative = if !self.non_negative {
// roots of negative numbers are complex numbers which we lack for now
assert(exponent.underlying.floor() == exponent.underlying);
assert(upf64_exponent.floor() == upf64_exponent);

let div_2 = exponent.underlying / UFP64::from(2);
let div_2 = upf64_exponent / UFP64::from(2);
div_2.floor() == div_2
} else {
true
};
let mut underlying = self.underlying.pow(exponent.underlying);
if !exponent.non_negative {
underlying = UFP64::recip(underlying);
}
let mut underlying = self.underlying.pow(exponent);
Self {
underlying: underlying,
non_negative: non_negative,
Expand Down
12 changes: 5 additions & 7 deletions libs/fixed_point/src/ifp256.sw
Original file line number Diff line number Diff line change
Expand Up @@ -492,20 +492,18 @@ impl Exponent for IFP256 {

impl Power for IFP256 {
/// Power function. x ^ exponent
fn pow(self, exponent: Self) -> Self {
fn pow(self, exponent: u32) -> Self {
let ufp128_exponent = UFP128::from((0, exponent.as_u64()));
let non_negative = if !self.non_negative {
// roots of negative numbers are complex numbers which we lack for now
assert(exponent.underlying.floor() == exponent.underlying);
assert(ufp128_exponent.floor() == ufp128_exponent);

let div_2 = exponent.underlying / UFP128::from((2, 0));
let div_2 = ufp128_exponent / UFP128::from((2, 0));
div_2.floor() == div_2
} else {
true
};
let mut underlying = self.underlying.pow(exponent.underlying);
if !exponent.non_negative {
underlying = UFP128::recip(underlying);
}
let mut underlying = self.underlying.pow(exponent);
Self {
underlying: underlying,
non_negative: non_negative,
Expand Down
12 changes: 5 additions & 7 deletions libs/fixed_point/src/ifp64.sw
Original file line number Diff line number Diff line change
Expand Up @@ -492,20 +492,18 @@ impl Exponent for IFP64 {

impl Power for IFP64 {
/// Power function. x ^ exponent
fn pow(self, exponent: Self) -> Self {
fn pow(self, exponent: u32) -> Self {
let ufp32_exponent = UFP32::from(exponent);
let non_negative = if !self.non_negative {
// roots of negative numbers are complex numbers which we lack for now
assert(exponent.underlying.floor() == exponent.underlying);
assert(ufp32_exponent.floor() == ufp32_exponent);

let div_2 = exponent.underlying / UFP32::from(2u32);
let div_2 = ufp32_exponent / UFP32::from(2u32);
div_2.floor() == div_2
} else {
true
};
let mut underlying = self.underlying.pow(exponent.underlying);
if !exponent.non_negative {
underlying = UFP32::recip(underlying);
}
let mut underlying = self.underlying.pow(exponent);
Self {
underlying: underlying,
non_negative: non_negative,
Expand Down
8 changes: 3 additions & 5 deletions libs/fixed_point/src/ufp128.sw
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,10 @@ impl Root for UFP128 {
}

impl Power for UFP128 {
fn pow(self, exponent: Self) -> Self {
let nominator_pow = self.value.pow(exponent.value);
let u128_1 = U128::from((0, 1));
fn pow(self, exponent: u32) -> Self {
let nominator_pow = self.value.pow(exponent);
let u128_2 = U128::from((0, 2));
let u128_64 = U128::from((0, 64));
let two_pow_64_n_minus_1 = u128_2.pow(u128_64 * (exponent.value - u128_1));
let two_pow_64_n_minus_1 = u128_2.pow((64u32 * (exponent - 1u32)));
let nominator = nominator_pow / two_pow_64_n_minus_1;
Self::from((nominator.upper, nominator.lower))
}
Expand Down
7 changes: 3 additions & 4 deletions libs/fixed_point/src/ufp32.sw
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,14 @@ impl Exponent for UFP32 {

impl Power for UFP32 {
/// Power function. x ^ exponent
fn pow(self, exponent: Self) -> Self {
fn pow(self, exponent: u32) -> Self {
let demoninator_power = UFP32::denominator();
let exponent_int = exponent.value >> 16;
let nominator_pow = self.value.pow(exponent_int);
let nominator_pow = self.value.pow(exponent);
// As we need to ensure the fixed point structure
// which means that the denominator is always 2 ^ 16
// we need to divide the nominator by 2 ^ (16 * exponent - 1)
// - 1 is the formula is due to denominator need to stay 2 ^ 16
let nominator = nominator_pow >> 16 * (exponent_int - 1u32).as_u64();
let nominator = nominator_pow >> 16 * (exponent - 1u32).as_u64();

if nominator > u32::max() {
// panic on overflow
Expand Down
7 changes: 3 additions & 4 deletions libs/fixed_point/src/ufp64.sw
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,14 @@ impl Exponent for UFP64 {

impl Power for UFP64 {
/// Power function. x ^ exponent
fn pow(self, exponent: Self) -> Self {
fn pow(self, exponent: u32) -> Self {
let demoninator_power = UFP64::denominator();
let exponent_int = exponent.value >> 32;
let nominator_pow = U128::from((0, self.value)).pow(U128::from((0, exponent_int)));
let nominator_pow = U128::from((0, self.value)).pow(exponent);
// As we need to ensure the fixed point structure
// which means that the denominator is always 2 ^ 32
// we need to delete the nominator by 2 ^ (32 * exponent - 1)
// - 1 is the formula is due to denominator need to stay 2 ^ 32
let nominator = nominator_pow >> demoninator_power * (exponent_int - 1);
let nominator = nominator_pow >> demoninator_power * (exponent.as_u64() - 1);

if nominator.upper != 0 {
// panic on overflow
Expand Down
5 changes: 4 additions & 1 deletion libs/merkle_proof/src/binary_merkle_proof.sw
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ pub fn process_proof(
proof: Vec<b256>,
) -> b256 {
let proof_length = proof.len();
require((num_leaves > 1 && proof_length == path_length_from_key(key, num_leaves)) || (num_leaves <= 1 && proof_length == 0), ProofError::InvalidProofLength);
require(
(num_leaves > 1 && proof_length == path_length_from_key(key, num_leaves)) || (num_leaves <= 1 && proof_length == 0),
ProofError::InvalidProofLength,
);
require(key < num_leaves, ProofError::InvalidKey);

let mut digest = merkle_leaf;
Expand Down
2 changes: 1 addition & 1 deletion libs/ownership/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ license = "Apache-2.0"
name = "ownership"

[dependencies]
src_5 = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.1.0" }
src_5 = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.2.0" }
18 changes: 15 additions & 3 deletions libs/ownership/src/ownable.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ pub mod events;
use errors::AccessError;
use events::{OwnershipRenounced, OwnershipSet, OwnershipTransferred};
use std::{auth::msg_sender, hash::sha256, storage::storage_api::{read, write}};
use src_5::{Ownership, State};
use src_5::State;

pub struct Ownership {
state: State,
}

impl Ownership {
/// Returns the `Ownership` struct in the `Uninitalized` state.
Expand Down Expand Up @@ -143,7 +147,11 @@ impl StorageKey<Ownership> {
/// ```
#[storage(read)]
pub fn only_owner(self) {
require(self.owner() == State::Initialized(msg_sender().unwrap()), AccessError::NotOwner);
require(
self
.owner() == State::Initialized(msg_sender().unwrap()),
AccessError::NotOwner,
);
}
}

Expand Down Expand Up @@ -217,7 +225,11 @@ impl StorageKey<Ownership> {
/// ```
#[storage(read, write)]
pub fn set_ownership(self, new_owner: Identity) {
require(self.owner() == State::Uninitialized, AccessError::CannotReinitialized);
require(
self
.owner() == State::Uninitialized,
AccessError::CannotReinitialized,
);

self.write(Ownership::initialized(new_owner));

Expand Down
11 changes: 8 additions & 3 deletions libs/pausable/src/lib.sw
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub fn _unpause() {
#[storage(read)]
pub fn _is_paused() -> bool {
let paused_key = StorageKey::new(PAUSABLE, 0, PAUSABLE);
paused_key.read()
paused_key.try_read().unwrap_or(false)
}

/// Requires that the contract is in the paused state.
Expand All @@ -179,7 +179,12 @@ pub fn _is_paused() -> bool {
#[storage(read)]
pub fn require_paused() {
let paused_key = StorageKey::<bool>::new(PAUSABLE, 0, PAUSABLE);
require(paused_key.read(), PauseError::NotPaused);
require(
paused_key
.try_read()
.unwrap_or(false),
PauseError::NotPaused,
);
}

/// Requires that the contract is in the unpaused state.
Expand All @@ -206,5 +211,5 @@ pub fn require_paused() {
#[storage(read)]
pub fn require_not_paused() {
let paused_key = StorageKey::<bool>::new(PAUSABLE, 0, PAUSABLE);
require(!paused_key.read(), PauseError::Paused);
require(!paused_key.try_read().unwrap_or(false), PauseError::Paused);
}
36 changes: 28 additions & 8 deletions libs/signed_integers/src/i128.sw
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,32 @@ impl core::ops::Divide for I128 {
|| self.underlying == Self::indent())
&& divisor.underlying > Self::indent()
{
res = Self::from_uint((self.underlying - Self::indent()) / (divisor.underlying - Self::indent()) + Self::indent());
res = Self::from_uint(
(self.underlying - Self::indent()) / (divisor
.underlying - Self::indent()) + Self::indent(),
);
} else if self.underlying < Self::indent()
&& divisor.underlying < Self::indent()
{
res = Self::from_uint((Self::indent() - self.underlying) / (Self::indent() - divisor.underlying) + Self::indent());
res = Self::from_uint(
(Self::indent() - self.underlying) / (Self::indent() - divisor
.underlying) + Self::indent(),
);
} else if (self.underlying > Self::indent()
|| self.underlying == Self::indent())
&& divisor.underlying < Self::indent()
{
res = Self::from_uint(Self::indent() - (self.underlying - Self::indent()) / (Self::indent() - divisor.underlying));
res = Self::from_uint(
Self::indent() - (self.underlying - Self::indent()) / (Self::indent() - divisor
.underlying),
);
} else if self.underlying < Self::indent()
&& divisor.underlying > Self::indent()
{
res = Self::from_uint(Self::indent() - (Self::indent() - self.underlying) / (divisor.underlying - Self::indent()));
res = Self::from_uint(
Self::indent() - (Self::indent() - self.underlying) / (divisor
.underlying - Self::indent()),
);
}
res
}
Expand All @@ -278,21 +290,29 @@ impl core::ops::Multiply for I128 {
&& (other.underlying > Self::indent()
|| other.underlying == Self::indent())
{
res = Self::from_uint((self.underlying - Self::indent()) * (other.underlying - Self::indent()) + Self::indent());
res = Self::from_uint(
(self.underlying - Self::indent()) * (other.underlying - Self::indent()) + Self::indent(),
);
} else if self.underlying < Self::indent()
&& other.underlying < Self::indent()
{
res = Self::from_uint((Self::indent() - self.underlying) * (Self::indent() - other.underlying) + Self::indent());
res = Self::from_uint(
(Self::indent() - self.underlying) * (Self::indent() - other.underlying) + Self::indent(),
);
} else if (self.underlying > Self::indent()
|| self.underlying == Self::indent())
&& other.underlying < Self::indent()
{
res = Self::from_uint(Self::indent() - (self.underlying - Self::indent()) * (Self::indent() - other.underlying));
res = Self::from_uint(
Self::indent() - (self.underlying - Self::indent()) * (Self::indent() - other.underlying),
);
} else if self.underlying < Self::indent()
&& (other.underlying > Self::indent()
|| other.underlying == Self::indent())
{
res = Self::from_uint(Self::indent() - (other.underlying - Self::indent()) * (Self::indent() - self.underlying));
res = Self::from_uint(
Self::indent() - (other.underlying - Self::indent()) * (Self::indent() - self.underlying),
);
}
res
}
Expand Down
36 changes: 28 additions & 8 deletions libs/signed_integers/src/i16.sw
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,31 @@ impl core::ops::Divide for I16 {
if self.underlying >= Self::indent()
&& divisor.underlying > Self::indent()
{
res = Self::from_uint((self.underlying - Self::indent()) / (divisor.underlying - Self::indent()) + Self::indent());
res = Self::from_uint(
(self.underlying - Self::indent()) / (divisor
.underlying - Self::indent()) + Self::indent(),
);
} else if self.underlying < Self::indent()
&& divisor.underlying < Self::indent()
{
res = Self::from_uint((Self::indent() - self.underlying) / (Self::indent() - divisor.underlying) + Self::indent());
res = Self::from_uint(
(Self::indent() - self.underlying) / (Self::indent() - divisor
.underlying) + Self::indent(),
);
} else if self.underlying >= Self::indent()
&& divisor.underlying < Self::indent()
{
res = Self::from_uint(Self::indent() - (self.underlying - Self::indent()) / (Self::indent() - divisor.underlying));
res = Self::from_uint(
Self::indent() - (self.underlying - Self::indent()) / (Self::indent() - divisor
.underlying),
);
} else if self.underlying < Self::indent()
&& divisor.underlying > Self::indent()
{
res = Self::from_uint(Self::indent() - (Self::indent() - self.underlying) / (divisor.underlying - Self::indent()));
res = Self::from_uint(
Self::indent() - (Self::indent() - self.underlying) / (divisor
.underlying - Self::indent()),
);
}
res
}
Expand All @@ -262,19 +274,27 @@ impl core::ops::Multiply for I16 {
if self.underlying >= Self::indent()
&& other.underlying >= Self::indent()
{
res = Self::from_uint((self.underlying - Self::indent()) * (other.underlying - Self::indent()) + Self::indent());
res = Self::from_uint(
(self.underlying - Self::indent()) * (other.underlying - Self::indent()) + Self::indent(),
);
} else if self.underlying < Self::indent()
&& other.underlying < Self::indent()
{
res = Self::from_uint((Self::indent() - self.underlying) * (Self::indent() - other.underlying) + Self::indent());
res = Self::from_uint(
(Self::indent() - self.underlying) * (Self::indent() - other.underlying) + Self::indent(),
);
} else if self.underlying >= Self::indent()
&& other.underlying < Self::indent()
{
res = Self::from_uint(Self::indent() - (self.underlying - Self::indent()) * (Self::indent() - other.underlying));
res = Self::from_uint(
Self::indent() - (self.underlying - Self::indent()) * (Self::indent() - other.underlying),
);
} else if self.underlying < Self::indent()
&& other.underlying >= Self::indent()
{
res = Self::from_uint(Self::indent() - (other.underlying - Self::indent()) * (Self::indent() - self.underlying));
res = Self::from_uint(
Self::indent() - (other.underlying - Self::indent()) * (Self::indent() - self.underlying),
);
}
res
}
Expand Down