godprotocol 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,25 +5,355 @@ class Virtual_machine extends Callable {
5
5
  super();
6
6
 
7
7
  this.account = account;
8
+ this.contexts = new Array();
8
9
  }
9
10
 
11
+ read_config = async (addr, options = {}) => {
12
+ if (typeof addr !== "string") {
13
+ return addr;
14
+ }
15
+ let oracle = this.account.manager.oracle;
16
+
17
+ let result = await oracle.read(addr, { current: true, config: true });
18
+
19
+ return result;
20
+ };
21
+
10
22
  sync = async () => {};
11
23
 
12
- execute = async (instruction) => {
13
- let { opcode, payload } = this.parse_instruction(instruction);
24
+ errors = ['MathError', 'ZeroDivision', 'RuntimeError']
25
+
26
+ handle_error = async(errline, {config, chain})=>{
27
+ let {objects} = config;
28
+ for (let o=0; o< objects.length; o++){
29
+ let err = objects[o], caught, line = errline;
30
+
31
+ if (line.value.type === 'variable'){
32
+ if (this.errors.includes(line.value.value)){
33
+ line = {value: {name: line.value.value}}
34
+ }else {
35
+ let res= await this.execute(line.value, {chain, caught: true})
36
+ if (res.type === 'address'){
37
+ res = await this.resolve_address(res.value)
38
+ }
39
+ line = res;
40
+ }
41
+ }else if(typeof line.value === 'string' || line.value.type){
42
+ let res = await this.execute(line.value, {chain, caught: true, cloth:true})
43
+ line = res;
44
+ }
45
+
46
+ if(line){
47
+ if (line.type === 'instance'){
48
+ let classifier = await line.get('__classifier__')
49
+
50
+ caught = err === classifier
51
+ if (config.alias){
52
+ let conf = await this.read_config(config.alias);
53
+ let hash = await this.account.manager.oracle.hash(conf);
54
+ await this.execute({...conf, value: {type: 'address', value: line.value.path}}, {chain, hash})
55
+ }
56
+
57
+
58
+ }else if(!line.type){
59
+ caught = err.endsWith(line.value.name)
60
+ }
61
+ }
62
+
63
+ if(caught){
64
+ await this.execute_body(config.catch_body, {chain, config: {...config, caught: true}})
65
+ return true;
66
+ }
67
+ }
68
+ }
69
+
70
+ execute_body = async (body, options)=>{
71
+ let {config, chain} = options;
72
+ let result;
73
+ for (let b=0; b< body.length; b++){
74
+ let line = body[b];
75
+
76
+ line = await this.execute(line, {...options, line});
77
+
78
+ if (line){
79
+ if (line.interrupt_loop){
80
+ result =line
81
+ break;
82
+ }
83
+ else if (line.done){
84
+ result = line.value;
85
+ break;
86
+ }else if (line.exit){
87
+ result = line;
88
+ break
89
+ }else if (line.error){
90
+ if (config.type === 'trycatch'){
91
+ let res = !config.caught && await this.handle_error(line, {chain, config})
92
+
93
+ if(!res){
94
+ result = line
95
+ }
96
+ break
97
+ }else {
98
+ result = line;
99
+ break
100
+ }
101
+ }
102
+ result = line
103
+ }
104
+ }
105
+
106
+ return result;
107
+ }
14
108
 
15
- let callable = await this.callable(opcode);
109
+ execute = async (config, options = {}) => {
110
+ let prev_config = config
111
+ config = await this.read_config(config);
112
+ // console.log(config, 'EXE')
113
+
114
+ let { chain, cloth, hash, obj, call_config } = options,
115
+ result;
116
+ hash = hash || (await this.account.manager.oracle.hash(config));
117
+
118
+ switch (config.type) {
119
+ case "module":
120
+ await this.execute_body(config.body, {chain, config})
121
+
122
+ break;
123
+ case "class":
124
+ if (call_config) {
125
+ let instance_chain = await chain.chain(call_config.location);
126
+ let object = {
127
+ content: `[${config.name}]`,
128
+ __classifier__: config.address,
129
+ uid: this.gen_uid(),
130
+ };
131
+ for (let name in config.methods) {
132
+ let addr = config.methods[name];
133
+ object[name] = addr;
134
+ }
135
+ let res = await instance_chain.write(object, { is_instance: true });
136
+ result = { type: "address", value: res };
137
+
138
+ if (object.__init__) {
139
+ await this.execute(
140
+ { ...call_config, object: result, identifier: object.__init__ },
141
+ { chain }
142
+ );
143
+ }
144
+ }
145
+ break;
146
+ case "function":
147
+ if (call_config) {
148
+ let fn_chain = await chain.chain(config.address);
149
+ let params = new Array();
150
+ for (let a = 0; a < call_config.arguments.length; a++) {
151
+ let arg = call_config.arguments[a];
152
+
153
+ for (let p = 0; p < config.parameters.length; p++) {
154
+ let param = await this.read_config(config.parameters[p]);
155
+
156
+ if (arg.identifier) {
157
+ } else {
158
+ if (p === arg.position) {
159
+ param.hash = await this.account.manager.oracle.hash(param);
160
+ param.value = arg;
161
+ params.push(param);
162
+ }
163
+ }
164
+ }
165
+ }
166
+ for (let p = 0; p < params.length; p++) {
167
+ let param = params[p];
168
+
169
+ await this.execute(param, { chain: fn_chain, hash: param.hash });
170
+ }
171
+ if (call_config.object) {
172
+ let conf = {
173
+ type: "assignment",
174
+ identifier: "this",
175
+ value: call_config.object,
176
+ };
177
+ let r = await fn_chain.add_config(conf);
178
+
179
+ await this.execute(r, { chain: fn_chain });
180
+ }
181
+
182
+ result = await this.execute_body(config.body, {chain: fn_chain, config})
183
+ }else result = {type:'address', value: prev_config}
184
+
185
+ break;
186
+ case "return":
187
+ let ret_value = await this.execute(config.output, { chain });
188
+
189
+ result = { done: true, value: ret_value };
190
+ break;
191
+ case "assignment":
192
+ let ass_chain = await chain.chain(config.identifier);
193
+ let value = await this.execute(config.value, { chain: ass_chain });
194
+
195
+ if (value && value.error){
196
+ result = value
197
+ }else {
198
+ if (value && value.account) {
199
+ value = { type: "address", value: value.value.path };
200
+ } else if (typeof value === "string")
201
+ value = { type: "address", value };
202
+ result = await ass_chain.write(value, { hash });
203
+ }
204
+
205
+ break;
206
+ case "call":
207
+ if (config.identifier.type === "nest") {
208
+ config.identifier = await this.execute(config.identifier.address, {
209
+ chain,
210
+ });
211
+ }
212
+ for (let a = 0; a < config.arguments.length; a++) {
213
+ let arg = await this.read_config(config.arguments[a]);
214
+
215
+ let obj = await this.execute(
216
+ arg.type === "assignment" ? arg.value : arg,
217
+ { chain }
218
+ );
219
+ if (obj.account) {
220
+ obj = { type: "address", value: obj.value.path };
221
+ }
222
+ if (arg.type === "assignment") {
223
+ obj.identifier = arg.identifier;
224
+ }
225
+ obj.position = a;
226
+ config.arguments[a] = obj;
227
+ }
228
+
229
+ result = await this.execute_call(config, { chain , obj});
230
+
231
+ break;
232
+ case 'loop':
233
+ let {loop_type } = config;
234
+ if (loop_type === 'for'){
235
+ await this.execute(config.iterator, {chain})
236
+ }
237
+ let cond = await this.execute(config.condition, {chain , cloth: true})
238
+ while(await cond.literal()){
239
+ result = await this.execute_body(config.body, {config, chain})
240
+
241
+ if (result && result.interrupt_loop){
242
+ if (result.interrupt_loop === 'break'){
243
+ result = null
244
+ break;
245
+ } else{
246
+ result = null
247
+ }
248
+ }
249
+ if(loop_type === 'for'){
250
+ await this.execute(config.update, {chain})
251
+ }
252
+ cond = await this.execute(config.condition, {chain , cloth: true})
253
+ }
254
+
255
+ break
256
+ case 'trycatch':
257
+ result = await this.execute_body(config.try_body, {chain, config})
258
+ break;
259
+ case 'raise':
260
+ result = {error: true, value: config.object}
261
+ break;
262
+ case "condition":
263
+ for (let i = 0; i < config.blocks.length; i++) {
264
+ let block = config.blocks[i];
265
+ let pred = await this.execute(block.predicate, {
266
+ chain,
267
+ cloth: true,
268
+ });
269
+ if (await pred.literal()) {
270
+ result = await this.execute_body(block.body, {chain, config})
271
+
272
+ break;
273
+ }
274
+ }
275
+ break;
276
+ case "nest":
277
+ let prev = await this.execute(config.nests[0], { chain, cloth: true }),
278
+ pprev;
279
+
280
+ for (
281
+ let n = 1;
282
+ n < config.nests.length - (config.assignment ? 1 : 0);
283
+ n++
284
+ ) {
285
+ let nst = await this.read_config(config.nests[n]);
286
+ pprev = prev;
287
+
288
+ if (nst.type === "call") {
289
+ let conf =prev.type === 'function'? prev.value: await prev.get(nst.identifier, {obj: prev, config: nst, chain});
290
+ if (!conf) await this.throw_error();
291
+ nst.object = { value: prev.value.path, type: "address" };
292
+ conf.prev_name = nst.identifier
293
+ prev = await this.execute(
294
+ { ...nst, identifier: conf.type ==='function'?conf.address:conf },
295
+ { chain, cloth: true, obj:prev }
296
+ );
297
+ if(prev && prev.error){
298
+ break;
299
+ }
300
+ } else {
301
+ let res = await this.execute(nst, { chain, cloth: true });
302
+ let prop = await res.literal();
303
+ prev = await prev.get(prop, {obj: res, config: nst, chain});
304
+ }
305
+ }
306
+ if (config.assignment) {
307
+ let ass = await this.execute(config.assignment, { chain });
308
+ let last = config.nests.slice(-1)[0];
309
+ last = await this.execute(last, { chain, cloth: true });
310
+
311
+ await prev.set(last, ass);
312
+ }
313
+
314
+ if (prev && prev.type !== "address") {
315
+ if(!prev.error)
316
+ prev = {
317
+ value: prev.value ? prev.value.path : prev,
318
+ type: "address",
319
+ object: prev.value ? null : pprev.value.path,
320
+ };
321
+ }
322
+ result = prev;
323
+ break;
324
+ default:
325
+ if (["number", "string", "boolean", 'twain', 'void', 'array'].includes(config.type)) {
326
+ result = await this.instantiate(config, { chain, hash });
327
+ result = { type: "address", value: result };
328
+ } else if (config.type === "address") {
329
+ result = config;
330
+ } else if (config.type === "variable") {
331
+ let var_chain = await chain.chain(config.location);
332
+ let res = await var_chain.read({ sibling: true });
333
+ if (!res) {
334
+ res = await this.account.manager.oracle.fs.read(
335
+ `${var_chain.path}/.config`
336
+ );
337
+ res = { type: "address", value: res.current };
338
+ }
339
+ result = res;
340
+ } else if (config.type === "reference") {
341
+ let oracle = this.account.manager.oracle;
342
+ let i_chain = await chain.chain(config.value);
343
+ let conf = await oracle.fs.read(`${i_chain.path}/.config`);
344
+ result = { type: "address", value: conf.current };
345
+ }else if (['continue', 'break'].includes(config.type)){
346
+ result = {interrupt_loop: config.type}
347
+ }
348
+ }
16
349
 
17
- if (!callable) {
18
- return await this.stderr({
19
- name: "runtime_error",
20
- message: `opcode:${opcode} is not set.`,
21
- });
350
+ if (result && cloth) {
351
+ if ( result.type === "address" && result.value.includes('/')) {
352
+ result = await this.resolve_address(result.value);
353
+ }
354
+ if (result && !result.error) result = await this.cloth_content(result);
22
355
  }
23
- let call = callable.callable;
24
- let args = await this.parse_arguments(payload, callable.config);
25
356
 
26
- let result = await call(args);
27
357
  return result;
28
358
  };
29
359
  }
@@ -1,3 +1,17 @@
1
- const display = async (args) => {};
1
+ const display = async (args) => {
2
+ let {data}= args;
3
+
4
+ if(!Array.isArray(data)) data = [data]
5
+
6
+ let lit= new Array()
7
+ for(let d =0; d < data.length; d++){
8
+ let tum = data[d]
9
+ lit.push(await tum.literal())
10
+ }
11
+
12
+ console.log(...lit)
13
+
14
+ return 0
15
+ };
2
16
 
3
17
  export default display;
@@ -0,0 +1,10 @@
1
+ const quit = async(args, vm)=>{
2
+ let {code} = args;
3
+ if (code) code = await code.literal()
4
+
5
+ console.log(`Program exit with code:${code ||0}`)
6
+
7
+ return {exit: true, code}
8
+ }
9
+
10
+ export default quit
@@ -0,0 +1,27 @@
1
+ import { post_request } from "../../utils/services";
2
+
3
+ const net = async(args, vm, options={})=>{
4
+ let {chain, config} = options;
5
+ let {server, path, payload, callback} = args;
6
+
7
+ server = await server.literal()
8
+ path = await path.literal()
9
+ if (!path.startsWith('/')) path = `/${path}`
10
+
11
+ payload = payload && await payload.literal()
12
+
13
+ let result;
14
+ if(callback){
15
+
16
+ }else {
17
+ result = await post_request({options: {hostname: server.ip, port: server.port, path}, data: payload})
18
+
19
+ result = await vm.parse_dynamic(result, {chain, config})
20
+ }
21
+
22
+
23
+ return result;
24
+ }
25
+
26
+
27
+ export default net;
@@ -0,0 +1,11 @@
1
+ const twain = async(args, vm)=>{
2
+ if (!args.instance){
3
+ return await vm.stderr({name: 'Name_error'})
4
+ }
5
+ let instance_object = args.instance;
6
+
7
+ instance_object.is_literal = true;
8
+ return instance_object
9
+ }
10
+
11
+ export default twain;
@@ -1,20 +1,36 @@
1
- import assign from "./functions/assign";
1
+ import twain from "./functions/twain";
2
2
  import display from "./functions/display";
3
+ import net from "godprotocol/callables/functions/net";
4
+ import quit from "godprotocol/callables/functions/exit";
3
5
 
4
6
  const register = async (account) => {
5
7
  let set = account.vm.set;
6
8
 
7
- set("assign", {
8
- callable: assign,
9
+ set("display", {
10
+ callable: display,
9
11
  config: {
10
- parameters: [{ name: "assignment", position: 0 }],
12
+ parameters: [{ name: "data", position: 0, spread: true }],
11
13
  },
12
14
  });
13
15
 
14
- set("display", {
15
- callable: display,
16
+ set("Twain", {
17
+ callable: twain,
16
18
  config: {
17
- parameters: [{ name: "data", position: 0, spread: true }],
19
+ parameters: [{ name: "instance", position: 0 }],
20
+ },
21
+ });
22
+
23
+ set("net", {
24
+ callable: net,
25
+ config: {
26
+ parameters: [{ name: "server", position: 0 }, { name: "path", position: 0 }, { name: "payload", position: 0 }],
27
+ },
28
+ });
29
+
30
+ set("exit", {
31
+ callable: quit,
32
+ config: {
33
+ parameters: [{ name: "code", position: 0 }],
18
34
  },
19
35
  });
20
36
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "A distributed computing environment",
5
5
  "main": "Index.js",
6
6
  "scripts": {
@@ -6,22 +6,36 @@ let PORT = Number(process.env.PORT) || 1408;
6
6
  const handle_routes = (req, res, manager, app) => {
7
7
  let data = "";
8
8
 
9
+
9
10
  req.on("data", (chunk) => {
10
11
  data += chunk;
11
12
  });
12
13
 
13
14
  req.on("end", async () => {
14
- if (req.url === "/sync") {
15
- data = JSON.parse(data);
16
-
17
- let response = await manager.oracle.add_server(data);
18
-
19
- console.log(response, "hello");
20
- res.end(JSON.stringify(response));
15
+ data = JSON.parse(data)
16
+ let result;
17
+ if (req.url === '/create_account'){
18
+ result = await manager.add_account(data.name, data.options)
19
+ res.end(JSON.stringify(result.stringify()))
20
+ }else if(req.url === '/load'){
21
+ let account = await manager.get_account(data.account);
22
+ result = await account.load(data)
23
+ res.end(JSON.stringify(result))
24
+ }else if(req.url === '/run'){
25
+ let account = await manager.get_account(data.account);
26
+ result = await account.run(data)
27
+
28
+ res.end(result? JSON.stringify(result): JSON.stringify({done: true}))
29
+ }else if(req.url === '/parse'){
30
+ let account = await manager.get_account(data.account);
31
+ result = await account.parse(data)
32
+
33
+ res.end(result? JSON.stringify(result): JSON.stringify({done: true}))
21
34
  }
22
35
  });
23
36
 
24
37
  req.on("error", (e) => {
38
+ console.log('hm')
25
39
  // res.writeHead(500, { "Content-Type": "application/json" });
26
40
  res.end(JSON.stringify({ status: "error", error_message: e.message }));
27
41
  });
@@ -33,7 +47,7 @@ const create_server = async (settings) => {
33
47
  let { port, app, manager } = settings;
34
48
 
35
49
  port = port || PORT;
36
-
50
+
37
51
  let server = https.createServer(
38
52
  {
39
53
  key: fs.readFileSync(`${__dirname}/server.key`),
package/utils/hash.js ADDED
@@ -0,0 +1,18 @@
1
+ const crypto = require("crypto");
2
+
3
+ const hash = (data, alg) => {
4
+ let hash = crypto.createHash(alg || "sha256");
5
+
6
+ hash.update(data);
7
+
8
+ let result = hash.digest("hex");
9
+
10
+ if (alg){
11
+ if (!isNaN(Number(result[0])))
12
+ result = `_${result.slice(0, -1)}`;
13
+ }
14
+
15
+ return result;
16
+ };
17
+
18
+ export { hash };
package/utils/ops.js ADDED
@@ -0,0 +1,17 @@
1
+ let ops = [
2
+ { name: "add", sign: "+" },
3
+ { name: "mul", sign: "*" },
4
+ { name: "sub", sign: "-" },
5
+ { name: "div", sign: "/" },
6
+ { name: "mod", sign: "%" },
7
+ { name: "floordiv", sign: "#" },
8
+ { name: "exp", sign: "**" },
9
+ { name: "eq", sign: "==" },
10
+ { name: "ne", sign: "!=" },
11
+ { name: "gt", sign: ">" },
12
+ { name: "lt", sign: "<" },
13
+ { name: "lte", sign: "<=" },
14
+ { name: "gte", sign: ">=" },
15
+ ];
16
+
17
+ export { ops };
package/utils/services.js CHANGED
@@ -50,6 +50,7 @@ const post_request = (payload, cb) => {
50
50
  typeof payload.data !== "string"
51
51
  ? JSON.stringify(payload.data)
52
52
  : payload.data;
53
+ console.log('uh')
53
54
 
54
55
  let req = http.request(
55
56
  {
@@ -71,6 +72,7 @@ const post_request = (payload, cb) => {
71
72
  });
72
73
 
73
74
  res.on("end", () => {
75
+ console.log('hey')
74
76
  resolve(JSON.parse(data));
75
77
  // console.log("Response:", data);
76
78
  });