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

Compute calldata offset on B256 argument #203

Merged
merged 4 commits into from
Apr 7, 2022
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
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
34 changes: 34 additions & 0 deletions packages/fuels-abigen-macro/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1424,3 +1424,37 @@ async fn test_multiple_args() {
let response = instance.get_single(5).call().await.unwrap();
assert_eq!(response.value, 5);
}

#[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());

let result = auth_instance
.returns_msg_sender_address(wallet.address())
digorithm marked this conversation as resolved.
Show resolved Hide resolved
.call()
.await
.unwrap();

assert!(result.value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[package]]
name = 'auth_testing_abi'
dependencies = ['std']

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

[[package]]
name = 'std'
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,12 @@
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 returns_msg_sender(expected_id: ContractId) -> bool;
fn returns_msg_sender_address(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,62 @@
[
{
"type": "function",
"inputs": [],
"name": "is_caller_external",
"outputs": [
{
"name": "",
"type": "bool",
"components": null
}
]
},
{
"type": "function",
"inputs": [
{
"name": "expected_id",
"type": "struct ContractId",
"components": [
{
"name": "value",
"type": "b256",
"components": null
}
]
}
],
"name": "returns_msg_sender",
"outputs": [
{
"name": "",
"type": "bool",
"components": null
}
]
},
{
"type": "function",
"inputs": [
{
"name": "expected_id",
"type": "struct Address",
"components": [
{
"name": "value",
"type": "b256",
"components": null
}
]
}
],
"name": "returns_msg_sender_address",
"outputs": [
{
"name": "",
"type": "bool",
"components": null
}
]
}
]
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
contract;
Copy link
Contributor

@mohammadfawaz mohammadfawaz Apr 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fuels-rs repo probably needs this: https://github.com/FuelLabs/sway/blob/master/.gitattributes for Sway syntax highlighting.


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 returns_msg_sender(expected_id: ContractId) -> bool {
digorithm marked this conversation as resolved.
Show resolved Hide resolved
digorithm marked this conversation as resolved.
Show resolved Hide resolved
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::ContractId(v) = unwrapped {
assert(v == expected_id);
ret = true;
} else {
ret = false;
};
};

ret
}

fn returns_msg_sender_address(expected_id: Address) -> bool {
digorithm marked this conversation as resolved.
Show resolved Hide resolved
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
}
}
2 changes: 1 addition & 1 deletion packages/fuels-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl Contract {
fn should_compute_call_data_offset(args: &[Token]) -> bool {
match args
.iter()
.any(|t| matches!(t, Token::Struct(_) | Token::Enum(_)))
.any(|t| matches!(t, Token::Struct(_) | Token::Enum(_) | Token::B256(_)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tuples should probably be on this list as well? Then there's probably the question about strings and arrays.

Maybe it's better if we invert the condition to check for copy types since they are a limited well defined set that will likely never change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't support tuples:

pub enum Token {
U8(u8),
U16(u16),
U32(u32),
U64(u64),
Bool(bool),
Byte(u8),
B256(Bits256),
Array(Vec<Token>),
String(String),
Struct(Vec<Token>),
Enum(Box<EnumSelector>),
}

Maybe it's better if we invert the condition to check for copy types since they are a limited well defined set that will likely never change?

That's a good idea, but this whole thing will go away very soon, as soon as this is done. So I didn't wanna shake things too much, but I'm not married to this idea. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah of course, you're right... let's keep it as is :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Rodrigo as well

{
true => true,
false => args.len() > 1,
Expand Down