Skip to content

Commit

Permalink
Init single-bucket example
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Mar 12, 2022
1 parent 951fdaf commit 6ab60c1
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 4 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/sb_example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Example

concurrency:
group: single-bucket-${{ github.head_ref }}
cancel-in-progress: true

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

workflow_dispatch:

jobs:
single-bucket-example:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive

- uses: cachix/install-nix-action@v15
with:
nix_path: nixpkgs=channel:nixos-unstable

- name: Run test
run: nix develop -c 'run-example-test' sb

- name: Deploy test
run: nix develop -c 'cli-build-test' sb
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ exclude = [
"examples/rdb-contract",
"examples/rusty-contract",
"examples/ballot-contract",
"examples/sb-contract",
]
2 changes: 2 additions & 0 deletions examples/sb-contract/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.'cfg(target_arch="wasm32")']
rustflags = ["-C", "link-arg=--export-table"]
33 changes: 33 additions & 0 deletions examples/sb-contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "sb-contract"
version = "0.1.0"
authors = ["Antonio Yang <[email protected]>"]
edition = "2021"
description = "The example contract using sewup single bucket feature"

[lib]
path = "src/lib.rs"
crate-type = ["cdylib", "lib"]

[dependencies]
anyhow = "1.0.40"
serde = "1.0"
serde_derive = "1.0"

sewup = { version = "*", path = "../../sewup", features = [ "single-bucket" ] }
sewup-derive = { version = "*", path = "../../sewup-derive", features = [ "single-bucket" ] }
thiserror = "1.0.24"

[profile.release]
incremental = false
panic = "abort"
lto = true
opt-level = "z"

[profile.release.package.kv-contract]
incremental = false
opt-level = "z"

[features]
constructor = []
constructor-test = []
5 changes: 5 additions & 0 deletions examples/sb-contract/sewup.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This config file is for examples, it is good for you to ignore this config in your sewup project
[deploy]
url = "http://localhost:8545"
private = "0000000000000000000000000000000000000000000000000000000000000000"
address = "0x0000000000000000000000000000000000000000"
44 changes: 44 additions & 0 deletions examples/sb-contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use serde_derive::{Deserialize, Serialize};
use sewup::SingleBucket;
use sewup_derive::{ewasm_constructor, ewasm_main, ewasm_test, Value};

#[derive(Default, Clone, Serialize, Deserialize, Debug, PartialEq, Value)]
struct SimpleStruct {
trust: bool,
description: String,
}

#[ewasm_constructor]
fn setup() {
use sewup::{
single_bucket::SingleBucket2,
types::{Raw, Row},
};
// (Raw, Row), (Row, SimpleStruct)
let bucket = SingleBucket2::<Raw, Row, Row, SimpleStruct>::default();
bucket
.commit()
.expect("there is no return for constructor currently");
}

#[ewasm_main(auto)]
fn main() -> anyhow::Result<sewup::primitives::EwasmAny> {
use sewup_derive::{ewasm_fn_sig, ewasm_input_from};

let contract = sewup::primitives::Contract::new()?;

let output = match contract.get_function_selector()? {
_ => return panic!("unhandled"),
};

Ok(output)
}

#[ewasm_test]
mod tests {
use super::*;
#[ewasm_test]
fn test_() {
assert!(true)
}
}
1 change: 1 addition & 0 deletions sewup-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ rdb = []
token = []
test = ["rdb", "kv"]
debug = []
single-bucket = ["kv"]

[package.metadata.docs.rs]
all-features = true
2 changes: 1 addition & 1 deletion sewup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ token = [ ]
kv = []
rdb = []
debug = []
single-bucket = []
single-bucket = ["kv"]

[package.metadata.docs.rs]
all-features = true
71 changes: 68 additions & 3 deletions sewup/src/single_bucket/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,80 @@
use std::iter::Iterator;
use std::marker::PhantomData;

use anyhow::Result;
use ewasm_api::storage_store;
use serde::Serialize;
use serde_derive::Serialize as SerializeDerive;

use crate::utils::storage_index_to_addr;

const CONFIG_ADDR: [u8; 32] = [0; 32];

pub trait SingleBucket
where
Self: Serialize + Default,
{
fn commit(&self) -> Result<u32> {
let mut buffer = [0u8; 32];
let bin = bincode::serialize(&self).expect("serialize db binary fail");
let length = bin.len();

let mut len_buffer = bin.len().to_be_bytes();
len_buffer.swap_with_slice(&mut buffer[28..32]);

storage_store(&CONFIG_ADDR.into(), &buffer.into());

let mut addr: [u8; 32] = [0; 32];
let mut storage_index = 0;
let mut iter = bin.chunks_exact(32);
while storage_index * 32 < length as usize {
storage_index += 1;
storage_index_to_addr(storage_index, &mut addr);

if let Some(chunk) = iter.next() {
let part: [u8; 32] = chunk.try_into().unwrap();
storage_store(&addr.into(), &part.into());
} else {
let remainder = iter.remainder();
storage_index_to_addr(storage_index, &mut addr);
let mut part = [0u8; 32];
for i in 0..length & 31 {
part[i] = remainder[i];
}
storage_store(&addr.into(), &part.into());
break;
}
}
Ok(length as u32)
}
}

pub struct Item<K, V> {
phantom_k: PhantomData<K>,
phantom_v: PhantomData<V>,
}

pub enum Pair<K, V> {
pub enum Pair1<K, V> {
Item1(Item<K, V>),
}

pub trait SingleBucket<K1, V1> {
type Pairs: Iterator<Item = Pair<K1, V1>>;
pub trait SingleBucket1<K1, V1>: SingleBucket + Default {
type Pairs: Iterator<Item = Pair1<K1, V1>>;
}

pub enum Pair2<K1, V1, K2, V2> {
Item1(Item<K1, V1>),
Item2(Item<K2, V2>),
}

#[derive(Default, SerializeDerive)]
pub struct SingleBucket2<K1: Default, V1: Default, K2: Default, V2: Default> {
phantom_k1: PhantomData<K1>,
phantom_v1: PhantomData<V1>,
phantom_k2: PhantomData<K2>,
phantom_v2: PhantomData<V2>,
}
impl<K1: Default, V1: Default, K2: Default, V2: Default> SingleBucket
for SingleBucket2<K1, V1, K2, V2>
{
}

0 comments on commit 6ab60c1

Please sign in to comment.