Skip to content

Commit

Permalink
Merge pull request #189 from KogarashiNetwork/feature/shot-fix
Browse files Browse the repository at this point in the history
Add pallet nova sample
  • Loading branch information
ashWhiteHat authored Jan 24, 2024
2 parents 6cfe4cd + 6f14962 commit 670d3a5
Show file tree
Hide file tree
Showing 12 changed files with 556 additions and 13 deletions.
108 changes: 100 additions & 8 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ members = [
"grumpkin",
"nova",
"zkstd",
"pallet/nova"
"pallet/nova",
"sample"
]

[profile.release]
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ RUN apk add --no-cache --update-cache \

WORKDIR /app

COPY . .
COPY sample .

WORKDIR /app/pallet/nova
WORKDIR /app

RUN cargo test --release
CMD cargo test --release
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ or
$ docker-compose up
```

If you want to run `pallet-nova` container without docker-compose, the command would be as follow.

```shell
$ docker build . -t nova
$ docker run nova
```

## Status

**We are in research and development phase and this is alpha quality software. Please use at your own risk**.
Expand Down
2 changes: 1 addition & 1 deletion nova/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ where
FC2: FunctionCircuit<E2::Scalar>,
{
#[codec(compact)]
pub(crate) i: u64,
pub i: u64,
pub(crate) z0_primary: DenseVectors<E1::Scalar>,
pub(crate) z0_secondary: DenseVectors<E2::Scalar>,
pub(crate) zi_primary: DenseVectors<E1::Scalar>,
Expand Down
37 changes: 37 additions & 0 deletions sample/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "zk-storage"
version = "3.0.0"
edition = "2018"
authors = ['Substrate DevHub <https://github.com/substrate-developer-hub>']
repository = "https://github.com/substrate-developer-hub/recipes"
description = "A pallet with two storage items whose sum is exposed via a custom runtime API"
license = "GPL-3.0-or-later"

[dependencies]
parity-scale-codec = { version = "2.0", default-features = false, features = ["derive"] }

# Substrate packages
frame-support = { branch = 'v3.0.0', default-features = false, git = "https://github.com/KogarashiNetwork/zksubstrate" }
frame-system = { branch = 'v3.0.0', default-features = false, git = "https://github.com/KogarashiNetwork/zksubstrate" }
sp-runtime = { branch = 'v3.0.0', default-features = false, git = "https://github.com/KogarashiNetwork/zksubstrate" }
sp-std = { branch = 'v3.0.0', default-features = false, git = "https://github.com/KogarashiNetwork/zksubstrate" }

# nova pallet
pallet-nova = { git = "https://github.com/KogarashiNetwork/Kogarashi", branch = "master", default-features = false }
rand_core = {version="0.6", default-features = false }

[dev-dependencies]
serde = { version = "1.0.102", default-features = false, features = ["derive"] }
sp-io = { branch = 'v3.0.0', default-features = false, git = "https://github.com/KogarashiNetwork/zksubstrate" }
sp-core = { branch = 'v3.0.0', default-features = false, git = "https://github.com/KogarashiNetwork/zksubstrate" }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"sp-std/std",
"sp-runtime/std",
"frame-support/std",
"frame-system/std",
"pallet-nova/std"
]
35 changes: 35 additions & 0 deletions sample/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "sum-storage-rpc"
version = "2.0.0"
edition = "2018"
authors = ['Substrate DevHub <https://github.com/substrate-developer-hub>']
repository = 'https://github.com/substrate-developer-hub/recipes'
description = "A pallet that demonstrates simple RPC for summing numbers"
license = "GPL-3.0-or-later"

[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.0" }
jsonrpc-core = "15.0"
jsonrpc-core-client = "15.0"
jsonrpc-derive = "15.0"
serde = { version = "1.0", features = ["derive"], optional = true }

# Substrate packages

sp-api = { version = '3.0', default-features = false }
sp-blockchain = { version = '3.0', default-features = false}
sp-rpc = { version = '3.0', default-features = false}
sp-runtime = { version = '3.0', default-features = false}

# local packages

sum-storage-runtime-api = { version = "2.0.0", path = "../runtime-api", default-features = false }

[features]
default = ["std"]
std = [
"serde",
"sp-api/std",
"sp-runtime/std",
"sum-storage-runtime-api/std"
]
73 changes: 73 additions & 0 deletions sample/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! RPC interface for the transaction payment module.

use jsonrpc_core::{Error as RpcError, ErrorCode, Result};
use jsonrpc_derive::rpc;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use std::sync::Arc;
use sum_storage_runtime_api::SumStorageApi as SumStorageRuntimeApi;

#[rpc]
pub trait SumStorageApi<BlockHash> {
#[rpc(name = "sumStorage_getSum")]
fn get_sum(&self, at: Option<BlockHash>) -> Result<u32>;
}

/// A struct that implements the `SumStorageApi`.
pub struct SumStorage<C, M> {
// If you have more generics, no need to SumStorage<C, M, N, P, ...>
// just use a tuple like SumStorage<C, (M, N, P, ...)>
client: Arc<C>,
_marker: std::marker::PhantomData<M>,
}

impl<C, M> SumStorage<C, M> {
/// Create new `SumStorage` instance with the given reference to the client.
pub fn new(client: Arc<C>) -> Self {
Self {
client,
_marker: Default::default(),
}
}
}

/// Error type of this RPC api.
// pub enum Error {
// /// The transaction was not decodable.
// DecodeError,
// /// The call to runtime failed.
// RuntimeError,
// }
//
// impl From<Error> for i64 {
// fn from(e: Error) -> i64 {
// match e {
// Error::RuntimeError => 1,
// Error::DecodeError => 2,
// }
// }
// }

impl<C, Block> SumStorageApi<<Block as BlockT>::Hash> for SumStorage<C, Block>
where
Block: BlockT,
C: Send + Sync + 'static,
C: ProvideRuntimeApi<Block>,
C: HeaderBackend<Block>,
C::Api: SumStorageRuntimeApi<Block>,
{
fn get_sum(&self, at: Option<<Block as BlockT>::Hash>) -> Result<u32> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash));

let runtime_api_result = api.get_sum(&at);
runtime_api_result.map_err(|e| RpcError {
code: ErrorCode::ServerError(9876), // No real reason for this value
message: "Something wrong".into(),
data: Some(format!("{:?}", e).into()),
})
}
}
18 changes: 18 additions & 0 deletions sample/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "sum-storage-runtime-api"
version = "2.0.0"
authors = ['Substrate DevHub <https://github.com/substrate-developer-hub>']
edition = "2018"
license = "GPL-3.0-or-later"

[dependencies]
sp-api = { version = '3.0', default-features = false}

[dev-dependencies]
serde_json = "1.0"

[features]
default = ["std"]
std = [
"sp-api/std",
]
Loading

0 comments on commit 670d3a5

Please sign in to comment.