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/Objects/Chain.js CHANGED
@@ -27,9 +27,27 @@ 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
+ this.height = this.folder.config.total_files;
32
+ this.prev_hash = this.folder.config.recent_file;
33
+
34
+ this.manage_config(true);
35
+
30
36
  this.account.set_paths(this);
31
37
  }
32
38
 
39
+ manage_config = (init) => {
40
+ if (init) {
41
+ let dir = this.account.manager.oracle.fs.readdirSync(
42
+ this.folder.folder_path
43
+ );
44
+ this.blocks = dir.filter((d) => d !== ".config");
45
+ this.height = this.blocks.length;
46
+ }
47
+
48
+ this.folder.config.object = { ...this.stringify() };
49
+ };
50
+
33
51
  append_buffer = () => {
34
52
  this.buffs.push({});
35
53
  this.cursors.push(this.cursor);
@@ -50,8 +68,18 @@ class Chain extends Explorer {
50
68
  this.hash = hash(this.physical_address);
51
69
  };
52
70
 
71
+ build_block = (blk) => {
72
+ if (typeof blk === "string") {
73
+ let blk_data = this.folder.readone(blk);
74
+
75
+ blk = Block.build(blk_data, this);
76
+ }
77
+ return blk;
78
+ };
79
+
53
80
  get_block = (index) => {
54
- return this.blocks[index];
81
+ let blk = this.blocks[index];
82
+ return this.build_block(blk);
55
83
  };
56
84
 
57
85
  add_tx = (instruction) => {
@@ -81,6 +109,17 @@ class Chain extends Explorer {
81
109
 
82
110
  block.timelapse = Date.now() - this.start_time;
83
111
 
112
+ this.manage_config();
113
+
114
+ let data = block.stringify();
115
+
116
+ data = this.account.manager.oracle.handle_compression({
117
+ addr: this.folder.folder_path,
118
+ data,
119
+ no_string: true,
120
+ });
121
+
122
+ this.folder.write(data);
84
123
  return block;
85
124
  };
86
125
 
@@ -89,7 +128,12 @@ class Chain extends Explorer {
89
128
  this.height = this.blocks.length;
90
129
  };
91
130
 
92
- get_latest_block = () => this.blocks.slice(-1)[0];
131
+ get_latest_block = () => {
132
+ let blk = this.blocks.slice(-1)[0];
133
+ blk = this.build_block(blk);
134
+
135
+ return blk;
136
+ };
93
137
 
94
138
  stringify = (str) => {
95
139
  let obj = {};
@@ -99,11 +143,16 @@ class Chain extends Explorer {
99
143
  obj.parent = this.parent.path;
100
144
  obj.account = this.parent.account.name;
101
145
  obj.name = this.name;
102
- obj.connections = this.connections.map(
103
- (b) => new Object({ chain: b.chain.hash, block: b.hash })
146
+ obj.connections = this.connections.map((b) =>
147
+ b.stringify
148
+ ? new Object({
149
+ chain: b.chain.hash,
150
+ block: { hash: b.hash, _id: b._id },
151
+ })
152
+ : b
104
153
  );
105
154
  obj.hash = this.hash;
106
- obj.blocks = this.blocks.map((blk) => blk.hash);
155
+ obj.blocks = this.blocks.map((blk) => blk.hash || blk);
107
156
 
108
157
  return str ? JSON.stringify(obj) : obj;
109
158
  };
@@ -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 = {};
@@ -18,22 +18,24 @@ 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
+ if (program.hold) continue;
23
24
 
24
- track.account.vm.execute(instruction, track);
25
- track.pointer++;
25
+ let instruction = program.sequence[program.pointer];
26
26
 
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]);
27
+ program.account.vm.execute(instruction, program);
28
+ program.pointer++;
31
29
 
32
- delete this.tracks[account];
33
- }
30
+ if (program.pointer === program.sequence.length) {
31
+ program.account.flush_buffer(program.pid);
34
32
 
35
- if (track.breakpoint.slice(-1)[0] === track.pointer) {
36
- track.account.flush_buffer(track.pids.splice(-1)[0]);
33
+ track.pop();
34
+ }
35
+ if (!track.length) {
36
+ this.running -= 1;
37
+ delete this.tracks[account];
38
+ program.account.chain.mine(program.account.vm);
37
39
  }
38
40
  }
39
41
 
@@ -48,8 +50,8 @@ class Manager {
48
50
 
49
51
  if (!account) {
50
52
  account = new Account(name, { ...meta, manager: this });
51
- framer(account);
52
53
  opcodes(account);
54
+ framer(account);
53
55
 
54
56
  this.accounts[account.name] = account;
55
57
  } else if (meta && meta.private && !account.private) {
@@ -63,23 +65,10 @@ class Manager {
63
65
  let track = this.tracks[program.account.name];
64
66
 
65
67
  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
- }
68
+ track.push({ ...program, pointer: 0 });
75
69
  } else {
76
- track = {
77
- ...program,
78
- pointer: 0,
79
- pids: [program.pid],
80
- breakpoint: new Array(),
81
- };
82
- this.tracks[track.account.name] = track;
70
+ track = [{ ...program, pointer: 0 }];
71
+ this.tracks[program.account.name] = track;
83
72
 
84
73
  this.running++;
85
74
  this.running === 1 && this.start_clock();
@@ -1,4 +1,4 @@
1
- import { transpile_object } from "../utils/functions";
1
+ import { obj } from "../framer";
2
2
 
3
3
  class Opcodes {
4
4
  constructor() {
@@ -7,6 +7,8 @@ class Opcodes {
7
7
 
8
8
  set = (opcode, circuit) => {
9
9
  this.opcodes[opcode] = circuit;
10
+ obj.Opcodes[opcode] = null;
11
+
10
12
  this.account.assembler.opcodes.push(opcode);
11
13
  };
12
14
 
@@ -17,19 +19,29 @@ class Opcodes {
17
19
 
18
20
  let buf = context.get_buffer();
19
21
 
20
- let args = buf && context.account.read(buf.inputs);
22
+ let args =
23
+ context.account.read(buf && buf.inputs) ||
24
+ this.account.stdin.splice(-1)[0];
21
25
 
22
- let res = circ(args);
26
+ let res = circ(args, this);
23
27
 
24
28
  this.stdin(res);
25
29
  };
26
30
 
27
31
  stdin = (object, cb) => {
28
- if (object == null) return;
32
+ if (object == null) {
33
+ this.flags.void = true;
34
+ return;
35
+ }
36
+ this.flags.void = false;
37
+
38
+ this.flags.zero = !object;
39
+ if (typeof object === "number") this.flags.neg = object < 0;
29
40
 
30
- let code_string = transpile_object(object);
41
+ let code_string =
42
+ typeof object !== "string" ? JSON.stringify(object) : object;
31
43
 
32
- this.account.compiler.run(code_string, { cb, pure: true });
44
+ this.account.assembler.run(code_string, { cb, pure: true });
33
45
  // console.log(`Writing in: ${code_string}`);
34
46
  };
35
47
  }
package/Objects/Oracle.js CHANGED
@@ -1,12 +1,27 @@
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
- constructor(mgr) {
6
+ constructor(mgr, meta) {
7
+ meta = meta || {};
8
+
9
+ this.compression = meta.compression;
5
10
  this.fs = fs;
6
11
  this.mgr = mgr;
7
12
  this.datapaths = new Object();
13
+ this.gds = new GDS(process.env.DATASTORE || "godprotocol").sync({
14
+ manager: this.mgr,
15
+ });
8
16
  }
9
17
 
18
+ get_folder = (physical_address, options) => {
19
+ if (typeof options !== "object") {
20
+ options = { no_new: options ? false : true };
21
+ }
22
+ return this.gds.folder(this.hash(physical_address));
23
+ };
24
+
10
25
  set = (path, { type, obj }) => {
11
26
  this.datapaths[path] = {
12
27
  obj,
@@ -14,12 +29,106 @@ class Oracle {
14
29
  };
15
30
  };
16
31
 
17
- get = (path) => this.datapaths[path];
32
+ get = (path) => {
33
+ let obj = this.datapaths[path];
34
+
35
+ if (!obj) {
36
+ try {
37
+ obj = this.fs.readFileSync(path);
38
+ obj = JSON.parse(obj).data;
39
+ let type = obj && obj.data && typeof obj.data;
40
+ type =
41
+ type === "object" ? (Array.isArray(obj) ? "array" : "twain") : type;
42
+
43
+ obj = { type, obj };
44
+ this.set(path, obj);
45
+ } catch (e) {}
46
+ }
47
+
48
+ return obj;
49
+ };
50
+
51
+ hash = (data) => {
52
+ data = typeof data !== "string" ? JSON.stringify(data) : data;
53
+ return hash(data);
54
+ };
55
+
56
+ handle_compression = ({ addr, data, no_string }) => {
57
+ if (this.compression) {
58
+ let res = this.compression({ addr, data, no_string });
59
+ if (res) data = res.data;
60
+ }
61
+ if (!no_string && (typeof addr !== "string" || typeof data !== "string"))
62
+ return;
63
+
64
+ return data;
65
+ };
66
+
67
+ write = (address, payload) => {
68
+ payload = JSON.stringify({ payload });
69
+ let data = this.handle_compression({
70
+ addr: address,
71
+ data: payload,
72
+ });
73
+
74
+ this.fs.writeFileSync(address, data, { encoding: "utf-8" });
75
+ };
76
+
77
+ fetch = (payload, callback, val_err_cb) => {
78
+ let { physical_address, config, query, account, signature } = payload,
79
+ result;
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
+ result = folder.config;
97
+ } else {
98
+ if (!folder.check_remote(query.operation)) {
99
+ result = {
100
+ error: true,
101
+ error_message: "Forbidden remote operations",
102
+ payload,
103
+ };
104
+ } else {
105
+ if (query.operation === "call") {
106
+ let compressed_result = this.compression({ payload, val_err_cb });
107
+ if (compressed_result && compressed_result.halt)
108
+ return (
109
+ typeof callback === "function" &&
110
+ callback({ compressed: true, data: compressed_result })
111
+ );
112
+ query = (compressed_result && compressed_result.query) || query;
113
+ }
114
+
115
+ let operation = folder[query.operation];
18
116
 
19
- write = (addr, data, meta = { encoding: "utf-8" }) => {
20
- if (typeof data !== "string") data = JSON.stringify(data);
117
+ result =
118
+ operation && operation(query.query, { ...query.options, payload });
21
119
 
22
- this.fs.writeFileSync(addr, data, meta);
120
+ if (result) {
121
+ if (account) {
122
+ // let content = account.read(result.data);
123
+ // if (content != null) result.content = content;
124
+ }
125
+ }
126
+ }
127
+ }
128
+ } catch (e) {
129
+ result = { error: true, message: e.message, payload };
130
+ }
131
+ typeof callback === "function" && callback(result);
23
132
  };
24
133
  }
25
134
 
@@ -7,6 +7,12 @@ 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
+ void: false,
15
+ };
10
16
  }
11
17
 
12
18
  get_context = (index) => {
@@ -68,6 +74,8 @@ class Virtual_machine extends Opcodes {
68
74
 
69
75
  context.add_tx(instruction);
70
76
 
77
+ // console.log(instruction);
78
+
71
79
  let chain;
72
80
 
73
81
  args = this.parse_arg(args, opcode === "cursor");
@@ -77,6 +85,13 @@ class Virtual_machine extends Opcodes {
77
85
  chain = context.account.add_chain(args, context);
78
86
  this.contexts.push(chain);
79
87
 
88
+ let dim = context.folder.config.dimensions;
89
+ if (!dim) {
90
+ dim = [];
91
+ context.folder.config.dimensions = dim;
92
+ }
93
+ !dim.find((d) => d === args) && dim.push(args);
94
+
80
95
  chain.append_buffer();
81
96
 
82
97
  break;
@@ -84,6 +99,12 @@ class Virtual_machine extends Opcodes {
84
99
  chain = context.account.manager.web.get(args);
85
100
 
86
101
  if (!chain) {
102
+ this.account.flush_buffer(this.track.pid, {
103
+ error: true,
104
+ error_message: `LINKing Failed. - ${args}`,
105
+ });
106
+ this.track.pointer = this.track.sequence.length;
107
+
87
108
  console.log(`LINKing Failed. - ${args}`);
88
109
  return;
89
110
  }
@@ -96,7 +117,7 @@ class Virtual_machine extends Opcodes {
96
117
  case "mine":
97
118
  let bloc = context.mine(this, true);
98
119
 
99
- this.account.buff(bloc, this.track.pids.slice(-1)[0]);
120
+ this.account.buff(bloc, this.track.pid);
100
121
 
101
122
  break;
102
123
  case "pop":
@@ -109,7 +130,7 @@ class Virtual_machine extends Opcodes {
109
130
 
110
131
  context.txs = [];
111
132
  }
112
- blk && this.account.buff(blk, this.track.pids.slice(-1)[0]);
133
+ blk && this.account.buff(blk, this.track.pid);
113
134
 
114
135
  this.contexts.pop();
115
136
  break;
package/framer.js CHANGED
@@ -1,61 +1,56 @@
1
1
  import Frame from "./Objects/Frame";
2
2
 
3
- const framer = (initiator) => {
4
- let frame = new Frame({
5
- Opcodes: {
6
- add: null,
7
- equal: null,
8
- multiply: null,
9
- subtract: null,
10
- divide: null,
11
- exp: null,
12
- equal: null,
13
- not_equal: null,
14
- greater_than: null,
15
- less_than: null,
16
- gte: null,
17
- lte: null,
3
+ let obj = {
4
+ Opcodes: {
5
+ add: null,
6
+ equal: null,
7
+ multiply: null,
8
+ subtract: null,
9
+ divide: null,
10
+ exp: null,
11
+ equal: null,
18
12
 
19
- send: null,
20
- readonly: null,
21
- socket_send: null,
13
+ send: null,
14
+ readonly: null,
15
+ socket_send: null,
22
16
 
23
- jmp: null,
24
- jmp_if: null,
25
- hold: null,
17
+ jmp: null,
26
18
 
27
- stdout: null,
28
- stdin: null,
19
+ stdout: null,
20
+ stdin: null,
29
21
 
30
- run: null,
31
- load: null,
32
- parse: null,
33
- create_account: null,
22
+ run: null,
23
+ load: null,
24
+ parse: null,
25
+ create_account: null,
26
+ },
27
+ Datatypes: {
28
+ Number: null,
29
+ String: null,
30
+ Twain: null,
31
+ Array: null,
32
+ },
33
+ Objects: {
34
+ Blockchain: {
35
+ Chain: null,
36
+ Block: null,
34
37
  },
35
- Datatypes: {
36
- Number: null,
37
- String: null,
38
- Twain: null,
39
- Array: null,
38
+ Folder: {
39
+ active_file: null,
40
+ folder: null,
41
+ file: null,
40
42
  },
41
- Objects: {
42
- Blockchain: {
43
- Chain: null,
44
- Block: null,
45
- },
46
- Folder: {
47
- active_file: null,
48
- folder: null,
49
- file: null,
50
- },
51
- File: {
52
- row: null,
53
- parse: null,
54
- column: null,
55
- write: null,
56
- },
43
+ File: {
44
+ row: null,
45
+ parse: null,
46
+ column: null,
47
+ write: null,
57
48
  },
58
- });
49
+ },
50
+ };
51
+
52
+ const framer = (initiator) => {
53
+ let frame = new Frame(obj);
59
54
  frame.to_instruction();
60
55
  let instructions = frame.flatten_instructions();
61
56
 
@@ -63,3 +58,4 @@ const framer = (initiator) => {
63
58
  };
64
59
 
65
60
  export default framer;
61
+ export { obj };
package/opcodes/add.js CHANGED
@@ -5,11 +5,6 @@ let ops = [
5
5
  { name: "divide", sign: "/" },
6
6
  { name: "exp", sign: "**" },
7
7
  { name: "equal", sign: "==" },
8
- { name: "not_equal", sign: "!=" },
9
- { name: "greater_than", sign: ">" },
10
- { name: "less_than", sign: "<" },
11
- { name: "gte", sign: ">=" },
12
- { name: "lte", sign: "<=" },
13
8
  ];
14
9
 
15
10
  export { ops };
@@ -0,0 +1,28 @@
1
+ const getter = (args) => {
2
+ let base = args.op0;
3
+ if (typeof base === "number") return base;
4
+
5
+ if (typeof base === "string") return base[args.op1];
6
+
7
+ if (Array.isArray(base)) return base.slice(args.op1)[0];
8
+
9
+ return base[args.op1];
10
+ };
11
+
12
+ const setter = (args) => {
13
+ let base = args.op0;
14
+ if (typeof base === "number") return base;
15
+
16
+ if (typeof base === "string") {
17
+ base = `${base.slice(0, args.op2)}${args.op1}${base.slice(args.op2 + 1)}`;
18
+ } else {
19
+ if (Array.isArray(base)) {
20
+ if (args.op2 == null || args.op2 >= base.length) base.push(args.op1);
21
+ else base[args.op2] = args.op1;
22
+ } else base[args.op2] = args.op1;
23
+ }
24
+
25
+ return base;
26
+ };
27
+
28
+ export { setter, getter };
package/opcodes/jmp.js CHANGED
@@ -1,15 +1,14 @@
1
- const jmp = (args, vm) => {
2
- vm.account.manager.tracker[vm.mgr_index].pointer += args.index;
3
- };
1
+ const jmp = (args, vm, meta) => {
2
+ meta = meta || {};
3
+ let { flag, inverse } = meta;
4
4
 
5
- const jmp_if = (args, vm) => {
6
- if (args.passed)
7
- vm.account.manager.tracker[vm.mgr_index].pointer += args.index;
8
- };
5
+ let condition;
6
+ if (flag) condition = vm.flags[flag];
7
+ if (inverse) condition = !!!condition;
9
8
 
10
- const hold = (args, vm) => {
11
- let track = vm.account.manager.tracker[vm.mgr_index];
12
- track.hold = args.hold;
9
+ if ((condition || !flag) && typeof args.op0 === "number") {
10
+ vm.track.pointer = args.op0;
11
+ }
13
12
  };
14
13
 
15
- export { jmp, jmp_if, hold };
14
+ export { jmp };
package/opcodes/std.js CHANGED
@@ -1,6 +1,8 @@
1
- const stdout = (args) => {
1
+ const stdout = (args, vm) => {
2
2
  if (!args) return;
3
3
 
4
+ vm.account.stdout.push({ data: args, pid: vm.track.pid });
5
+
4
6
  for (let v in args) console.log(args[v]);
5
7
  };
6
8
 
package/opcodes.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // import { socket_send } from "./Socket";
2
2
  import { ops } from "./opcodes/add";
3
- import { hold, jmp, jmp_if } from "./opcodes/jmp";
3
+ import { getter, setter } from "./opcodes/indices";
4
+ import { jmp } from "./opcodes/jmp";
4
5
  import { stdout } from "./opcodes/std";
5
6
  import { post_request } from "./utils/services";
6
7
 
@@ -13,6 +14,10 @@ const opcodes = (account) => {
13
14
 
14
15
  try {
15
16
  let result = eval(`${args.op0} ${op.sign} ${args.op1}`);
17
+ if (op.sign === "==") {
18
+ account.vm.flags.equal = !!result;
19
+ return;
20
+ }
16
21
 
17
22
  return result;
18
23
  } catch (e) {
@@ -21,16 +26,27 @@ const opcodes = (account) => {
21
26
  });
22
27
  });
23
28
 
24
- set("hold", hold);
29
+ Object.keys(account.vm.flags).map((flag) => {
30
+ set(`jmp_${flag}`, (args, vm) => jmp(args, vm, { flag }));
31
+ set(`jmp_not_${flag}`, (args, vm) =>
32
+ jmp(args, vm, { flag, inverse: true })
33
+ );
34
+ });
25
35
  set("jmp", jmp);
26
- set("jmp_if", jmp_if);
36
+
37
+ set("setter", setter);
38
+ set("getter", getter);
27
39
 
28
40
  set("send", (data) => post_request(data, account.vm.stdin));
29
41
  // set("socket_send", socket_send);
30
42
 
31
43
  set("stdout", stdout);
32
44
 
33
- ["run", "load", "parse", "create_account"].map((op) =>
45
+ set("hold", (arg) => (vm.track.hold = !!arg.op0));
46
+
47
+ set("add_server", (args, vm) => account.add_server(args && args.op0));
48
+
49
+ [("run", "load", "parse", "create_account")].map((op) =>
34
50
  set(op, (arg) => arg && arg.op0 && account[op](arg && arg.op0))
35
51
  );
36
52
  };