Skip to content

Commit

Permalink
feat: add constractHelper and gen_cid method
Browse files Browse the repository at this point in the history
  • Loading branch information
eng-cc committed Jul 20, 2023
1 parent 65393fb commit 830cac8
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 52 deletions.
3 changes: 3 additions & 0 deletions packages/contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"gen-proto": "protoc --ts_out ./src/proto_ts --proto_path ../skvm/proto ../skvm/proto/*.proto",
"build": "tsc -p ./tsconfig.build.json"
},
"engines": {
"node": ">=16.0.0"
},
"keywords": [],
"author": "",
"license": "ISC",
Expand Down
10 changes: 10 additions & 0 deletions packages/contract/src/contract.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare interface SkUtilsI {
name: 'sk_utils';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
log(arg: any): void;
storage: string;
save_storage(data: string): void;
gen_cid(data: string | number): string;
}

declare const __sk_utils__: SkUtilsI;
26 changes: 26 additions & 0 deletions packages/contract/src/contractHelper.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).__sk_utils__ = {};

export type ContractFuncReruen<T> = Promise<{
origin: T;
trans: string;
}>;

export class SliceDb<T> {
db: Map<string, T> = new Map();

get = this.db.get;
set = this.db.set;
has = this.db.has;
delete = this.db.delete;
}

export const createSliceDb: <T>() => SliceDb<T> = () => {
return new SliceDb();
};

export const hash = (str: string): string => {
return __sk_utils__.gen_cid(str);
};

export const log = __sk_utils__.log;
1 change: 1 addition & 0 deletions packages/contract/src/index.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from '@trustack/contract_builder';
export * as constractHelper from './contractHelper.mjs';

import { evaluate as evalFn } from '@trustack/vm';
import { EvalResult } from './proto_ts/eval_result.js';
Expand Down
1 change: 1 addition & 0 deletions packages/land_app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@math.gl/core": "^3.6.3",
"@math.gl/web-mercator": "^3.6.3",
"@trustack/common": "workspace:^1.0.0",
"@trustack/contract": "workspace:^1.0.0",
"@trustack/node-modules-polyfill": "^0.2.5",
"@trustack/rollup-plugin-node-polyfills": "^0.2.2",
"@xstate/react": "^3.2.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/land_app/src/pages/mainWindow/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export class MapAction {
});
this.map = map;
await Promise.all([this.mapLoad(), preLoadMapSource(this.map)]);
const deck = new MapboxOverlay({ id: 'deckgl' });
this.map.addControl(deck);
this.deckMap = new MapboxOverlay({ id: 'deckgl' });
this.map.addControl(this.deckMap);
this.inited = true;
this.center = this.map.getCenter();
// this.getinggGridList = true;
Expand Down Expand Up @@ -87,7 +87,7 @@ export class MapAction {
],
},
});
this.deckLayers.push(layer);
this.deckLayers = [layer];
this.deckMap.setProps({ layers: this.deckLayers });
// mapStateService.send(MapEventType.UPDATE_GRID, {
// data: { showGridDetail: true, activeHex: hex },
Expand Down
1 change: 1 addition & 0 deletions packages/land_app/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="vite/client" />
/// <reference types="@trustack/common/config" />
/// <reference types="@trustack/contract/src/contract" />
declare const __privateConfigs__: PrivateConfigs;
45 changes: 0 additions & 45 deletions packages/skchain/src/contractHelper.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,8 @@
import type { Address } from './mate/address.js';
// import type { Transaction } from './mate/transaction.js';

// export type SliceKeyType = 'base58' | 'base32';

// export namespace ConstractHelper {
// export type SliceDb<T> = {
// get: (key: string) => T;
// has: (key: string) => boolean;
// delete: (key: string) => void;
// set: (key: string, value: T) => void;
// };
// // export type ContractResultItem = CRI;
// export type ContractFuncReruen<T> = Promise<{
// origin: T;
// trans: Transaction;
// }>;
// }

// class SliceDb<T> implements ConstractHelper.SliceDb<T> {
// constructor(keyType: SliceKeyType) {
// this.keyType = keyType;
// }
// // TODO 用keyType,用来做分片存储
// keyType: SliceKeyType;
// db: Map<string, T> = new Map();

// get = this.db.get as ConstractHelper.SliceDb<T>['get'];
// set = this.db.set;
// has = this.db.has;
// delete = this.db.delete;
// }

// const createSliceDb = <T>(keyType: SliceKeyType): SliceDb<T> => {
// return new SliceDb<T>(keyType);
// };

// const hash = (_str: string): void => {
// // return sk.genCidString(str);
// };

export class BaseContract {
msg = {
sender: {} as Address,
ts: 0,
};
}
// export const constractHelper = {
// createSliceDb,
// hash,
// // eslint-disable-next-line no-console
// log: console.log,
// };
1 change: 1 addition & 0 deletions packages/skvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ boa_engine = { path = "./skboa/boa_engine" }
wasm-bindgen-test = "0.3.13"
js-sys = "0.3.61"
protobuf = "3.2.0"
cid = '0.10'

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
Expand Down
4 changes: 2 additions & 2 deletions packages/skvm/pkg/node/skvm_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,8 @@ export function __wbindgen_memory() {
return addHeapObject(ret);
};

export function __wbindgen_closure_wrapper5044(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 1371, __wbg_adapter_28);
export function __wbindgen_closure_wrapper5271(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 1395, __wbg_adapter_28);
return addHeapObject(ret);
};

Binary file modified packages/skvm/pkg/node/skvm_bg.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions packages/skvm/pkg/web/skvm.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,8 @@ function __wbg_get_imports() {
const ret = wasm.memory;
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper5044 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 1371, __wbg_adapter_28);
imports.wbg.__wbindgen_closure_wrapper5271 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 1395, __wbg_adapter_28);
return addHeapObject(ret);
};

Expand Down
Binary file modified packages/skvm/pkg/web/skvm_bg.wasm
Binary file not shown.
23 changes: 23 additions & 0 deletions packages/skvm/src/sk/window_sk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,28 @@ use boa_engine::{
};
use js_sys::Uint8Array;
use wasm_bindgen_test::__rt::js_console_log;
use cid::multihash::{Code, MultihashDigest};
use cid::Cid;

use crate::SAVED_STORAGE;
// use wasm_bindgen::prelude::*;

const RAW: u64 = 0x55;

// generate cid hash for string
pub(crate) fn gen_cid_raw(_: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
let text = args[0]
.to_string(context)
.expect("gen_hash: get arg error")
.to_std_string()
.unwrap();
let h = Code::Sha2_256.digest(text.as_bytes());

let cid = Cid::new_v1(RAW, h);
let cid_string = cid.to_string();
Ok(JsValue::from(cid_string))
}

pub(crate) fn log(_: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
let text = args[0]
.to_string(context)
Expand Down Expand Up @@ -57,6 +75,11 @@ pub(crate) fn init_sk(context: &mut Context<'_>, storage: Uint8Array) -> Result<
FunctionBinding::from("save_storage"),
1,
)
.function(
NativeFunction::from_fn_ptr(gen_cid_raw),
FunctionBinding::from("gen_cid"),
1,
)
.build();

context.register_global_property("__sk_utils__", obj, attribute).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 830cac8

Please sign in to comment.