-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
186 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
{ | ||
} |