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

Address trail of bits issues #4

Merged
merged 15 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod data;
mod errors;
mod events;
mod utils;
mod cast;

use fungible_bridge_abi::FungibleBridge;
use FRC20_abi::FRC20;
Expand Down Expand Up @@ -40,27 +41,7 @@ use utils::{
parse_message_data,
};

impl From<b256> for U256 {
fn from(value: b256) -> U256 {
let (word1, word2, word3, word4) = asm(r1: value) { r1: (u64, u64, u64, u64) };
let result = U256::from((word1, word2, word3, word4));

result
}

fn into(self) -> b256 {
let result: b256 = ZERO_B256;

asm(output: result, r1: self.a, r2: self.b, r3: self.c, r4: self.d) {
sw output r1 i0; // store the word in r4 in output + 0 words
sw output r2 i1; // store the word in r5 in output + 1 word
sw output r3 i2; // store the word in r5 in output + 1 word
sw output r4 i3; // store the word in r5 in output + 1 word
}

result
}
}
use cast::*;

storage {
refund_amounts: StorageMap<b256, StorageMap<b256, b256>> = StorageMap {},
Expand Down
82 changes: 82 additions & 0 deletions packages/fungible-token/bridge-fungible-token/src/cast.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
library;

use std::u256::U256;
use std::constants::ZERO_B256;

impl From<b256> for U256 {
fn from(value: b256) -> U256 {
let (word1, word2, word3, word4) = asm(r1: value) { r1: (u64, u64, u64, u64) };
let result = U256::from((word1, word2, word3, word4));

result
}

fn into(self) -> b256 {
let result: b256 = ZERO_B256;

asm(output: result, r1: self.a, r2: self.b, r3: self.c, r4: self.d) {
sw output r1 i0; // store the word in r1 in output + 0 words
sw output r2 i1; // store the word in r2 in output + 1 word
sw output r3 i2; // store the word in r3 in output + 1 word
sw output r4 i3; // store the word in r4 in output + 1 word
}

result
}
}

#[test]
fn test_b256_addition() {
let one = U256::from((0, 0, 0, 1));
let two = U256::from((0, 0, 0, 2));

let addition = one.add(two);
let three = U256::from((0, 0, 0, 3));

assert(three == addition);

let three_b256: b256 = 0x0000000000000000000000000000000000000000000000000000000000000003;

assert(U256::from(three_b256) == three);
assert(U256::from(three_b256) == addition);

let three_into_b256: b256 = three.into();
assert(three_into_b256 == three_b256);

let addition_into_b256: b256 = addition.into();
assert(addition_into_b256 == three_b256);
}

#[test]
fn test_b256_conversion() {
let u64_max = 0xffffffffffffffff;

let max = U256::from((u64_max, u64_max, u64_max, u64_max));
let max_b256: b256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;

let max_into_b256: b256 = max.into();
assert(max_into_b256 == max_b256); // test into
assert(max == U256::from(max_b256)); // test from
let random = 0x0123456789abcdef;

let unsigned = U256::from((random, 0, random, 0));
let bits: b256 = 0x0123456789abcdef00000000000000000123456789abcdef0000000000000000;

let unsigned_into_b256: b256 = unsigned.into();
assert(U256::from(bits) == unsigned);
assert(unsigned_into_b256 == bits);
}

#[test(should_revert)]
fn test_b256_overflow() {
let max_b256: b256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
let max = U256::from(max_b256);
let overflow = max.add(U256::from((0, 0, 0, 1)));
}

#[test(should_revert)]
fn test_b256_underflow() {
let min_b256: b256 = 0x0000000000000000000000000000000000000000000000000000000000000000;
let min = U256::from(min_b256);
let overflow = min.subtract(U256::from((0, 0, 0, 1)));
}
17 changes: 8 additions & 9 deletions packages/fungible-token/bridge-fungible-token/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,13 @@ mod success {
// verify that trying to claim funds from
// the correct token fails
let error_response = test_contract
.methods()
.claim_refund(
Bits256::from_hex_str(FROM).unwrap(),
Bits256::from_hex_str(BRIDGED_TOKEN).unwrap(),
)
.call()
.await;
.methods()
.claim_refund(
Bits256::from_hex_str(FROM).unwrap(),
Bits256::from_hex_str(BRIDGED_TOKEN).unwrap(),
)
.call()
.await;
assert!(error_response.is_err());

let call_response = test_contract
Expand Down Expand Up @@ -1485,7 +1485,7 @@ mod revert {

let one = Unsigned256::from(1);

let (message2,_,_) = env::construct_msg_data(
let (message2, _, _) = env::construct_msg_data(
wrong_token_value,
FROM,
*Address::from_str(TO).unwrap(),
Expand Down Expand Up @@ -1553,7 +1553,6 @@ mod revert {
// Verify the message value was received by the test contract
assert_eq!(test_contract_balance, 200);


// check that the RefundRegisteredEvent receipt is populated correctly
assert_eq!(
refund_registered_event[0].amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ use fuel_core_types::{
};
use fuels::{
accounts::{
fuel_crypto::{
SecretKey,
fuel_types::Nonce
},
predicate::Predicate,
wallet::WalletUnlocked,
Signer,
ViewOnlyAccount,
fuel_crypto::{fuel_types::Nonce, SecretKey},
predicate::Predicate,
wallet::WalletUnlocked,
Signer, ViewOnlyAccount,
},
prelude::{
abigen, setup_custom_assets_coins, setup_test_provider, Address, AssetConfig, AssetId,
Expand Down Expand Up @@ -190,7 +186,7 @@ pub async fn setup_environment(
.collect();
let all_coins = setup_custom_assets_coins(wallet.address(), &asset_configs[..]);

// Generate message
// Generate message
let mut message_nonce = Nonce::zeroed();
let message_sender = match sender {
Some(v) => Address::from_str(v).unwrap(),
Expand Down