Skip to content

Commit

Permalink
Bump to forc v0.53.0 (#226)
Browse files Browse the repository at this point in the history
## Type of change

<!--Delete points that do not apply-->

- Improvement (refactoring, restructuring repository, cleaning tech
debt, ...)

## Changes

The following changes have been made:

- Updates Sway-Libs to forc v0.53.0

## Notes

- Dependent on #225
  • Loading branch information
bitzoic authored Apr 16, 2024
1 parent 6c3cc04 commit 57dbed5
Show file tree
Hide file tree
Showing 29 changed files with 258 additions and 277 deletions.
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.75.0
FORC_VERSION: 0.50.0
CORE_VERSION: 0.22.0
FORC_VERSION: 0.53.0
CORE_VERSION: 0.23.0

jobs:
build-sway-lib:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ on:

env:
RUST_VERSION: 1.75.0
FORC_VERSION: 0.50.0
CORE_VERSION: 0.22.0
FORC_VERSION: 0.53.0
CORE_VERSION: 0.23.0

jobs:
deploy-contributing:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ forc test && 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.50.0`, `fuels-rs v0.53.0` and `fuel-core 0.22.0`.
> All projects currently use `forc v0.53.0`, `fuels-rs v0.53.0` and `fuel-core 0.23.0`.
## Contributing

Expand Down
3 changes: 1 addition & 2 deletions libs/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ license = "Apache-2.0"
name = "sway_libs"

[dependencies]
src5 = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.3.3" }
src7 = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.3.3" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.4.0" }
26 changes: 7 additions & 19 deletions libs/src/admin.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod errors;

use ::admin::errors::AdminError;
use ::ownership::{_owner, only_owner};
use src5::State;
use standards::src5::State;
use std::{auth::msg_sender, storage::storage_api::clear,};

// Sets a new administrator.
Expand Down Expand Up @@ -36,11 +36,8 @@ use std::{auth::msg_sender, storage::storage_api::clear,};
#[storage(read, write)]
pub fn add_admin(new_admin: Identity) {
only_owner();
let admin_value = match new_admin {
Identity::Address(addr) => addr.value,
Identity::ContractId(contr) => contr.value,
};
let admin_key = StorageKey::<Identity>::new(admin_value, 0, admin_value);

let admin_key = StorageKey::<Identity>::new(new_admin.bits(), 0, new_admin.bits());
admin_key.write(new_admin);
}

Expand Down Expand Up @@ -72,14 +69,9 @@ pub fn add_admin(new_admin: Identity) {
#[storage(read, write)]
pub fn revoke_admin(old_admin: Identity) {
only_owner();
let admin_value = match old_admin {
Identity::Address(addr) => addr.value,
Identity::ContractId(contr) => contr.value,
};
let admin_key = StorageKey::<Identity>::new(admin_value, 0, admin_value);
// TODO: Update to use StorageKey::clear() on next release
// https://github.com/FuelLabs/sway/pull/5284
let _ = clear::<Identity>(admin_key.slot, admin_key.offset);

let admin_key = StorageKey::<Identity>::new(old_admin.bits(), 0, old_admin.bits());
let _ = admin_key.clear();
}

/// Returns whether `admin` is an administrator.
Expand Down Expand Up @@ -107,11 +99,7 @@ pub fn revoke_admin(old_admin: Identity) {
/// ```
#[storage(read)]
pub fn is_admin(admin: Identity) -> bool {
let admin_value = match admin {
Identity::Address(addr) => addr.value,
Identity::ContractId(contr) => contr.value,
};
let admin_key = StorageKey::<Identity>::new(admin_value, 0, admin_value);
let admin_key = StorageKey::<Identity>::new(admin.bits(), 0, admin.bits());
match admin_key.try_read() {
Some(identity) => {
admin == identity
Expand Down
12 changes: 6 additions & 6 deletions libs/src/asset/metadata.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library;

use src7::Metadata;
use standards::src7::Metadata;
use std::{
bytes::Bytes,
hash::{
Expand Down Expand Up @@ -59,21 +59,21 @@ impl StorageKey<StorageMetadata> {
match metadata {
Metadata::Int(data) => {
write(hashed_key, 0, data);
write(sha256((hashed_key, self.slot)), 0, 0);
write(sha256((hashed_key, self.slot())), 0, 0);
},
Metadata::B256(data) => {
write(hashed_key, 0, data);
write(sha256((hashed_key, self.slot)), 0, 1);
write(sha256((hashed_key, self.slot())), 0, 1);
},
Metadata::String(data) => {
let storage_string: StorageKey<StorageString> = StorageKey::new(hashed_key, 0, hashed_key);
storage_string.write_slice(data);
write(sha256((hashed_key, self.slot)), 0, 2);
write(sha256((hashed_key, self.slot())), 0, 2);
},
Metadata::Bytes(data) => {
let storage_bytes: StorageKey<StorageBytes> = StorageKey::new(hashed_key, 0, hashed_key);
storage_bytes.write_slice(data);
write(sha256((hashed_key, self.slot)), 0, 3);
write(sha256((hashed_key, self.slot())), 0, 3);
}
}
}
Expand Down Expand Up @@ -112,7 +112,7 @@ impl StorageKey<StorageMetadata> {
pub fn get(self, asset: AssetId, key: String) -> Option<Metadata> {
let hashed_key = sha256((asset, key));

match read::<u64>(sha256((hashed_key, self.slot)), 0) {
match read::<u64>(sha256((hashed_key, self.slot())), 0) {
Option::Some(0) => {
Option::Some(Metadata::Int(read::<u64>(hashed_key, 0).unwrap()))
},
Expand Down
11 changes: 11 additions & 0 deletions libs/src/asset/supply.sw
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ use std::{

/// Unconditionally mints new assets using the `sub_id` sub-identifier.
///
/// # Additional Information
///
/// **Warning** This function increases the total supply by the number of coins minted.
///
/// # Arguments
///
/// * `total_assets_key`: [StorageKey<u64>] - The location in storage that the `u64` which represents the total assets is stored.
Expand Down Expand Up @@ -66,12 +70,15 @@ pub fn _mint(
) -> AssetId {
let asset_id = AssetId::new(contract_id(), sub_id);
let supply = _total_supply(total_supply_key, asset_id);

// Only increment the number of assets minted by this contract if it hasn't been minted before.
if supply.is_none() {
total_assets_key.write(_total_assets(total_assets_key) + 1);
}

let current_supply = supply.unwrap_or(0);
total_supply_key.insert(asset_id, current_supply + amount);

mint_to(recipient, sub_id, amount);
asset_id
}
Expand All @@ -81,6 +88,7 @@ pub fn _mint(
/// # Additional Information
///
/// **Warning** This function burns assets unequivocally. It does not check that assets are sent to the calling contract.
/// **Warning** This function reduces the total supply by the number of coins burned. Using this value when minting may cause unintended consequences.
///
/// # Arguments
///
Expand Down Expand Up @@ -120,9 +128,12 @@ pub fn _burn(
amount: u64,
) {
let asset_id = AssetId::new(contract_id(), sub_id);

require(this_balance(asset_id) >= amount, BurnError::NotEnoughCoins);

// If we pass the check above, we can assume it is safe to unwrap.
let supply = _total_supply(total_supply_key, asset_id).unwrap();
total_supply_key.insert(asset_id, supply - amount);

burn(sub_id, amount);
}
32 changes: 14 additions & 18 deletions libs/src/bytecode/utils.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library;

use std::{alloc::{alloc, realloc_bytes}, bytes::Bytes,};
use std::{alloc::{alloc, alloc_bytes, realloc_bytes}, bytes::Bytes,};

/// Pre-defined number of bytes of a leaf in a bytecode merkle tree.
const LEAF_SIZE = 16 * 1024;
Expand All @@ -26,14 +26,14 @@ pub fn _predicate_address_from_root(bytecode_root: b256) -> Address {
bytes.push(SEED[2]);
bytes.push(SEED[3]);
__addr_of(bytecode_root)
.copy_bytes_to(bytes.buf.ptr.add_uint_offset(4), 32);
.copy_bytes_to(bytes.ptr().add_uint_offset(4), 32);

// Compute Digest and return predicate address
let mut result_buffer = b256::min();
Address::from(
asm(
hash: result_buffer,
ptr: bytes.buf.ptr,
ptr: bytes.ptr(),
bytes: 36,
) {
s256 hash ptr bytes;
Expand Down Expand Up @@ -146,8 +146,8 @@ pub fn _swap_configurables(
let (offset, data) = configurables.get(configurable_iterator).unwrap();

// Overwrite the configurable data into the bytecode
data.buf
.ptr
data
.ptr()
.copy_bytes_to(bytecode.ptr().add::<u8>(offset), data.len());

configurable_iterator += 1;
Expand All @@ -157,35 +157,31 @@ pub fn _swap_configurables(
/// Takes some bytes and creates a new leaf digest.
fn leaf_digest(data: raw_slice, ref mut result_buffer: raw_ptr) {
let number_of_bytes = data.number_of_bytes();
let mut bytes = Bytes::with_capacity(number_of_bytes + 1);
let ptr = alloc_bytes(number_of_bytes + 1);

// Prepend LEAF to the leaf bytes
bytes.buf.ptr().write_byte(LEAF);
ptr.write_byte(LEAF);
data
.ptr()
.copy_bytes_to(bytes.buf.ptr().add_uint_offset(1), number_of_bytes);
bytes.len = number_of_bytes + 1;
.copy_bytes_to(ptr.add_uint_offset(1), number_of_bytes);

// Compute the digest
asm(hash: result_buffer, ptr: bytes.buf.ptr, bytes: bytes.len) {
asm(hash: result_buffer, ptr: ptr, bytes: number_of_bytes + 1) {
s256 hash ptr bytes;
};
}

/// Takes two nodes and creates a new node digest.
fn node_digest(left: raw_ptr, right: raw_ptr, result_buffer: raw_ptr) {
let mut bytes = Bytes::with_capacity(65);
let new_ptr_left = bytes.buf.ptr().add_uint_offset(1);
let new_ptr_right = bytes.buf.ptr().add_uint_offset(33);
let ptr = alloc_bytes(65);

// Prepend NODE and concat the 2 node bytes for the current node
bytes.buf.ptr().write_byte(NODE);
left.copy_bytes_to(new_ptr_left, 32);
right.copy_bytes_to(new_ptr_right, 32);
bytes.len = 65;
ptr.write_byte(NODE);
left.copy_bytes_to(ptr.add_uint_offset(1), 32);
right.copy_bytes_to(ptr.add_uint_offset(33), 32);

// Compute the digest
asm(hash: result_buffer, ptr: bytes.buf.ptr, bytes: bytes.len) {
asm(hash: result_buffer, ptr: ptr, bytes: 65) {
s256 hash ptr bytes;
};
}
55 changes: 37 additions & 18 deletions libs/src/fixed_point/ufp128.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library;
// A wrapper around U128 type for a library for Sway for mathematical functions operating with unsigned 64.64-bit fixed point numbers.
use std::{math::{Exponent, Power, Root}, u128::U128, u256::U256};
use std::{math::{Exponent, Power, Root}, u128::U128};

/// The 128-bit unsigned fixed point number type.
///
Expand Down Expand Up @@ -179,16 +179,26 @@ impl core::ops::Subtract for UFP128 {
impl core::ops::Multiply for UFP128 {
/// Multiply a UFP128 with a UFP128. Panics on overflow.
fn multiply(self, other: Self) -> Self {
let self_u256 = U256::from((0, 0, self.value.upper, self.value.lower));
let other_u256 = U256::from((0, 0, other.value.upper, other.value.lower));
let self_u64 = (0, 0, self.value.upper(), self.value.lower());
let other_u64 = (0, 0, other.value.upper(), other.value.lower());
let self_u256 = asm(r1: self_u64) {
r1: u256
};
let other_u256 = asm(r1: other_u64) {
r1: u256
};

let self_multiply_other = self_u256 * other_u256;
let res_u256 = self_multiply_other >> 64;
if res_u256.a != 0 || res_u256.b != 0 {

let (a, b, c, d) = asm(r1: res_u256) {
r1: (u64, u64, u64, u64)
};
if a != 0 || b != 0 {
// panic on overflow
revert(0);
}
Self::from((res_u256.c, res_u256.d))
Self::from((c, d))
}
}

Expand All @@ -201,21 +211,30 @@ impl core::ops::Divide for UFP128 {

assert(divisor != zero);

// Conversion to U256 done to ensure no overflow happen
// Conversion to u256 done to ensure no overflow happen
// and maximal precision is avaliable
// as it makes possible to multiply by the denominator in
// all cases
let self_u256 = U256::from((0, 0, self.value.upper, self.value.lower));
let divisor_u256 = U256::from((0, 0, divisor.value.upper, divisor.value.lower));
let self_u64 = (0, 0, self.value.upper(), self.value.lower());
let divisor_u64 = (0, 0, divisor.value.upper(), divisor.value.lower());
let self_u256 = asm(r1: self_u64) {
r1: u256
};
let divisor_u256 = asm(r1: divisor_u64) {
r1: u256
};

// Multiply by denominator to ensure accuracy
let res_u256 = (self_u256 << 64) / divisor_u256;
let (a, b, c, d) = asm(r1: res_u256) {
r1: (u64, u64, u64, u64)
};

if res_u256.a != 0 || res_u256.b != 0 {
if a != 0 || b != 0 {
// panic on overflow
revert(0);
}
Self::from((res_u256.c, res_u256.d))
Self::from((c, d))
}
}

Expand Down Expand Up @@ -265,7 +284,7 @@ impl UFP128 {
/// }
/// ```
pub fn floor(self) -> Self {
Self::from((self.value.upper, 0))
Self::from((self.value.upper(), 0))
}

/// Returns the smallest integer greater than or equal to `self`.
Expand All @@ -287,10 +306,10 @@ impl UFP128 {
/// ```
pub fn ceil(self) -> Self {
let val = self.value;
if val.lower == 0 {
return Self::from((val.upper, 0));
if val.lower() == 0 {
return Self::from((val.upper(), 0));
} else {
return Self::from((val.upper + 1, 0));
return Self::from((val.upper() + 1, 0));
}
}
}
Expand Down Expand Up @@ -347,7 +366,7 @@ impl UFP128 {
/// }
/// ```
pub fn trunc(self) -> Self {
Self::from((self.value.upper, 0))
Self::from((self.value.upper(), 0))
}

/// Returns the fractional part of `self`.
Expand All @@ -368,15 +387,15 @@ impl UFP128 {
/// }
/// ```
pub fn fract(self) -> Self {
Self::from((0, self.value.lower))
Self::from((0, self.value.lower()))
}
}

impl Root for UFP128 {
fn sqrt(self) -> Self {
let numerator_root = self.value.sqrt();
let numerator = numerator_root * U128::from((0, 2 << 32));
Self::from((numerator.upper, numerator.lower))
Self::from((numerator.upper(), numerator.lower()))
}
}

Expand All @@ -386,7 +405,7 @@ impl Power for UFP128 {
let u128_2 = U128::from((0, 2));
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))
Self::from((nominator.upper(), nominator.lower()))
}
}

Expand Down
Loading

0 comments on commit 57dbed5

Please sign in to comment.