godprotocol 1.1.2 → 1.1.31

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,374 @@ 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
+ }
108
+
109
+ execute = async (config, options = {}) => {
110
+ let prev_config = config
111
+ config = await this.read_config(config);
112
+ // console.log(config, 'EXE')
14
113
 
15
- let callable = await this.callable(opcode);
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
+ result = 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 'adm':
147
+ let props = await this.execute(config.props, {chain})
148
+ if(config.name.includes('/')){
149
+ result = await this.execute({
150
+ type:'call',
151
+ identifier: config.name,
152
+ arguments: [{...props, position: 0}],
153
+ location: config.location
154
+ }, {chain})
155
+ }else {
156
+ result = await this.native_components(config, chain)
157
+ result = await this.parse_aircode(result, {config, chain})
158
+ }
159
+ break
160
+ case "function":
161
+ if (call_config) {
162
+ let fn_chain = await chain.chain(config.address);
163
+ let params = new Array();
164
+ for (let a = 0; a < call_config.arguments.length; a++) {
165
+ let arg = call_config.arguments[a];
166
+
167
+ for (let p = 0; p < config.parameters.length; p++) {
168
+ let param = await this.read_config(config.parameters[p]);
169
+
170
+ if (arg.identifier) {
171
+ } else {
172
+ if (p === arg.position) {
173
+ param.hash = await this.account.manager.oracle.hash(param);
174
+ param.value = arg;
175
+ params.push(param);
176
+ }
177
+ }
178
+ }
179
+ }
180
+ for (let p = 0; p < params.length; p++) {
181
+ let param = params[p];
182
+
183
+ await this.execute(param, { chain: fn_chain, hash: param.hash });
184
+ }
185
+ if (call_config.object) {
186
+ let conf = {
187
+ type: "assignment",
188
+ identifier: "this",
189
+ value: call_config.object,
190
+ };
191
+ let r = await fn_chain.add_config(conf);
192
+
193
+ await this.execute(r, { chain: fn_chain });
194
+ }
195
+
196
+ result = await this.execute_body(config.body, {chain: fn_chain, config})
197
+ }else result = {type:'address', value: prev_config}
198
+
199
+ break;
200
+ case "return":
201
+ let ret_value = await this.execute(config.output, { chain });
202
+
203
+ result = { done: true, value: ret_value };
204
+ break;
205
+ case "assignment":
206
+ let ass_chain = await chain.chain(config.identifier);
207
+ let value = await this.execute(config.value, { chain: ass_chain });
208
+
209
+ if (value && value.error){
210
+ result = value
211
+ }else {
212
+ if (value && value.account) {
213
+ value = { type: "address", value: value.value.path };
214
+ } else if (typeof value === "string")
215
+ value = { type: "address", value };
216
+ result = await ass_chain.write(value, { hash });
217
+ }
218
+
219
+ break;
220
+ case "call":
221
+ if (config.identifier.type === "nest") {
222
+ config.identifier = await this.execute(config.identifier.address, {
223
+ chain,
224
+ });
225
+ }
226
+ for (let a = 0; a < config.arguments.length; a++) {
227
+ let arg = await this.read_config(config.arguments[a]);
228
+
229
+ let obj = await this.execute(
230
+ arg.type === "assignment" ? arg.value : arg,
231
+ { chain }
232
+ );
233
+ if (obj.account) {
234
+ obj = { type: "address", value: obj.value.path };
235
+ }
236
+ if (arg.type === "assignment") {
237
+ obj.identifier = arg.identifier;
238
+ }
239
+ obj.position = a;
240
+ config.arguments[a] = obj;
241
+ }
242
+
243
+ result = await this.execute_call(config, { chain , obj});
244
+
245
+ break;
246
+ case 'loop':
247
+ let {loop_type } = config;
248
+ if (loop_type === 'for'){
249
+ await this.execute(config.iterator, {chain})
250
+ }
251
+ let cond = await this.execute(config.condition, {chain , cloth: true})
252
+ while(await cond.literal()){
253
+ result = await this.execute_body(config.body, {config, chain})
254
+
255
+ if (result && result.interrupt_loop){
256
+ if (result.interrupt_loop === 'break'){
257
+ result = null
258
+ break;
259
+ } else{
260
+ result = null
261
+ }
262
+ }
263
+ if(loop_type === 'for'){
264
+ await this.execute(config.update, {chain})
265
+ }
266
+ cond = await this.execute(config.condition, {chain , cloth: true})
267
+ }
268
+
269
+ break
270
+ case 'trycatch':
271
+ result = await this.execute_body(config.try_body, {chain, config})
272
+ break;
273
+ case 'raise':
274
+ result = {error: true, value: config.object}
275
+ break;
276
+ case "condition":
277
+ for (let i = 0; i < config.blocks.length; i++) {
278
+ let block = config.blocks[i];
279
+ let pred = await this.execute(block.predicate, {
280
+ chain,
281
+ cloth: true,
282
+ });
283
+ if (await pred.literal()) {
284
+ result = await this.execute_body(block.body, {chain, config})
285
+
286
+ break;
287
+ }
288
+ }
289
+ break;
290
+ case "nest":
291
+ let prev = await this.execute(config.nests[0], { chain, cloth: true }),
292
+ pprev;
293
+
294
+ for (
295
+ let n = 1;
296
+ n < config.nests.length - (config.assignment ? 1 : 0);
297
+ n++
298
+ ) {
299
+ let nst = await this.read_config(config.nests[n]);
300
+ pprev = prev;
301
+
302
+ if (nst.type === "call") {
303
+ let conf =prev.type === 'function'? prev.value: await prev.get(nst.identifier, {obj: prev, config: nst, chain});
304
+ if (!conf) await this.throw_error();
305
+ nst.object = { value: prev.value.path, type: "address" };
306
+ conf.prev_name = nst.identifier
307
+ prev = await this.execute(
308
+ { ...nst, identifier: conf.type ==='function'?conf.address:conf },
309
+ { chain, cloth: true, obj:prev }
310
+ );
311
+ if(prev && prev.error){
312
+ break;
313
+ }
314
+ } else {
315
+ let res = await this.execute(nst, { chain, cloth: true });
316
+ let prop = await res.literal();
317
+
318
+ if(prev.type === 'address'){
319
+ let conf = await this.resolve_address(prev.value)
320
+ prev = await this.cloth_content(conf)
321
+ }
322
+ prev = await prev.get(prop, {obj: res, config: nst, chain});
323
+ }
324
+ }
325
+ if (config.assignment) {
326
+ let ass = await this.execute(config.assignment, { chain });
327
+ let last = config.nests.slice(-1)[0];
328
+ last = await this.execute(last, { chain, cloth: true });
329
+
330
+ await prev.set(last, ass);
331
+ }
332
+
333
+ if (prev && prev.type !== "address") {
334
+ if(!prev.error)
335
+ prev = {
336
+ value: prev.value ? prev.value.path : prev,
337
+ type: "address",
338
+ object: prev.value ? null : pprev.value.path,
339
+ };
340
+ }
341
+ result = prev;
342
+ break;
343
+ default:
344
+ if (["number", "string", "boolean", 'twain', 'void', 'array'].includes(config.type)) {
345
+ result = await this.instantiate(config, { chain, hash });
346
+ result = { type: "address", value: result };
347
+ } else if (config.type === "address") {
348
+ result = config;
349
+ } else if (config.type === "variable") {
350
+ let var_chain = await chain.chain(config.location);
351
+ let res = await var_chain.read({ sibling: true });
352
+ if (!res) {
353
+ res = await this.account.manager.oracle.fs.read(
354
+ ${var_chain.path}/.config
355
+ );
356
+ res = { type: "address", value: res.current };
357
+ }
358
+ result = res;
359
+ } else if (config.type === "reference") {
360
+ let oracle = this.account.manager.oracle;
361
+ let i_chain = await chain.chain(config.value);
362
+ let conf = await oracle.fs.read(${i_chain.path}/.config);
363
+ result = { type: "address", value: conf.current };
364
+ }else if (['continue', 'break'].includes(config.type)){
365
+ result = {interrupt_loop: config.type}
366
+ }
367
+ }
16
368
 
17
- if (!callable) {
18
- return await this.stderr({
19
- name: "runtime_error",
20
- message: `opcode:${opcode} is not set.`,
21
- });
369
+ if (result && cloth) {
370
+ if ( result.type === "address" && result.value.includes('/')) {
371
+ result = await this.resolve_address(result.value);
372
+ }
373
+ if (result && !result.error) result = await this.cloth_content(result);
22
374
  }
23
- let call = callable.callable;
24
- let args = await this.parse_arguments(payload, callable.config);
25
375
 
26
- let result = await call(args);
27
376
  return result;
28
377
  };
29
378
  }
@@ -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,24 @@
1
+ const render = async (args, { vm, config, chain }) => {
2
+ let { object } = args;
3
+
4
+ let res = await object.get("render");
5
+ if (!res) return null;
6
+
7
+ let result = await vm.execute(
8
+ {
9
+ type: "call",
10
+ identifier: {
11
+ type: "address",
12
+ value: res,
13
+ object: object.value.path,
14
+ },
15
+ arguments: [],
16
+ location: config.location,
17
+ },
18
+ { chain }
19
+ );
20
+
21
+ return result;
22
+ };
23
+
24
+ export default render;
@@ -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,48 @@
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";
5
+ import render from "./functions/render";
3
6
 
4
7
  const register = async (account) => {
5
8
  let set = account.vm.set;
6
9
 
7
- set("assign", {
8
- callable: assign,
10
+ set("display", {
11
+ callable: display,
9
12
  config: {
10
- parameters: [{ name: "assignment", position: 0 }],
13
+ parameters: [{ name: "data", position: 0, spread: true }],
11
14
  },
12
15
  });
13
16
 
14
- set("display", {
15
- callable: display,
17
+ set("Twain", {
18
+ callable: twain,
16
19
  config: {
17
- parameters: [{ name: "data", position: 0, spread: true }],
20
+ parameters: [{ name: "instance", position: 0 }],
21
+ },
22
+ });
23
+
24
+ set("net", {
25
+ callable: net,
26
+ config: {
27
+ parameters: [
28
+ { name: "server", position: 0 },
29
+ { name: "path", position: 0 },
30
+ { name: "payload", position: 0 },
31
+ ],
32
+ },
33
+ });
34
+
35
+ set("exit", {
36
+ callable: quit,
37
+ config: {
38
+ parameters: [{ name: "code", position: 0 }],
39
+ },
40
+ });
41
+
42
+ set("render", {
43
+ callable: render,
44
+ config: {
45
+ parameters: [{ name: "object", position: 0 }],
18
46
  },
19
47
  });
20
48
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "1.1.2",
3
+ "version": "1.1.31",
4
4
  "description": "A distributed computing environment",
5
5
  "main": "Index.js",
6
6
  "scripts": {
@@ -1,30 +1,68 @@
1
1
  import https from "https";
2
- import fs from "fs";
2
+ import http from "http";
3
3
 
4
4
  let PORT = Number(process.env.PORT) || 1408;
5
5
 
6
6
  const handle_routes = (req, res, manager, app) => {
7
7
  let data = "";
8
+ res.setHeader("Access-Control-Allow-Origin", "*");
9
+ res.setHeader("Access-Control-Allow-Headers", "Content-Type");
8
10
 
9
- req.on("data", (chunk) => {
10
- data += chunk;
11
- });
11
+ if (req.method === "OPTIONS") {
12
+ res.writeHead(204, {
13
+ "Access-Control-Allow-Origin": "*",
14
+ "Access-Control-Allow-Headers": "Content-Type",
15
+ "Access-Control-Allow-Methods": "POST, GET, OPTIONS",
16
+ });
17
+ res.end();
18
+ return;
19
+ }
12
20
 
13
- req.on("end", async () => {
14
- if (req.url === "/sync") {
15
- data = JSON.parse(data);
21
+ if (['/create_account', '/load', '/run', '/parse'].includes(req.url)){
22
+ req.on("data", (chunk) => {
23
+ data += chunk;
24
+ });
25
+
26
+ req.on("end", async () => {
27
+ if (!data) {
28
+ res.writeHead(400, { "Content-Type": "application/json" });
29
+ res.end(JSON.stringify({ error: "Request body cannot be empty" }));
30
+ return;
31
+ }
16
32
 
17
- let response = await manager.oracle.add_server(data);
33
+ data = JSON.parse(data)
34
+ let result;
18
35
 
19
- console.log(response, "hello");
20
- res.end(JSON.stringify(response));
21
- }
22
- });
36
+ if (req.url === '/create_account'){
37
+ result = await manager.add_account(data.name, data.options)
38
+ res.end(JSON.stringify(result.stringify()))
39
+ }else if(req.url === '/load'){
40
+ let account = await manager.get_account(data.account);
41
+ result = await account.load(data)
42
+ res.end(JSON.stringify(result))
43
+ }else if(req.url === '/run'){
44
+ let account = await manager.get_account(data.account);
45
+ result = await account.run(data)
46
+
47
+ res.writeHead(200, { "Content-type": "application/json" });
23
48
 
24
- req.on("error", (e) => {
25
- // res.writeHead(500, { "Content-Type": "application/json" });
26
- res.end(JSON.stringify({ status: "error", error_message: e.message }));
27
- });
49
+ res.end(result? JSON.stringify(result): JSON.stringify({done: true}))
50
+ }else if(req.url === '/parse'){
51
+ let account = await manager.get_account(data.account);
52
+ result = await account.parse(data)
53
+
54
+ res.end(result? JSON.stringify(result): JSON.stringify({done: true}))
55
+ }
56
+ });
57
+
58
+ req.on("error", (e) => {
59
+ res.writeHead(500, { "Content-Type": "application/json" });
60
+ res.end(JSON.stringify({ status: "error", error_message: e.message }));
61
+ });
62
+ }else if (app){
63
+ app(req, res);
64
+ }
65
+
28
66
  };
29
67
 
30
68
  const create_server = async (settings) => {
@@ -33,12 +71,13 @@ const create_server = async (settings) => {
33
71
  let { port, app, manager } = settings;
34
72
 
35
73
  port = port || PORT;
36
-
37
- let server = https.createServer(
38
- {
39
- key: fs.readFileSync(`${__dirname}/server.key`),
40
- cert: fs.readFileSync(`${__dirname}/server.cert`),
41
- },
74
+
75
+ // console.log(__dirname)
76
+ let server = http.createServer(
77
+ // {
78
+ // key: fs.readFileSync(`${__dirname}/server.key`),
79
+ // cert: fs.readFileSync(`${__dirname}/server.cert`),
80
+ // },
42
81
  (req, res) => handle_routes(req, res, manager, app)
43
82
  );
44
83
 
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 };