Skip to content

Commit

Permalink
feat: added runtime-api for eth-event polling (#376)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Cholakov <icholakov1@gmail.com>
Co-authored-by: Thanos Doukoudakis <56822898+thadouk@users.noreply.github.com>
  • Loading branch information
ivan-cholakov and thadouk committed Jun 26, 2024
1 parent 926ce67 commit e163adf
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 5 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ members = [
"node/avn-service",
"node/avn-lower-rpc",
"pallets/*",
"pallets/*/runtime-api",
"primitives/*",
"runtime/*",
]
1 change: 1 addition & 0 deletions Dockerfile.22_04
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ RUN apt-get update && \
ca-certificates \
curl \
jq && \
update-ca-certificates && \
# apt cleanup
apt-get autoremove -y && \
apt-get clean && \
Expand Down
2 changes: 2 additions & 0 deletions pallets/eth-bridge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ frame-system = { default-features = false, git = "https://github.com/paritytech/
pallet-timestamp = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
pallet-session = {default-features = false, features = ["historical"], git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
pallet-avn = { default-features = false, path = "../avn" }
sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v1.0.0" }

# Optional imports for benchmarking
frame-benchmarking = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", optional = true }
Expand All @@ -42,6 +43,7 @@ std = [
"frame-benchmarking?/std",
"scale-info/std",
"codec/std",
"sp-api/std",
"sp-std/std",
"sp-core/std",
"sp-io/std",
Expand Down
37 changes: 37 additions & 0 deletions pallets/eth-bridge/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "pallet-eth-bridge-runtime-api"
description = "Runtime API for module"
license = "GPL-3.0"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
homepage = { workspace = true }
repository = { workspace = true }
rust-version = { workspace = true }


[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", features = ["derive"], default-features = false }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
pallet-eth-bridge = { default-features = false, path = "../../eth-bridge" }
pallet-avn = { path = "../../avn", default-features = false }
sp-api = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v1.0.0" }
sp-avn-common = { default-features = false, path = "../../../primitives/avn-common" }
sp-application-crypto = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }



[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"pallet-eth-bridge/std",
"sp-api/std",
]


25 changes: 25 additions & 0 deletions pallets/eth-bridge/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![cfg_attr(not(feature = "std"), no_std)]
use codec::Codec;
use frame_support::dispatch::Vec;
use sp_avn_common::event_discovery::{EthBlockRange, EthereumEventsPartition};
use sp_core::{H160, H256};

sp_api::decl_runtime_apis! {

#[api_version(1)]
pub trait EthEventHandlerApi<AccountId>
where
AccountId: Codec,
{
fn query_active_block_range()-> (EthBlockRange, u16);
fn query_has_author_casted_event_vote(account_id: AccountId) -> bool;
fn query_signatures() -> Vec<H256>;
fn query_bridge_contract() -> H160;
fn create_proof(account_id:AccountId, events_partition:EthereumEventsPartition)-> Vec<u8>;
fn submit_vote(
author: AccountId,
events_partition: EthereumEventsPartition,
signature: sp_core::sr25519::Signature
) -> Result<(), ()>;
}
}
63 changes: 59 additions & 4 deletions pallets/eth-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ use pallet_session::historical::IdentificationTuple;
use sp_staking::offence::ReportOffence;

use sp_application_crypto::RuntimeAppPublic;
use sp_avn_common::{event_discovery::*, event_types::Validator};
use sp_core::{ecdsa, ConstU32, H256};
use sp_avn_common::{
event_discovery::*,
event_types::{ValidEvents, Validator},
};
use sp_core::{ecdsa, ConstU32, H160, H256};
use sp_io::hashing::keccak_256;
use sp_runtime::{scale_info::TypeInfo, traits::Dispatchable};
use sp_std::prelude::*;
Expand All @@ -86,7 +89,7 @@ mod call;
mod eth;
mod request;
mod tx;
mod types;
pub mod types;
mod util;
use crate::types::*;

Expand Down Expand Up @@ -319,6 +322,7 @@ pub mod pallet {
EventVoteExists,
NonActiveEthereumRange,
VotingEnded,
ValidatorNotFound
}

#[pallet::call]
Expand Down Expand Up @@ -682,7 +686,7 @@ pub mod pallet {

Ok(())
}
fn author_has_cast_event_vote<T: Config>(author: &T::AccountId) -> bool {
pub fn author_has_cast_event_vote<T: Config>(author: &T::AccountId) -> bool {
for (_partition, votes) in EthereumEvents::<T>::iter() {
if votes.contains(&author) {
return true
Expand Down Expand Up @@ -880,3 +884,54 @@ pub mod pallet {
}
}
}

impl<T: Config> Pallet<T> {
pub fn create_eth_events_proof(account_id:T::AccountId, events_partition:EthereumEventsPartition) -> Vec<u8>{
create_ethereum_events_proof_data::<T>(&account_id, &events_partition)
}
pub fn signatures() -> Vec<H256> {
let signatures: Vec<H256> = match Self::active_ethereum_range() {
Some(active_range) => {
let _events =
active_range.event_types_filter.into_iter().collect::<Vec<ValidEvents>>();

let decoded_hex =
hex::decode("418da8f85cfa851601f87634c6950491b6b8785a6445c8584f5658048d512cae").
expect("test");

let mut array = [0; 32];
array.copy_from_slice(&decoded_hex);
let decoded_event_sig = H256::from(array);

vec![decoded_event_sig]
},
None => {
// TODO use values from pallet constant
// vec![]
let decoded_hex =
hex::decode("418da8f85cfa851601f87634c6950491b6b8785a6445c8584f5658048d512cae").
expect("test");

let mut array = [0; 32];
array.copy_from_slice(&decoded_hex);
let decoded_event_sig = H256::from(array);

vec![decoded_event_sig]
},
};
signatures
}
pub fn submit_vote(
account_id: T::AccountId,
events_partition: EthereumEventsPartition,
signature: <T::AuthorityId as RuntimeAppPublic>::Signature
) -> Result<(), ()>{
let validator: Author<T> = AVN::<T>::validators().into_iter().filter(|v| v.account_id == account_id).nth(0).unwrap();

submit_ethereum_events::<T>(validator, events_partition, signature)
}

pub fn get_bridge_contract() -> H160 {
AVN::<T>::get_bridge_contract_address()
}
}
1 change: 1 addition & 0 deletions runtime/avn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pallet-nft-manager = { path = "../../pallets/nft-manager", default-features = fa
pallet-avn-proxy = { path = "../../pallets/avn-proxy", default-features = false }
pallet-avn-transaction-payment = { path = "../../pallets/avn-transaction-payment", default-features = false }
pallet-eth-bridge = { path = "../../pallets/eth-bridge", default-features = false }
pallet-eth-bridge-runtime-api = { path = "../../pallets/eth-bridge/runtime-api", default-features = false }
pallet-parachain-staking = { path = "../../pallets/parachain-staking", default-features = false }

# Common Runtime
Expand Down
35 changes: 34 additions & 1 deletion runtime/avn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use pallet_avn::sr25519::AuthorityId as AvnId;

pub use pallet_avn_proxy::{Event as AvnProxyEvent, ProvableProxy};
use pallet_avn_transaction_payment::AvnCurrencyAdapter;
use sp_avn_common::{InnerCallValidator, Proof};
use sp_avn_common::{event_discovery::{EthBlockRange, EthereumEventsPartition}, InnerCallValidator, Proof};

use pallet_parachain_staking;
pub type NegativeImbalance<T> = <pallet_balances::Pallet<T> as Currency<
Expand Down Expand Up @@ -936,6 +936,39 @@ impl_runtime_apis! {
TransactionPayment::length_to_fee(length)
}
}

impl pallet_eth_bridge_runtime_api::EthEventHandlerApi<Block, AccountId> for Runtime {
fn query_active_block_range()-> (EthBlockRange, u16){
if let Some(active_eth_range) = EthBridge::active_ethereum_range(){
(active_eth_range.range, active_eth_range.partition)
}else {
(EthBlockRange::default(), 0)
}
}
fn query_has_author_casted_event_vote(account_id: AccountId) -> bool{
pallet_eth_bridge::author_has_cast_event_vote::<Runtime>(&account_id)
}

fn query_signatures() -> Vec<sp_core::H256> {
EthBridge::signatures()
}

fn query_bridge_contract() -> H160 {
EthBridge::get_bridge_contract()
}

fn create_proof(account_id:AccountId, events_partition:EthereumEventsPartition)->Vec<u8>{
EthBridge::create_eth_events_proof(account_id, events_partition)
}

fn submit_vote(author: AccountId,
events_partition: EthereumEventsPartition,
signature: sp_core::sr25519::Signature,
) -> Result<(),()>{
EthBridge::submit_vote(author, events_partition, signature.into())
}
}

impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {
fn collect_collation_info(header: &<Block as BlockT>::Header) -> cumulus_primitives_core::CollationInfo {
ParachainSystem::collect_collation_info(header)
Expand Down

0 comments on commit e163adf

Please sign in to comment.