Skip to content

Commit

Permalink
bug: fix code being shifted when loaded via LDC (#1503)
Browse files Browse the repository at this point in the history
  • Loading branch information
segfault-magnet authored Sep 6, 2024
1 parent c7f6806 commit fc6eb56
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
3 changes: 2 additions & 1 deletion e2e/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ members = [
'sway/bindings/simple_contract',
'sway/bindings/type_paths',
'sway/contracts/asserts',
'sway/contracts/huge_contract',
'sway/contracts/auth_testing_abi',
'sway/contracts/auth_testing_contract',
'sway/contracts/block_timestamp',
'sway/contracts/configurables',
'sway/contracts/contract_test',
'sway/contracts/huge_contract',
'sway/contracts/large_return_data',
'sway/contracts/lib_contract',
'sway/contracts/lib_contract_abi',
Expand All @@ -23,6 +23,7 @@ members = [
'sway/contracts/multiple_read_calls',
'sway/contracts/needs_custom_decoder',
'sway/contracts/payable_annotation',
'sway/contracts/proxy',
'sway/contracts/require',
'sway/contracts/revert_transaction_error',
'sway/contracts/storage',
Expand Down
7 changes: 6 additions & 1 deletion e2e/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ impl Downloader {
const LINK_TEMPLATE: &str = "https://github.com/FuelLabs/fuel-core/releases/download/vVERSION/fuel-core-VERSION-x86_64-unknown-linux-gnu.tar.gz";
let link = LINK_TEMPLATE.replace("VERSION", &SUPPORTED_FUEL_CORE_VERSION.to_string());

let response = reqwest::blocking::get(link)?;
let response = reqwest::blocking::Client::builder()
.timeout(std::time::Duration::from_secs(60))
.build()?
.get(link)
.send()?;

if !response.status().is_success() {
anyhow::bail!("Failed to download wasm executor: {}", response.status());
}
Expand Down
7 changes: 7 additions & 0 deletions e2e/sway/contracts/proxy/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "proxy"

[dependencies]
29 changes: 29 additions & 0 deletions e2e/sway/contracts/proxy/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
contract;

use std::execution::run_external;

abi Proxy {
#[storage(write)]
fn set_target_contract(id: ContractId);

// this targets the method of the `huge_contract` in our e2e sway contracts
#[storage(read)]
fn something() -> u64;
}

storage {
target_contract: Option<ContractId> = None,
}

impl Proxy for Contract {
#[storage(write)]
fn set_target_contract(id: ContractId) {
storage.target_contract.write(Some(id));
}

#[storage(read)]
fn something() -> u64 {
let target = storage.target_contract.read().unwrap();
run_external(target)
}
}
50 changes: 50 additions & 0 deletions e2e/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2309,3 +2309,53 @@ async fn loader_blob_already_uploaded_not_an_issue() -> Result<()> {

Ok(())
}

#[tokio::test]
async fn loader_works_via_proxy() -> Result<()> {
let wallet = launch_provider_and_get_wallet().await?;

abigen!(
Contract(
name = "MyContract",
abi = "e2e/sway/contracts/huge_contract/out/release/huge_contract-abi.json"
),
Contract(
name = "MyProxy",
abi = "e2e/sway/contracts/proxy/out/release/proxy-abi.json"
)
);

let contract_binary = "sway/contracts/huge_contract/out/release/huge_contract.bin";

let contract = Contract::load_from(contract_binary, LoadConfiguration::default())?;

let contract_id = contract
.convert_to_loader(100)?
.deploy(&wallet, TxPolicies::default())
.await?;

let contract_binary = "sway/contracts/proxy/out/release/proxy.bin";

let proxy_id = Contract::load_from(contract_binary, LoadConfiguration::default())?
.deploy(&wallet, TxPolicies::default())
.await?;

let proxy = MyProxy::new(proxy_id, wallet.clone());
proxy
.methods()
.set_target_contract(contract_id.clone())
.call()
.await?;

let response = proxy
.methods()
.something()
.with_contract_ids(&[contract_id])
.call()
.await?
.value;

assert_eq!(response, 1001);

Ok(())
}
2 changes: 1 addition & 1 deletion packages/fuels-programs/src/contract/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn loader_contract_asm(blob_ids: &[BlobId]) -> Result<Vec<u8>> {
[
// 1. Load the blob contents into memory
// Find the start of the hardcoded blob IDs, which are located after the code ends.
op::move_(0x10, RegId::IS),
op::move_(0x10, RegId::PC),
// 0x10 to hold the address of the current blob ID.
op::addi(0x10, 0x10, num_of_instructions * Instruction::SIZE as u16),
// The contract is going to be loaded from the current value of SP onwards, save
Expand Down

0 comments on commit fc6eb56

Please sign in to comment.