Skip to content

Commit

Permalink
Compute calldata offset on B256 argument (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
digorithm committed Apr 7, 2022
1 parent a86dffe commit 7fda121
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Syntax highlighting of sway files as rust
*.sw linguist-language=Rust
35 changes: 35 additions & 0 deletions packages/fuels-abigen-macro/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,3 +1458,38 @@ async fn test_tuples() {
let response = instance.returns_tuple((1, 2)).call().await.unwrap();
assert_eq!(response.value, (1, 2));
}

#[tokio::test]
async fn test_auth_msg_sender_from_sdk() {
let mut rng = StdRng::seed_from_u64(2322u64);

abigen!(
AuthContract,
"packages/fuels-abigen-macro/tests/test_projects/auth_testing_contract/out/debug/auth_testing_contract-abi.json"
);

let salt: [u8; 32] = rng.gen();
let salt = Salt::from(salt);

let (provider, wallet) = setup_test_provider_and_wallet().await;
let compiled = Contract::load_sway_contract(
"tests/test_projects/auth_testing_contract/out/debug/auth_testing_contract.bin",
salt,
)
.unwrap();

let id = Contract::deploy(&compiled, &provider, &wallet, TxParameters::default())
.await
.unwrap();

let auth_instance = AuthContract::new(id.to_string(), provider.clone(), wallet.clone());

// Contract returns true if `msg_sender()` matches `wallet.address()`.
let result = auth_instance
.check_msg_sender(wallet.address())
.call()
.await
.unwrap();

assert!(result.value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[package]]
name = 'auth_testing_abi'
dependencies = ['std git+https://github.com/FuelLabs/sway?branch=master#b0ab3f8a67f71f6697266c36394ef56d429637aa']

[[package]]
name = 'core'
dependencies = []

[[package]]
name = 'std'
source = 'git+https://github.com/FuelLabs/sway?branch=master#b0ab3f8a67f71f6697266c36394ef56d429637aa'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "auth_testing_abi"

[dependencies]
std = { git = "https://github.com/FuelLabs/sway", branch = "master" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
library auth_testing_abi;

use std::address::Address;
use std::contract_id::ContractId;
use std::chain::auth::*;
use std::result::*;

abi AuthTesting {
fn is_caller_external() -> bool;
fn check_msg_sender(expected_id: Address) -> bool;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[[package]]
name = 'auth_testing_abi'
dependencies = ['std git+https://github.com/FuelLabs/sway?branch=master#35cac1b6b4a1ca21e2b617d9334dcafa1363f737']

[[package]]
name = 'auth_testing_contract'
dependencies = [
'auth_testing_abi',
'std git+https://github.com/FuelLabs/sway?branch=master#35cac1b6b4a1ca21e2b617d9334dcafa1363f737',
]

[[package]]
name = 'core'
dependencies = []

[[package]]
name = 'std'
source = 'git+https://github.com/FuelLabs/sway?branch=master#35cac1b6b4a1ca21e2b617d9334dcafa1363f737'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "auth_testing_contract"

[dependencies]
std = { git = "https://github.com/FuelLabs/sway", branch = "master" }
auth_testing_abi = { path = "../auth_testing_abi"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"type": "function",
"inputs": [],
"name": "is_caller_external",
"outputs": [
{
"name": "",
"type": "bool",
"components": null
}
]
},
{
"type": "function",
"inputs": [
{
"name": "expected_id",
"type": "struct Address",
"components": [
{
"name": "value",
"type": "b256",
"components": null
}
]
}
],
"name": "check_msg_sender",
"outputs": [
{
"name": "",
"type": "bool",
"components": null
}
]
}
]
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
contract;

use std::address::Address;
use std::chain::auth::*;
use std::contract_id::ContractId;
use auth_testing_abi::*;
use std::result::*;
use std::assert::assert;

impl AuthTesting for Contract {
fn is_caller_external() -> bool {
caller_is_external()
}

fn check_msg_sender(expected_id: Address) -> bool {
let result: Result<Sender, AuthError> = msg_sender();
let mut ret = false;
if result.is_err() {
ret = false;
} else {
let unwrapped = result.unwrap();
if let Sender::Address(v) = unwrapped {
assert(v == expected_id);
ret = true;
} else {
ret = false;
}
};

ret
}
}
10 changes: 6 additions & 4 deletions packages/fuels-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,12 @@ impl Contract {

// Returns true if the method call takes custom inputs or has more than one argument. This is used to determine whether we need to compute the `call_data_offset`.
fn should_compute_call_data_offset(args: &[Token]) -> bool {
match args
.iter()
.any(|t| matches!(t, Token::Struct(_) | Token::Enum(_) | Token::Tuple(_)))
{
match args.iter().any(|t| {
matches!(
t,
Token::Struct(_) | Token::Enum(_) | Token::B256(_) | Token::Tuple(_)
)
}) {
true => true,
false => args.len() > 1,
}
Expand Down

0 comments on commit 7fda121

Please sign in to comment.