-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatic EVM revert code registration for XC20 (#1001)
* Automatic EVM revert code registration for XC20 * Add negative test * Fmt fix * Add EVM module * Asset benchmarks * Cleanup xc asset config * Update asset benchmarks * Fix tests features
- Loading branch information
Showing
34 changed files
with
2,323 additions
and
290 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,4 @@ std = [ | |
"sp-runtime/std", | ||
"sp-std/std", | ||
] | ||
runtime-benchmarks = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// This file is part of Astar. | ||
|
||
// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// Astar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Astar is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Astar. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use crate::AssetId; | ||
|
||
#[cfg(feature = "runtime-benchmarks")] | ||
/// Benchmark helper for `pallet-assets`. | ||
pub struct AssetsBenchmarkHelper; | ||
impl<AssetIdParameter: From<u128>> pallet_assets::BenchmarkHelper<AssetIdParameter> | ||
for AssetsBenchmarkHelper | ||
{ | ||
fn create_asset_id_parameter(id: u32) -> AssetIdParameter { | ||
AssetId::from(id).into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// This file is part of Astar. | ||
|
||
// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// Astar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Astar is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Astar. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use crate::{AccountId, AssetId}; | ||
|
||
use frame_support::ensure; | ||
use sp_std::marker::PhantomData; | ||
|
||
use pallet_assets::AssetsCallback; | ||
use pallet_evm_precompile_assets_erc20::AddressToAssetId; | ||
|
||
/// Revert opt code. It's inserted at the precompile addresses, to make them functional in EVM. | ||
pub const EVM_REVERT_CODE: &[u8] = &[0x60, 0x00, 0x60, 0x00, 0xfd]; | ||
|
||
/// Handler for automatic revert code registration. | ||
/// | ||
/// When an asset is created, it automatically becomes available to the EVM via an `ERC20-like` interface. | ||
/// In order for the precompile to work, dedicated asset address needs to have the revert code registered, otherwise the call will fail. | ||
/// | ||
/// It is important to note that if the dedicated asset EVM address is already taken, asset creation should fail. | ||
/// After asset has been destroyed, it is also safe to remove the revert code and free the address for future usage. | ||
pub struct EvmRevertCodeHandler<A, R>(PhantomData<(A, R)>); | ||
impl<A, R> AssetsCallback<AssetId, AccountId> for EvmRevertCodeHandler<A, R> | ||
where | ||
A: AddressToAssetId<AssetId>, | ||
R: pallet_evm::Config, | ||
{ | ||
fn created(id: &AssetId, _: &AccountId) -> Result<(), ()> { | ||
let address = A::asset_id_to_address(*id); | ||
// In case of collision, we need to cancel the asset creation. | ||
ensure!(!pallet_evm::AccountCodes::<R>::contains_key(&address), ()); | ||
pallet_evm::AccountCodes::<R>::insert(address, EVM_REVERT_CODE.to_vec()); | ||
Ok(()) | ||
} | ||
|
||
fn destroyed(id: &AssetId) -> Result<(), ()> { | ||
let address = A::asset_id_to_address(*id); | ||
pallet_evm::AccountCodes::<R>::remove(address); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.