Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into fix/evm-base-weight…
Browse files Browse the repository at this point in the history
…-issue

# Conflicts:
#	Cargo.lock
#	integration-tests/Cargo.toml
#	runtime/hydradx/Cargo.toml
#	runtime/hydradx/src/evm/mod.rs
#	runtime/hydradx/src/lib.rs
  • Loading branch information
mrq1911 committed Oct 17, 2024
2 parents b4ab33c + ee99573 commit c2d54b7
Show file tree
Hide file tree
Showing 95 changed files with 14,082 additions and 1,510 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ fork-testing/local-raw.json

SNAPSHOT*

db.sqlite
db.sqlite*
779 changes: 356 additions & 423 deletions Cargo.lock

Large diffs are not rendered by default.

1,046 changes: 524 additions & 522 deletions Cargo.toml

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
cargo := cargo --config net.git-fetch-with-cli=true
ifeq ($(shell uname),Darwin)
# macOS-specific commands
sha256sum := shasum -a 256
else
# Default commands for other systems
sha256sum := sha256sum
endif

.PHONY: build
build:
Expand Down Expand Up @@ -60,11 +67,12 @@ clean:
.PHONY: docker
docker:
docker build -t hydra-dx .
docker tag hydra-dx galacticcouncil/hydra-dx:latest

checksum:
sha256sum target/release/hydradx > target/release/hydradx.sha256
$(sha256sum) target/release/hydradx > target/release/hydradx.sha256
cp target/release/wbuild/hydradx-runtime/hydradx_runtime.compact.compressed.wasm target/release/
sha256sum target/release/hydradx_runtime.compact.compressed.wasm > target/release/hydradx_runtime.compact.compressed.wasm.sha256
$(sha256sum) target/release/hydradx_runtime.compact.compressed.wasm > target/release/hydradx_runtime.compact.compressed.wasm.sha256

release: build-release checksum

Expand Down
8 changes: 7 additions & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "runtime-integration-tests"
version = "1.23.8"
version = "1.23.7"
description = "Integration tests"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down Expand Up @@ -132,6 +132,12 @@ polkadot-primitives = { workspace = true }
polkadot-service = { workspace = true, features = ["full-node"] }
polkadot-runtime-parachains = { workspace = true }
rococo-runtime = { workspace = true }
module-evm-utility-macro = { workspace = true }
num_enum = { workspace = true }

ethabi = { workspace = true }
serde_json = { workspace = true }
hex = { workspace = true }

[dev-dependencies]
pretty_assertions = { workspace = true }
Expand Down
214 changes: 214 additions & 0 deletions integration-tests/src/contracts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
use crate::evm::dai_ethereum_address;
use crate::polkadot_test_net::Hydra;
use crate::polkadot_test_net::TestNet;
use crate::polkadot_test_net::ALICE;
use crate::polkadot_test_net::BOB;
use crate::polkadot_test_net::UNITS;
use crate::polkadot_test_net::WETH;
use crate::utils::contracts::deploy_contract;
use crate::utils::contracts::deploy_contract_code;
use crate::utils::contracts::get_contract_bytecode;
use fp_evm::ExitReason::Succeed;
use fp_evm::ExitSucceed::Stopped;
use fp_evm::FeeCalculator;
use frame_support::assert_ok;
use hex_literal::hex;
use hydradx_runtime::evm::precompiles::handle::EvmDataWriter;
use hydradx_runtime::evm::precompiles::Bytes;
use hydradx_runtime::evm::Executor;
use hydradx_runtime::AccountId;
use hydradx_runtime::EVMAccounts;
use hydradx_runtime::Runtime;
use hydradx_runtime::RuntimeEvent;
use hydradx_runtime::System;
use hydradx_traits::evm::CallContext;
use hydradx_traits::evm::EvmAddress;
use hydradx_traits::evm::InspectEvmAccounts;
use hydradx_traits::evm::EVM;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use pretty_assertions::assert_eq;
use sp_core::H256;
use sp_core::{RuntimeDebug, U256};
use test_utils::expect_events;
use xcm_emulator::Network;
use xcm_emulator::TestExt;

pub fn deployer() -> EvmAddress {
EVMAccounts::evm_address(&Into::<AccountId>::into(ALICE))
}

#[module_evm_utility_macro::generate_function_selector]
#[derive(RuntimeDebug, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum Function {
IsContract = "isContract(address)",
Check = "check(address)",
Initialize = "initialize(address)",
InitializePayload = "initialize(address,address,bytes)",
}

fn is_contract(checker: EvmAddress, address: EvmAddress) -> bool {
let mut data = Into::<u32>::into(Function::Check).to_be_bytes().to_vec();
data.extend_from_slice(H256::from(address).as_bytes());
let context = CallContext {
contract: checker,
sender: Default::default(),
origin: Default::default(),
};
let (res, _) = Executor::<Runtime>::call(context, data, U256::zero(), 100_000);
matches!(res, Succeed(_))
}

#[test]
fn contract_check_succeeds_on_deployed_contract() {
TestNet::reset();
Hydra::execute_with(|| {
let checker = deploy_contract("ContractCheck", deployer());

assert_eq!(is_contract(checker, checker), true);
});
}

#[test]
fn contract_check_fails_on_eoa() {
TestNet::reset();
Hydra::execute_with(|| {
let checker = deploy_contract("ContractCheck", deployer());

assert_eq!(is_contract(checker, deployer()), false);
});
}

#[test]
fn contract_check_succeeds_on_currencies_precompile() {
TestNet::reset();
Hydra::execute_with(|| {
let checker = deploy_contract("ContractCheck", deployer());

assert_eq!(is_contract(checker, dai_ethereum_address()), true);
});
}

#[test]
fn contract_check_succeeds_on_precompile_with_code() {
TestNet::reset();
Hydra::execute_with(|| {
let checker = deploy_contract("ContractCheck", deployer());
pallet_evm::AccountCodes::<Runtime>::insert(
dai_ethereum_address(),
&hex!["365f5f375f5f365f73bebebebebebebebebebebebebebebebebebebebe5af43d5f5f3e5f3d91602a57fd5bf3"][..],
);
assert_eq!(is_contract(checker, dai_ethereum_address()), true);
});
}

#[test]
fn contract_check_fails_on_precompile_without_code() {
TestNet::reset();
Hydra::execute_with(|| {
let checker = deploy_contract("ContractCheck", deployer());
pallet_evm::AccountCodes::<Runtime>::remove(dai_ethereum_address());
assert_eq!(is_contract(checker, dai_ethereum_address()), false);
});
}

#[test]
fn contract_check_succeeds_on_precompile_with_invalid_code() {
TestNet::reset();
Hydra::execute_with(|| {
let checker = deploy_contract("ContractCheck", deployer());
pallet_evm::AccountCodes::<Runtime>::insert(dai_ethereum_address(), &hex!["00"][..]);
assert_eq!(is_contract(checker, dai_ethereum_address()), true);
});
}

#[test]
fn contract_check_should_succeed_when_called_from_extrinsic() {
TestNet::reset();
Hydra::execute_with(|| {
// Arrange
let checker = deploy_contract("ContractCheck", deployer());
let mut data = Into::<u32>::into(Function::Check).to_be_bytes().to_vec();
data.extend_from_slice(H256::from(dai_ethereum_address()).as_bytes());
assert_ok!(EVMAccounts::bind_evm_address(hydradx_runtime::RuntimeOrigin::signed(
BOB.into()
)));
assert_ok!(hydradx_runtime::Currencies::update_balance(
hydradx_runtime::RuntimeOrigin::root(),
BOB.into(),
WETH,
(10_000_000 * UNITS) as i128,
));
pallet_evm::AccountCodes::<Runtime>::insert(
dai_ethereum_address(),
&hex!["365f5f375f5f365f73bebebebebebebebebebebebebebebebebebebebe5af43d5f5f3e5f3d91602a57fd5bf3"][..],
);

// Act
assert_ok!(hydradx_runtime::EVM::call(
hydradx_runtime::RuntimeOrigin::signed(BOB.into()),
EVMAccounts::evm_address(&Into::<AccountId>::into(BOB)),
checker,
data,
U256::from(0),
1000000,
hydradx_runtime::DynamicEvmFee::min_gas_price().0 * 10,
None,
Some(System::account_nonce(AccountId::from(BOB)).into()),
[].into()
));

// Assert
expect_events::<RuntimeEvent, Runtime>(vec![pallet_evm::Event::Executed { address: checker }.into()]);
});
}

#[test]
fn proxy_should_be_initialized_correctly() {
TestNet::reset();
Hydra::execute_with(|| {
// Arrange
let proxy = deploy_contract("TreasuryProxy", deployer());
let mut controller_code = get_contract_bytecode("Treasury-Controller");
controller_code.extend_from_slice(H256::from(deployer()).as_bytes());
let controller = deploy_contract_code(controller_code, deployer());
let implementation = deploy_contract("Treasury-Implementation", deployer());
let implementation_init = EvmDataWriter::new_with_selector(Function::Initialize)
.write(H256::from(EvmAddress::default()))
.build();
let (res, _) = Executor::<Runtime>::call(
CallContext {
contract: implementation,
sender: deployer(),
origin: deployer(),
},
implementation_init,
U256::zero(),
100_000,
);
assert_eq!(res, Succeed(Stopped), "Failed to initialize implementation");

// Act
let payload = EvmDataWriter::new_with_selector(Function::Initialize)
.write(H256::from(controller))
.build();
let proxy_init = EvmDataWriter::new_with_selector(Function::InitializePayload)
.write(H256::from(implementation))
.write(H256::from(deployer()))
.write(Bytes(payload))
.build();
let (res, _) = Executor::<Runtime>::call(
CallContext {
contract: proxy,
sender: deployer(),
origin: deployer(),
},
proxy_init,
U256::zero(),
1_000_000,
);

// Assert
assert_eq!(res, Succeed(Stopped), "Failed to initialize proxy");
});
}
23 changes: 23 additions & 0 deletions integration-tests/src/cross_chain_transfer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![cfg(test)]
use crate::erc20::bind_erc20;
use crate::erc20::deploy_token_contract;
use crate::polkadot_test_net::Rococo;
use crate::polkadot_test_net::*;
use xcm_emulator::ConvertLocation;
Expand Down Expand Up @@ -113,6 +115,27 @@ fn rococo_should_receive_asset_when_sent_from_hydra() {
});
}

#[test]
fn xtoken_transfer_of_erc20_should_fail() {
//Arrange
Hydra::execute_with(|| {
// Arrange
let asset = bind_erc20(deploy_token_contract());

//Act & Assert
assert_noop!(
hydradx_runtime::XTokens::transfer(
hydradx_runtime::RuntimeOrigin::signed(ALICE.into()),
asset,
3 * UNITS,
Box::new(MultiLocation::new(1, X1(Junction::AccountId32 { id: BOB, network: None })).into_versioned()),
WeightLimit::Unlimited,
),
orml_xtokens::Error::<hydradx_runtime::Runtime>::AssetHasNoReserve
);
});
}

#[test]
fn hydra_should_receive_asset_when_transferred_from_acala() {
// Arrange
Expand Down
Loading

0 comments on commit c2d54b7

Please sign in to comment.