Skip to content

Commit

Permalink
Flatten the stdlib (FuelLabs#3247)
Browse files Browse the repository at this point in the history
While a final design for the organization of `std` will likely involve
more discussion (& consideration of the proposed `Contract` type with
some of the existing free functions converted to methods on the
`Contract` type), the changes I've made here remove some weirdness.
This is a breaking change, so it might be a good time to clean at least
this up.
I've left the `vm` directory as currently, it seems like the only good
use of a subdirectory in `std`.
If anyone thinks this needs more discussion we can mark this a draft.

ref FuelLabs#1759
  • Loading branch information
nfurfaro authored Nov 2, 2022
1 parent b83ad9b commit bcfbd1b
Show file tree
Hide file tree
Showing 33 changed files with 42 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/identity/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use abi::IdentityExample;
use errors::MyError;

use std::{
chain::auth::{
auth::{
AuthError,
msg_sender,
},
Expand Down
2 changes: 1 addition & 1 deletion examples/liquidity_pool/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
contract;

use std::{
context::call_frames::{
call_frames::{
contract_id,
msg_asset_id,
},
Expand Down
2 changes: 1 addition & 1 deletion examples/msg_sender/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
contract;

use std::chain::auth::{AuthError, msg_sender};
use std::auth::{AuthError, msg_sender};

abi MyOwnedContract {
fn receive(field_1: u64) -> bool;
Expand Down
2 changes: 1 addition & 1 deletion examples/subcurrency/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ANCHOR: body
contract;

use std::{chain::auth::{AuthError, msg_sender}, hash::sha256, logging::log};
use std::{auth::{AuthError, msg_sender}, hash::sha256, logging::log};

////////////////////////////////////////
// Event declarations
Expand Down
8 changes: 3 additions & 5 deletions examples/wallet_smart_contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
contract;

use std::{
chain::auth::{
auth::{
AuthError,
msg_sender,
},
call_frames::msg_asset_id,
constants::BASE_ASSET_ID,
context::{
call_frames::msg_asset_id,
msg_amount,
},
context::msg_amount,
token::transfer_to_address,
};

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! https://fuellabs.github.io/fuel-specs/master/vm#call-frames
library call_frames;

use ::context::registers::frame_ptr;
use ::registers::frame_ptr;
use ::contract_id::ContractId;
use ::intrinsics::is_reference_type;

Expand Down
2 changes: 0 additions & 2 deletions sway-lib-std/src/chain.sw

This file was deleted.

6 changes: 2 additions & 4 deletions sway-lib-std/src/context.sw
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! Functionality for accessing context-specific information about the current contract or message.
library context;
dep context/registers;
dep context/call_frames;

use ::contract_id::ContractId;
use ::context::call_frames::contract_id;
use ::context::registers::balance;
use ::call_frames::contract_id;
use ::registers::balance;

/// Get the balance of coin `asset_id` for the current contract.
pub fn this_balance(asset_id: ContractId) -> u64 {
Expand Down
2 changes: 1 addition & 1 deletion sway-lib-std/src/ecr.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ library ecr;

use ::address::Address;
use ::b512::B512;
use ::context::registers::error;
use ::registers::error;
use ::hash::sha256;
use ::result::Result;

Expand Down
2 changes: 1 addition & 1 deletion sway-lib-std/src/flags.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Functionality for setting and unsetting FuelVM flags to modify behavior related to the `$err` and `$of` registers
library flags;

use ::context::registers::flags;
use ::registers::flags;

/// Call this function to allow overflowing operations to occur without a FuelVM panic.
/// IMPORTANT !!! Don't forget to call enable_panic_on_overflow() after performing the operations for which you disabled the default panic-on-overflow behavior in the first place !
Expand Down
4 changes: 3 additions & 1 deletion sway-lib-std/src/lib.sw
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ dep alloc;
dep contract_id;
dep constants;
dep external;
dep registers;
dep call_frames;
dep context;
dep hash;
dep r#storage;
Expand All @@ -21,7 +23,7 @@ dep identity;
dep tx;
dep inputs;
dep outputs;
dep chain;
dep auth;
dep math;
dep block;
dep token;
Expand Down
3 changes: 2 additions & 1 deletion sway-lib-std/src/reentrancy.sw
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
library reentrancy;

use ::assert::assert;
use ::context::{call_frames::*, registers::frame_ptr};
use ::call_frames::*;
use ::registers::frame_ptr;

pub fn reentrancy_guard() {
assert(!is_reentrant());
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion sway-lib-std/src/token.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
library token;

use ::address::Address;
use ::context::call_frames::contract_id;
use ::call_frames::contract_id;
use ::contract_id::ContractId;
use ::error_signals::FAILED_TRANSFER_TO_ADDRESS_SIGNAL;
use ::identity::Identity;
Expand Down
2 changes: 1 addition & 1 deletion sway-lib-std/src/vm/evm/ecr.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library ecr;

use ::b512::B512;
use ::context::registers::error;
use ::registers::error;
use ::ecr::{ec_recover, EcRecoverError};
use ::hash::keccak256;
use ::result::Result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
script;

use std::chain::*;
use std::revert::revert;

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
script;

use std::chain::auth::*;
use std::auth::*;
use std::identity::*;
//use std::b512::*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
script;

use std::chain::*;
use std::revert::revert;

enum Result<T, E> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
contract;
use std::chain::auth::caller_is_external;
use std::auth::caller_is_external;
use auth_testing_abi::AuthTesting;

impl AuthTesting for Contract {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
contract;

use std::{context::{balance_of, call_frames::{contract_id, msg_asset_id}, gas, msg_amount, registers::global_gas, this_balance}, contract_id::ContractId};
use std::{call_frames::{contract_id, msg_asset_id}, context::{balance_of, gas, msg_amount, this_balance}, contract_id::ContractId, registers::global_gas};
use context_testing_abi::*;

impl ContextTesting for Contract {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
contract;

use auth_testing_abi::*;
use std::chain::auth::*;
use std::auth::*;

abi AuthCaller {
fn call_auth_contract(auth_id: ContractId, expected_id: ContractId) -> bool;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
script;

use auth_testing_abi::AuthTesting;
use std::chain::auth::*;
use std::auth::*;

fn main() -> u64 {
// TODO: ContractId for auth_testing_contract should ideally be passed to script as an arg when possible.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library auth_testing_abi;

use std::chain::auth::*;
use std::auth::*;

abi AuthTesting {
fn is_caller_external() -> bool;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
contract;

use std::chain::auth::*;
use std::auth::*;
use auth_testing_abi::*;

impl AuthTesting for Contract {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
contract;

use context_testing_abi::ContextTesting;
use std::{context::call_frames::contract_id, token::mint};
use std::{call_frames::contract_id, token::mint};

abi ContextCaller {
fn call_get_this_balance_with_coins(send_amount: u64, context_id: ContractId) -> u64;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ contract;

use methods_abi::MethodsContract;

use std::chain::auth::*;
use std::auth::*;

fn bogus() -> Identity {
let sender = msg_sender();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
contract;

use std::{chain::auth::*, context::call_frames::contract_id};
use std::{auth::*, call_frames::contract_id};

use reentrancy_target_abi::Target;
use reentrancy_attacker_abi::Attacker;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
contract;

use std::{chain::auth::*, context::{call_frames::contract_id, gas}, reentrancy::*};
use std::{auth::*, call_frames::contract_id, context::gas, reentrancy::*};

use reentrancy_attacker_abi::Attacker;
use reentrancy_target_abi::Target;
Expand Down
2 changes: 1 addition & 1 deletion test/src/sdk-harness/test_projects/call_frames/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
contract;

use call_frames_test_abi::{CallFramesTest, TestStruct, TestStruct2};
use std::{context::call_frames::*};
use std::call_frames::*;

impl CallFramesTest for Contract {
fn get_id() -> ContractId {
Expand Down
6 changes: 5 additions & 1 deletion test/src/sdk-harness/test_projects/context/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
contract;

use std::context::{*, call_frames::*, registers::*};
use std::{
context::*,
call_frames::*,
registers::*,
};
use context_testing_abi::*;

impl ContextTesting for Contract {
Expand Down
6 changes: 5 additions & 1 deletion test/src/sdk-harness/test_projects/registers/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
contract;

use std::context::{*, call_frames::*, registers::*};
use std::{
context::*,
call_frames::*,
registers::*
};

abi Registers {
fn get_overflow() -> u64;
Expand Down
2 changes: 1 addition & 1 deletion test/src/sdk-harness/test_projects/storage/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
contract;

use std::{context::registers::stack_ptr, storage::{get, store}};
use std::{registers::stack_ptr, storage::{get, store}};

pub enum MediumEnum {
One: u64,
Expand Down

0 comments on commit bcfbd1b

Please sign in to comment.