Skip to content

Commit

Permalink
Update types for EVM bridge (#79)
Browse files Browse the repository at this point in the history
* Update types for EVM bridge

* Fix warnings

* Fix warnings

* Remove Native asset kind
  • Loading branch information
vovac12 authored Apr 22, 2024
1 parent 37a579c commit 87eadea
Show file tree
Hide file tree
Showing 20 changed files with 344 additions and 3,152 deletions.
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
18 changes: 18 additions & 0 deletions pallets/liberland-bridge-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,24 @@ impl<T: Config> bridge_types::traits::BridgeAssetLocker<T::AccountId> for Pallet
}
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

0 comments on commit 87eadea

Please sign in to comment.