godprotocol 1.0.835 → 1.0.837

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
@@ -1,5 +1,30 @@
1
1
  import Manager from "./Objects/Manager";
2
2
  import create_server from "./utils/create_server";
3
3
 
4
+ let manager = new Manager();
5
+
6
+ let initiator = manager.initiate({
7
+ on_framed: () =>
8
+ initiator.assembler.run(
9
+ `@/datastore/main
10
+
11
+ @/datastore/main/grey
12
+ >matter 'lola'
13
+ ;
14
+
15
+
16
+ >storage {}
17
+
18
+ ;`,
19
+ {
20
+ cb: (blks) => {
21
+ console.log(!!blks, "did");
22
+ },
23
+ }
24
+ ),
25
+ });
26
+
27
+ create_server(null, { manager });
28
+
4
29
  export default Manager;
5
30
  export { create_server };
@@ -1,4 +1,5 @@
1
1
  import { _id } from "generalised-datastore/utils/functions";
2
+ import Repository from "./repository";
2
3
 
3
4
  let num_pattern = /^[+-]?\d+(\.\d+)?$/;
4
5
  // let str_pattern = /^["'][^"']*["']$/;
@@ -18,20 +19,7 @@ class Loader {
18
19
  this.programs_index = -1;
19
20
 
20
21
  // 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: [] });
22
+ this.repository = new Repository(this);
35
23
  }
36
24
 
37
25
  instruction_index = () => {
@@ -537,6 +525,8 @@ class Loader {
537
525
 
538
526
  this.compile(codes);
539
527
 
528
+ if (meta.instructions) return [...this.instructions];
529
+
540
530
  // console.log(JSON.stringify(this.instructions, null, 2), "hiya");
541
531
 
542
532
  this.account.load({
@@ -544,48 +534,8 @@ class Loader {
544
534
  callback: cb,
545
535
  });
546
536
 
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
- }
537
+ this.program_configs.map((config) => this.repository.add_program(config));
566
538
 
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
539
  this.program_configs = [];
590
540
  this.instructions = [];
591
541
  this.pure = false;
@@ -0,0 +1,32 @@
1
+ class Repository {
2
+ constructor(main) {
3
+ this.main = main;
4
+ this.oracle = this.main.account.manager.oracle;
5
+ this.gds = this.oracle.gds;
6
+ this.programs = this.gds.folder("programs");
7
+ this.codes = this.gds.folder("codes");
8
+ }
9
+
10
+ add_program = (config) => {
11
+ let code_hash = this.oracle.hash(config.codes);
12
+ let code_exist = this.codes.readone({ hash: code_hash });
13
+
14
+ let code = this.codes.write({
15
+ body: config.codes,
16
+ hash: code_hash,
17
+ _id: code_exist && code_exist._id,
18
+ });
19
+ config.codes = code._id;
20
+
21
+ let program_hash = this.oracle.hash(JSON.stringify(config));
22
+ let program_exist = this.programs.readone({ hash: program_hash });
23
+
24
+ this.programs.write({
25
+ ...config,
26
+ hash: program_hash,
27
+ _id: program_exist && program_exist._id,
28
+ });
29
+ };
30
+ }
31
+
32
+ export default Repository;
@@ -31,14 +31,42 @@ class Account extends Filesystem {
31
31
  this.mine_buffer = {};
32
32
  this.privileges = new Object();
33
33
 
34
+ this.events = new Object();
35
+
34
36
  console.log(`Account Instance: ${this.name}`);
35
37
  }
36
38
 
39
+ on = (event, handler) => {
40
+ let handlers = this.events[event];
41
+ if (!handlers) {
42
+ handlers = new Array();
43
+ this.events[event] = handlers;
44
+ }
45
+ handlers.push(handler);
46
+ };
47
+
48
+ emit = (event, data) => {
49
+ let handlers = this.events[event];
50
+ Array.isArray(handlers) &&
51
+ handlers.map((handle) => this.run_callback(handle, data));
52
+ };
53
+
37
54
  propagate = (data, endpoint) => {
38
55
  if (data.is_propagated) return;
39
56
 
40
57
  let servers = this.privileges[endpoint];
41
58
 
59
+ if (data.exclusive) {
60
+ if (typeof data.exclusive === "boolean") return;
61
+ else if (Array.isArray(data.exclusive)) {
62
+ servers =
63
+ servers &&
64
+ servers.filter((serv) => {
65
+ return data.exclusive.includes(serv.domain);
66
+ });
67
+ }
68
+ }
69
+
42
70
  data.is_propagated = true;
43
71
  data = JSON.stringify(data);
44
72
 
@@ -97,57 +125,49 @@ class Account extends Filesystem {
97
125
  };
98
126
 
99
127
  load = (payload) => {
100
- let { program, signature, callback } = payload;
101
- let { instructions, account } = program;
102
- account = this.get_account(account);
103
- account.propagate(payload, "load");
128
+ let { program, callback } = payload;
129
+ let { instructions, assemble } = program;
104
130
 
105
- if (!account.validate(program, signature, callback)) return;
131
+ this.propagate(payload, "load");
106
132
 
107
- let pid = account.manage_buffer(callback);
133
+ if (assemble) {
134
+ this.assembler.run(instructions, { cb: callback });
135
+ } else {
136
+ let pid = this.manage_buffer(callback);
108
137
 
109
- this.manager.push({ sequence: instructions, account, pid });
138
+ this.manager.push({ sequence: instructions, account: this, pid });
139
+ }
110
140
  };
111
141
 
112
142
  parse = (program) => {
113
- let { payload, callback } = program;
114
- let { account } = payload;
115
- account = this.get_account(account);
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
- })
143
+ let { payload, callback, signature } = program;
144
+
145
+ this.propagate(program, "parse");
146
+
147
+ this.manager.oracle.fetch({ ...payload, signature }, (result) =>
148
+ this.run_callback(callback, result)
127
149
  );
128
150
  };
129
151
 
130
152
  run = (request) => {
131
- let { payload, signature, callback } = request;
153
+ let { payload, callback } = request;
132
154
 
133
- let { physical_address, account, query, history } = payload;
155
+ let { physical_address, query, history } = payload;
134
156
 
135
157
  history = Math.abs(Number(history)) || 0;
136
- account = this.get_account(account);
137
- account.propagate(request, "run");
138
158
 
139
- if (!account.validate(payload, signature, callback)) return;
159
+ this.propagate(request, "run");
140
160
 
141
- let pid = account.manage_buffer(callback);
161
+ let pid = this.manage_buffer(callback);
142
162
 
143
163
  let context = physical_address
144
164
  ? this.manager.web.get(physical_address)
145
- : account.vm.get_context();
165
+ : this.vm.get_context();
146
166
 
147
167
  if (!context) {
148
168
  if (physical_address)
149
169
  context = this.manager.web.split_set(physical_address);
150
- else return account.flush_buffer(pid);
170
+ else return this.flush_buffer(pid);
151
171
  }
152
172
 
153
173
  let blk = query ? context.explore(query) : context.get_latest_block();
@@ -179,17 +199,17 @@ class Account extends Filesystem {
179
199
  if (blk && blk.metadata && blk.metadata.program) {
180
200
  let program = this.read(blk.metadata.program);
181
201
 
182
- account.vm.contexts.push(context);
202
+ this.vm.contexts.push(context);
183
203
  program.push("pop");
184
204
 
185
205
  Array.isArray(program) &&
186
206
  program.length &&
187
207
  this.manager.push({
188
208
  sequence: program,
189
- account,
209
+ account: this,
190
210
  pid,
191
211
  });
192
- } else account.flush_buffer(pid);
212
+ } else this.flush_buffer(pid);
193
213
  };
194
214
 
195
215
  run_callback = (callback, payload) => {
@@ -257,11 +277,6 @@ class Account extends Filesystem {
257
277
 
258
278
  return this.manager.add_account(name, meta);
259
279
  };
260
-
261
- endpoint = (endpoint, payload) => {
262
- let method = this[endpoint];
263
- typeof method === "function" && method(payload);
264
- };
265
280
  }
266
281
 
267
282
  export default Account;
package/Objects/Block.js CHANGED
@@ -54,16 +54,7 @@ class Block {
54
54
  obj.hash = this.hash;
55
55
  obj.data = this.data;
56
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
66
- );
57
+ obj.children = this.children.map((ch) => ch._id);
67
58
  obj.chain = {
68
59
  hash: this.chain.hash,
69
60
  physical_address: this.chain.physical_address,
package/Objects/Chain.js CHANGED
@@ -28,24 +28,22 @@ class Chain extends Explorer {
28
28
  this.generate_hash();
29
29
 
30
30
  this.folder = this.account.manager.oracle.gds.folder(this.hash);
31
- this.height = this.folder.config.total_files;
32
- this.prev_hash = this.folder.config.recent_file;
33
-
34
- this.manage_config(true);
31
+ this.height = this.folder.config.stats.total_files;
32
+ this.prev_hash = this.folder.config.stats.recent_file;
35
33
 
36
34
  this.account.set_paths(this);
35
+
36
+ this.manage_config(true);
37
37
  }
38
38
 
39
39
  manage_config = (init) => {
40
40
  if (init) {
41
- let dir = this.account.manager.oracle.fs.readdirSync(
42
- this.folder.folder_path
43
- );
41
+ let dir = this.account.manager.oracle.fs.readdirSync(this.folder.path);
44
42
  this.blocks = dir.filter((d) => d !== ".config");
45
43
  this.height = this.blocks.length;
46
44
  }
47
45
 
48
- this.folder.config.object = { ...this.stringify() };
46
+ this.folder.config.add_entry("object", this.stringify());
49
47
  };
50
48
 
51
49
  append_buffer = () => {
@@ -70,7 +68,7 @@ class Chain extends Explorer {
70
68
 
71
69
  build_block = (blk) => {
72
70
  if (typeof blk === "string") {
73
- let blk_data = this.folder.readone(blk);
71
+ let blk_data = this.folder.readone(blk, { depth: 1 });
74
72
 
75
73
  blk = Block.build(blk_data, this);
76
74
  }
@@ -1,3 +1,4 @@
1
+ import { _id } from "generalised-datastore/utils/functions";
1
2
  import framer from "../framer";
2
3
  import opcodes from "../opcodes";
3
4
  import Account from "./Account";
@@ -9,21 +10,31 @@ class Manager {
9
10
  meta = meta || {};
10
11
 
11
12
  this.compression = meta.compression;
12
-
13
+ this.manager_id = _id("manager");
13
14
  this.accounts = new Object();
14
15
  this.running = 0;
15
16
  this.tracks = new Object();
16
17
  this.web = new Blockweb(this);
17
18
  this.oracle = new Oracle(this, { compression: this.compression });
19
+ this.servers = new Array();
18
20
  }
19
21
 
22
+ stringify = (str) => {
23
+ let obj = {
24
+ _id: this.manager_id,
25
+ servers: this.servers,
26
+ };
27
+
28
+ return str ? JSON.stringify(obj) : obj;
29
+ };
30
+
20
31
  initiate = (meta) => {
21
- let initiator = this.add_account(
32
+ this.initiator = this.add_account(
22
33
  process.env.INITIATOR || "initiator",
23
34
  meta
24
35
  );
25
36
 
26
- return initiator;
37
+ return this.initiator;
27
38
  };
28
39
 
29
40
  start_clock = () => {
@@ -59,19 +70,29 @@ class Manager {
59
70
  get_account = (name) => this.accounts[name];
60
71
 
61
72
  add_account = (name, meta) => {
62
- let account = this.get_account(name);
73
+ meta = meta || {};
74
+ if (!name) name = meta.name;
63
75
 
64
- if (!account) {
65
- account = new Account(name, { ...meta, manager: this });
66
- opcodes(account);
67
- framer(account);
76
+ let account;
77
+ if (!name) {
78
+ account = { error: true, error_message: "Name cannot be blank" };
79
+ } else {
80
+ account = this.get_account(name);
81
+
82
+ if (!account) {
83
+ account = new Account(name, { ...meta, manager: this });
84
+
85
+ opcodes(account);
86
+ framer(account, meta.on_framed);
68
87
 
69
- this.accounts[account.name] = account;
70
- } else if (meta && meta.private && !account.private) {
71
- account.private = true;
88
+ this.accounts[account.name] = account;
89
+ if (meta.string) account = account.stringify();
90
+ } else if (meta && meta.private && !account.private) {
91
+ account.private = true;
92
+ }
72
93
  }
73
94
 
74
- return account;
95
+ return meta.string ? JSON.stringify(account) : account;
75
96
  };
76
97
 
77
98
  push = (program) => {
@@ -87,6 +108,36 @@ class Manager {
87
108
  this.running === 1 && this.start_clock();
88
109
  }
89
110
  };
111
+
112
+ get_initiator = () => this.initiator || this.initiate();
113
+
114
+ endpoint = (endpoint, payload, callback) => {
115
+ let account = this.get_account(payload.account);
116
+ if (!account) {
117
+ return this.get_initiator().run_callback(callback, {
118
+ error: true,
119
+ error_message: "Account is not found",
120
+ payload,
121
+ endpoint,
122
+ });
123
+ }
124
+
125
+ if (
126
+ !account.validate(
127
+ payload.program || payload.payload,
128
+ payload.signature,
129
+ callback || payload.callback
130
+ )
131
+ )
132
+ return;
133
+
134
+ let method = account[endpoint];
135
+ typeof method === "function" && method({ ...payload, callback });
136
+ };
137
+
138
+ add_server = (server) => {
139
+ this.servers.push(server);
140
+ };
90
141
  }
91
142
 
92
143
  export default Manager;
package/Objects/Oracle.js CHANGED
@@ -74,57 +74,30 @@ class Oracle {
74
74
  this.fs.writeFileSync(address, data, { encoding: "utf-8" });
75
75
  };
76
76
 
77
- fetch = (payload, callback, val_err_cb) => {
78
- let { physical_address, config, query, account, signature } = payload,
77
+ fetch = async (payload, callback) => {
78
+ let { physical_address, config, remote, query } = payload,
79
79
  result;
80
80
 
81
- account = this.mgr.get_account(account);
82
- if (account && account.private)
83
- if (
84
- !account.validate(
85
- { physical_address, account, query },
86
- signature,
87
- callback
88
- )
89
- )
90
- return typeof val_err_cb === "function" && val_err_cb();
91
-
92
- try {
93
- let folder = this.gds.folder(this.hash(physical_address));
94
-
95
- if (config) {
96
- folder.add_remote(config);
97
-
98
- result = folder.config;
81
+ let folder = this.get_folder(physical_address);
82
+
83
+ if (config) {
84
+ result = folder.config.stringify();
85
+ folder.add_remote(remote);
86
+ } else {
87
+ if (!folder.check_remote(query.operation)) {
88
+ result = {
89
+ error: true,
90
+ error_message: "Query operation cannot be performed remotely.",
91
+ };
99
92
  } else {
100
- if (!folder.check_remote(query.operation)) {
101
- result = {
102
- error: true,
103
- error_message: "Forbidden remote operations",
104
- payload,
105
- };
106
- } else {
107
- if (query.operation === "call") {
108
- let compressed_result = this.compression({ payload, val_err_cb });
109
- if (compressed_result && compressed_result.halt)
110
- return (
111
- typeof callback === "function" &&
112
- callback({ compressed: true, data: compressed_result })
113
- );
114
- query = (compressed_result && compressed_result.query) || query;
115
- }
116
-
117
- let operation = folder[query.operation];
118
-
119
- result =
120
- operation &&
121
- operation(query.query, { ...query.options, account, payload });
122
- }
93
+ let op = folder[query.operation];
94
+
95
+ result = await op(query.query, query.options);
96
+ folder.remote_stuff(remote, result);
123
97
  }
124
- } catch (e) {
125
- result = { error: true, message: e.message, payload };
126
98
  }
127
- typeof callback === "function" && callback(result);
99
+
100
+ callback(result);
128
101
  };
129
102
  }
130
103
 
package/framer.js CHANGED
@@ -49,12 +49,22 @@ let obj = {
49
49
  },
50
50
  };
51
51
 
52
- const framer = (initiator) => {
52
+ const framer = (initiator, cb) => {
53
53
  let frame = new Frame(obj);
54
54
  frame.to_instruction();
55
55
  let instructions = frame.flatten_instructions();
56
56
 
57
- initiator.load({ program: { instructions } });
57
+ let start = Date.now();
58
+ initiator.load({
59
+ program: { instructions },
60
+ callback: (blks) => {
61
+ console.log(
62
+ `Account:${initiator.name} is ready in ${Date.now() - start}ms.`
63
+ );
64
+
65
+ cb && cb(blks);
66
+ },
67
+ });
58
68
  };
59
69
 
60
70
  export default framer;
package/opcodes.js CHANGED
@@ -44,10 +44,14 @@ const opcodes = (account) => {
44
44
 
45
45
  set("hold", (arg) => (vm.track.hold = !!arg.op0));
46
46
 
47
- set("add_server", (args, vm) => account.add_server(args && args.op0));
47
+ set("add_server", (args) => account.add_server(args && args.op0));
48
48
 
49
- [("run", "load", "parse", "create_account")].map((op) =>
50
- set(op, (arg) => arg && arg.op0 && account[op](arg && arg.op0))
49
+ ["run", "load", "parse"].map((op) =>
50
+ set(op, (arg) => arg && arg.op0 && manager.endpoint(op, arg && arg.op0))
51
+ );
52
+
53
+ set("add_account", (arg) =>
54
+ manager.add_account({ ...arg.op0, string: true })
51
55
  );
52
56
  };
53
57
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "1.0.835",
3
+ "version": "1.0.837",
4
4
  "description": "A data mediator.",
5
5
  "main": "Index.js",
6
6
  "types": "types/index.d.ts",
@@ -46,7 +46,7 @@
46
46
  "crypto": "^1.0.1",
47
47
  "elliptic": "^6.5.5",
48
48
  "fs": "^0.0.1-security",
49
- "generalised-datastore": "^1.7.85"
49
+ "generalised-datastore": "^2.0.112"
50
50
  },
51
51
  "publishConfig": {
52
52
  "ignore": [
@@ -1,7 +1,7 @@
1
1
  import http from "http";
2
2
  import { is_port_available } from "./functions";
3
3
 
4
- let PORT = Number(process.env.PORT) || 1408;
4
+ let PORT = Number(process.env.PORT) || 1409;
5
5
 
6
6
  const cb = (res, data) => {
7
7
  if (typeof data !== "string") data = JSON.stringify(data);
@@ -12,16 +12,10 @@ const cb = (res, data) => {
12
12
  let extensions = new Object();
13
13
 
14
14
  const extension = (route, handler) => {
15
- let handlers = extensions[route];
16
- if (!handlers) {
17
- handlers = new Array();
18
- extensions[route] = handlers;
19
- }
20
-
21
- handlers.push(handler);
15
+ extensions[route] = handler;
22
16
  };
23
17
 
24
- const handle_routes = (req, res, app, initiator) => {
18
+ const handle_routes = (req, res, app, manager) => {
25
19
  let data = "";
26
20
 
27
21
  let is_in_default_routes = [
@@ -48,27 +42,26 @@ const handle_routes = (req, res, app, initiator) => {
48
42
  }
49
43
 
50
44
  if (req.url === "/create_account") {
51
- let account = initiator.create_account(data);
52
- res.end(account.stringify(true));
45
+ let account = manager.add_account(null, { ...data, string: true });
46
+
47
+ res.end(account);
53
48
  } else if (["run", "load", "parse"].includes(req.url.slice(1))) {
54
- let payload = {
55
- ...data,
56
- callback: (datagram) => cb(res, datagram),
57
- };
58
- initiator.endpoint(req.url.slice(1), payload);
49
+ manager.endpoint(req.url.slice(1), payload, (datagram) =>
50
+ cb(res, datagram)
51
+ );
59
52
  } else {
60
53
  let extension = extensions[req.url.slice(1)];
61
54
 
62
- extension &&
63
- extension.map((ex) => {
64
- typeof ex === "function" && ex(data, { req, res });
65
- });
55
+ if (typeof extension !== "function") {
56
+ res.writeHead(404, { "Content-Type": "application/json" });
57
+ res.end(JSON.stringify({ error: true, error_message: "Not Found" }));
58
+ } else extension(data, { req, res });
66
59
  }
67
60
  });
68
61
 
69
62
  req.on("error", (e) => {
70
63
  // res.writeHead(500, { "Content-Type": "application/json" });
71
- res.end(JSON.stringify({ status: "error", message: e.message }));
64
+ res.end(JSON.stringify({ status: "error", error_message: e.message }));
72
65
  });
73
66
  } else {
74
67
  app && app(req, res);
@@ -92,11 +85,12 @@ const create_server = async (app, settings) => {
92
85
  PORT = port;
93
86
 
94
87
  let server = http.createServer((req, res) =>
95
- handle_routes(req, res, app, settings.initiator)
88
+ handle_routes(req, res, app, settings.manager)
96
89
  );
97
90
 
98
91
  server.listen(port, () => {
99
92
  console.log(`\n...GOD PROTOCOL RUNNING :${port}`);
93
+ manager.add_server({ ...settings.server_details, port: PORT });
100
94
  });
101
95
  };
102
96