Skip to content

Commit

Permalink
Add error code for assert (FuelLabs#3951)
Browse files Browse the repository at this point in the history
closes FuelLabs#3950

---------

Co-authored-by: Braqzen <[email protected]>
  • Loading branch information
nfurfaro and Braqzen authored Feb 2, 2023
1 parent 37583c4 commit bdfae6c
Showing 19 changed files with 65 additions and 61 deletions.
4 changes: 2 additions & 2 deletions sway-lib-std/src/assert.sw
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ library assert;

use ::logging::log;
use ::revert::revert;
use ::error_signals::FAILED_ASSERT_EQ_SIGNAL;
use ::error_signals::{FAILED_ASSERT_SIGNAL, FAILED_ASSERT_EQ_SIGNAL};


/// Asserts that the given `condition` will always be `true` during runtime.
@@ -28,7 +28,7 @@ use ::error_signals::FAILED_ASSERT_EQ_SIGNAL;
/// ```
pub fn assert(condition: bool) {
if !condition {
revert(0);
revert(FAILED_ASSERT_SIGNAL);
}
}

4 changes: 4 additions & 0 deletions sway-lib-std/src/error_signals.sw
Original file line number Diff line number Diff line change
@@ -16,3 +16,7 @@ pub const FAILED_SEND_MESSAGE_SIGNAL = 0xffff_ffff_ffff_0002;
/// Revert with this value for a failing call to `std::assert::assert_eq`.
/// 18446744073709486083
pub const FAILED_ASSERT_EQ_SIGNAL = 0xffff_ffff_ffff_0003;

/// Revert with this value for a failing call to `std::assert::assert`.
/// 18446744073709486084
pub const FAILED_ASSERT_SIGNAL = 0xffff_ffff_ffff_0004;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
category = "run"
expected_result = { action = "revert", value = 0 }
expected_result = { action = "revert", value = -65532 } # 0xffffffffffff0004 as i64
validate_abi = false
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
category = "run"
expected_result = { action = "revert", value = 0 }
expected_result = { action = "revert", value = -65532 } # 0xffffffffffff0004 as i64
validate_abi = false
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
category = "run"
expected_result = { action = "revert", value = 0 }
expected_result = { action = "revert", value = -65532 } # 0xffffffffffff0004 as i64
validate_abi = false
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
"typeArguments": null
},
"name": "C0",
"offset": 3244
"offset": 3260
},
{
"configurableType": {
@@ -16,7 +16,7 @@
"typeArguments": null
},
"name": "C1",
"offset": 3252
"offset": 3268
},
{
"configurableType": {
@@ -25,7 +25,7 @@
"typeArguments": null
},
"name": "C2",
"offset": 3268
"offset": 3284
},
{
"configurableType": {
@@ -34,7 +34,7 @@
"typeArguments": []
},
"name": "C3",
"offset": 3332
"offset": 3348
},
{
"configurableType": {
@@ -43,7 +43,7 @@
"typeArguments": []
},
"name": "C4",
"offset": 3348
"offset": 3364
},
{
"configurableType": {
@@ -52,7 +52,7 @@
"typeArguments": []
},
"name": "C5",
"offset": 3364
"offset": 3380
},
{
"configurableType": {
@@ -61,7 +61,7 @@
"typeArguments": null
},
"name": "C6",
"offset": 3412
"offset": 3428
},
{
"configurableType": {
@@ -70,7 +70,7 @@
"typeArguments": null
},
"name": "C7",
"offset": 3436
"offset": 3452
}
],
"functions": [
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ script;
use basic_storage_abi::{BasicStorage, Quad};

fn main() -> u64 {
let addr = abi(BasicStorage, 0xd0897bfc4fd711f847986c1a22444c23da58a9458133c5e99519afab57ccd242);
let addr = abi(BasicStorage, 0x558d7f3f2cc625751596ca69a5c670a02651f1b84852e93adc9675368df26959);
let key = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
let value = 4242;

4 changes: 2 additions & 2 deletions test/src/sdk-harness/test_projects/reentrancy/mod.rs
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ async fn can_detect_reentrancy() {
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn can_block_reentrancy() {
let wallet = launch_provider_and_get_wallet().await;
let (attacker_instance, _) = get_attacker_instance(wallet.clone()).await;
@@ -45,7 +45,7 @@ async fn can_block_reentrancy() {
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn can_block_cross_function_reentrancy() {
let wallet = launch_provider_and_get_wallet().await;
let (attacker_instance, _) = get_attacker_instance(wallet.clone()).await;
8 changes: 4 additions & 4 deletions test/src/sdk-harness/test_projects/storage_vec/array/mod.rs
Original file line number Diff line number Diff line change
@@ -255,31 +255,31 @@ mod failure {
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_swap_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = swap_remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_insert() {
let (instance, _id) = get_contract_instance().await;

insert(&instance, 1, [5; 3]).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_set() {
let (instance, _id) = get_contract_instance().await;

8 changes: 4 additions & 4 deletions test/src/sdk-harness/test_projects/storage_vec/b256/mod.rs
Original file line number Diff line number Diff line change
@@ -261,31 +261,31 @@ mod failure {
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_swap_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = swap_remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_insert() {
let (instance, _id) = get_contract_instance().await;

insert(&instance, 1, BYTES5).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_set() {
let (instance, _id) = get_contract_instance().await;

8 changes: 4 additions & 4 deletions test/src/sdk-harness/test_projects/storage_vec/bool/mod.rs
Original file line number Diff line number Diff line change
@@ -255,31 +255,31 @@ mod failure {
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_swap_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = swap_remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_insert() {
let (instance, _id) = get_contract_instance().await;

insert(&instance, 1, true).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_set() {
let (instance, _id) = get_contract_instance().await;

8 changes: 4 additions & 4 deletions test/src/sdk-harness/test_projects/storage_vec/enum/mod.rs
Original file line number Diff line number Diff line change
@@ -262,31 +262,31 @@ mod failure {
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_swap_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = swap_remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_insert() {
let (instance, _id) = get_contract_instance().await;

insert(&instance, 1, ENUM5).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_set() {
let (instance, _id) = get_contract_instance().await;

8 changes: 4 additions & 4 deletions test/src/sdk-harness/test_projects/storage_vec/str/mod.rs
Original file line number Diff line number Diff line change
@@ -255,31 +255,31 @@ mod failure {
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_swap_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = swap_remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_insert() {
let (instance, _id) = get_contract_instance().await;

insert(&instance, 1, String::from("0250")).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_set() {
let (instance, _id) = get_contract_instance().await;

8 changes: 4 additions & 4 deletions test/src/sdk-harness/test_projects/storage_vec/struct/mod.rs
Original file line number Diff line number Diff line change
@@ -262,31 +262,31 @@ mod failure {
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_swap_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = swap_remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_insert() {
let (instance, _id) = get_contract_instance().await;

insert(&instance, 1, STRUCT5).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_set() {
let (instance, _id) = get_contract_instance().await;

8 changes: 4 additions & 4 deletions test/src/sdk-harness/test_projects/storage_vec/tuple/mod.rs
Original file line number Diff line number Diff line change
@@ -261,31 +261,31 @@ mod failure {
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_swap_remove() {
let (instance, _id) = get_contract_instance().await;

let _ = swap_remove(&instance, 0).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_insert() {
let (instance, _id) = get_contract_instance().await;

insert(&instance, 1, TUP5).await;
}

#[tokio::test]
#[should_panic(expected = "Revert(0)")]
#[should_panic(expected = "Revert(18446744073709486084)")]
async fn cant_set() {
let (instance, _id) = get_contract_instance().await;

Loading

0 comments on commit bdfae6c

Please sign in to comment.