godprotocol 1.0.79 → 1.0.82
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 +8 -14
- package/Loader_sequence/main.js +384 -206
- package/Objects/Account.js +191 -157
- package/Objects/Block.js +60 -58
- package/Objects/Blockweb.js +27 -34
- package/Objects/Chain.js +115 -118
- package/Objects/Explorer.js +30 -37
- package/Objects/Filesystem.js +95 -80
- package/Objects/Frame.js +43 -48
- package/Objects/Manager.js +67 -89
- package/Objects/Opcodes.js +43 -36
- package/Objects/Oracle.js +63 -40
- package/Objects/Virtual_machine.js +135 -127
- package/build/Account.js +187 -0
- package/build/Block.js +68 -0
- package/build/Blockweb.js +36 -0
- package/build/Chain.js +119 -0
- package/build/Explorer.js +47 -0
- package/build/Filesystem.js +127 -0
- package/build/Frame.js +52 -0
- package/build/Index.js +20 -0
- package/build/Manager.js +86 -0
- package/build/Opcodes.js +109 -0
- package/build/Oracle.js +81 -0
- package/build/Virtual_machine.js +136 -0
- package/build/add.js +26 -0
- package/build/create_server.js +132 -0
- package/build/framer.js +59 -0
- package/build/functions.js +82 -0
- package/build/hash.js +14 -0
- package/build/indices.js +31 -0
- package/build/jmp.js +19 -0
- package/build/main.js +327 -0
- package/build/opcodes.js +60 -0
- package/build/privacy.js +27 -0
- package/build/services.js +50 -0
- package/build/std.js +15 -0
- package/framer.js +58 -54
- package/opcodes/add.js +9 -40
- package/opcodes/indices.js +28 -0
- package/opcodes/jmp.js +12 -17
- package/opcodes/std.js +7 -9
- package/opcodes.js +36 -28
- package/package.json +4 -3
- package/tsconfig.json +8 -0
- package/types/Account.d.ts +73 -0
- package/types/Block.d.ts +22 -0
- package/types/Blockweb.d.ts +14 -0
- package/types/Chain.d.ts +35 -0
- package/types/Explorer.d.ts +9 -0
- package/types/Filesystem.d.ts +23 -0
- package/types/Frame.d.ts +14 -0
- package/types/Manager.d.ts +21 -0
- package/types/Opcodes.d.ts +11 -0
- package/types/Oracle.d.ts +17 -0
- package/types/Virtual_machine.d.ts +20 -0
- package/types/index.d.ts +4 -0
- package/utils/create_server.js +88 -108
- package/utils/functions.js +31 -43
- package/utils/hash.js +10 -11
- package/utils/privacy.js +10 -15
- package/utils/services.js +32 -33
package/Objects/Manager.js
CHANGED
|
@@ -1,99 +1,77 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
var frequency = Number(process.env.HERTS) || 10;
|
|
33
|
-
_this.clock = setInterval(function () {
|
|
34
|
-
for (var account in _this.tracks) {
|
|
35
|
-
var track = _this.tracks[account];
|
|
36
|
-
var instruction = track.sequence[track.pointer];
|
|
37
|
-
track.account.vm.execute(instruction, track);
|
|
38
|
-
track.pointer++;
|
|
39
|
-
if (track.pointer === track.sequence.length) {
|
|
40
|
-
_this.running--;
|
|
41
|
-
track.account.chain.mine(track.account.vm);
|
|
42
|
-
track.account.flush_buffer(track.pids.splice(-1)[0]);
|
|
43
|
-
delete _this.tracks[account];
|
|
1
|
+
import framer from "../framer";
|
|
2
|
+
import opcodes from "../opcodes";
|
|
3
|
+
import Account from "./Account";
|
|
4
|
+
import Blockweb from "./Blockweb";
|
|
5
|
+
import Oracle from "./Oracle";
|
|
6
|
+
|
|
7
|
+
class Manager {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.accounts = new Object();
|
|
10
|
+
this.running = 0;
|
|
11
|
+
this.tracks = new Object();
|
|
12
|
+
this.web = new Blockweb(this);
|
|
13
|
+
this.oracle = new Oracle(this);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
start_clock = () => {
|
|
17
|
+
let frequency = Number(process.env.HERTS) || 10;
|
|
18
|
+
this.clock = setInterval(() => {
|
|
19
|
+
for (let account in this.tracks) {
|
|
20
|
+
let track = this.tracks[account];
|
|
21
|
+
let program = track.slice(-1)[0];
|
|
22
|
+
|
|
23
|
+
let instruction = program.sequence[program.pointer];
|
|
24
|
+
|
|
25
|
+
program.account.vm.execute(instruction, program);
|
|
26
|
+
program.pointer++;
|
|
27
|
+
|
|
28
|
+
if (program.pointer === program.sequence.length) {
|
|
29
|
+
program.account.flush_buffer(program.pid);
|
|
30
|
+
|
|
31
|
+
track.pop();
|
|
44
32
|
}
|
|
45
|
-
if (track.
|
|
46
|
-
|
|
33
|
+
if (!track.length) {
|
|
34
|
+
this.running -= 1;
|
|
35
|
+
delete this.tracks[account];
|
|
36
|
+
program.account.chain.mine(program.account.vm);
|
|
47
37
|
}
|
|
48
38
|
}
|
|
49
|
-
|
|
39
|
+
|
|
40
|
+
!this.running && clearInterval(this.clock);
|
|
50
41
|
}, frequency);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
get_account = (name) => this.accounts[name];
|
|
45
|
+
|
|
46
|
+
add_account = (name, meta) => {
|
|
47
|
+
let account = this.get_account(name);
|
|
48
|
+
|
|
57
49
|
if (!account) {
|
|
58
|
-
account = new
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
account["private"] = true;
|
|
50
|
+
account = new Account(name, { ...meta, manager: this });
|
|
51
|
+
opcodes(account);
|
|
52
|
+
framer(account);
|
|
53
|
+
|
|
54
|
+
this.accounts[account.name] = account;
|
|
55
|
+
} else if (meta && meta.private && !account.private) {
|
|
56
|
+
account.private = true;
|
|
66
57
|
}
|
|
58
|
+
|
|
67
59
|
return account;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
push = (program) => {
|
|
63
|
+
let track = this.tracks[program.account.name];
|
|
64
|
+
|
|
71
65
|
if (track) {
|
|
72
|
-
|
|
73
|
-
track.breakpoint.push(track.pointer + program.sequence.length + 1);
|
|
74
|
-
(_track$sequence = track.sequence).splice.apply(_track$sequence, [track.pointer + 1, 0].concat(_toConsumableArray(program.sequence)));
|
|
75
|
-
track.pids.push(program.pid);
|
|
76
|
-
if (!_this.running) {
|
|
77
|
-
_this.running++;
|
|
78
|
-
_this.tracks[program.account.name] = track;
|
|
79
|
-
_this.start_clock();
|
|
80
|
-
}
|
|
66
|
+
track.push({ ...program, pointer: 0 });
|
|
81
67
|
} else {
|
|
82
|
-
track =
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
_this.tracks[track.account.name] = track;
|
|
88
|
-
_this.running++;
|
|
89
|
-
_this.running === 1 && _this.start_clock();
|
|
68
|
+
track = [{ ...program, pointer: 0 }];
|
|
69
|
+
this.tracks[program.account.name] = track;
|
|
70
|
+
|
|
71
|
+
this.running++;
|
|
72
|
+
this.running === 1 && this.start_clock();
|
|
90
73
|
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
this.web = new _Blockweb["default"](this);
|
|
96
|
-
this.oracle = new _Oracle["default"](this);
|
|
97
|
-
});
|
|
98
|
-
var _default = Manager;
|
|
99
|
-
exports["default"] = _default;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export default Manager;
|
package/Objects/Opcodes.js
CHANGED
|
@@ -1,39 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
_this.opcodes[opcode] = circuit;
|
|
20
|
-
_this.account.compiler.opcodes.push(opcode);
|
|
21
|
-
});
|
|
22
|
-
_defineProperty(this, "prepare_operation", function (opcode, context) {
|
|
23
|
-
var circ = _this.opcodes[opcode];
|
|
1
|
+
import { obj } from "../framer";
|
|
2
|
+
import { transpile_object } from "../utils/functions";
|
|
3
|
+
|
|
4
|
+
class Opcodes {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.opcodes = new Object();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
set = (opcode, circuit) => {
|
|
10
|
+
this.opcodes[opcode] = circuit;
|
|
11
|
+
obj.Opcodes[opcode] = null;
|
|
12
|
+
|
|
13
|
+
this.account.assembler.opcodes.push(opcode);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
prepare_operation = (opcode, context) => {
|
|
17
|
+
let circ = this.opcodes[opcode];
|
|
18
|
+
|
|
24
19
|
if (typeof circ !== "function") return;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
20
|
+
|
|
21
|
+
let buf = context.get_buffer();
|
|
22
|
+
|
|
23
|
+
let args =
|
|
24
|
+
context.account.read(buf && buf.inputs) ||
|
|
25
|
+
this.account.stdin.splice(-1)[0];
|
|
26
|
+
|
|
27
|
+
let res = circ(args, this);
|
|
28
|
+
|
|
29
|
+
this.stdin(res);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
stdin = (object, cb) => {
|
|
31
33
|
if (object == null) return;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
|
|
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;
|
|
40
|
+
|
|
41
|
+
this.account.assembler.run(code_string, { cb, pure: true });
|
|
34
42
|
// console.log(`Writing in: ${code_string}`);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
exports["default"] = _default;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default Opcodes;
|
package/Objects/Oracle.js
CHANGED
|
@@ -1,42 +1,65 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
var _this = this;
|
|
18
|
-
_classCallCheck(this, Oracle);
|
|
19
|
-
_defineProperty(this, "set", function (path, _ref) {
|
|
20
|
-
var type = _ref.type,
|
|
21
|
-
obj = _ref.obj;
|
|
22
|
-
_this.datapaths[path] = {
|
|
23
|
-
obj: obj,
|
|
24
|
-
type: type
|
|
25
|
-
};
|
|
26
|
-
});
|
|
27
|
-
_defineProperty(this, "get", function (path) {
|
|
28
|
-
return _this.datapaths[path];
|
|
29
|
-
});
|
|
30
|
-
_defineProperty(this, "write", function (addr, data) {
|
|
31
|
-
var meta = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
|
|
32
|
-
encoding: "utf-8"
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import GDS from "generalised-datastore";
|
|
3
|
+
import { hash } from "../utils/hash";
|
|
4
|
+
|
|
5
|
+
class Oracle {
|
|
6
|
+
constructor(mgr) {
|
|
7
|
+
this.fs = fs;
|
|
8
|
+
this.mgr = mgr;
|
|
9
|
+
this.datapaths = new Object();
|
|
10
|
+
this.gds = new GDS("godprotocol").sync();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
set = (path, { type, obj }) => {
|
|
14
|
+
this.datapaths[path] = {
|
|
15
|
+
obj,
|
|
16
|
+
type,
|
|
33
17
|
};
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
get = (path) => this.datapaths[path];
|
|
21
|
+
|
|
22
|
+
hash = (data) => {
|
|
23
|
+
data = JSON.stringify(data);
|
|
24
|
+
return hash(hash);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
write = (addr, data, account) => {
|
|
34
28
|
if (typeof data !== "string") data = JSON.stringify(data);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
29
|
+
|
|
30
|
+
let folder = this.gds.folder(
|
|
31
|
+
hash(account.vm.get_context().physical_address)
|
|
32
|
+
);
|
|
33
|
+
folder.write({ data: addr });
|
|
34
|
+
|
|
35
|
+
this.fs.writeFileSync(addr, data, { encoding: "utf-8" });
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
fetch = (payload, callback) => {
|
|
39
|
+
let { physical_address, query, account, signature } = payload,
|
|
40
|
+
result;
|
|
41
|
+
|
|
42
|
+
account = this.mgr.get_account(account);
|
|
43
|
+
if (account && account.private)
|
|
44
|
+
if (!account.validate({ physical_address, query }, signature, callback))
|
|
45
|
+
return;
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
let operation = this.gds.folder(hash(physical_address))[query.operation];
|
|
49
|
+
|
|
50
|
+
result = operation && operation(query.query, query.options);
|
|
51
|
+
|
|
52
|
+
if (result) {
|
|
53
|
+
if (account) {
|
|
54
|
+
let content = account.read(result.data);
|
|
55
|
+
if (content != null) result.content = content;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
} catch (e) {
|
|
59
|
+
result = { error: true, message: e.message, payload };
|
|
60
|
+
}
|
|
61
|
+
typeof callback === "function" && callback(result);
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default Oracle;
|
|
@@ -1,128 +1,136 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = _getPrototypeOf(t); if (r) { var s = _getPrototypeOf(this).constructor; e = Reflect.construct(o, arguments, s); } else e = o.apply(this, arguments); return _possibleConstructorReturn(this, e); }; }
|
|
16
|
-
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
|
|
17
|
-
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
|
|
18
|
-
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
19
|
-
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
|
|
20
|
-
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
21
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
22
|
-
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
23
|
-
var Virtual_machine = /*#__PURE__*/function (_Opcodes) {
|
|
24
|
-
_inherits(Virtual_machine, _Opcodes);
|
|
25
|
-
var _super = _createSuper(Virtual_machine);
|
|
26
|
-
function Virtual_machine(_context) {
|
|
27
|
-
var _this;
|
|
28
|
-
_classCallCheck(this, Virtual_machine);
|
|
29
|
-
_this = _super.call(this);
|
|
30
|
-
_defineProperty(_assertThisInitialized(_this), "get_context", function (index) {
|
|
31
|
-
return _this.contexts.slice(index == null ? -1 : index)[0];
|
|
32
|
-
});
|
|
33
|
-
_defineProperty(_assertThisInitialized(_this), "parse_arg", function (arg, is_cursor) {
|
|
34
|
-
var context = _this.get_context();
|
|
35
|
-
var args = arg.split("/"),
|
|
36
|
-
i = 0;
|
|
37
|
-
while (args[0] === "..") {
|
|
38
|
-
i++;
|
|
39
|
-
args.splice(0, 1);
|
|
40
|
-
}
|
|
41
|
-
i = (i + 1) * -1;
|
|
42
|
-
context = i ? _this.get_context(i) : context;
|
|
43
|
-
arg = args.join("/");
|
|
44
|
-
while (arg.includes("^")) {
|
|
45
|
-
var blk = context.connections.slice(-1)[0];
|
|
46
|
-
if (!blk) break;
|
|
47
|
-
context = blk.chain;
|
|
48
|
-
var _i = arg.indexOf("^");
|
|
49
|
-
arg = "".concat(arg.slice(0, _i)).concat(arg.slice(_i + 1));
|
|
50
|
-
}
|
|
51
|
-
if (arg.startsWith("{*") && is_cursor) {
|
|
52
|
-
arg = context.explore(arg.slice(2, -1));
|
|
53
|
-
} else if (arg.startsWith("{") && !is_cursor) {
|
|
54
|
-
arg = context.explore(arg.slice(1, -1));
|
|
55
|
-
}
|
|
56
|
-
return arg;
|
|
57
|
-
});
|
|
58
|
-
_defineProperty(_assertThisInitialized(_this), "split_instruction", function (instruction) {
|
|
59
|
-
var split = instruction.split(" ");
|
|
60
|
-
var opcode = split[0],
|
|
61
|
-
args = split.slice(1).join(" ");
|
|
62
|
-
return {
|
|
63
|
-
opcode: opcode,
|
|
64
|
-
args: args
|
|
65
|
-
};
|
|
66
|
-
});
|
|
67
|
-
_defineProperty(_assertThisInitialized(_this), "execute", function (instruction, track) {
|
|
68
|
-
_this.track = track;
|
|
69
|
-
if (!instruction) return;
|
|
70
|
-
var _this$split_instructi = _this.split_instruction(instruction),
|
|
71
|
-
opcode = _this$split_instructi.opcode,
|
|
72
|
-
args = _this$split_instructi.args;
|
|
73
|
-
var context = _this.get_context();
|
|
74
|
-
if (!context) return;
|
|
75
|
-
context.add_tx(instruction);
|
|
76
|
-
var chain;
|
|
77
|
-
args = _this.parse_arg(args, opcode === "cursor");
|
|
78
|
-
switch (opcode) {
|
|
79
|
-
case "chain":
|
|
80
|
-
chain = context.account.add_chain(args, context);
|
|
81
|
-
_this.contexts.push(chain);
|
|
82
|
-
chain.append_buffer();
|
|
83
|
-
break;
|
|
84
|
-
case "link":
|
|
85
|
-
chain = context.account.manager.web.get(args);
|
|
86
|
-
if (!chain) {
|
|
87
|
-
console.log("LINKing Failed. - ".concat(args));
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
chain.append_buffer();
|
|
91
|
-
_this.contexts.push(chain);
|
|
92
|
-
break;
|
|
93
|
-
case "mine":
|
|
94
|
-
var bloc = context.mine(_assertThisInitialized(_this), true);
|
|
95
|
-
_this.account.buff(bloc, _this.track.pid);
|
|
96
|
-
break;
|
|
97
|
-
case "pop":
|
|
98
|
-
var blk;
|
|
99
|
-
if (context.txs.length > 1) {
|
|
100
|
-
blk = context.mine(_assertThisInitialized(_this));
|
|
101
|
-
} else {
|
|
102
|
-
blk = context.get_latest_block();
|
|
103
|
-
if (blk) _this.get_context(-2).connections.push(blk);
|
|
104
|
-
context.txs = [];
|
|
105
|
-
}
|
|
106
|
-
blk && _this.account.buff(blk, _this.track.pid);
|
|
107
|
-
_this.contexts.pop();
|
|
108
|
-
break;
|
|
109
|
-
case "cursor":
|
|
110
|
-
context.set_cursor(args);
|
|
111
|
-
break;
|
|
112
|
-
case "write":
|
|
113
|
-
context.account.write(args, context);
|
|
114
|
-
break;
|
|
115
|
-
default:
|
|
116
|
-
_this.prepare_operation(opcode, context);
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
_this.account = _context.account;
|
|
121
|
-
_this.contexts = [_context];
|
|
122
|
-
_this.stack = new Array();
|
|
123
|
-
return _this;
|
|
1
|
+
import Opcodes from "./Opcodes";
|
|
2
|
+
|
|
3
|
+
class Virtual_machine extends Opcodes {
|
|
4
|
+
constructor(context) {
|
|
5
|
+
super();
|
|
6
|
+
|
|
7
|
+
this.account = context.account;
|
|
8
|
+
this.contexts = [context];
|
|
9
|
+
this.stack = new Array();
|
|
10
|
+
this.flags = {
|
|
11
|
+
neg: false,
|
|
12
|
+
equal: false,
|
|
13
|
+
zero: false,
|
|
14
|
+
};
|
|
124
15
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
16
|
+
|
|
17
|
+
get_context = (index) => {
|
|
18
|
+
return this.contexts.slice(index == null ? -1 : index)[0];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
parse_arg = (arg, is_cursor) => {
|
|
22
|
+
let context = this.get_context();
|
|
23
|
+
|
|
24
|
+
let args = arg.split("/"),
|
|
25
|
+
i = 0;
|
|
26
|
+
|
|
27
|
+
while (args[0] === "..") {
|
|
28
|
+
i++;
|
|
29
|
+
|
|
30
|
+
args.splice(0, 1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
i = (i + 1) * -1;
|
|
34
|
+
context = i ? this.get_context(i) : context;
|
|
35
|
+
|
|
36
|
+
arg = args.join("/");
|
|
37
|
+
|
|
38
|
+
while (arg.includes("^")) {
|
|
39
|
+
let blk = context.connections.slice(-1)[0];
|
|
40
|
+
if (!blk) break;
|
|
41
|
+
context = blk.chain;
|
|
42
|
+
let i = arg.indexOf("^");
|
|
43
|
+
arg = `${arg.slice(0, i)}${arg.slice(i + 1)}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (arg.startsWith("{*") && is_cursor) {
|
|
47
|
+
arg = context.explore(arg.slice(2, -1));
|
|
48
|
+
} else if (arg.startsWith("{") && !is_cursor) {
|
|
49
|
+
arg = context.explore(arg.slice(1, -1));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return arg;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
split_instruction = (instruction) => {
|
|
56
|
+
let split = instruction.split(" ");
|
|
57
|
+
let opcode = split[0],
|
|
58
|
+
args = split.slice(1).join(" ");
|
|
59
|
+
|
|
60
|
+
return { opcode, args };
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
execute = (instruction, track) => {
|
|
64
|
+
this.track = track;
|
|
65
|
+
|
|
66
|
+
if (!instruction) return;
|
|
67
|
+
|
|
68
|
+
let { opcode, args } = this.split_instruction(instruction);
|
|
69
|
+
|
|
70
|
+
let context = this.get_context();
|
|
71
|
+
|
|
72
|
+
if (!context) return;
|
|
73
|
+
|
|
74
|
+
context.add_tx(instruction);
|
|
75
|
+
|
|
76
|
+
// console.log(instruction);
|
|
77
|
+
|
|
78
|
+
let chain;
|
|
79
|
+
|
|
80
|
+
args = this.parse_arg(args, opcode === "cursor");
|
|
81
|
+
|
|
82
|
+
switch (opcode) {
|
|
83
|
+
case "chain":
|
|
84
|
+
chain = context.account.add_chain(args, context);
|
|
85
|
+
this.contexts.push(chain);
|
|
86
|
+
|
|
87
|
+
chain.append_buffer();
|
|
88
|
+
|
|
89
|
+
break;
|
|
90
|
+
case "link":
|
|
91
|
+
chain = context.account.manager.web.get(args);
|
|
92
|
+
|
|
93
|
+
if (!chain) {
|
|
94
|
+
console.log(`LINKing Failed. - ${args}`);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
chain.append_buffer();
|
|
99
|
+
|
|
100
|
+
this.contexts.push(chain);
|
|
101
|
+
|
|
102
|
+
break;
|
|
103
|
+
case "mine":
|
|
104
|
+
let bloc = context.mine(this, true);
|
|
105
|
+
|
|
106
|
+
this.account.buff(bloc, this.track.pid);
|
|
107
|
+
|
|
108
|
+
break;
|
|
109
|
+
case "pop":
|
|
110
|
+
let blk;
|
|
111
|
+
if (context.txs.length > 1) {
|
|
112
|
+
blk = context.mine(this);
|
|
113
|
+
} else {
|
|
114
|
+
blk = context.get_latest_block();
|
|
115
|
+
if (blk) this.get_context(-2).connections.push(blk);
|
|
116
|
+
|
|
117
|
+
context.txs = [];
|
|
118
|
+
}
|
|
119
|
+
blk && this.account.buff(blk, this.track.pid);
|
|
120
|
+
|
|
121
|
+
this.contexts.pop();
|
|
122
|
+
break;
|
|
123
|
+
case "cursor":
|
|
124
|
+
context.set_cursor(args);
|
|
125
|
+
break;
|
|
126
|
+
case "write":
|
|
127
|
+
context.account.write(args, context);
|
|
128
|
+
break;
|
|
129
|
+
default:
|
|
130
|
+
this.prepare_operation(opcode, context);
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export default Virtual_machine;
|