godprotocol 1.0.799 → 1.0.833

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.
package/Index.js CHANGED
@@ -5,5 +5,39 @@ let manager = new Manager();
5
5
 
6
6
  let initiator = manager.add_account(process.env.INITIATOR || "initiator");
7
7
 
8
+ setTimeout(() => {
9
+ initiator.assembler.run(
10
+ `
11
+ @/adder
12
+ // coder: Savvy
13
+
14
+ >num 5
15
+ // dimensions.0.type: number
16
+
17
+ ./subtr
18
+
19
+ >sub_result sub 7 8
20
+ ;
21
+
22
+ >result add num 6
23
+ stdout result
24
+
25
+ ;
26
+ `,
27
+ {
28
+ cb: (blocks) => {
29
+ initiator.run({
30
+ payload: { physical_address: `${initiator.physical_address}/adder` },
31
+ callback: (blks) => {
32
+ // console.log(blks, "blocks");
33
+ },
34
+ });
35
+ },
36
+ }
37
+ );
38
+ }, 1500);
39
+
40
+ create_server(null, { port_search: true });
41
+
8
42
  export default manager;
9
43
  export { initiator, create_server };
@@ -1,3 +1,5 @@
1
+ import { _id } from "generalised-datastore/utils/functions";
2
+
1
3
  let num_pattern = /^[+-]?\d+(\.\d+)?$/;
2
4
  // let str_pattern = /^["'][^"']*["']$/;
3
5
  let var_pattern = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
@@ -11,6 +13,25 @@ class Loader {
11
13
  this.instruction_indexes = new Array();
12
14
  this.instruction_stacks = [[]];
13
15
  this.stacks = new Array(account.physical_address);
16
+ this.markers = new Array();
17
+ this.program_configs = new Array();
18
+ this.programs_index = -1;
19
+
20
+ // Code Repository
21
+ this.oracle = this.account.manager.oracle;
22
+ this.programs_folder = this.oracle.get_folder("programs", {
23
+ joins: ["codes"],
24
+ });
25
+ this.codes_folder = this.oracle.get_folder("codes");
26
+
27
+ this.globals = this.oracle.get_folder("globals", {
28
+ subfolder: ["global"],
29
+ });
30
+
31
+ if (!this.globals.readone({ global: "programs" }))
32
+ this.globals.write({ global: "programs", programs: [] });
33
+ if (!this.globals.readone({ global: "codes" }))
34
+ this.globals.write({ global: "codes", codes: [] });
14
35
  }
15
36
 
16
37
  instruction_index = () => {
@@ -219,6 +240,7 @@ class Loader {
219
240
  if (line.type === "address")
220
241
  return this.push_instruction(`pop ${path[path.length - 1]}`);
221
242
  } else {
243
+ this.current_program().dimensions.push({ name: identifier.value });
222
244
  this.push_instruction(`chain ${identifier.value}`);
223
245
  this.stack_instruction([
224
246
  `link ${this.stacks.slice(-1)[0]}`,
@@ -252,6 +274,12 @@ class Loader {
252
274
  } else this.push_instruction(`pop ${identifier.value}`);
253
275
  };
254
276
 
277
+ current_program = () => {
278
+ return this.program_configs[this.programs_index];
279
+ };
280
+
281
+ marker_pattern = /@[a-zA-Z_][a-zA-Z0-9_]*/;
282
+
255
283
  parse_opcode = (line) => {
256
284
  let tokens = Array.isArray(line) ? line : this.parse_tokens(line);
257
285
 
@@ -265,11 +293,19 @@ class Loader {
265
293
  ]);
266
294
 
267
295
  tokens.slice(1).map((tok, i) => {
268
- this.parse(tok);
296
+ if (tok.type === "address" && tok.value.match(this.marker_pattern)) {
297
+ this.push_instruction([
298
+ `link ${this.account.physical_address}/Datatypes/Number`,
299
+ `write ${tok.value}`,
300
+ `pop Number`,
301
+ ]);
302
+ } else this.parse(tok);
303
+
269
304
  this.push_instruction([
270
305
  `cursor op${i}`,
271
306
  `write ${
272
- this.is_not_literal(tok.type)
307
+ this.is_not_literal(tok.type) &&
308
+ !(tok.type === "address" && tok.value.match(this.marker_pattern))
273
309
  ? "{metadata.output:-1}"
274
310
  : "{datapath:-1}"
275
311
  }`,
@@ -311,11 +347,23 @@ class Loader {
311
347
  let curr_stack = this.stacks.slice(-1)[0].split("/");
312
348
  new_stack = new_stack.split("/");
313
349
 
350
+ let physical_address = new_stack.join("/");
351
+ let program = {
352
+ program_name: new_stack[new_stack.length - 1],
353
+ physical_address,
354
+ codes: [],
355
+ dimensions: [],
356
+ sub_programs: [],
357
+ _id: _id("programs"),
358
+ };
359
+ let curr_program = this.current_program();
360
+ if (curr_program) curr_program.sub_programs.push(program._id);
361
+
362
+ this.programs_index++;
363
+ this.program_configs.push(program);
364
+
314
365
  if (curr_stack[1] !== new_stack[1]) {
315
366
  } else {
316
- let i = 0;
317
- while (curr_stack[i] === new_stack[i]) i++;
318
-
319
367
  for (let j = 2; j < new_stack.length; j++)
320
368
  this.stack_instruction(`chain ${new_stack[j]}`);
321
369
 
@@ -347,31 +395,145 @@ class Loader {
347
395
  this.stack_instruction(`pop ${addr[j]}`);
348
396
 
349
397
  this.instructions.push(...this.instruction_stacks.splice(-1)[0]);
398
+ this.programs_index--;
350
399
 
351
400
  this.instruction_indexes.pop();
352
401
  };
353
402
 
403
+ revamp_marks = () => {
404
+ let instruction_stack = this.instruction_stacks.slice(-1)[0];
405
+
406
+ instruction_stack = instruction_stack.map((ins) =>
407
+ this.resolve_offset(ins, true)
408
+ );
409
+
410
+ this.instruction_stacks[this.instruction_stacks.length - 1] =
411
+ instruction_stack;
412
+ };
413
+
414
+ handle_marker = (line) => {
415
+ let splits = line.slice(1).split(" ");
416
+ let name = splits[0];
417
+
418
+ let curr_stack = this.stacks.slice(-1)[0];
419
+ let markers = this.markers[curr_stack];
420
+ if (!markers) {
421
+ markers = new Object();
422
+ this.markers[curr_stack] = markers;
423
+ }
424
+ markers[name] = this.instruction_index();
425
+
426
+ this.revamp_marks();
427
+
428
+ return splits.slice(1).join(" ");
429
+ };
430
+
431
+ resolve_offset = (line, revamping) => {
432
+ let matches = line.match(this.marker_pattern),
433
+ value;
434
+ if (matches) {
435
+ let match = matches[0].slice(1);
436
+
437
+ let curr_stack = this.stacks.slice(-1)[0];
438
+ let markers = this.markers[curr_stack];
439
+ if (!markers) {
440
+ markers = new Object();
441
+ this.markers[curr_stack] = markers;
442
+ }
443
+ value = markers[match];
444
+ }
445
+
446
+ if (typeof value === "number")
447
+ line = line.replace(
448
+ this.marker_pattern,
449
+ (revamping && value > 0 ? value - 1 : value).toString()
450
+ );
451
+
452
+ return line;
453
+ };
454
+
455
+ parse_comment = (line) => {
456
+ line = line.split(":");
457
+ if (line.length <= 1) return;
458
+
459
+ let curr_program = this.current_program();
460
+ let line1 = line[0].split(".");
461
+ let obj = curr_program;
462
+
463
+ for (let l = 0; l < line1.length - 1; l++) {
464
+ let term = line1[l];
465
+
466
+ if (term.trim() === "_id") break;
467
+
468
+ let n_term = Number(term);
469
+
470
+ if (n_term) {
471
+ obj = obj[n_term];
472
+ } else obj = obj[term];
473
+ }
474
+ if (!obj) return;
475
+
476
+ let index = line1[line1.length - 1];
477
+ let n_indx = Number(index);
478
+ if (!isNaN(n_indx)) index = n_indx;
479
+
480
+ if (index === "_id") return;
481
+
482
+ obj[index] = line.slice(1).join(":").trim();
483
+ };
484
+
354
485
  compile = (codes) => {
355
486
  let code_array = codes.split("\n");
356
487
 
357
488
  for (let c = 0; c < code_array.length; c++) {
358
489
  let line = code_array[c].trim();
490
+
491
+ let curr_program = this.current_program();
492
+ this.is_routine = line.startsWith(".") || line.startsWith("@");
493
+
494
+ this.handle_codes(line, curr_program);
495
+
359
496
  if (!line) continue;
360
497
 
498
+ if (line.startsWith(":")) {
499
+ line = this.handle_marker(line);
500
+
501
+ if (!line) continue;
502
+ }
503
+
504
+ line = this.resolve_offset(line);
505
+
361
506
  if (line === ";") this.pop();
362
- else if (line.startsWith("link ")) this.parse_routine(line);
363
- else if (line.startsWith(".") || line.startsWith("@"))
364
- this.parse_routine(line);
365
- else if (line.startsWith("//")) continue;
507
+ else if (this.is_routine) this.parse_routine(line);
508
+ else if (line.startsWith("//")) this.parse_comment(line.slice(2).trim());
366
509
  else if (line.startsWith(">@")) this.parse_assignment(line, true);
367
510
  else if (line.startsWith(">")) this.parse_assignment(line);
368
511
  else this.parse_opcode(line);
512
+
513
+ (!curr_program || this.is_routine) &&
514
+ this.handle_codes(line, this.current_program());
515
+ }
516
+ };
517
+
518
+ handle_codes = (line, curr_program) => {
519
+ if (this.is_routine) {
520
+ let tilline = `~${line}`;
521
+ let prev_program = this.program_configs[this.programs_index - 1];
522
+ prev_program && prev_program.codes.push(tilline);
523
+
524
+ curr_program &&
525
+ !curr_program.codes.find((line) => !!line.trim()) &&
526
+ curr_program.codes.push(line);
369
527
  }
528
+
529
+ !this.is_routine && curr_program && curr_program.codes.push(line);
370
530
  };
371
531
 
372
- run = (codes, meta = { cb, pure }) => {
532
+ run = (codes, meta) => {
373
533
  if (!meta) meta = {};
374
- this.pure = pure;
534
+ this.pure = meta && meta.pure;
535
+
536
+ let cb = meta && meta.cb;
375
537
 
376
538
  this.compile(codes);
377
539
 
@@ -381,6 +543,50 @@ class Loader {
381
543
  program: { instructions: [...this.instructions] },
382
544
  callback: cb,
383
545
  });
546
+
547
+ let programs = this.globals.readone({ global: "programs" });
548
+ let codes_global = this.globals.readone({ global: "codes" });
549
+ this.program_configs.map((config) => {
550
+ let program = programs.programs.find(
551
+ (prog) => prog.physical_address === config.physical_address
552
+ );
553
+
554
+ let code_str = config.codes.join("\n");
555
+ let code_hash = this.oracle.hash(code_str);
556
+ let code = codes_global.codes.find((cod) => cod.hash === code_hash);
557
+ let code_id = code && code.code_id;
558
+ if (!code_id) {
559
+ code_id = _id("codes");
560
+
561
+ this.globals.update(
562
+ { global: "codes" },
563
+ { codes: { $unshift: { code_id, hash: code_hash } } }
564
+ );
565
+ }
566
+
567
+ if (program) {
568
+ config._id = program.program_id;
569
+ this.programs_folder.update(config._id, {
570
+ ...config,
571
+ codes: { $unshift: code_id },
572
+ });
573
+ } else {
574
+ this.programs_folder.write({ ...config, codes: [code_id] });
575
+ this.globals.update(
576
+ { global: "programs" },
577
+ {
578
+ programs: {
579
+ $unshift: {
580
+ physical_address: config.physical_address,
581
+ program_id: config._id,
582
+ },
583
+ },
584
+ }
585
+ );
586
+ }
587
+ !code && this.codes_folder.write({ codes: code_str, _id: code_id });
588
+ });
589
+ this.program_configs = [];
384
590
  this.instructions = [];
385
591
  this.pure = false;
386
592
  };
@@ -1,5 +1,6 @@
1
1
  import Loader_sequence from "../Loader_sequence/main";
2
2
  import validate_signature from "../utils/privacy";
3
+ import { post_request } from "../utils/services";
3
4
  import Filesystem from "./Filesystem";
4
5
  import Virtual_machine from "./Virtual_machine";
5
6
 
@@ -13,6 +14,8 @@ class Account extends Filesystem {
13
14
  this.private = meta.private;
14
15
  this.manager = meta.manager;
15
16
  this.account = this;
17
+ this.stdin = new Array();
18
+ this.stdout = new Array();
16
19
 
17
20
  if (!this.manager) throw new Error("Manager instance missing.");
18
21
 
@@ -20,16 +23,53 @@ class Account extends Filesystem {
20
23
  this.physical_address = `${this.base_address}/${this.name}`;
21
24
  this.set_paths(this);
22
25
 
23
- this.compiler = new Loader_sequence(this);
26
+ this.assembler = new Loader_sequence(this);
24
27
 
25
28
  this.chain = this.account_chain(this);
26
29
  this.vm = new Virtual_machine(this.chain);
27
30
 
28
31
  this.mine_buffer = {};
32
+ this.privileges = new Object();
29
33
 
30
34
  console.log(`Account Instance: ${this.name}`);
31
35
  }
32
36
 
37
+ propagate = (data, endpoint) => {
38
+ if (data.is_propagated) return;
39
+
40
+ let servers = this.privileges[endpoint];
41
+
42
+ data.is_propagated = true;
43
+ data = JSON.stringify(data);
44
+
45
+ if (servers && servers.length) {
46
+ servers.map((serv) => {
47
+ post_request({
48
+ options: {
49
+ ...serv,
50
+ path: `/${endpoint}`,
51
+ },
52
+ data,
53
+ });
54
+ });
55
+ }
56
+ };
57
+
58
+ add_server = ({ server, privileges }) => {
59
+ if (!Array.isArray(privileges) && privileges)
60
+ privileges = ["load", "parse", "run", "create_account"];
61
+
62
+ privileges.map((privilege) => {
63
+ let priv = this.privileges[privilege];
64
+ if (!priv) {
65
+ priv = [];
66
+ this.privileges[privilege] = priv;
67
+ }
68
+ if (!priv.find((serv) => serv.domain === server.domain))
69
+ priv.push(server);
70
+ });
71
+ };
72
+
33
73
  get_account = (name) => {
34
74
  return this.manager.get_account(name) || this;
35
75
  };
@@ -48,54 +88,53 @@ class Account extends Filesystem {
48
88
  buffer.blocks.push(block.stringify());
49
89
  };
50
90
 
51
- flush_buffer = (pid) => {
91
+ flush_buffer = (pid, payload) => {
52
92
  let buff = this.mine_buffer[pid];
53
93
  if (!buff) return;
54
94
 
55
- buff.callback && this.run_callback(buff.callback, buff.blocks);
95
+ buff.callback && this.run_callback(buff.callback, payload || buff.blocks);
56
96
  delete this.mine_buffer[pid];
57
97
  };
58
98
 
59
- load = ({ program, signature, callback }) => {
99
+ load = (payload) => {
100
+ let { program, signature, callback } = payload;
60
101
  let { instructions, account } = program;
61
102
  account = this.get_account(account);
103
+ account.propagate(payload, "load");
62
104
 
63
- if (!account.validate(payload, signature, callback)) return;
105
+ if (!account.validate(program, signature, callback)) return;
64
106
 
65
107
  let pid = account.manage_buffer(callback);
66
108
 
67
109
  this.manager.push({ sequence: instructions, account, pid });
68
110
  };
69
111
 
70
- parse = ({ payload, signature, callback }) => {
71
- let { physical_address, account, query } = payload;
112
+ parse = (program) => {
113
+ let { payload, callback } = program;
114
+ let { account } = payload;
72
115
  account = this.get_account(account);
73
-
74
- if (!account.validate(payload, signature))
75
- return this.run_callback(callback, {
76
- private: account.private,
77
- error: true,
78
- error_message: "Invalid signature",
79
- });
80
-
81
- let chain = account.manager.web.get(physical_address);
82
-
83
- if (!chain)
84
- return this.run_callback(callback, {
85
- error: true,
86
- error_message: "Address not found",
87
- });
88
-
89
- let data = query ? chain.explore(query) : chain;
90
-
91
- this.run_callback(callback, data && data.stringify());
116
+ account.propagate(program, "parse");
117
+
118
+ account.manager.oracle.fetch(
119
+ payload,
120
+ (result) => account.run_callback(callback, result),
121
+ () =>
122
+ account.run_callback(callback, {
123
+ private: account.private,
124
+ error: true,
125
+ error_message: "Invalid signature",
126
+ })
127
+ );
92
128
  };
93
129
 
94
130
  run = (request) => {
95
131
  let { payload, signature, callback } = request;
96
132
 
97
- let { physical_address, account, query } = payload;
133
+ let { physical_address, account, query, history } = payload;
134
+
135
+ history = Math.abs(Number(history)) || 0;
98
136
  account = this.get_account(account);
137
+ account.propagate(request, "run");
99
138
 
100
139
  if (!account.validate(payload, signature, callback)) return;
101
140
 
@@ -105,10 +144,38 @@ class Account extends Filesystem {
105
144
  ? this.manager.web.get(physical_address)
106
145
  : account.vm.get_context();
107
146
 
108
- if (!context) return account.flush_buffer(pid);
147
+ if (!context) {
148
+ if (physical_address)
149
+ context = this.manager.web.split_set(physical_address);
150
+ else return account.flush_buffer(pid);
151
+ }
109
152
 
110
153
  let blk = query ? context.explore(query) : context.get_latest_block();
111
154
 
155
+ if (!query) {
156
+ let index = context.height;
157
+ index -= 2;
158
+
159
+ if (blk && blk.metadata.program) {
160
+ if (history) {
161
+ history--;
162
+ blk = null;
163
+ }
164
+ }
165
+
166
+ while (!blk && index >= 0) {
167
+ index--;
168
+ blk = context.get_block(index);
169
+
170
+ if (blk && blk.metadata.program) {
171
+ if (history) {
172
+ history--;
173
+ blk = null;
174
+ }
175
+ }
176
+ }
177
+ }
178
+
112
179
  if (blk && blk.metadata && blk.metadata.program) {
113
180
  let program = this.read(blk.metadata.program);
114
181
 
@@ -137,6 +204,22 @@ class Account extends Filesystem {
137
204
  }, 0);
138
205
  };
139
206
 
207
+ flush_stdout = (block) => {
208
+ if (
209
+ block.chain.physical_address === `${this.physical_address}/Opcodes/stdout`
210
+ )
211
+ return;
212
+
213
+ for (let s = 0; s < this.stdout.length; s++) {
214
+ let out = this.stdout[s];
215
+ if (out.pid === this.vm.track.pid) {
216
+ block.metadata.stdout.push(out.data);
217
+ this.stdout[s] = null;
218
+ }
219
+ }
220
+ this.stdout = this.stdout.filter((out) => !!out);
221
+ };
222
+
140
223
  validate = (payload, signature, callback) => {
141
224
  if (!this.private) return true;
142
225
 
@@ -145,7 +228,7 @@ class Account extends Filesystem {
145
228
  JSON.stringify(payload),
146
229
  signature
147
230
  );
148
- if (!valid)
231
+ if (!valid && callback)
149
232
  this.run_callback(callback, {
150
233
  private: this.private,
151
234
  error: true,
@@ -169,6 +252,7 @@ class Account extends Filesystem {
169
252
  };
170
253
 
171
254
  create_account = (payload) => {
255
+ this.propagate(payload, "create_account");
172
256
  let { name, meta } = payload;
173
257
 
174
258
  return this.manager.add_account(name, meta);
package/Objects/Block.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { hash } from "../utils/hash";
2
+ import { _id } from "generalised-datastore/utils/functions";
2
3
 
3
4
  class Block {
4
5
  constructor(chain) {
@@ -7,6 +8,11 @@ class Block {
7
8
  this.metadata = {};
8
9
  this.children = new Array();
9
10
  this.index = this.chain.blocks.length;
11
+ this._id = _id(this.chain.hash);
12
+
13
+ this.blocks_folder = this.chain.account.manager.oracle.gds.folder(
14
+ process.env.BLOCKS_FOLDER || "blocks"
15
+ );
10
16
  }
11
17
 
12
18
  generate_hash = () => {
@@ -31,6 +37,8 @@ class Block {
31
37
  this.metadata[curs.slice(1, -1)] = content[curs];
32
38
  delete content[curs];
33
39
  }
40
+ this.metadata.stdout = new Array();
41
+ this.chain.account.flush_stdout(this);
34
42
 
35
43
  let datapath = this.chain.account.save(content, this.chain);
36
44
  this.datapath = datapath;
@@ -45,8 +53,16 @@ class Block {
45
53
  obj.index = this.index;
46
54
  obj.hash = this.hash;
47
55
  obj.data = this.data;
48
- obj.children = this.children.map(
49
- (ch) => new Object({ hash: ch.hash, chain: ch.chain.hash })
56
+ obj._id = this._id;
57
+ obj.children = this.children.map((ch) =>
58
+ ch.stringify
59
+ ? new Object({
60
+ hash: ch.hash,
61
+ chain: ch.chain.hash,
62
+ _id: ch._id,
63
+ physical_address: ch.chain.physical_address,
64
+ })
65
+ : ch
50
66
  );
51
67
  obj.chain = {
52
68
  hash: this.chain.hash,
@@ -55,6 +71,22 @@ class Block {
55
71
 
56
72
  return str ? JSON.stringify(obj) : obj;
57
73
  };
74
+
75
+ static build = (data, chain) => {
76
+ let blck = new Block(chain);
77
+ blck.chain = chain;
78
+ blck.metadata = data.metadata;
79
+ blck.datapath = data.datapath;
80
+ blck.timelapse = data.timelapse;
81
+ blck.index = data.index;
82
+ blck.hash = data.hash;
83
+ blck.data = data.data;
84
+ blck.children = data.children || [];
85
+
86
+ blck._id = data._id;
87
+
88
+ return blck;
89
+ };
58
90
  }
59
91
 
60
92
  export default Block;
@@ -15,7 +15,21 @@ class Blockweb {
15
15
  return chain;
16
16
  };
17
17
 
18
+ split_set = (physical_address) => {
19
+ let spli = physical_address.split("/");
20
+
21
+ let chain = this.get(physical_address);
22
+ if (!chain) {
23
+ chain = this.set(spli.slice(0, -1).join("/"), spli.slice(-1)[0]);
24
+ }
25
+ return chain;
26
+ };
27
+
18
28
  set = (parent, name) => {
29
+ if (typeof parent === "string") {
30
+ parent = this.get(parent) || this.split_set(parent);
31
+ }
32
+
19
33
  let chain = new Chain(parent, name);
20
34
  this.chains[chain.hash] = chain;
21
35