Skip to content

Commit

Permalink
P 1275 omni bridge precompile (#3254)
Browse files Browse the repository at this point in the history
* feat: initial impl without logic

* feat: impl

* chore: small fix

* chore: lock file

* chore: update member

* chore: fix

* feat: ts-test seems tricky

* chore: lock file

* feat: add ts-test

* chore: clean up

* chore: small fix ts

* chore: fix

* chore: fix abi file

* chore: small fix

* chore: more hint

* chore: fix

* chore: fix

* bump runtime versions

* chore: fix

* chore: fix

* chore: fix

* chore: fix

---------

Co-authored-by: Kai <[email protected]>
Co-authored-by: Kailai-Wang <[email protected]>
  • Loading branch information
3 people authored Feb 14, 2025
1 parent f86d72d commit 4a5e812
Show file tree
Hide file tree
Showing 17 changed files with 299 additions and 33 deletions.
26 changes: 26 additions & 0 deletions parachain/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ members = [
'precompiles/collab-ai/guardian',
'precompiles/collab-ai/pool-proposal',
'precompiles/collab-ai/investing-pool',
'precompiles/omni-bridge',
'precompiles/parachain-staking',
'precompiles/score-staking',
'runtime/litentry',
Expand Down Expand Up @@ -293,6 +294,7 @@ pallet-teebag = { path = "pallets/teebag", default-features = false }
pallet-vc-management = { path = "pallets/vc-management", default-features = false }
pallet-evm-precompile-assets-erc20 = { path = "precompiles/assets-erc20", default-features = false }
pallet-evm-precompile-bridge-transfer = { path = "precompiles/bridge-transfer", default-features = false }
pallet-evm-precompile-omni-bridge = { path = "precompiles/omni-bridge", default-features = false }
pallet-evm-precompile-parachain-staking = { path = "precompiles/parachain-staking", default-features = false }
pallet-evm-precompile-score-staking = { path = "precompiles/score-staking", default-features = false }

Expand Down
15 changes: 15 additions & 0 deletions parachain/precompiles/bridge-transfer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
// Copyright 2020-2024 Trust Computing GmbH.
// This file is part of Litentry.
//
// Litentry 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.
//
// Litentry 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 Litentry. If not, see <https://www.gnu.org/licenses/>.
#![cfg_attr(not(feature = "std"), no_std)]

use fp_evm::{PrecompileFailure, PrecompileHandle};
Expand Down
54 changes: 54 additions & 0 deletions parachain/precompiles/omni-bridge/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[package]
authors = ["Trust Computing GmbH <[email protected]>"]
edition = '2021'
name = "pallet-evm-precompile-omni-bridge"
version = '0.1.0'

[dependencies]
precompile-utils = { workspace = true }

frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-omni-bridge = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true, features = ["derive"] }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

fp-evm = { workspace = true }
pallet-evm = { workspace = true }

core-primitives = { workspace = true }

[dev-dependencies]
derive_more = { workspace = true }
hex-literal = { workspace = true }
libsecp256k1 = { workspace = true, features = ["std"] }
serde = { workspace = true }
sha3 = { workspace = true }
precompile-utils = { workspace = true, features = ["std", "testing"] }
pallet-timestamp = { workspace = true, features = ["std"] }
parity-scale-codec = { workspace = true, features = ["std"] }
sp-runtime = { workspace = true, features = ["std"] }

[features]
default = ["std"]
std = [
"core-primitives/std",
"fp-evm/std",
"frame-support/std",
"frame-system/std",
"libsecp256k1/std",
"pallet-omni-bridge/std",
"pallet-evm/std",
"pallet-timestamp/std",
"parity-scale-codec/std",
"precompile-utils/std",
"scale-info/std",
"serde/std",
"sha3/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
]
14 changes: 14 additions & 0 deletions parachain/precompiles/omni-bridge/OmniBridgeInterface.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.8.3;

interface IOmniBridge {
/// @notice Used to transfer assets through token bridge.
/// @param amount: The amount of tokens to be transferred.
/// @param dest_id: The destination chain id indicator
/// @param native: Indicator of if asset is native. If true, asset_id will be ignored
/// @param asset_id: Resource indicator of type of assets transferred (In substrate runtime it is u128)
/// @param recipient: Recipient address, typically H160/H256
/// @custom:selector 0xef185624
/// payIn(uint256,uint8,bool,uint256,bytes)
function payIn(uint256 amount, uint8 dest_id, bool native, uint256 asset_id, bytes calldata recipient) external;
}
91 changes: 91 additions & 0 deletions parachain/precompiles/omni-bridge/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2020-2024 Trust Computing GmbH.
// This file is part of Litentry.
//
// Litentry 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.
//
// Litentry 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 Litentry. If not, see <https://www.gnu.org/licenses/>.
#![cfg_attr(not(feature = "std"), no_std)]

use fp_evm::{PrecompileFailure, PrecompileHandle};

use core_primitives::AssetId;
use frame_support::{
dispatch::{GetDispatchInfo, PostDispatchInfo},
traits::fungible::NativeOrWithId,
};
use pallet_evm::AddressMapping;
use precompile_utils::prelude::*;
use sp_runtime::traits::Dispatchable;

use sp_core::U256;
use sp_std::{marker::PhantomData, vec::Vec};

use pallet_omni_bridge::{ChainType, PayInRequest};

pub struct OmniBridgePrecompile<Runtime>(PhantomData<Runtime>);

type BridgeBalanceOf<Runtime> = <Runtime as pallet_omni_bridge::Config>::Balance;

#[precompile_utils::precompile]
impl<Runtime> OmniBridgePrecompile<Runtime>
where
Runtime: pallet_omni_bridge::Config<AssetKind = NativeOrWithId<AssetId>> + pallet_evm::Config,
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
Runtime::RuntimeCall: From<pallet_omni_bridge::Call<Runtime>>,
<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
BridgeBalanceOf<Runtime>: TryFrom<U256> + Into<U256>,
{
#[precompile::public("payIn(uint256,uint8,bool,uint256,bytes)")]
fn pay_in(
handle: &mut impl PrecompileHandle,
amount: U256,
dest_id: u8,
native: bool,
asset_id: U256,
recipient: UnboundedBytes,
) -> EvmResult {
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);

let amount: BridgeBalanceOf<Runtime> = amount.try_into().map_err(|_| {
Into::<PrecompileFailure>::into(RevertReason::value_is_too_large("balance type"))
})?;
let recipient: Vec<u8> = recipient.into();
let asset_id: AssetId = asset_id.try_into().map_err(|_| {
Into::<PrecompileFailure>::into(RevertReason::value_is_too_large("asset id type"))
})?;

let pay_in_request: PayInRequest<NativeOrWithId<AssetId>, BridgeBalanceOf<Runtime>> =
match native {
true => PayInRequest {
asset: NativeOrWithId::Native,
// This is substrate parachain precompile
// So always be non native chain
dest_chain: ChainType::Ethereum(dest_id.into()),
dest_account: recipient,
amount,
},
false => PayInRequest {
asset: NativeOrWithId::WithId(asset_id),
// This is substrate parachain precompile
// So always be non native chain
dest_chain: ChainType::Ethereum(dest_id.into()),
dest_account: recipient,
amount,
},
};

let call = pallet_omni_bridge::Call::<Runtime>::pay_in { req: pay_in_request };
RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;

Ok(())
}
}
2 changes: 2 additions & 0 deletions parachain/runtime/litentry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pallet-evm-precompile-sha3fips = { workspace = true }
pallet-evm-precompile-simple = { workspace = true }

pallet-evm-precompile-bridge-transfer = { workspace = true }
pallet-evm-precompile-omni-bridge = { workspace = true }
pallet-evm-precompile-parachain-staking = { workspace = true }
pallet-evm-precompile-score-staking = { workspace = true }

Expand Down Expand Up @@ -240,6 +241,7 @@ std = [
"pallet-evm-precompile-blake2/std",
"pallet-evm-precompile-bn128/std",
"pallet-evm-precompile-bridge-transfer/std",
"pallet-evm-precompile-omni-bridge/std",
"pallet-evm-precompile-dispatch/std",
"pallet-evm-precompile-ed25519/std",
"pallet-evm-precompile-modexp/std",
Expand Down
2 changes: 1 addition & 1 deletion parachain/runtime/litentry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_name: create_runtime_str!("heima"),
authoring_version: 1,
// same versioning-mechanism as polkadot: use last digit for minor updates
spec_version: 9230,
spec_version: 9231,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
9 changes: 8 additions & 1 deletion parachain/runtime/litentry/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use pallet_evm_precompile_bridge_transfer::BridgeTransferPrecompile;
use pallet_evm_precompile_dispatch::{Dispatch, DispatchValidateT};
use pallet_evm_precompile_ed25519::Ed25519Verify;
use pallet_evm_precompile_modexp::Modexp;
use pallet_evm_precompile_omni_bridge::OmniBridgePrecompile;
use pallet_evm_precompile_parachain_staking::ParachainStakingPrecompile;
use pallet_evm_precompile_score_staking::ScoreStakingPrecompile;
use pallet_evm_precompile_sha3fips::Sha3FIPS256;
Expand Down Expand Up @@ -135,6 +136,12 @@ pub type PrecompilesSetAt<R> = (
ScoreStakingPrecompile<R>,
(CallableByContract, CallableByPrecompile),
>,
// OmniBridge: pallet_omni_bridge = 85 + 20480
PrecompileAt<
AddressU64<20565>,
OmniBridgePrecompile<R>,
(CallableByContract, CallableByPrecompile),
>,
);

pub type LitentryNetworkPrecompiles<R> = PrecompileSetBuilder<
Expand All @@ -143,7 +150,7 @@ pub type LitentryNetworkPrecompiles<R> = PrecompileSetBuilder<
// Skip precompiles if out of range.
PrecompilesInRangeInclusive<
// We take range as last precompile index, UPDATE this once new prcompile is added
(AddressU64<1>, AddressU64<20556>),
(AddressU64<1>, AddressU64<20565>),
PrecompilesSetAt<R>,
>,
// Prefixed precompile sets (XC20)
Expand Down
2 changes: 2 additions & 0 deletions parachain/runtime/paseo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pallet-evm-precompile-sha3fips = { workspace = true }
pallet-evm-precompile-simple = { workspace = true }

pallet-evm-precompile-bridge-transfer = { workspace = true }
pallet-evm-precompile-omni-bridge = { workspace = true }
pallet-evm-precompile-parachain-staking = { workspace = true }
pallet-evm-precompile-score-staking = { workspace = true }

Expand Down Expand Up @@ -271,6 +272,7 @@ std = [
"pallet-evm-precompile-blake2/std",
"pallet-evm-precompile-bn128/std",
"pallet-evm-precompile-bridge-transfer/std",
"pallet-evm-precompile-omni-bridge/std",
"pallet-evm-precompile-dispatch/std",
"pallet-evm-precompile-ed25519/std",
"pallet-evm-precompile-modexp/std",
Expand Down
2 changes: 1 addition & 1 deletion parachain/runtime/paseo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_name: create_runtime_str!("heima"),
authoring_version: 1,
// same versioning-mechanism as polkadot: use last digit for minor updates
spec_version: 9230,
spec_version: 9231,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
9 changes: 8 additions & 1 deletion parachain/runtime/paseo/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use pallet_evm_precompile_bridge_transfer::BridgeTransferPrecompile;
use pallet_evm_precompile_dispatch::{Dispatch, DispatchValidateT};
use pallet_evm_precompile_ed25519::Ed25519Verify;
use pallet_evm_precompile_modexp::Modexp;
use pallet_evm_precompile_omni_bridge::OmniBridgePrecompile;
use pallet_evm_precompile_parachain_staking::ParachainStakingPrecompile;
use pallet_evm_precompile_score_staking::ScoreStakingPrecompile;
use pallet_evm_precompile_sha3fips::Sha3FIPS256;
Expand Down Expand Up @@ -142,6 +143,12 @@ pub type PrecompilesSetAt<R> = (
ScoreStakingPrecompile<R>,
(CallableByContract, CallableByPrecompile),
>,
// OmniBridge: pallet_omni_bridge = 85 + 20480
PrecompileAt<
AddressU64<20565>,
OmniBridgePrecompile<R>,
(CallableByContract, CallableByPrecompile),
>,
// Curator: pallet_curator = 150 + 20480
PrecompileAt<
AddressU64<20630>,
Expand Down Expand Up @@ -180,7 +187,7 @@ pub type RococoNetworkPrecompiles<R> = PrecompileSetBuilder<
// Skip precompiles if out of range.
PrecompilesInRangeInclusive<
// We take range as last precompile index, UPDATE this once new prcompile is added
(AddressU64<1>, AddressU64<20635>),
(AddressU64<1>, AddressU64<20634>),
PrecompilesSetAt<R>,
>,
// Prefixed precompile sets (XC20)
Expand Down
5 changes: 3 additions & 2 deletions parachain/scripts/run-ts-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ LITENTRY_PARACHAIN_DIR=${LITENTRY_PARACHAIN_DIR:-"/tmp/parachain_dev"}
pnpm install
pnpm run test-filter 2>&1 | tee -a "$LITENTRY_PARACHAIN_DIR/parachain_ci_test.log"

$ROOTDIR/parachain/scripts/launch-bridge.sh
pnpm run test-bridge 2>&1 | tee -a "$LITENTRY_PARACHAIN_DIR/parachain_ci_test.log"
# comment out for now - we use omni-bridge now
# $ROOTDIR/parachain/scripts/launch-bridge.sh
# pnpm run test-bridge 2>&1 | tee -a "$LITENTRY_PARACHAIN_DIR/parachain_ci_test.log"

pnpm run test-evm-contract 2>&1 | tee -a "$LITENTRY_PARACHAIN_DIR/parachain_ci_test.log"
pnpm run test-precompile-contract 2>&1 | tee -a "$LITENTRY_PARACHAIN_DIR/parachain_ci_test.log"
Loading

0 comments on commit 4a5e812

Please sign in to comment.