Skip to content

Commit

Permalink
Fix from in evm_address to make a mut local copy of the bits
Browse files Browse the repository at this point in the history
…argument before calling `mcli` (#2227)

* fixing evm_address to make a local copy of bits before calling mcli

* Add missing lock file

* split into two tests
  • Loading branch information
mohammadfawaz authored Jul 5, 2022
1 parent 88d8592 commit e705caa
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 2 deletions.
6 changes: 4 additions & 2 deletions sway-lib-std/src/vm/evm/evm_address.sw
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ pub trait From {
impl From for EvmAddress {
fn from(bits: b256) -> EvmAddress {
// An EVM address is only 20 bytes, so the first 12 are set to zero
asm(r1: bits) {
// Create a mutable local copy of `bits`
let mut local_bits = bits;
asm(r1: local_bits) {
mcli r1 i12;
};

EvmAddress {
value: bits,
value: local_bits,
}
}
}
2 changes: 2 additions & 0 deletions test/src/sdk-harness/test_artifacts/evm_test_abi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
14 changes: 14 additions & 0 deletions test/src/sdk-harness/test_artifacts/evm_test_abi/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[package]]
name = 'core'
source = 'path+from-root-DD6C78AF57ACC141'
dependencies = []

[[package]]
name = 'evm_test_abi'
source = 'root'
dependencies = ['std']

[[package]]
name = 'std'
source = 'path+from-root-DD6C78AF57ACC141'
dependencies = ['core']
8 changes: 8 additions & 0 deletions test/src/sdk-harness/test_artifacts/evm_test_abi/Forc.toml
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 = "evm_test_abi"

[dependencies]
std = { path = "../../../../../sway-lib-std" }
8 changes: 8 additions & 0 deletions test/src/sdk-harness/test_artifacts/evm_test_abi/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
library evm_test_abi;

use std::vm::evm::evm_address::EvmAddress;

abi EvmTest {
fn evm_address_from_literal() -> EvmAddress;
fn evm_address_from_argument(raw_address: b256) -> EvmAddress;
}
2 changes: 2 additions & 0 deletions test/src/sdk-harness/test_projects/evm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
22 changes: 22 additions & 0 deletions test/src/sdk-harness/test_projects/evm/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[[package]]
name = 'core'
source = 'path+from-root-4947D78C815C1154'
dependencies = []

[[package]]
name = 'evm'
source = 'root'
dependencies = [
'evm_test_abi',
'std',
]

[[package]]
name = 'evm_test_abi'
source = 'path+from-root-4947D78C815C1154'
dependencies = ['std']

[[package]]
name = 'std'
source = 'path+from-root-4947D78C815C1154'
dependencies = ['core']
9 changes: 9 additions & 0 deletions test/src/sdk-harness/test_projects/evm/Forc.toml
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 = "evm"

[dependencies]
evm_test_abi = { path = "../../test_artifacts/evm_test_abi" }
std = { path = "../../../../../sway-lib-std" }
40 changes: 40 additions & 0 deletions test/src/sdk-harness/test_projects/evm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use fuels::{prelude::*, tx::ContractId};

abigen!(
EvmTestContract,
"test_projects/evm/out/debug/evm-abi.json"
);

async fn get_evm_test_instance() -> (EvmTestContract, ContractId) {
let wallet = launch_provider_and_get_single_wallet().await;
let id = Contract::deploy(
"test_projects/evm/out/debug/evm.bin",
&wallet,
TxParameters::default(),
)
.await
.unwrap();
let instance = EvmTestContract::new(id.to_string(), wallet);

(instance, id)
}

#[tokio::test]
async fn can_call_from_literal() {
let (instance, _) = get_evm_test_instance().await;

let raw_address = [6; 32]; // hardcoded in the contract to test calling `from()` with a literal
let result = instance.evm_address_from_literal().call().await.unwrap();
assert_eq!(result.value.value[0..12], [0; 12]);
assert_eq!(result.value.value[12..32], raw_address[12..32]);
}

#[tokio::test]
async fn can_call_from_argument() {
let (instance, _) = get_evm_test_instance().await;

let raw_address = [7; 32];
let result = instance.evm_address_from_argument(raw_address.into()).call().await.unwrap();
assert_eq!(result.value.value[0..12], [0; 12]);
assert_eq!(result.value.value[12..32], raw_address[12..32]);
}
14 changes: 14 additions & 0 deletions test/src/sdk-harness/test_projects/evm/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
contract;

use evm_test_abi::EvmTest;
use std::vm::evm::evm_address::EvmAddress;

impl EvmTest for Contract {
fn evm_address_from_literal() -> EvmAddress {
~EvmAddress::from(0x0606060606060606060606060606060606060606060606060606060606060606)
}

fn evm_address_from_argument(raw_address: b256) -> EvmAddress {
~EvmAddress::from(raw_address)
}
}
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 @@ -3,6 +3,7 @@
mod auth;
mod call_frames;
mod context;
mod evm;
mod exponentiation;
mod hashing;
mod logging;
Expand Down

0 comments on commit e705caa

Please sign in to comment.