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

WebbGadget #5

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 20 additions & 24 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
[package]
name = "gadget"
version = "0.0.1"
authors = ["Thomas P Braun"]
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
edition = "2021"

[features]
substrate = [
"sp-runtime",
"sc-client-api",
"sp-api",
"futures"
[workspace]
members = [
"gadget-core",
"webb-gadget",
"zk-gadget"
]

[dependencies]
sync_wrapper = "0.1.2"
parking_lot = "0.12.1"
tokio = { version = "1.32.0", features = ["sync", "time", "macros", "rt"] }
hex = "0.4.3"
async-trait = "0.1.73"
[workspace.dependencies]
gadget-core = { path = "./gadget-core" }
webb-gadget = { path = "./webb-gadget" }
zk-gadget = { path = "./zk-gadget" }

sp-runtime = { optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
sc-client-api = { optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
sp-api = { optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
futures = { optional = true, version = "0.3.28" }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }

[dev-dependencies]
mpc-net = { git = "https://github.com/webb-tools/zk-SaaS/" }
tokio-rustls = "0.24.1"
tokio = "1.32.0"
bincode2 = "2"
futures-util = "0.3.28"
serde = "1.0.188"
async-trait = "0.1.73"
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Gadget

## Design

The core library is `gadget-core`. The core library allows gadgets to hold standardization of use across different blockchains. The core library is the base of all gadgets, and expects to receive `FinalityNotifications` and `BlockImportNotifications`.

Once such blockchain is a substrate blockchain. This is where `webb-gadget` comes into play. The `webb-gadget` is the core-gadget endowed with a connection to a substrate blockchain, a networking layer to communicate with other gadgets, and a *WebbModule* that has application-specific logic.

Since `webb-gadget` allows varying connections to a substrate blockchain and differing network layers, we can thus design above it the `zk-gadget` and `tss-gadget`. These gadgets are endowed with the same functionalities as the `webb-gadget` but with a different connection and networking layer.
28 changes: 28 additions & 0 deletions gadget-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "gadget-core"
version = "0.0.1"
authors = ["Thomas P Braun"]
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
edition = "2021"

[features]
substrate = [
"sp-runtime",
"sc-client-api",
"sp-api",
"futures"
]

[dependencies]
sync_wrapper = "0.1.2"
parking_lot = "0.12.1"
tokio = { workspace = true, features = ["sync", "time", "macros", "rt"] }
hex = "0.4.3"
async-trait = "0.1.73"

sp-runtime = { optional = true, workspace = true, default-features = false }
sc-client-api = { optional = true, workspace = true, default-features = false }
sp-api = { optional = true, workspace = true, default-features = false }
futures = { optional = true, version = "0.3.28" }

[dev-dependencies]
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ use sp_api::ProvideRuntimeApi;
use sp_runtime::traits::Block;
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::sync::Arc;
use tokio::sync::Mutex;

pub struct SubstrateGadget<B: Block, BE, API, Module> {
pub struct SubstrateGadget<Module: SubstrateGadgetModule> {
module: Module,
finality_notification_stream: Mutex<FinalityNotifications<B>>,
block_import_notification_stream: Mutex<ImportNotifications<B>>,
_pd: std::marker::PhantomData<(B, BE, API)>,
finality_notification_stream: Mutex<FinalityNotifications<Module::Block>>,
block_import_notification_stream: Mutex<ImportNotifications<Module::Block>>,
client: Arc<Module::Client>,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand All @@ -25,18 +26,19 @@ pub struct SubstrateGadgetError {}
#[async_trait]
pub trait SubstrateGadgetModule: Send + Sync {
type Error: Error + Send;
type FinalityNotification: Send;
type BlockImportNotification: Send;
type ProtocolMessage: Send;
type Block: Block;
type Backend: Backend<Self::Block>;
type Client: Client<Self::Block, Self::Backend>;

async fn get_next_protocol_message(&self) -> Option<Self::ProtocolMessage>;
async fn process_finality_notification(
&self,
notification: Self::FinalityNotification,
notification: FinalityNotification<Self::Block>,
) -> Result<(), Self::Error>;
async fn process_block_import_notification(
&self,
notification: Self::BlockImportNotification,
notification: BlockImportNotification<Self::Block>,
) -> Result<(), Self::Error>;
async fn process_protocol_message(
&self,
Expand All @@ -61,39 +63,34 @@ where
{
}

impl<B, BE, Api, Module> SubstrateGadget<B, BE, Api, Module>
impl<Module> SubstrateGadget<Module>
where
B: Block,
BE: Backend<B>,
Module: SubstrateGadgetModule,
Api: Send + Sync,
{
pub fn new<C: Client<B, BE, Api = Api>>(client: &C, module: Module) -> Self {
pub fn new(client: Module::Client, module: Module) -> Self {
let finality_notification_stream = client.finality_notification_stream();
let block_import_notification_stream = client.import_notification_stream();

Self {
module,
finality_notification_stream: Mutex::new(finality_notification_stream),
block_import_notification_stream: Mutex::new(block_import_notification_stream),
_pd: std::marker::PhantomData,
client: Arc::new(client),
}
}

pub fn client(&self) -> &Arc<Module::Client> {
&self.client
}
}

#[async_trait]
impl<B, BE, Api, Module> AbstractGadget for SubstrateGadget<B, BE, Api, Module>
impl<Module> AbstractGadget for SubstrateGadget<Module>
where
B: Block,
BE: Backend<B>,
Module: SubstrateGadgetModule<
FinalityNotification = FinalityNotification<B>,
BlockImportNotification = BlockImportNotification<B>,
>,
Api: Send + Sync,
Module: SubstrateGadgetModule,
{
type FinalityNotification = FinalityNotification<B>;
type BlockImportNotification = BlockImportNotification<B>;
type FinalityNotification = FinalityNotification<Module::Block>;
type BlockImportNotification = BlockImportNotification<Module::Block>;
type ProtocolMessage = Module::ProtocolMessage;
type Error = Module::Error;

Expand Down
Loading
Loading