Skip to content

Commit

Permalink
fix(*): fix hybrid-vm and port for different AccountId
Browse files Browse the repository at this point in the history
  • Loading branch information
wd30130 committed Jul 25, 2024
1 parent 88dc83d commit a0b40eb
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
1 change: 1 addition & 0 deletions frame/hybrid-vm-port/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pallet-evm = { workspace = true }
ink_env = { workspace = true }
#local
pallet-hybrid-vm = { path = "../hybrid-vm" }
hp-system = { path = "../../primitives/system" }

[dev-dependencies]
hex = { workspace = true }
Expand Down
6 changes: 4 additions & 2 deletions frame/hybrid-vm-port/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use sp_runtime::{
transaction_validity::{
InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransactionBuilder,
},
AccountId32, DispatchError, RuntimeDebug, SaturatedConversion,
DispatchError, RuntimeDebug, SaturatedConversion,
};
// Frontier
use fp_consensus::{PostLog, PreLog, FRONTIER_ENGINE_ID};
Expand All @@ -75,6 +75,7 @@ use fp_evm::{
};
pub use fp_rpc::TransactionStatus;
use fp_storage::{EthereumStorageSchema, PALLET_ETHEREUM_SCHEMA};
use hp_system::U256BalanceMapping;
use ink_env::call::{ExecutionInput, Selector};
use pallet_contracts::chain_extension::SysConfig;
use pallet_contracts::{CollectEvents, DebugInfo, Determinism};
Expand Down Expand Up @@ -680,7 +681,8 @@ impl<T: Config> Pallet<T> {
);

let origin = <T as pallet_evm::Config>::AddressMapping::into_account_id(source);
let balance_result = <T as pallet_hybrid_vm::Config>::U256BalanceMapping::u256_to_balance(value);
let balance_result =
<T as pallet_hybrid_vm::Config>::U256BalanceMapping::u256_to_balance(value);
let balance = match balance_result {
Some(t) => t,
None => {
Expand Down
3 changes: 2 additions & 1 deletion frame/hybrid-vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pallet-contracts = { workspace = true, default-features = false}
pallet-evm = { workspace = true, default-features = false}
fp-evm = { workspace = true, default-features = false}

# local
hp-system = { version = "0.1.0", path = "../../primitives/system" }

[dev-dependencies]
assert_matches = { workspace = true }
Expand All @@ -54,7 +56,6 @@ pallet-evm-precompile-simple = { workspace = true, default-features = false}
ink_env = { workspace = true }

pallet-evm-precompile-call-hybrid-vm = { version = "0.1.0", path = "../evm-precompile/call-hybrid-vm" }
hp-system = { version = "0.1.0", path = "../../primitives/system" }

[features]
default = ["std"]
Expand Down
3 changes: 1 addition & 2 deletions frame/hybrid-vm/src/interoperate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use sp_runtime::DispatchError;

use serde::{Deserialize, Serialize};

use byte_slice_cast::AsByteSlice;
use fp_evm::ExecutionInfoV2;
use frame_support::sp_runtime::AccountId32;
use pallet_evm::Runner;
Expand Down Expand Up @@ -141,7 +140,7 @@ impl<C: Config> InterCall<C> {
}

let caller = env.ext().caller();
let source = T::AccountIdMapping::into_address(caller.account_id()?);
let source = C::AccountIdMapping::into_address(caller.account_id()?.clone());

let mut envbuf = env.buf_in_buf_out();
let input0: Vec<u8> = envbuf.read_as_unbounded(envbuf.in_len())?;
Expand Down
27 changes: 14 additions & 13 deletions frame/hybrid-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ mod tests;
mod interoperate;

use self::interoperate::InterCall;
use byte_slice_cast::*;
use ethereum::TransactionV2 as Transaction;
use frame_support::traits::{tokens::fungible::Inspect, Currency, Get};
use frame_support::RuntimeDebugNoBound;
use pallet_contracts::chain_extension::{Environment, Ext, InitState, RetVal};
use sp_core::{H160, U256};
use sp_runtime::{AccountId32, DispatchError};
use sp_std::vec::Vec;
use hp_system::{AccountIdMapping, AccountId32Mapping, U256BalanceMapping};
//use sp_std::fmt::Debug;
use hp_system::{AccountId32Mapping, AccountIdMapping, U256BalanceMapping};

pub use self::pallet::*;

Expand All @@ -42,7 +43,8 @@ pub mod pallet {

type Result<T> = sp_std::result::Result<T, DispatchError>;

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, Debug, PartialEq)]
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, RuntimeDebugNoBound, PartialEq)]
#[scale_info(skip_type_params(T))]
pub enum UnifiedAddress<T: Config> {
WasmVM(T::AccountId),
}
Expand All @@ -53,11 +55,11 @@ pub mod pallet {

// Currency type for balance storage.
type Currency: Currency<Self::AccountId> + Inspect<Self::AccountId>;
type U256BalanceMapping: U256BalanceMapping<Self>;

type U256BalanceMapping: U256BalanceMapping<Balance = <<Self as pallet_contracts::Config>::Currency as Inspect<Self::AccountId>>::Balance>;

type AccountIdMapping: AccountIdMapping<Self>;

type AccountId32Mapping: AccountId32Mapping<Self>;

#[pallet::constant]
Expand All @@ -80,15 +82,15 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn hvm_contracts)]
pub type HvmContracts<T: Config> =
StorageMap<_, Twox64Concat, H160, UnifiedAddress, OptionQuery>;
StorageMap<_, Twox64Concat, H160, UnifiedAddress<T>, OptionQuery>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
EVMExecuted(H160),
WasmVMExecuted(T::AccountId),
HybridVMCalled(T::AccountId),
RegistContract(H160, UnifiedAddress, T::AccountId),
RegistContract(H160, UnifiedAddress<T>, T::AccountId),
}

#[pallet::error]
Expand Down Expand Up @@ -123,14 +125,13 @@ pub mod pallet {
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
pub fn regist_contract(
origin: OriginFor<T>,
unified_address: UnifiedAddress,
unified_address: UnifiedAddress<T>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;

match unified_address.clone() {
UnifiedAddress::<T>::WasmVM(account) => {
let value =
pallet_contracts::Pallet::<T>::get_storage(account.clone(), vec![]);
let value = pallet_contracts::Pallet::<T>::get_storage(account.clone(), vec![]);
match value {
Err(t) => {
if t == pallet_contracts::ContractAccessError::DoesntExist {
Expand All @@ -140,7 +141,7 @@ pub mod pallet {
_ => {},
}
let address = T::AccountIdMapping::into_address(account);

HvmContracts::<T>::insert(address, unified_address.clone());

Self::deposit_event(Event::RegistContract(address, unified_address, who));
Expand Down
8 changes: 4 additions & 4 deletions primitives/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

#![cfg_attr(not(feature = "std"), no_std)]

use frame_system::pallet_prelude::*;
use frame_support::traits::tokens::Balance;
use sp_std::vec::Vec;
use frame_system::pallet_prelude::*;
use sp_core::{H160, U256};
use sp_runtime::AccountId32;
use sp_std::vec::Vec;

pub trait EvmHybridVMExtension<C: frame_system::Config> {
fn call_hybrid_vm(
Expand All @@ -28,7 +28,7 @@ pub trait EvmHybridVMExtension<C: frame_system::Config> {
target_gas: Option<u64>,
) -> Result<(Vec<u8>, u64), sp_runtime::DispatchError>;
}

pub trait U256BalanceMapping {
type Balance: Balance;
fn u256_to_balance(value: U256) -> Option<Self::Balance>;
Expand All @@ -41,4 +41,4 @@ pub trait AccountIdMapping<C: frame_system::Config> {
pub trait AccountId32Mapping<C: frame_system::Config> {
fn id32_to_id(id32: AccountId32) -> C::AccountId;
fn id_to_id32(account_id: C::AccountId) -> AccountId32;
}
}

0 comments on commit a0b40eb

Please sign in to comment.