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

initialize design for supporting events #45

Merged
merged 4 commits into from
Dec 29, 2024
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
9 changes: 2 additions & 7 deletions abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub fn conclude_tx_info(data: &[u8]) -> [u64;4] {
macro_rules! create_zkwasm_apis {
($T: ident, $S: ident, $C: ident) => {
#[wasm_bindgen]
pub fn handle_tx(params: Vec<u64>) -> u32 {
pub fn handle_tx(params: Vec<u64>) -> Vec<u64> {
let user_address = [params[0], params[1], params[2], params[3]];
let sig_r = [params[16], params[17], params[18], params[19]];
let command = &params[20..];
Expand Down Expand Up @@ -259,8 +259,7 @@ macro_rules! create_zkwasm_apis {
}
}


#[wasm_bindgen]
#[wasm_bindgen]
pub fn zkmain() {
use zkwasm_rust_sdk::wasm_input;
use zkwasm_rust_sdk::wasm_output;
Expand All @@ -283,21 +282,17 @@ macro_rules! create_zkwasm_apis {
}
let command = unsafe {wasm_input(0)};
let command_length = ((command & 0xff00) >> 8) as usize;
zkwasm_rust_sdk::dbg!("cmd length: {}\n", command_length);
unsafe { zkwasm_rust_sdk::require(command_length < 16) };
params.push(command);
for _ in 0..command_length - 1 {
params.push(unsafe {wasm_input(0)});
}
zkwasm_rust_sdk::dbg!("sig verify\n");
verify_tx_signature(params.clone());
zkwasm_rust_sdk::dbg!("success\n");
handle_tx(params);
let trace = unsafe {wasm_trace_size()};
zkwasm_rust_sdk::dbg!("trace track: {}\n", trace);
}

zkwasm_rust_sdk::dbg!("trace after tx handlers\n");
unsafe { zkwasm_rust_sdk::require(preempt()) };

let bytes = finalize();
Expand Down
3 changes: 3 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ clean:
rm -rf ../ts/src/application/application.d.ts
rm -rf ../ts/src/application/application_bg.js
rm -rf ../ts/src/application/application_bg.wasm.d.ts

run:
node ../ts/src/run.js
34 changes: 24 additions & 10 deletions example/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ impl StorageData for PlayerData {
pub type HelloWorldPlayer = Player<PlayerData>;

#[derive(Serialize, Default)]
pub struct State {}
pub struct State {
tick: u64
}

pub struct SafeState(pub RefCell<State>);
unsafe impl Sync for SafeState {}
Expand All @@ -52,10 +54,14 @@ impl CommonState for State {
}

impl StorageData for State {
fn from_data(_u64data: &mut IterMut<u64>) -> Self {
State {}
fn from_data(u64data: &mut IterMut<u64>) -> Self {
State {
tick: *u64data.next().unwrap()
}
}
fn to_data(&self, data: &mut Vec<u64>) {
data.push(self.tick);
}
fn to_data(&self, _data: &mut Vec<u64>) {}
}

impl State {
Expand All @@ -64,21 +70,25 @@ impl State {
data
}
pub fn new() -> Self {
State {}
State {
tick: 0
}
}
pub fn preempt() -> bool {
return Self::get_global().tick % 5 == 0
}

pub fn store() {
unsafe { STATE.store() };
unsafe { Self::get_global().store() };
}
}

pub static mut STATE: State = State {};

pub struct Transaction {
pub command: u64,
pub data: Vec<u64>,
}

const TICK: u64 = 0;
const INSTALL_PLAYER: u64 = 1;
const INC_COUNTER: u64 = 2;

Expand Down Expand Up @@ -118,14 +128,18 @@ impl Transaction {
todo!()
}

pub fn process(&self, pkey: &[u64; 4], _rand: &[u64; 4]) -> u32 {
pub fn process(&self, pkey: &[u64; 4], _rand: &[u64; 4]) -> Vec<u64> {
let b = match self.command {
TICK => {
State::get_global_mut().tick += 1;
0
},
INSTALL_PLAYER => self.install_player(pkey),
INC_COUNTER => self.inc_counter(pkey),
_ => 0,
};
let kvpair = unsafe { &mut MERKLE_MAP.merkle.root };
zkwasm_rust_sdk::dbg!("root after process {:?}\n", kvpair);
b
vec![b as u64]
}
}
2 changes: 1 addition & 1 deletion ts/package-lock.json

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

4 changes: 2 additions & 2 deletions ts/src/application/application.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
/* eslint-disable */
/**
* @param {BigUint64Array} params
* @returns {number}
* @returns {BigUint64Array}
*/
export function handle_tx(params: BigUint64Array): number;
export function handle_tx(params: BigUint64Array): BigUint64Array;
/**
* @param {BigUint64Array} pid
* @returns {string}
Expand Down
38 changes: 24 additions & 14 deletions ts/src/application/application_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ function passArray64ToWasm0(arg, malloc) {
WASM_VECTOR_LEN = arg.length;
return ptr;
}
/**
* @param {BigUint64Array} params
* @returns {number}
*/
export function handle_tx(params) {
const ptr0 = passArray64ToWasm0(params, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.handle_tx(ptr0, len0);
return ret >>> 0;
}

let cachedInt32Memory0 = null;

Expand All @@ -41,6 +31,30 @@ function getInt32Memory0() {
return cachedInt32Memory0;
}

function getArrayU64FromWasm0(ptr, len) {
ptr = ptr >>> 0;
return getBigUint64Memory0().subarray(ptr / 8, ptr / 8 + len);
}
/**
* @param {BigUint64Array} params
* @returns {BigUint64Array}
*/
export function handle_tx(params) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray64ToWasm0(params, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.handle_tx(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v2 = getArrayU64FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 8, 8);
return v2;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
Expand Down Expand Up @@ -204,10 +218,6 @@ export function zkmain() {
wasm.zkmain();
}

function getArrayU64FromWasm0(ptr, len) {
ptr = ptr >>> 0;
return getBigUint64Memory0().subarray(ptr / 8, ptr / 8 + len);
}
/**
* @returns {BigUint64Array}
*/
Expand Down
Binary file modified ts/src/application/application_bg.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions ts/src/application/application_bg.wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function handle_tx(a: number, b: number): number;
export function handle_tx(a: number, b: number, c: number): void;
export function get_state(a: number, b: number, c: number): void;
export function snapshot(a: number): void;
export function decode_error(a: number, b: number): void;
Expand All @@ -15,6 +15,6 @@ export function zkmain(): void;
export function query_root(a: number): void;
export function verify_tx_signature(a: number, b: number): void;
export function test_merkle(): void;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_free(a: number, b: number, c: number): void;
20 changes: 15 additions & 5 deletions ts/src/application/application_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,25 @@ function passArray64ToWasm0(arg, malloc) {
WASM_VECTOR_LEN = arg.length;
return ptr;
}

/**
* @param {BigUint64Array} params
* @returns {number}
* @returns {BigUint64Array}
*/
export function handle_tx(params) {
const ptr0 = passArray64ToWasm0(params, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.handle_tx(ptr0, len0);
return ret >>> 0;
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray64ToWasm0(params, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.handle_tx(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v2 = getArrayU64FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 8, 8);
return v2;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

let cachedInt32Memory0 = null;
Expand Down
22 changes: 17 additions & 5 deletions ts/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,21 @@ export const jobSchema = new mongoose.Schema({

export const bundleSchema = new mongoose.Schema({
merkleRoot: {
type: String,
required: true,
unique: true,
type: String,
required: true,
unique: true,
},
preMerkleRoot: {
type: String,
default: '',
},
postMerkleRoot: {
type: String,
default: '',
},
taskId: {
type: String,
default: '',
type: String,
default: '',
},
withdrawArray: [{
address: { type: String, default:'' },
Expand All @@ -140,6 +148,10 @@ export const bundleSchema = new mongoose.Schema({
type: String,
default: '',
},
bundleIndex: {
type: Number,
default: 0,
}
});

export const randSchema = new mongoose.Schema({
Expand Down
2 changes: 1 addition & 1 deletion ts/src/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Service } from "./service.js";

const service = new Service(()=>{return;});
const service = new Service(()=>{return;}, ()=>{return;});
service.initialize();
service.serve();

Loading