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

Allow using a different serialization format for the initial state and the Rust WASM<->JS bridge #302

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"fast-copy": "^3.0.0",
"level": "^8.0.0",
"memory-level": "^1.0.0",
"msgpackr": "^1.8.1",
"redstone-isomorphic": "1.1.8",
"redstone-wasm-metering": "1.0.5",
"safe-stable-stringify": "2.4.1",
Expand All @@ -89,7 +90,7 @@
"arlocal": "1.1.42",
"cheerio": "^1.0.0-rc.10",
"colors": "^1.4.0",
"esbuild": "0.15.12",
"esbuild": "0.17.2",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This directory includes the build and the types of Pianity's ERC1155 WASM contract written in Rust.

The specific commit used to make this build is available here
<https://github.com/pianity/pianity-smartcontracts-next/tree/2f008ce5d032574ed4d0443ee5e22ef390365e29>.
It includes patches that makes it use Msgpack instead of JSON (that is the default at the time of
writing) as its serialization format for the WASM<->JS bridge.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "warp-erc1155-implementation",
"collaborators": [
"Eyal Chojnowski"
],
"version": "0.1.0",
"files": [
"rust-contract_bg.wasm",
"rust-contract.js",
"rust-contract.d.ts"
],
"main": "rust-contract.js",
"types": "rust-contract.d.ts"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* tslint:disable */
/* eslint-disable */
/**
* @param {Uint8Array} interaction
* @returns {Promise<Uint8Array | undefined>}
*/
export function handle(interaction: Uint8Array): Promise<Uint8Array | undefined>;
/**
* @param {Uint8Array} state
*/
export function initState(state: Uint8Array): void;
/**
* @returns {Uint8Array}
*/
export function currentState(): Uint8Array;
/**
* @returns {number}
*/
export function version(): number;
/**
* @returns {number}
*/
export function lang(): number;
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
let imports = {};
imports['__wbindgen_placeholder__'] = module.exports;
let wasm;
const { TextDecoder, TextEncoder } = require(`util`);

const heap = new Array(32).fill(undefined);

heap.push(undefined, null, true, false);

function getObject(idx) { return heap[idx]; }

let heap_next = heap.length;

function dropObject(idx) {
if (idx < 36) return;
heap[idx] = heap_next;
heap_next = idx;
}

function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}

let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();

let cachegetUint8Memory0 = null;
function getUint8Memory0() {
if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachegetUint8Memory0;
}

function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}

function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];

heap[idx] = obj;
return idx;
}

function makeMutClosure(arg0, arg1, dtor, f) {
const state = { a: arg0, b: arg1, cnt: 1, dtor };
const real = (...args) => {
// First up with a closure we increment the internal reference
// count. This ensures that the Rust closure environment won't
// be deallocated while we're invoking it.
state.cnt++;
const a = state.a;
state.a = 0;
try {
return f(a, state.b, ...args);
} finally {
if (--state.cnt === 0) {
wasm.__wbindgen_export_0.get(state.dtor)(a, state.b);

} else {
state.a = a;
}
}
};
real.original = state;

return real;
}
function __wbg_adapter_10(arg0, arg1, arg2) {
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hb281bc39dbcfb59f(arg0, arg1, addHeapObject(arg2));
}

/**
* @param {Uint8Array} interaction
* @returns {Promise<Uint8Array | undefined>}
*/
module.exports.handle = function(interaction) {
var ret = wasm.handle(addHeapObject(interaction));
return takeObject(ret);
};

/**
* @param {Uint8Array} state
*/
module.exports.initState = function(state) {
wasm.initState(addHeapObject(state));
};

let cachegetInt32Memory0 = null;
function getInt32Memory0() {
if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachegetInt32Memory0;
}

function getArrayU8FromWasm0(ptr, len) {
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
}
/**
* @returns {Uint8Array}
*/
module.exports.currentState = function() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.currentState(retptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v0 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v0;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
};

/**
* @returns {number}
*/
module.exports.version = function() {
var ret = wasm.version();
return ret;
};

/**
* @returns {number}
*/
module.exports.lang = function() {
var ret = wasm.lang();
return ret;
};

let WASM_VECTOR_LEN = 0;

let cachedTextEncoder = new TextEncoder('utf-8');

const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
? function (arg, view) {
return cachedTextEncoder.encodeInto(arg, view);
}
: function (arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length
};
});

function passStringToWasm0(arg, malloc, realloc) {

if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length);
getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}

let len = arg.length;
let ptr = malloc(len);

const mem = getUint8Memory0();

let offset = 0;

for (; offset < len; offset++) {
const code = arg.charCodeAt(offset);
if (code > 0x7F) break;
mem[ptr + offset] = code;
}

if (offset !== len) {
if (offset !== 0) {
arg = arg.slice(offset);
}
ptr = realloc(ptr, len, len = offset + arg.length * 3);
const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);

offset += ret.written;
}

WASM_VECTOR_LEN = offset;
return ptr;
}

function handleError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
}
function __wbg_adapter_24(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__he0b27a323ce8a8e6(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
}

module.exports.__wbindgen_object_drop_ref = function(arg0) {
takeObject(arg0);
};

module.exports.__wbg_owner_8557539ae4390681 = function(arg0) {
var ret = Transaction.owner();
var ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
};

module.exports.__wbg_caller_d4b16475811bcec3 = function(arg0) {
var ret = SmartWeave.caller();
var ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
};

module.exports.__wbg_id_9dc3f8f1cb3d8f46 = function(arg0) {
var ret = Transaction.id();
var ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
};

module.exports.__wbindgen_cb_drop = function(arg0) {
const obj = takeObject(arg0).original;
if (obj.cnt-- == 1) {
obj.a = 0;
return true;
}
var ret = false;
return ret;
};

module.exports.__wbg_call_94697a95cb7e239c = function() { return handleError(function (arg0, arg1, arg2) {
var ret = getObject(arg0).call(getObject(arg1), getObject(arg2));
return addHeapObject(ret);
}, arguments) };

module.exports.__wbg_new_4beacc9c71572250 = function(arg0, arg1) {
try {
var state0 = {a: arg0, b: arg1};
var cb0 = (arg0, arg1) => {
const a = state0.a;
state0.a = 0;
try {
return __wbg_adapter_24(a, state0.b, arg0, arg1);
} finally {
state0.a = a;
}
};
var ret = new Promise(cb0);
return addHeapObject(ret);
} finally {
state0.a = state0.b = 0;
}
};

module.exports.__wbg_resolve_4f8f547f26b30b27 = function(arg0) {
var ret = Promise.resolve(getObject(arg0));
return addHeapObject(ret);
};

module.exports.__wbg_then_a6860c82b90816ca = function(arg0, arg1) {
var ret = getObject(arg0).then(getObject(arg1));
return addHeapObject(ret);
};

module.exports.__wbg_buffer_5e74a88a1424a2e0 = function(arg0) {
var ret = getObject(arg0).buffer;
return addHeapObject(ret);
};

module.exports.__wbg_newwithbyteoffsetandlength_278ec7532799393a = function(arg0, arg1, arg2) {
var ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
return addHeapObject(ret);
};

module.exports.__wbg_new_e3b800e570795b3c = function(arg0) {
var ret = new Uint8Array(getObject(arg0));
return addHeapObject(ret);
};

module.exports.__wbg_set_5b8081e9d002f0df = function(arg0, arg1, arg2) {
getObject(arg0).set(getObject(arg1), arg2 >>> 0);
};

module.exports.__wbg_length_30803400a8f15c59 = function(arg0) {
var ret = getObject(arg0).length;
return ret;
};

module.exports.__wbindgen_throw = function(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};

module.exports.__wbindgen_memory = function() {
var ret = wasm.memory;
return addHeapObject(ret);
};

module.exports.__wbindgen_closure_wrapper336 = function(arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 92, __wbg_adapter_10);
return addHeapObject(ret);
};

const path = require('path').join(__dirname, 'rust-contract_bg.wasm');
const bytes = require('fs').readFileSync(path);

const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
wasm = wasmInstance.exports;
module.exports.__wasm = wasm;

Binary file not shown.
Loading