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

Fix from in evm_address to make a mut local copy of the bits argument before calling mcli #2227

Merged
merged 4 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
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" }
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" }
35 changes: 35 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,35 @@
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() {
mohammadfawaz marked this conversation as resolved.
Show resolved Hide resolved
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]);

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