Skip to content

Commit

Permalink
Proof that #6324 is not a bug.
Browse files Browse the repository at this point in the history
The reproduction presented in #6324 was missing loading the storage
configuration properly.

To do that we have to use the following in test/src/sdk-harness/test_projects/run_external_proxy_with_storage/mod.rs:
    StorageConfiguration::default().add_slot_overrides_from_file("test_projects/run_external_target_with_storage/out/release/run_external_target_with_storage-storage_slots.json").unwrap();

This commit changes the presented reproduction to load the storage slots
and the test runs without reverting.

Closes #6324
  • Loading branch information
esdrubal committed Sep 20, 2024
1 parent a0f5cf0 commit 0da96a6
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/src/sdk-harness/Forc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,21 @@ name = "run_external_proxy"
source = "member"
dependencies = ["std"]

[[package]]
name = "run_external_proxy_with_storage"
source = "member"
dependencies = ["std"]

[[package]]
name = "run_external_target"
source = "member"
dependencies = ["std"]

[[package]]
name = "run_external_target_with_storage"
source = "member"
dependencies = ["std"]

[[package]]
name = "script_bytecode"
source = "member"
Expand Down
2 changes: 2 additions & 0 deletions test/src/sdk-harness/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ members = [
"test_projects/result_option_expect",
"test_projects/run_external_proxy",
"test_projects/run_external_target",
"test_projects/run_external_proxy_with_storage",
"test_projects/run_external_target_with_storage",
"test_projects/script_bytecode",
"test_projects/script_data",
"test_projects/storage",
Expand Down
1 change: 1 addition & 0 deletions test/src/sdk-harness/test_projects/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mod registers;
mod result_in_abi;
mod result_option_expect;
mod run_external_proxy;
mod run_external_proxy_with_storage;
mod script_data;
mod storage;
mod storage_access;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-37E4B1588712FA61'

[[package]]
name = 'std'
source = 'path+from-root-37E4B1588712FA61'
dependencies = ['core']

[[package]]
name = 'run_external_proxy'
source = 'member'
dependencies = ['std']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "run_external_proxy_with_storage"

[dependencies]
std = { path = "../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use fuels::{prelude::*, types::Bits256};

abigen!(Contract(
name = "RunExternalProxyContract",
abi = "test_projects/run_external_proxy_with_storage/out/release/run_external_proxy_with_storage-abi.json",
));

#[tokio::test]
async fn run_external_can_proxy_call() {
let wallet = launch_provider_and_get_wallet().await.unwrap();

let storage_configuration =
StorageConfiguration::default().add_slot_overrides_from_file("test_projects/run_external_target_with_storage/out/release/run_external_target_with_storage-storage_slots.json").unwrap();

let target_id = Contract::load_from(
"test_projects/run_external_target_with_storage/out/release/run_external_target_with_storage.bin",
LoadConfiguration::default()
.with_storage_configuration(storage_configuration.clone()),
)
.unwrap()
.deploy(&wallet, TxPolicies::default())
.await
.unwrap();

let configurables = RunExternalProxyContractConfigurables::default()
.with_TARGET(target_id.clone().into())
.unwrap();
let id = Contract::load_from(
"test_projects/run_external_proxy_with_storage/out/release/run_external_proxy_with_storage.bin",
LoadConfiguration::default().with_configurables(configurables).with_storage_configuration(storage_configuration),
)
.unwrap()
.deploy(&wallet, TxPolicies::default())
.await
.unwrap();
let instance = RunExternalProxyContract::new(id.clone(), wallet);
// Call "large_value"
// Will call run_external_proxy::large_value
// that will call run_external_target::large_value
// and return the value doubled.
let result = instance
.methods()
.large_value()
.with_contract_ids(&[target_id.clone().into()])
.call()
.await
.unwrap();
for r in result.receipts.iter() {
match r {
Receipt::LogData { data, .. } => {
if let Some(data) = data {
if data.len() > 8 {
if let Ok(s) = std::str::from_utf8(&data[8..]) {
print!("{:?} ", s);
}
}
println!("{:?}", data);
}
}
_ => {}
}
}
let expected_large =
Bits256::from_hex_str("0x00000000000000000000000059F2f1fCfE2474fD5F0b9BA1E73ca90b143Eb8d0")
.unwrap();
assert_eq!(result.value, expected_large);
// Call "double_value"
// Will call run_external_proxy::double_value
// that will call run_external_target::double_value
// and return the value doubled.
let result = instance
.methods()
.double_value(42)
.with_contract_ids(&[target_id.clone().into()])
.call()
.await
.unwrap();
for r in result.receipts.iter() {
match r {
Receipt::LogData { data, .. } => {
if let Some(data) = data {
if data.len() > 8 {
if let Ok(s) = std::str::from_utf8(&data[8..]) {
print!("{:?} ", s);
}
}
println!("{:?}", data);
}
}
_ => {}
}
}
assert_eq!(result.value, 84);
// Call "does_not_exist_in_the_target"
// Will call run_external_proxy::does_not_exist_in_the_target
// it will proxy the call to run_external_target,
// and endup in the fallback, fn that will triple the input value
let result = instance
.methods()
.does_not_exist_in_the_target(42)
.with_contract_ids(&[target_id.into()])
.call()
.await
.unwrap();
assert_eq!(result.value, 126);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
contract;

use std::execution::run_external;

configurable {
TARGET: ContractId = ContractId::zero(),
}

abi RunExternalTest {
fn double_value(foo: u64) -> u64;
fn large_value() -> b256;
fn does_not_exist_in_the_target(foo: u64) -> u64;
}
impl RunExternalTest for Contract {
fn double_value(_foo: u64) -> u64 {
__log(1);
run_external(TARGET)
}

fn large_value() -> b256 {
run_external(TARGET)
}

// ANCHOR: does_not_exist_in_the_target
fn does_not_exist_in_the_target(_foo: u64) -> u64 {
run_external(TARGET)
}
// ANCHOR_END: does_not_exist_in_the_target
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-37E4B1588712FA61'

[[package]]
name = 'std'
source = 'path+from-root-37E4B1588712FA61'
dependencies = ['core']

[[package]]
name = 'run_external_target'
source = 'member'
dependencies = ['std']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "run_external_target_with_storage"

[dependencies]
std = { path = "../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
contract;

use std::constants::ZERO_B256;

storage {
owner: Identity = Identity::Address(Address::from(ZERO_B256)),
simple_value: u64 = 0,
}

abi RunExternalTest {
fn double_value(foo: u64) -> u64;
fn large_value() -> b256;
}

impl RunExternalTest for Contract {
fn double_value(foo: u64) -> u64 {
__log(2);
foo * 2
}
fn large_value() -> b256 {
0x00000000000000000000000059F2f1fCfE2474fD5F0b9BA1E73ca90b143Eb8d0
}
}

// ANCHOR: fallback
#[fallback, storage(read, write)]
fn fallback() -> u64 {
// STORAGE READS are failing
let iden = storage.owner.read(); // this will fail
// storage.simple_value.read(); // this will fail

// BUT STORAGE WRITES are succesfull

Check warning on line 32 in test/src/sdk-harness/test_projects/run_external_target_with_storage/src/main.sw

View workflow job for this annotation

GitHub Actions / find-typos

"succesfull" should be "successful".
// storage.simple_value.write(foo); // this will succeed

use std::call_frames::*;
__log(3);
__log(called_method());
__log("double_value");
__log(called_method() == "double_value");
let foo = called_args::<u64>();
foo * 3
}
// ANCHOR_END: fallback

0 comments on commit 0da96a6

Please sign in to comment.