Skip to content

Commit

Permalink
Changes panic to revert in sway-lib-std (#1494)
Browse files Browse the repository at this point in the history
* replaced panic with revert

* changed file name

* corrected tests

Co-authored-by: Mitch Mackert <mitchmackert@MacBook-Pro-2.local>
  • Loading branch information
gr00vytvniks and Mitch Mackert committed May 8, 2022
1 parent b35f30a commit 4634451
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 43 deletions.
4 changes: 2 additions & 2 deletions examples/msg_sender/src/main.sw
@@ -1,6 +1,6 @@
contract;

use std::{address::Address, assert::assert, chain::auth::{AuthError, Sender, msg_sender}, panic::panic, result::*};
use std::{address::Address, assert::assert, chain::auth::{AuthError, Sender, msg_sender}, result::*, revert::revert};

abi MyOwnedContract {
fn receive(field_1: u64) -> bool;
Expand All @@ -14,7 +14,7 @@ impl MyOwnedContract for Contract {
if let Sender::Address(addr) = sender.unwrap() {
assert(addr.into() == OWNER);
} else {
panic(0);
revert(0);
};

true
Expand Down
4 changes: 2 additions & 2 deletions examples/signatures/src/main.sw
Expand Up @@ -2,7 +2,7 @@ script;

use std::result::Result;
use std::b512::B512;
use std::panic::panic;
use std::revert::revert;
use std::chain::log_b256;
use std::ecr::{EcRecoverError, ec_recover, ec_recover_address};

Expand All @@ -21,6 +21,6 @@ fn main() {
if let Result::Ok(address) = result_address {
log_b256(address.value);
} else {
panic(0);
revert(0);
};
}
6 changes: 3 additions & 3 deletions examples/subcurrency/src/main.sw
@@ -1,7 +1,7 @@
// ANCHOR: body
contract;

use std::{address::Address, assert::assert, chain::auth::{AuthError, Sender, msg_sender}, hash::*, panic::panic, result::*, storage::{get, store}};
use std::{address::Address, assert::assert, chain::auth::{AuthError, Sender, msg_sender}, hash::*, result::*, revert::revert, storage::{get, store}};

////////////////////////////////////////
// Event declarations
Expand Down Expand Up @@ -61,7 +61,7 @@ impl Token for Contract {
let sender = if let Sender::Address(addr) = sender.unwrap() {
assert(addr.into() == MINTER);
} else {
panic(0);
revert(0);
};

// Increase the balance of receiver
Expand All @@ -75,7 +75,7 @@ impl Token for Contract {
let sender = if let Sender::Address(addr) = sender.unwrap() {
addr
} else {
panic(0);
revert(0);
};

// Reduce the balance of sender
Expand Down
4 changes: 2 additions & 2 deletions examples/wallet_smart_contract/src/main.sw
@@ -1,6 +1,6 @@
contract;

use std::{address::Address, assert::assert, chain::auth::{AuthError, Sender, msg_sender}, constants::NATIVE_ASSET_ID, context::{call_frames::msg_asset_id, msg_amount}, contract_id::ContractId, panic::panic, result::*, token::transfer_to_output};
use std::{address::Address, assert::assert, chain::auth::{AuthError, Sender, msg_sender}, constants::NATIVE_ASSET_ID, context::{call_frames::msg_asset_id, msg_amount}, contract_id::ContractId, result::*, revert::revert, token::transfer_to_output};

const OWNER_ADDRESS: b256 = 0x8900c5bec4ca97d4febf9ceb4754a60d782abbf3cd815836c1872116f203f861;

Expand All @@ -25,7 +25,7 @@ impl Wallet for Contract {
if let Sender::Address(addr) = sender.unwrap() {
assert(addr.into() == OWNER_ADDRESS);
} else {
panic(0);
revert(0);
};

let current_balance = storage.balance;
Expand Down
4 changes: 2 additions & 2 deletions sway-lib-std/src/assert.sw
@@ -1,11 +1,11 @@
library assert;

use ::panic::panic;
use ::revert::revert;

/// Assert that a value is true
pub fn assert(a: bool) {
if !a {
panic(0);
revert(0);
} else {
()
}
Expand Down
2 changes: 1 addition & 1 deletion sway-lib-std/src/chain.sw
@@ -1,7 +1,7 @@
library chain;
dep chain/auth;

use ::panic::panic;
use ::revert::revert;

// When generics land, these will be generic.
pub fn log_b256(value: b256) {
Expand Down
2 changes: 1 addition & 1 deletion sway-lib-std/src/lib.sw
@@ -1,6 +1,6 @@
library std;

dep panic;
dep revert;
dep assert;
dep option;
dep result;
Expand Down
6 changes: 3 additions & 3 deletions sway-lib-std/src/option.sw
Expand Up @@ -6,7 +6,7 @@

library option;

use ::panic::panic;
use ::revert::revert;

/// `Option` is a type that represents either the existence of a value ([`Some`]) or a value's absence
/// ([`None`]).
Expand Down Expand Up @@ -47,14 +47,14 @@ impl<T> Option<T> {

/// Returns the contained [`Some`] value, consuming the `self` value.
///
/// Because this function may panic, its use is generally discouraged.
/// Because this function may revert, its use is generally discouraged.
/// Instead, prefer to use pattern matching and handle the [`None`]
/// case explicitly.
fn unwrap(self) -> T {
if let Option::Some(inner_value) = self {
inner_value
} else {
panic(0);
revert(0);
}
}
}
6 changes: 3 additions & 3 deletions sway-lib-std/src/result.sw
Expand Up @@ -7,7 +7,7 @@

library result;

use ::panic::panic;
use ::revert::revert;

/// `Result` is a type that represents either success ([`Ok`]) or failure
/// ([`Err`]).
Expand Down Expand Up @@ -48,14 +48,14 @@ impl<T, E> Result<T, E> {

/// Returns the contained [`Ok`] value, consuming the `self` value.
///
/// Because this function may panic, its use is generally discouraged.
/// Because this function may revert, its use is generally discouraged.
/// Instead, prefer to use pattern matching and handle the [`Err`]
/// case explicitly.
fn unwrap(self) -> T {
if let Result::Ok(inner_value) = self {
inner_value
} else {
panic(0);
revert(0);
}
}
}
4 changes: 2 additions & 2 deletions sway-lib-std/src/panic.sw → sway-lib-std/src/revert.sw
@@ -1,9 +1,9 @@
library panic;
library revert;

/// Context-dependent:
/// will panic if used in a predicate
/// will revert if used in a contract
pub fn panic(code: u64) {
pub fn revert(code: u64) {
asm(r1: code) {
rvrt r1;
}
Expand Down
4 changes: 2 additions & 2 deletions sway-lib-std/src/token.sw
Expand Up @@ -3,7 +3,7 @@ library token;

use ::address::Address;
use ::contract_id::ContractId;
use ::panic::panic;
use ::revert::revert;
use ::tx::*;
use ::context::call_frames::contract_id;

Expand Down Expand Up @@ -69,7 +69,7 @@ pub fn transfer_to_output(amount: u64, asset_id: ContractId, recipient: Address)
}

if !output_found {
panic(0);
revert(0);
} else {
asm(r1: recipient.value, r2: output_index, r3: amount, r4: asset_id.value) {
tro r1 r2 r3 r4;
Expand Down
Expand Up @@ -11,6 +11,6 @@ fn main() {
} else {
return true;
};
panic(0)
revert(0)
};
}
@@ -1,7 +1,7 @@
script;

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

enum Result<T, E> {
Ok: T,
Expand All @@ -20,7 +20,7 @@ fn main() -> u64 {
let x = if true {
42u64
} else {
panic(0)
revert(0)
};
let x: u64 = local_panic();
let x = if let Result::Ok(ok) = Result::Ok::<u64, u64>(5) {
Expand Down
Expand Up @@ -4,7 +4,7 @@ use std::address::Address;
use std::assert::assert;
use std::b512::B512;
use std::ecr::*;
use std::panic::panic;
use std::revert::revert;
use std::result::Result;

fn main() -> bool {
Expand Down Expand Up @@ -37,15 +37,15 @@ fn main() -> bool {
if let Result::Ok(a) = address_result {
assert(a.value == address.value);
} else {
panic(0);
revert(0);
};

// recover the public key:
let pubkey_result: Result<B512, EcRecoverError> = ec_recover(signature, msg_hash);
if let Result::Ok(p) = pubkey_result {
assert(p == pubkey);
} else {
panic(0);
revert(0);
};

/////////////////////////////////////////
Expand Down
@@ -1,6 +1,6 @@
contract;

use std::{chain::auth::*, constants::ZERO, context::call_frames::contract_id, contract_id::ContractId, panic::panic, result::*};
use std::{chain::auth::*, constants::ZERO, context::call_frames::contract_id, contract_id::ContractId, result::*, revert::revert};
use reentrancy_target_abi::Target;
use reentrancy_attacker_abi::Attacker;

Expand All @@ -10,10 +10,10 @@ fn get_msg_sender_id_or_panic(result: Result<Sender, AuthError>) -> ContractId {
if let Sender::ContractId(v) = s {
v
} else {
panic(0);
revert(0);
}
} else {
panic(0);
revert(0);
}
}

Expand Down
@@ -1,6 +1,6 @@
contract;

use std::{assert::assert, chain::auth::*, constants::ZERO, context::{call_frames::contract_id, gas}, contract_id::ContractId, panic::panic, reentrancy::*, result::*};
use std::{assert::assert, chain::auth::*, constants::ZERO, context::{call_frames::contract_id, gas}, contract_id::ContractId, reentrancy::*, result::*, revert::revert};
use reentrancy_attacker_abi::Attacker;
use reentrancy_target_abi::Target;

Expand All @@ -10,10 +10,10 @@ fn get_msg_sender_id_or_panic(result: Result<Sender, AuthError>) -> ContractId {
if let Sender::ContractId(v) = s {
v
} else {
panic(0);
revert(0);
}
} else {
panic(0);
revert(0);
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/src/sdk-harness/test_projects/option/src/main.sw
@@ -1,7 +1,7 @@
script;

use std::panic::panic;
use std::option::*;
use std::revert::revert;

fn main() {
test_some();
Expand All @@ -13,15 +13,15 @@ fn test_some() {
let o = Option::Some(42u64);

if (!o.is_some() || o.is_none()) {
panic(0);
revert(0);
}
}

fn test_none() {
let o = Option::None::<()>();

if (o.is_some() || !o.is_none()) {
panic(0);
revert(0);
}
}

Expand All @@ -30,6 +30,6 @@ fn test_unwrap_some() {

let u = o.unwrap();
if (u != 42) {
panic(0);
revert(0);
}
}
8 changes: 4 additions & 4 deletions test/src/sdk-harness/test_projects/result/src/main.sw
@@ -1,7 +1,7 @@
script;

use std::panic::panic;
use std::result::*;
use std::revert::revert;

fn main() {
test_ok();
Expand All @@ -13,15 +13,15 @@ fn test_ok() {
let r = Result::Ok::<u64, ()>(42u64);

if (!r.is_ok() || r.is_err()) {
panic(0);
revert(0);
}
}

fn test_err() {
let r = Result::Err::<(), ()>(());

if (r.is_ok() || !r.is_err()) {
panic(0);
revert(0);
}
}

Expand All @@ -30,6 +30,6 @@ fn test_unwrap_ok() {

let u = r.unwrap();
if (u != 42) {
panic(0);
revert(0);
}
}

0 comments on commit 4634451

Please sign in to comment.