godprotocol 1.0.799 → 1.0.831

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.
@@ -11,6 +11,7 @@ class Loader {
11
11
  this.instruction_indexes = new Array();
12
12
  this.instruction_stacks = [[]];
13
13
  this.stacks = new Array(account.physical_address);
14
+ this.markers = new Array();
14
15
  }
15
16
 
16
17
  instruction_index = () => {
@@ -252,6 +253,8 @@ class Loader {
252
253
  } else this.push_instruction(`pop ${identifier.value}`);
253
254
  };
254
255
 
256
+ marker_pattern = /@[a-zA-Z_][a-zA-Z0-9_]*/;
257
+
255
258
  parse_opcode = (line) => {
256
259
  let tokens = Array.isArray(line) ? line : this.parse_tokens(line);
257
260
 
@@ -265,11 +268,19 @@ class Loader {
265
268
  ]);
266
269
 
267
270
  tokens.slice(1).map((tok, i) => {
268
- this.parse(tok);
271
+ if (tok.type === "address" && tok.value.match(this.marker_pattern)) {
272
+ this.push_instruction([
273
+ `link ${this.account.physical_address}/Datatypes/Number`,
274
+ `write ${tok.value}`,
275
+ `pop Number`,
276
+ ]);
277
+ } else this.parse(tok);
278
+
269
279
  this.push_instruction([
270
280
  `cursor op${i}`,
271
281
  `write ${
272
- this.is_not_literal(tok.type)
282
+ this.is_not_literal(tok.type) &&
283
+ !(tok.type === "address" && tok.value.match(this.marker_pattern))
273
284
  ? "{metadata.output:-1}"
274
285
  : "{datapath:-1}"
275
286
  }`,
@@ -351,6 +362,58 @@ class Loader {
351
362
  this.instruction_indexes.pop();
352
363
  };
353
364
 
365
+ revamp_marks = () => {
366
+ let instruction_stack = this.instruction_stacks.slice(-1)[0];
367
+
368
+ instruction_stack = instruction_stack.map((ins) =>
369
+ this.resolve_offset(ins, true)
370
+ );
371
+
372
+ this.instruction_stacks[this.instruction_stacks.length - 1] =
373
+ instruction_stack;
374
+ };
375
+
376
+ handle_marker = (line) => {
377
+ let splits = line.slice(1).split(" ");
378
+ let name = splits[0];
379
+
380
+ let curr_stack = this.stacks.slice(-1)[0];
381
+ let markers = this.markers[curr_stack];
382
+ if (!markers) {
383
+ markers = new Object();
384
+ this.markers[curr_stack] = markers;
385
+ }
386
+ markers[name] = this.instruction_index();
387
+
388
+ this.revamp_marks();
389
+
390
+ return splits.slice(1).join(" ");
391
+ };
392
+
393
+ resolve_offset = (line, revamping) => {
394
+ let matches = line.match(this.marker_pattern),
395
+ value;
396
+ if (matches) {
397
+ let match = matches[0].slice(1);
398
+
399
+ let curr_stack = this.stacks.slice(-1)[0];
400
+ let markers = this.markers[curr_stack];
401
+ if (!markers) {
402
+ markers = new Object();
403
+ this.markers[curr_stack] = markers;
404
+ }
405
+ value = markers[match];
406
+ }
407
+
408
+ if (typeof value === "number")
409
+ line = line.replace(
410
+ this.marker_pattern,
411
+ (revamping && value > 0 ? value - 1 : value).toString()
412
+ );
413
+
414
+ return line;
415
+ };
416
+
354
417
  compile = (codes) => {
355
418
  let code_array = codes.split("\n");
356
419
 
@@ -358,8 +421,15 @@ class Loader {
358
421
  let line = code_array[c].trim();
359
422
  if (!line) continue;
360
423
 
424
+ if (line.startsWith(":")) {
425
+ line = this.handle_marker(line);
426
+
427
+ if (!line) continue;
428
+ }
429
+
430
+ line = this.resolve_offset(line);
431
+
361
432
  if (line === ";") this.pop();
362
- else if (line.startsWith("link ")) this.parse_routine(line);
363
433
  else if (line.startsWith(".") || line.startsWith("@"))
364
434
  this.parse_routine(line);
365
435
  else if (line.startsWith("//")) continue;
@@ -369,9 +439,11 @@ class Loader {
369
439
  }
370
440
  };
371
441
 
372
- run = (codes, meta = { cb, pure }) => {
442
+ run = (codes, meta) => {
373
443
  if (!meta) meta = {};
374
- this.pure = pure;
444
+ this.pure = meta && meta.pure;
445
+
446
+ let cb = meta && meta.cb;
375
447
 
376
448
  this.compile(codes);
377
449
 
@@ -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
  };
@@ -56,20 +96,24 @@ class Account extends Filesystem {
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 }) => {
112
+ parse = (program) => {
113
+ let { payload, signature, callback } = program;
71
114
  let { physical_address, account, query } = payload;
72
115
  account = this.get_account(account);
116
+ this.propagate(program, "parse");
73
117
 
74
118
  if (!account.validate(payload, signature))
75
119
  return this.run_callback(callback, {
@@ -96,6 +140,7 @@ class Account extends Filesystem {
96
140
 
97
141
  let { physical_address, account, query } = payload;
98
142
  account = this.get_account(account);
143
+ account.propagate(request, "run");
99
144
 
100
145
  if (!account.validate(payload, signature, callback)) return;
101
146
 
@@ -137,6 +182,17 @@ class Account extends Filesystem {
137
182
  }, 0);
138
183
  };
139
184
 
185
+ flush_stdout = (block) => {
186
+ for (let s = 0; s < this.stdout.length; s++) {
187
+ let out = this.stdout[s];
188
+ if (out.pid === this.vm.track.pid) {
189
+ block.metadata.stdout.push(out.data);
190
+ this.stdout[s] = null;
191
+ }
192
+ }
193
+ this.stdout = this.stdout.filter((out) => !!out);
194
+ };
195
+
140
196
  validate = (payload, signature, callback) => {
141
197
  if (!this.private) return true;
142
198
 
@@ -145,7 +201,7 @@ class Account extends Filesystem {
145
201
  JSON.stringify(payload),
146
202
  signature
147
203
  );
148
- if (!valid)
204
+ if (!valid && callback)
149
205
  this.run_callback(callback, {
150
206
  private: this.private,
151
207
  error: true,
@@ -169,6 +225,7 @@ class Account extends Filesystem {
169
225
  };
170
226
 
171
227
  create_account = (payload) => {
228
+ this.propagate(payload, "create_account");
172
229
  let { name, meta } = payload;
173
230
 
174
231
  return this.manager.add_account(name, meta);
package/Objects/Block.js CHANGED
@@ -7,6 +7,10 @@ class Block {
7
7
  this.metadata = {};
8
8
  this.children = new Array();
9
9
  this.index = this.chain.blocks.length;
10
+
11
+ this.blocks_folder = this.chain.account.manager.oracle.gds.folder(
12
+ process.env.BLOCKS_FOLDER || "blocks"
13
+ );
10
14
  }
11
15
 
12
16
  generate_hash = () => {
@@ -31,6 +35,8 @@ class Block {
31
35
  this.metadata[curs.slice(1, -1)] = content[curs];
32
36
  delete content[curs];
33
37
  }
38
+ this.metadata.stdout = new Array();
39
+ this.chain.account.flush_stdout(this);
34
40
 
35
41
  let datapath = this.chain.account.save(content, this.chain);
36
42
  this.datapath = datapath;
@@ -46,7 +52,12 @@ class Block {
46
52
  obj.hash = this.hash;
47
53
  obj.data = this.data;
48
54
  obj.children = this.children.map(
49
- (ch) => new Object({ hash: ch.hash, chain: ch.chain.hash })
55
+ (ch) =>
56
+ new Object({
57
+ hash: ch.hash,
58
+ chain: ch.chain.hash,
59
+ physical_address: ch.chain.physical_address,
60
+ })
50
61
  );
51
62
  obj.chain = {
52
63
  hash: this.chain.hash,
package/Objects/Chain.js CHANGED
@@ -27,9 +27,15 @@ class Chain extends Explorer {
27
27
 
28
28
  this.generate_hash();
29
29
 
30
+ this.folder = this.account.manager.oracle.gds.folder(this.hash);
31
+
32
+ this.manage_config();
33
+
30
34
  this.account.set_paths(this);
31
35
  }
32
36
 
37
+ manage_config = () => {};
38
+
33
39
  append_buffer = () => {
34
40
  this.buffs.push({});
35
41
  this.cursors.push(this.cursor);
@@ -81,6 +87,7 @@ class Chain extends Explorer {
81
87
 
82
88
  block.timelapse = Date.now() - this.start_time;
83
89
 
90
+ this.folder.write(block.stringify());
84
91
  return block;
85
92
  };
86
93
 
@@ -39,14 +39,14 @@ class Filesystem {
39
39
  save = (content, chain, just_obj) => {
40
40
  let addr, obj;
41
41
 
42
- let type = this.get_type(chain);
42
+ let type = typeof chain === "string" ? chain : this.get_type(chain);
43
43
 
44
44
  if (type === "array" || type === "string") {
45
45
  obj = [];
46
46
 
47
- for (let curs in content) {
47
+ for (let curs in content)
48
48
  obj.splice(Number(curs) || -1, 0, content[curs]);
49
- }
49
+
50
50
  if (type === "string") obj = obj.join(" ");
51
51
  } else if (type === "twain") {
52
52
  obj = {};
@@ -64,7 +64,7 @@ class Filesystem {
64
64
 
65
65
  addr = `${chain.path}/${hash(JSON.stringify(obj))}`;
66
66
  this;
67
- if (obj != null) this.manager.oracle.write(addr, obj);
67
+ if (obj != null) this.manager.oracle.write(addr, obj, this.account);
68
68
 
69
69
  this.manager.oracle.set(addr, { obj, type });
70
70
 
@@ -18,22 +18,22 @@ class Manager {
18
18
  this.clock = setInterval(() => {
19
19
  for (let account in this.tracks) {
20
20
  let track = this.tracks[account];
21
+ let program = track.slice(-1)[0];
21
22
 
22
- let instruction = track.sequence[track.pointer];
23
+ let instruction = program.sequence[program.pointer];
23
24
 
24
- track.account.vm.execute(instruction, track);
25
- track.pointer++;
25
+ program.account.vm.execute(instruction, program);
26
+ program.pointer++;
26
27
 
27
- if (track.pointer === track.sequence.length) {
28
- this.running--;
29
- track.account.chain.mine(track.account.vm);
30
- track.account.flush_buffer(track.pids.splice(-1)[0]);
28
+ if (program.pointer === program.sequence.length) {
29
+ program.account.flush_buffer(program.pid);
31
30
 
32
- delete this.tracks[account];
31
+ track.pop();
33
32
  }
34
-
35
- if (track.breakpoint.slice(-1)[0] === track.pointer) {
36
- track.account.flush_buffer(track.pids.splice(-1)[0]);
33
+ if (!track.length) {
34
+ this.running -= 1;
35
+ delete this.tracks[account];
36
+ program.account.chain.mine(program.account.vm);
37
37
  }
38
38
  }
39
39
 
@@ -48,8 +48,8 @@ class Manager {
48
48
 
49
49
  if (!account) {
50
50
  account = new Account(name, { ...meta, manager: this });
51
- framer(account);
52
51
  opcodes(account);
52
+ framer(account);
53
53
 
54
54
  this.accounts[account.name] = account;
55
55
  } else if (meta && meta.private && !account.private) {
@@ -63,23 +63,10 @@ class Manager {
63
63
  let track = this.tracks[program.account.name];
64
64
 
65
65
  if (track) {
66
- track.breakpoint.push(track.pointer + program.sequence.length + 1);
67
- track.sequence.splice(track.pointer + 1, 0, ...program.sequence);
68
- track.pids.push(program.pid);
69
-
70
- if (!this.running) {
71
- this.running++;
72
- this.tracks[program.account.name] = track;
73
- this.start_clock();
74
- }
66
+ track.push({ ...program, pointer: 0 });
75
67
  } else {
76
- track = {
77
- ...program,
78
- pointer: 0,
79
- pids: [program.pid],
80
- breakpoint: new Array(),
81
- };
82
- this.tracks[track.account.name] = track;
68
+ track = [{ ...program, pointer: 0 }];
69
+ this.tracks[program.account.name] = track;
83
70
 
84
71
  this.running++;
85
72
  this.running === 1 && this.start_clock();
@@ -1,3 +1,4 @@
1
+ import { obj } from "../framer";
1
2
  import { transpile_object } from "../utils/functions";
2
3
 
3
4
  class Opcodes {
@@ -7,6 +8,8 @@ class Opcodes {
7
8
 
8
9
  set = (opcode, circuit) => {
9
10
  this.opcodes[opcode] = circuit;
11
+ obj.Opcodes[opcode] = null;
12
+
10
13
  this.account.assembler.opcodes.push(opcode);
11
14
  };
12
15
 
@@ -17,9 +20,11 @@ class Opcodes {
17
20
 
18
21
  let buf = context.get_buffer();
19
22
 
20
- let args = buf && context.account.read(buf.inputs);
23
+ let args =
24
+ context.account.read(buf && buf.inputs) ||
25
+ this.account.stdin.splice(-1)[0];
21
26
 
22
- let res = circ(args);
27
+ let res = circ(args, this);
23
28
 
24
29
  this.stdin(res);
25
30
  };
@@ -27,9 +32,13 @@ class Opcodes {
27
32
  stdin = (object, cb) => {
28
33
  if (object == null) return;
29
34
 
30
- let code_string = transpile_object(object);
35
+ this.flags.zero = !object;
36
+ if (typeof object === "number") this.flags.neg = object < 0;
37
+
38
+ let code_string =
39
+ typeof object !== "string" ? JSON.stringify(object) : object;
31
40
 
32
- this.account.compiler.run(code_string, { cb, pure: true });
41
+ this.account.assembler.run(code_string, { cb, pure: true });
33
42
  // console.log(`Writing in: ${code_string}`);
34
43
  };
35
44
  }
package/Objects/Oracle.js CHANGED
@@ -1,12 +1,19 @@
1
1
  import fs from "fs";
2
+ import GDS from "generalised-datastore";
3
+ import { hash } from "../utils/hash";
2
4
 
3
5
  class Oracle {
4
6
  constructor(mgr) {
5
7
  this.fs = fs;
6
8
  this.mgr = mgr;
7
9
  this.datapaths = new Object();
10
+ this.gds = new GDS(process.env.DATASTORE || "godprotocol").sync();
8
11
  }
9
12
 
13
+ get_folder = (physical_address) => {
14
+ return this.gds.folder(this.hash(physical_address));
15
+ };
16
+
10
17
  set = (path, { type, obj }) => {
11
18
  this.datapaths[path] = {
12
19
  obj,
@@ -14,12 +21,62 @@ class Oracle {
14
21
  };
15
22
  };
16
23
 
17
- get = (path) => this.datapaths[path];
24
+ get = (path) => {
25
+ let obj = this.datapaths[path];
26
+
27
+ if (!obj) {
28
+ try {
29
+ obj = this.fs.readFileSync(path);
30
+ obj = JSON.parse(obj).data;
31
+ let type = typeof obj.data;
32
+ type =
33
+ type === "object" ? (Array.isArray(obj) ? "array" : "twain") : type;
34
+
35
+ obj = { type, obj };
36
+ this.set(path, obj);
37
+ } catch (e) {}
38
+ }
39
+
40
+ return obj;
41
+ };
42
+
43
+ hash = (data) => {
44
+ data = typeof data !== "string" ? JSON.stringify(data) : data;
45
+ return hash(data);
46
+ };
47
+
48
+ write = (addr, data) => {
49
+ data = JSON.stringify({ data });
50
+
51
+ this.fs.writeFileSync(addr, data, { encoding: "utf-8" });
52
+ };
53
+
54
+ fetch = (payload, callback) => {
55
+ let { physical_address, query, account, signature } = payload,
56
+ result;
57
+
58
+ account = this.mgr.get_account(account);
59
+ if (account && account.private)
60
+ if (!account.validate({ physical_address, query }, signature, callback))
61
+ return;
62
+
63
+ try {
64
+ let operation = this.gds.folder(this.hash(physical_address))[
65
+ query.operation
66
+ ];
18
67
 
19
- write = (addr, data, meta = { encoding: "utf-8" }) => {
20
- if (typeof data !== "string") data = JSON.stringify(data);
68
+ result = operation && operation(query.query, query.options);
21
69
 
22
- this.fs.writeFileSync(addr, data, meta);
70
+ if (result) {
71
+ if (account) {
72
+ let content = account.read(result.data);
73
+ if (content != null) result.content = content;
74
+ }
75
+ }
76
+ } catch (e) {
77
+ result = { error: true, message: e.message, payload };
78
+ }
79
+ typeof callback === "function" && callback(result);
23
80
  };
24
81
  }
25
82
 
@@ -7,6 +7,11 @@ class Virtual_machine extends Opcodes {
7
7
  this.account = context.account;
8
8
  this.contexts = [context];
9
9
  this.stack = new Array();
10
+ this.flags = {
11
+ neg: false,
12
+ equal: false,
13
+ zero: false,
14
+ };
10
15
  }
11
16
 
12
17
  get_context = (index) => {
@@ -68,6 +73,8 @@ class Virtual_machine extends Opcodes {
68
73
 
69
74
  context.add_tx(instruction);
70
75
 
76
+ // console.log(instruction);
77
+
71
78
  let chain;
72
79
 
73
80
  args = this.parse_arg(args, opcode === "cursor");
@@ -84,6 +91,9 @@ class Virtual_machine extends Opcodes {
84
91
  chain = context.account.manager.web.get(args);
85
92
 
86
93
  if (!chain) {
94
+ this.account.flush_buffer(this.track.pid);
95
+ this.track.pointer = this.track.sequence.length;
96
+
87
97
  console.log(`LINKing Failed. - ${args}`);
88
98
  return;
89
99
  }
@@ -96,7 +106,7 @@ class Virtual_machine extends Opcodes {
96
106
  case "mine":
97
107
  let bloc = context.mine(this, true);
98
108
 
99
- this.account.buff(bloc, this.track.pids.slice(-1)[0]);
109
+ this.account.buff(bloc, this.track.pid);
100
110
 
101
111
  break;
102
112
  case "pop":
@@ -109,7 +119,7 @@ class Virtual_machine extends Opcodes {
109
119
 
110
120
  context.txs = [];
111
121
  }
112
- blk && this.account.buff(blk, this.track.pids.slice(-1)[0]);
122
+ blk && this.account.buff(blk, this.track.pid);
113
123
 
114
124
  this.contexts.pop();
115
125
  break;