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

Update types for EVM bridge #79

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions pallets/data-signer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

use super::Call;
use crate::{mock::*, Error};
use bridge_types::{SubNetworkId, H256, U256};
use bridge_types::{SubNetworkId, H256};
use frame_support::{assert_noop, assert_ok};
use sp_core::{
bounded::BoundedVec,
Expand Down Expand Up @@ -300,7 +300,7 @@ fn it_fails_add_peer_already_exists() {
#[test]
fn it_fails_add_peer_evm_network_not_supported() {
new_test_ext().execute_with(|| {
let network_id = bridge_types::GenericNetworkId::EVM(U256::from(1));
let network_id = bridge_types::GenericNetworkId::EVM(H256::from_low_u64_be(1));
let (peers, _) = test_peers();
let peers: BoundedVec<ecdsa::Public, BridgeMaxPeers> = peers.try_into().unwrap();

Expand Down Expand Up @@ -376,7 +376,7 @@ fn it_fails_remove_peer_pending_update() {
#[test]
fn it_fails_remove_peer_evm_network_not_supported() {
new_test_ext().execute_with(|| {
let network_id = bridge_types::GenericNetworkId::EVM(U256::from(1));
let network_id = bridge_types::GenericNetworkId::EVM(H256::from_low_u64_be(1));
let (peers, _) = test_peers();
let peers: BoundedVec<ecdsa::Public, BridgeMaxPeers> = peers.try_into().unwrap();

Expand Down
8 changes: 4 additions & 4 deletions pallets/dispatch/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ benchmarks_instance_pallet! {
crate::Event::<T, I>: Into<<T as frame_system::Config>::RuntimeEvent>
}
dispatch_success {
let message_id = MessageId::basic(GenericNetworkId::EVM(1.into()), GenericNetworkId::Sub(SubNetworkId::Mainnet), 1);
let message_id = MessageId::basic(GenericNetworkId::EVM([1u8; 32].into()), GenericNetworkId::Sub(SubNetworkId::Mainnet), 1);
}: {
crate::Pallet::<T, I>::dispatch(
1.into(),
[1u8; 32].into(),
message_id,
Default::default(),
// system.remark()
Expand All @@ -84,10 +84,10 @@ benchmarks_instance_pallet! {
}

dispatch_decode_failed {
let message_id = MessageId::basic(GenericNetworkId::EVM(1.into()), GenericNetworkId::Sub(SubNetworkId::Mainnet), 1);
let message_id = MessageId::basic(GenericNetworkId::EVM([1u8; 32].into()), GenericNetworkId::Sub(SubNetworkId::Mainnet), 1);
}: {
crate::Pallet::<T, I>::dispatch(
1.into(),
[1u8; 32].into(),
message_id,
Default::default(),
&[],
Expand Down
6 changes: 3 additions & 3 deletions pallets/dispatch/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn test_dispatch_bridge_message() {

System::set_block_number(1);
Dispatch::dispatch(
2u32.into(),
H256::from_low_u64_be(2),
id,
Default::default(),
&message,
Expand Down Expand Up @@ -60,7 +60,7 @@ fn test_message_decode_failed() {

System::set_block_number(1);
Dispatch::dispatch(
2u32.into(),
H256::from_low_u64_be(2),
id,
Default::default(),
&message,
Expand Down Expand Up @@ -95,7 +95,7 @@ fn test_message_rejected() {

System::set_block_number(1);
Dispatch::dispatch(
2u32.into(),
H256::from_low_u64_be(2),
id,
Default::default(),
&message,
Expand Down
23 changes: 23 additions & 0 deletions pallets/liberland-bridge-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ pub mod pallet {
NoTechAccFound,
WrongAccount,
WrongSidechainAsset,
WrongAssetKind,
}

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -299,6 +300,7 @@ impl<T: Config> bridge_types::traits::BridgeAssetLocker<T::AccountId> for Pallet
)?;
},
bridge_types::types::AssetKind::Sidechain => fail!(Error::<T>::WrongSidechainAsset),
bridge_types::types::AssetKind::Native => fail!(Error::<T>::WrongAssetKind),
}
},
LiberlandAssetId::Asset(asset) => {
Expand All @@ -319,6 +321,7 @@ impl<T: Config> bridge_types::traits::BridgeAssetLocker<T::AccountId> for Pallet
*amount,
)?;
},
bridge_types::types::AssetKind::Native => fail!(Error::<T>::WrongAssetKind),
}
}
}
Expand All @@ -345,6 +348,7 @@ impl<T: Config> bridge_types::traits::BridgeAssetLocker<T::AccountId> for Pallet
)?;
},
bridge_types::types::AssetKind::Sidechain => fail!(Error::<T>::WrongSidechainAsset),
bridge_types::types::AssetKind::Native => fail!(Error::<T>::WrongAssetKind),
}
},
LiberlandAssetId::Asset(asset) => {
Expand All @@ -365,11 +369,30 @@ impl<T: Config> bridge_types::traits::BridgeAssetLocker<T::AccountId> for Pallet
*amount,
)?;
}
bridge_types::types::AssetKind::Native => fail!(Error::<T>::WrongAssetKind),
}
}
}
Ok(())
}

fn refund_fee(
_network_id: GenericNetworkId,
_who: &T::AccountId,
_asset_id: &Self::AssetId,
_amount: &Self::Balance,
) -> DispatchResult {
Err(DispatchError::Unavailable)
}

fn withdraw_fee(
_network_id: GenericNetworkId,
_who: &T::AccountId,
_asset_id: &Self::AssetId,
_amount: &Self::Balance,
) -> DispatchResult {
Err(DispatchError::Unavailable)
}
}

impl<T: Config>
Expand Down
31 changes: 31 additions & 0 deletions pallets/multisig-verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,34 @@ impl<T: Config> bridge_types::traits::Verifier for Pallet<T> {
None
}
}

pub struct MultiEVMVerifier<T>(PhantomData<T>);

#[derive(Clone, RuntimeDebug, Encode, Decode, PartialEq, Eq, scale_info::TypeInfo)]
pub struct MultiEVMProof {
pub proof: Vec<ecdsa::Signature>,
}

impl<T: Config> bridge_types::traits::Verifier for MultiEVMVerifier<T> {
type Proof = MultiEVMProof;

fn verify(
network_id: GenericNetworkId,
commitment_hash: H256,
proof: &Self::Proof,
) -> DispatchResult {
let this_network_id = T::ThisNetworkId::get();
let approved_hash = Keccak256::hash_of(&(network_id, this_network_id, commitment_hash));
Pallet::<T>::verify_signatures(network_id, approved_hash, &proof.proof)?;
Ok(())
}

fn verify_weight(proof: &Self::Proof) -> Weight {
<T as Config>::WeightInfo::verifier_verify(proof.proof.len() as u32)
}

#[cfg(feature = "runtime-benchmarks")]
fn valid_proof() -> Option<Self::Proof> {
None
}
}
32 changes: 16 additions & 16 deletions pallets/types/src/channel_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,28 @@ use ethabi::{Function, Param, ParamType, StateMutability, Token};
use frame_support::RuntimeDebug;
use sp_std::prelude::*;

fn authorize_operator_function() -> Function {
fn register_app_function() -> Function {
Function {
name: "authorizeDefaultOperator".into(),
name: "registerApp".into(),
constant: None,
state_mutability: StateMutability::NonPayable,
outputs: vec![],
inputs: vec![Param {
name: "operator".into(),
name: "newApp".into(),
kind: ParamType::Address,
internal_type: None,
}],
}
}

fn revoke_operator_function() -> Function {
fn remove_app_function() -> Function {
Function {
name: "revokeDefaultOperator".into(),
name: "removeApp".into(),
constant: None,
state_mutability: StateMutability::NonPayable,
outputs: vec![],
inputs: vec![Param {
name: "operator".into(),
name: "app".into(),
kind: ParamType::Address,
internal_type: None,
}],
Expand All @@ -65,28 +65,28 @@ fn revoke_operator_function() -> Function {

// Message to Ethereum (ABI-encoded)
#[derive(Copy, Clone, PartialEq, Eq, RuntimeDebug)]
pub struct DeregisterOperatorPayload {
pub operator: H160,
pub struct RemoveAppPayload {
pub app: H160,
}

impl DeregisterOperatorPayload {
impl RemoveAppPayload {
/// ABI-encode this payload
pub fn encode(&self) -> Result<Vec<u8>, ethabi::Error> {
let tokens = &[Token::Address(self.operator)];
revoke_operator_function().encode_input(tokens.as_ref())
let tokens = &[Token::Address(self.app)];
remove_app_function().encode_input(tokens.as_ref())
}
}

// Message to Ethereum (ABI-encoded)
#[derive(Copy, Clone, PartialEq, Eq, RuntimeDebug)]
pub struct RegisterOperatorPayload {
pub operator: H160,
pub struct RegisterAppPayload {
pub app: H160,
}

impl RegisterOperatorPayload {
impl RegisterAppPayload {
/// ABI-encode this payload
pub fn encode(&self) -> Result<Vec<u8>, ethabi::Error> {
let tokens = &[Token::Address(self.operator)];
authorize_operator_function().encode_input(tokens.as_ref())
let tokens = &[Token::Address(self.app)];
register_app_function().encode_input(tokens.as_ref())
}
}
Loading
Loading