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

Add dummy precompile hook with rust and typescript tests #478

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 46 additions & 2 deletions container-chains/templates/frontier/runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>

use pallet_foreign_asset_creator::{
AssetBalance, AssetId as AssetIdOf, ForeignAssetCreatedHook, ForeignAssetDestroyedHook,
};
use {
super::{
precompiles::FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX, AccountId, AllPalletsWithSystem,
Expand Down Expand Up @@ -376,6 +379,33 @@ impl pallet_assets::Config<ForeignAssetsInstance> for Runtime {
type BenchmarkHelper = ForeignAssetBenchmarkHelper;
}

pub struct RevertCodePrecompileHook;

impl ForeignAssetCreatedHook<MultiLocation, AssetIdOf<Runtime>, AssetBalance<Runtime>>
for RevertCodePrecompileHook
{
fn on_asset_created(
_foreign_asset: &MultiLocation,
asset_id: &AssetIdOf<Runtime>,
_min_balance: &AssetBalance<Runtime>,
) {
let revert_bytecode = [0x60, 0x00, 0x60, 0x00, 0xFD].to_vec();
let prefix_slice = [255u8; 18];
let account_id = Runtime::asset_id_to_account(prefix_slice.as_slice(), *asset_id);

pallet_evm::Pallet::<Runtime>::create_account(account_id.into(), revert_bytecode.clone());
}
}

impl ForeignAssetDestroyedHook<MultiLocation, AssetIdOf<Runtime>> for RevertCodePrecompileHook {
fn on_asset_destroyed(_foreign_asset: &MultiLocation, asset_id: &AssetIdOf<Runtime>) {
let prefix_slice = [255u8; 18];
let account_id = Runtime::asset_id_to_account(prefix_slice.as_slice(), *asset_id);

pallet_evm::Pallet::<Runtime>::remove_account(&account_id.into());
}
}

impl pallet_foreign_asset_creator::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type ForeignAsset = MultiLocation;
Expand All @@ -384,8 +414,8 @@ impl pallet_foreign_asset_creator::Config for Runtime {
type ForeignAssetDestroyerOrigin = EnsureRoot<AccountId>;
type Fungibles = ForeignAssets;
type WeightInfo = pallet_foreign_asset_creator::weights::SubstrateWeight<Runtime>;
type OnForeignAssetCreated = ();
type OnForeignAssetDestroyed = ();
type OnForeignAssetCreated = RevertCodePrecompileHook;
type OnForeignAssetDestroyed = RevertCodePrecompileHook;
}

impl pallet_asset_rate::Config for Runtime {
Expand Down Expand Up @@ -439,3 +469,17 @@ pub type ForeignFungiblesTransactor = FungiblesAdapter<
/// Multiplier used for dedicated `TakeFirstAssetTrader` with `ForeignAssets` instance.
pub type AssetRateAsMultiplier =
AssetFeeAsExistentialDepositMultiplier<Runtime, WeightToFee, AssetRate, ForeignAssetsInstance>;

#[test]
fn test_asset_id_to_account_conversion() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need this test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently asset_id_to_account takes prefix as slice, instead of hard bound array. Then it does invoke copy_from_slice, which can panic, if the slice's size is not exact.

If the implementation changes tomorrow (i.e the expected size of the slice in the implementation), it need to be captured by the unit test (otherwise you would see panic during integration test or worse in dev environment). Which this test prevents.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's discuss this offline. I am merging this as it is not blocker as of now.

let prefix_slice = [255u8].repeat(18);
let asset_ids_to_check = vec![0u16, 123u16, 3453u16, 10000u16, 65535u16];
for current_asset_id in asset_ids_to_check {
let account_id = Runtime::asset_id_to_account(prefix_slice.as_slice(), current_asset_id);
assert_eq!(
account_id.to_string().to_lowercase(),
String::from("0xffffffffffffffffffffffffffffffffffff")
+ format!("{:04x}", current_asset_id).as_str()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { expect, describeSuite } from "@moonwall/cli";
import { STATEMINT_LOCATION_EXAMPLE } from "../../../util/constants.ts";
import { alith } from "@moonwall/util";

describeSuite({
id: "DF0901",
title: "Ethereum asset dummy precompile address creation",
foundationMethods: "dev",
testCases: ({ context, it }) => {
it({
id: "T01",
title: "dummy precompile address is created when creating the asset and removed when destroyed",
test: async function () {
const assetId = 5;
const assetIdAddress = new Uint8Array([
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 5,
]);
const revertBytecode = "0x60006000fd";
const addressInHex = "0x" + Buffer.from(assetIdAddress).toString("hex");

await context.createBlock(
context
.polkadotJs()
.tx.sudo.sudo(
context
.polkadotJs()
.tx.foreignAssetsCreator.createForeignAsset(
STATEMINT_LOCATION_EXAMPLE,
assetId,
alith.address,
true,
1
)
)
);

// After the foreign asset creation, the address should contain revert byte code.
expect(await context.web3().eth.getCode(addressInHex)).to.equal(revertBytecode);

await context.createBlock(
context
.polkadotJs()
.tx.sudo.sudo(context.polkadotJs().tx.foreignAssetsCreator.destroyForeignAsset(assetId))
);

// After the foreign asset destruction, the revert bytecode from that address should be removed.
expect(await context.web3().eth.getCode(addressInHex)).to.equal("0x");
},
});
},
});
Loading