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

MP-ECDSA #37

Merged
merged 17 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 0 additions & 15 deletions .github/workflows/validate_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@ concurrency:
cancel-in-progress: true

jobs:
integration-tests:
name: Integration Tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install protobuf
run: sudo apt-get install protobuf-compiler

- name: Generate certs
run: bash ./playground/generate_certs.sh

- name: Run ZK MVP
run: docker-compose up --abort-on-container-exit
formatting:
name: Rustfmt
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
resolver = "2"
members = [
"gadget-core",
"webb-gadget",
"gadget-common",
"zk-gadget",
"test-gadget",
"protocols/mp-ecdsa",
]

[workspace.dependencies]
gadget-core = { path = "./gadget-core" }
webb-gadget = { path = "./webb-gadget" }
gadget-common = { path = "./gadget-common" }
zk-gadget = { path = "./zk-gadget" }
mp-ecdsa-protocol = { path = "./protocols/mp-ecdsa" }

Expand Down
8 changes: 6 additions & 2 deletions webb-gadget/Cargo.toml → gadget-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "webb-gadget"
name = "gadget-common"
version = "0.1.0"
edition = "2021"

Expand All @@ -19,4 +19,8 @@ sc-client-api = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
hex = { workspace = true }
bincode2 = { workspace = true }
bincode2 = { workspace = true }

sp-api = { workspace = true, default-features = false, features = ["std"] }
tangle-primitives = { workspace = true, default-features = false, features = ["std"] }
tbraun96 marked this conversation as resolved.
Show resolved Hide resolved
pallet-jobs-rpc-runtime-api = { workspace = true, default-features = false, features = ["std"] }
17 changes: 6 additions & 11 deletions protocols/mp-ecdsa/src/client.rs → gadget-common/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::debug_logger::DebugLogger;
use crate::keystore::{ECDSAKeyStore, KeystoreBackend};
use crate::util::DebugLogger;
use crate::MpEcdsaProtocolConfig;
use async_trait::async_trait;
use gadget_core::gadget::substrate::Client;
use pallet_jobs_rpc_runtime_api::{JobsApi, RpcResponseJobsData};
Expand All @@ -15,9 +14,8 @@ use tangle_primitives::jobs::{JobId, JobKey, JobResult};

pub struct MpEcdsaClient<B: Block, BE, KBE: KeystoreBackend, C> {
client: Arc<C>,
pub(crate) key_store: ECDSAKeyStore<KBE>,
pub key_store: ECDSAKeyStore<KBE>,
logger: DebugLogger,
account_id: AccountId,
_block: std::marker::PhantomData<(BE, B)>,
}

Expand All @@ -27,7 +25,6 @@ impl<B: Block, BE, KBE: KeystoreBackend, C> Clone for MpEcdsaClient<B, BE, KBE,
client: self.client.clone(),
key_store: self.key_store.clone(),
logger: self.logger.clone(),
account_id: self.account_id,
_block: self._block,
}
}
Expand All @@ -39,7 +36,6 @@ pub async fn create_client<
KBE: KeystoreBackend,
C: ClientWithApi<B, BE>,
>(
config: &MpEcdsaProtocolConfig,
client: Arc<C>,
logger: DebugLogger,
key_store: ECDSAKeyStore<KBE>,
Expand All @@ -51,7 +47,6 @@ where
client,
key_store,
logger,
account_id: config.account_id,
_block: std::marker::PhantomData,
})
}
Expand Down Expand Up @@ -91,15 +86,15 @@ where
&self,
at: <B as Block>::Hash,
validator: AccountId,
) -> Result<Vec<RpcResponseJobsData<AccountId>>, webb_gadget::Error> {
) -> Result<Vec<RpcResponseJobsData<AccountId>>, crate::Error> {
exec_client_function(&self.client, move |client| {
client.runtime_api().query_jobs_by_validator(at, validator)
})
.await
.map_err(|err| webb_gadget::Error::ClientError {
.map_err(|err| crate::Error::ClientError {
err: format!("Failed to query jobs by validator: {err:?}"),
})?
.map_err(|err| webb_gadget::Error::ClientError {
.map_err(|err| crate::Error::ClientError {
err: format!("Failed to query jobs by validator: {err:?}"),
})
}
Expand All @@ -110,7 +105,7 @@ where
_job_key: JobKey,
_job_id: JobId,
_result: JobResult,
) -> Result<(), webb_gadget::Error> {
) -> Result<(), crate::Error> {
Ok(())
}
}
Expand Down
28 changes: 28 additions & 0 deletions gadget-common/src/debug_logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::fmt::Display;

#[derive(Clone)]
pub struct DebugLogger {
pub peer_id: String,
}

impl DebugLogger {
pub fn trace<T: Display>(&self, msg: T) {
log::trace!(target: "gadget", "[{}] {msg}", &self.peer_id);
}

pub fn debug<T: Display>(&self, msg: T) {
log::debug!(target: "gadget", "[{}] {msg}", &self.peer_id);
}

pub fn info<T: Display>(&self, msg: T) {
log::info!(target: "gadget", "[{}] {msg}", &self.peer_id);
}

pub fn warn<T: Display>(&self, msg: T) {
log::warn!(target: "gadget", "[{}] {msg}", &self.peer_id);
}

pub fn error<T: Display>(&self, msg: T) {
log::error!(target: "gadget", "[{}] {msg}", &self.peer_id);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
83 changes: 83 additions & 0 deletions gadget-common/src/keystore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use async_trait::async_trait;
use parking_lot::RwLock;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::collections::HashMap;
use std::sync::Arc;
use tangle_primitives::jobs::JobId;

#[derive(Clone)]
pub struct ECDSAKeyStore<BE: KeystoreBackend> {
backend: BE,
}

impl<BE: KeystoreBackend> ECDSAKeyStore<BE> {
pub fn in_memory() -> ECDSAKeyStore<InMemoryBackend> {
ECDSAKeyStore {
backend: InMemoryBackend::new(),
}
}

pub async fn get<T: DeserializeOwned>(
&self,
job_id: &JobId,
) -> Result<Option<T>, crate::Error> {
self.backend.get(job_id).await
}

pub async fn set<T: Serialize + Send>(
&self,
job_id: JobId,
value: T,
) -> Result<(), crate::Error> {
self.backend.set(job_id, value).await
}
}

#[async_trait]
pub trait KeystoreBackend: Clone + Send + Sync + 'static {
async fn get<T: DeserializeOwned>(&self, job_id: &JobId) -> Result<Option<T>, crate::Error>;
async fn set<T: Serialize + Send>(&self, job_id: JobId, value: T) -> Result<(), crate::Error>;
}

#[derive(Clone)]
pub struct InMemoryBackend {
map: Arc<RwLock<HashMap<JobId, Vec<u8>>>>,
}

impl Default for InMemoryBackend {
fn default() -> Self {
Self::new()
}
}

impl InMemoryBackend {
pub fn new() -> Self {
Self {
map: Arc::new(RwLock::new(HashMap::new())),
}
}
}

#[async_trait]
impl KeystoreBackend for InMemoryBackend {
async fn get<T: DeserializeOwned>(&self, job_id: &JobId) -> Result<Option<T>, crate::Error> {
if let Some(bytes) = self.map.read().get(job_id).cloned() {
let value: T =
bincode2::deserialize(&bytes).map_err(|rr| crate::Error::KeystoreError {
err: format!("Failed to deserialize value: {:?}", rr),
})?;
Ok(Some(value))
} else {
Ok(None)
}
}

async fn set<T: Serialize + Send>(&self, job_id: JobId, value: T) -> Result<(), crate::Error> {
let serialized = bincode2::serialize(&value).map_err(|rr| crate::Error::KeystoreError {
err: format!("Failed to serialize value: {:?}", rr),
})?;
self.map.write().insert(job_id, serialized);
Ok(())
}
}
5 changes: 4 additions & 1 deletion webb-gadget/src/lib.rs → gadget-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ use sp_runtime::SaturatedConversion;
use std::fmt::{Debug, Display, Formatter};
use std::sync::Arc;

pub mod client;
pub mod debug_logger;
pub mod gadget;

pub mod helpers;
pub mod keystore;
pub mod protocol;

#[derive(Debug)]
Expand All @@ -33,6 +35,7 @@ pub enum Error {
ClientError { err: String },
JobError { err: JobError },
NetworkError { err: String },
KeystoreError { err: String },
}

impl Display for Error {
Expand Down
File renamed without changes.
65 changes: 0 additions & 65 deletions playground/Cargo.toml

This file was deleted.

Loading
Loading