godprotocol 1.1.0 → 1.1.2
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/Datatypes/arr.js +11 -0
- package/Datatypes/bool.js +11 -0
- package/Datatypes/callable.js +11 -0
- package/Datatypes/functions/storage.js +5 -0
- package/Datatypes/num.js +11 -0
- package/Datatypes/str.js +11 -0
- package/Datatypes/twa.js +11 -0
- package/Datatypes/voi.js +11 -0
- package/Index.js +36 -2
- package/Objects/Account.js +35 -322
- package/Objects/Block.js +7 -80
- package/Objects/Blockweb.js +19 -26
- package/Objects/Callable.js +40 -0
- package/Objects/Chain.js +20 -244
- package/Objects/GDS/Arena_classic/.config +1 -0
- package/Objects/GDS/Arena_classic/Accounts/lola/.config +1 -0
- package/Objects/Manager.js +28 -294
- package/Objects/Oracle.js +67 -84
- package/Objects/Repository.js +67 -0
- package/Objects/Trinity.js +24 -0
- package/Objects/Virtual_machine.js +16 -206
- package/callables/functions/assign.js +6 -0
- package/callables/functions/display.js +3 -0
- package/callables/register.js +22 -0
- package/package.json +24 -43
- package/utils/create_server.js +35 -88
- package/utils/functions.js +0 -70
- package/utils/server.cert +23 -0
- package/utils/server.key +28 -0
- package/utils/services.js +61 -21
- package/utils/vm_utils.js +53 -275
- package/Executables/callables.js +0 -131
- package/Executables/executables.js +0 -230
- package/Executables/functions/adminstrator/adm_register.js +0 -29
- package/Executables/functions/adminstrator/native_components.js +0 -10
- package/Executables/functions/adminstrator/query_selectors.js +0 -10
- package/Executables/functions/adminstrator/render.js +0 -24
- package/Executables/functions/array_register.js +0 -161
- package/Executables/functions/assign.js +0 -19
- package/Executables/functions/display.js +0 -55
- package/Executables/functions/folder.js +0 -95
- package/Executables/functions/folder_register.js +0 -87
- package/Executables/functions/importcheck.js +0 -13
- package/Executables/functions/indices.js +0 -56
- package/Executables/functions/len.js +0 -22
- package/Executables/functions/methods/array.js +0 -272
- package/Executables/functions/methods/string.js +0 -197
- package/Executables/functions/methods/twain.js +0 -148
- package/Executables/functions/op.js +0 -15
- package/Executables/functions/price_register.js +0 -30
- package/Executables/functions/pricing.js +0 -26
- package/Executables/functions/servers/account.js +0 -81
- package/Executables/functions/servers/account_register.js +0 -56
- package/Executables/functions/servers/block.js +0 -21
- package/Executables/functions/servers/block_register.js +0 -18
- package/Executables/functions/servers/chain.js +0 -42
- package/Executables/functions/servers/chain_register.js +0 -28
- package/Executables/functions/set_error_catch.js +0 -34
- package/Executables/functions/string_register.js +0 -130
- package/Executables/functions/twain_register.js +0 -56
- package/Executables/functions/typecast.js +0 -43
- package/Executables/functions/utils.js +0 -9
- package/Executables/stack.js +0 -53
- package/Loader_sequence/main.js +0 -805
- package/Loader_sequence/repository.js +0 -33
- package/Objects/Explorer.js +0 -40
- package/Objects/Fee.js +0 -33
- package/Objects/Filesystem.js +0 -216
- package/Objects/Frame.js +0 -54
- package/Objects/Opcodes.js +0 -87
- package/Objects/Protocol.js +0 -5
- package/Trinity.js +0 -20
- package/callables.js +0 -172
- package/framer.js +0 -124
- package/opcodes/add.js +0 -15
- package/opcodes/indices.js +0 -82
- package/opcodes/jmp.js +0 -16
- package/opcodes/std.js +0 -12
- package/opcodes.js +0 -96
- package/readme.md +0 -133
- package/starter_scripts/constants.seq +0 -11
- package/tsconfig.json +0 -8
- package/types/Account.d.ts +0 -73
- package/types/Block.d.ts +0 -22
- package/types/Blockweb.d.ts +0 -14
- package/types/Chain.d.ts +0 -35
- package/types/Explorer.d.ts +0 -9
- package/types/Filesystem.d.ts +0 -23
- package/types/Frame.d.ts +0 -14
- package/types/Manager.d.ts +0 -21
- package/types/Opcodes.d.ts +0 -11
- package/types/Oracle.d.ts +0 -17
- package/types/Virtual_machine.d.ts +0 -20
- package/types/index.d.ts +0 -4
- package/utils/hash.js +0 -13
- package/utils/logger.js +0 -79
- package/utils/privacy.js +0 -22
- package/utils/type_coersion.js +0 -3
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
class Callable {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.objects = new Object();
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
parse_arguments = async (payload, config) => {
|
|
7
|
+
let { args } = payload;
|
|
8
|
+
let aggs = new Object();
|
|
9
|
+
|
|
10
|
+
for (let a = 0; a < args.length; a++) {
|
|
11
|
+
let arg = args[a];
|
|
12
|
+
|
|
13
|
+
let content = await this.read_chain(arg),
|
|
14
|
+
param;
|
|
15
|
+
if (content.type === "variable") {
|
|
16
|
+
param = config.parameters.find((p) => p.name === content.identifier);
|
|
17
|
+
|
|
18
|
+
if (!param) return await this.stderr({ name: "unidentified_argument" });
|
|
19
|
+
|
|
20
|
+
content = await this.read_chain(content.value);
|
|
21
|
+
} else param = config.parameters[a];
|
|
22
|
+
|
|
23
|
+
aggs[param.name] = content;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return aggs;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
callable = async (name) => {
|
|
30
|
+
let object = this.objects[name];
|
|
31
|
+
|
|
32
|
+
return object;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
set = (name, config) => {
|
|
36
|
+
this.objects[name] = config;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default Callable;
|
package/Objects/Chain.js
CHANGED
|
@@ -1,262 +1,38 @@
|
|
|
1
|
-
import { _id } from "generalised-datastore/utils/functions";
|
|
2
|
-
import { hash } from "../utils/hash";
|
|
3
1
|
import Block from "./Block";
|
|
4
|
-
import Explorer from "./Explorer";
|
|
5
2
|
|
|
6
|
-
class Chain
|
|
7
|
-
constructor(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
this.
|
|
11
|
-
this.
|
|
12
|
-
this.name = name || this.parent.name;
|
|
13
|
-
|
|
14
|
-
this.physical_address =
|
|
15
|
-
this.parent.manager || !name
|
|
16
|
-
? this.parent.physical_address
|
|
17
|
-
: `${this.parent.physical_address}/${this.name}`;
|
|
18
|
-
|
|
19
|
-
this.pending_children = new Array();
|
|
20
|
-
this.connections = new Array();
|
|
21
|
-
|
|
22
|
-
this.txs = new Array();
|
|
23
|
-
|
|
24
|
-
this.height = 0;
|
|
25
|
-
this.cursor = -1;
|
|
26
|
-
this.cursors = new Array();
|
|
27
|
-
this.buffs = new Array();
|
|
28
|
-
|
|
29
|
-
this.generate_hash();
|
|
30
|
-
|
|
31
|
-
this.folder = this.account.manager.oracle.gds.folder(this.hash);
|
|
32
|
-
this.height = this.folder.config.stats.files;
|
|
33
|
-
this.blocks = new Array(this.height);
|
|
34
|
-
|
|
35
|
-
if (this.folder.config.stats.chain)
|
|
36
|
-
this.datapath = this.folder.config.stats.chain.datapath
|
|
37
|
-
|
|
38
|
-
// console.log(this.folder.config)
|
|
39
|
-
|
|
40
|
-
this.account.set_paths(this);
|
|
41
|
-
|
|
42
|
-
this.manage_config(true);
|
|
43
|
-
|
|
44
|
-
if (this.folder.config.stats.recent_file){
|
|
45
|
-
this.recent_block = this.build_block(this.folder.config.stats.recent_file);
|
|
46
|
-
this.blocks[this.recent_block.index] = this.recent_block
|
|
47
|
-
}
|
|
3
|
+
class Chain {
|
|
4
|
+
constructor(address, account) {
|
|
5
|
+
this.physical_address = address;
|
|
6
|
+
this.account = account;
|
|
7
|
+
this.blocks = new Array();
|
|
8
|
+
this.servers = new Array();
|
|
48
9
|
}
|
|
49
10
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
this.folder.config.add_entry("chain", this.stringify());
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
set_fee = ({purpose, fee, validator})=>{
|
|
58
|
-
this.fees.set({purpose, fee, validator})
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
get_fee = purpose =>{
|
|
62
|
-
return this.fees.get(purpose)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
pay = (purpose, payer, cb) =>{
|
|
66
|
-
let account = this.account.manager.get_account(payer)
|
|
67
|
-
let token = this.get_fee(purpose)
|
|
68
|
-
|
|
69
|
-
account.process_fee(token, cb)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
set_obj_config = config=>{
|
|
73
|
-
let addr = typeof config === 'string'? config: this.account.save(config)
|
|
74
|
-
|
|
75
|
-
this.datapath = addr;
|
|
76
|
-
this.manage_config()
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
append_buffer = () => {
|
|
80
|
-
this.buffs.push({});
|
|
81
|
-
this.cursors.push(this.cursor);
|
|
82
|
-
this.cursor = -1;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
set_cursor = (cursor) => {
|
|
86
|
-
this.cursor = cursor;
|
|
87
|
-
this.cursors.push(cursor);
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
get_buffer = () => {
|
|
91
|
-
this.cursor = this.cursors.pop();
|
|
92
|
-
return this.buffs.slice(-1)[0];
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
generate_hash = () => {
|
|
96
|
-
this.hash = hash(this.physical_address);
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
build_block = (blk, forge) => {
|
|
100
|
-
if (typeof blk === "string") {
|
|
101
|
-
let blk_data = this.folder.readone(blk, { depth: 1 });
|
|
102
|
-
|
|
103
|
-
if (blk_data){
|
|
104
|
-
blk = Block.build(blk_data, this);
|
|
105
|
-
}
|
|
106
|
-
}else if (forge){
|
|
107
|
-
if(!blk.hash) blk.hash = hash(JSON.stringify(blk))
|
|
108
|
-
blk = Block.build(blk, this)
|
|
109
|
-
}
|
|
110
|
-
return blk;
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
traverse_blocks = (stop) => {
|
|
114
|
-
let blk = this.blocks.slice(stop).find(b=>b && b._id);
|
|
115
|
-
|
|
116
|
-
while (blk&& blk.index !== stop) {
|
|
117
|
-
blk = this.build_block(blk.previous_hash);
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
return blk;
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
get_block = (index) => {
|
|
124
|
-
index = index == null ? this.height -1 : index
|
|
125
|
-
let blk = this.blocks[index];
|
|
126
|
-
|
|
127
|
-
if(blk) blk = this.build_block(blk);
|
|
128
|
-
|
|
129
|
-
if (!blk||(blk&& blk.index !== index)) {
|
|
130
|
-
console.log("Twisted chain", index,blk&& blk.index, this.physical_address);
|
|
131
|
-
if (index < this.height) {
|
|
132
|
-
blk = this.traverse_blocks(index);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if(blk&& !isNaN(Number(blk.index)))this.blocks[blk.index] = blk
|
|
136
|
-
|
|
137
|
-
return blk;
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
add_tx = (instruction) => {
|
|
141
|
-
if (!this.txs.length) this.start_time = Date.now();
|
|
142
|
-
|
|
143
|
-
this.txs.push(instruction);
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
fabricate_datapath_block=()=>{
|
|
147
|
-
if(!this.datapath)return this.stderr({"name":"Chain_error",'message':'Cannot fabricate datapath block of no-call chain'})
|
|
148
|
-
|
|
149
|
-
return this.build_block({metadata:{output:this.datapath}, index:0, _id: _id(this.hash)}, true)
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
forge_block = (obj)=>{
|
|
153
|
-
obj.index = this.height
|
|
154
|
-
obj._id = _id(this.hash)
|
|
155
|
-
obj.previous_hash = this.recent_block && this.recent_block._id
|
|
156
|
-
|
|
157
|
-
let blk = this.build_block(obj, true)
|
|
158
|
-
|
|
159
|
-
this.add_block(blk)
|
|
160
|
-
|
|
161
|
-
return blk
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
mine = (vm, no_curs) => {
|
|
165
|
-
|
|
166
|
-
if (this.physical_address === `${this.account.physical_address}/Datatypes/Void` && this.height){
|
|
167
|
-
let blk = this.get_latest_block()
|
|
168
|
-
vm.connect_block(blk)
|
|
169
|
-
|
|
170
|
-
return blk
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
let block = new Block(this);
|
|
174
|
-
|
|
175
|
-
block.generate_hash();
|
|
176
|
-
block.store(no_curs);
|
|
177
|
-
|
|
178
|
-
block.children = this.pending_children;
|
|
179
|
-
this.pending_children = new Array();
|
|
180
|
-
|
|
181
|
-
block.timelapse = Date.now() - this.start_time;
|
|
182
|
-
|
|
183
|
-
this.add_block(block);
|
|
184
|
-
|
|
185
|
-
vm.connect_block(block)
|
|
186
|
-
|
|
187
|
-
this.txs = new Array();
|
|
188
|
-
|
|
189
|
-
this.manage_config();
|
|
190
|
-
|
|
191
|
-
return block;
|
|
11
|
+
sync = async () => {
|
|
12
|
+
this.folder = await this.account.manager.oracle.folder(
|
|
13
|
+
this.physical_address
|
|
14
|
+
);
|
|
192
15
|
};
|
|
193
16
|
|
|
194
|
-
|
|
195
|
-
let
|
|
196
|
-
|
|
197
|
-
data = this.account.manager.oracle.handle_compression({
|
|
198
|
-
addr: this.folder.folder_path,
|
|
199
|
-
data,
|
|
200
|
-
no_string: true,
|
|
201
|
-
});
|
|
17
|
+
write = async (payload) => {
|
|
18
|
+
let address = await this.account.manager.oracle.write(payload);
|
|
202
19
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
return this.account.vm.stderr({name:"Chain_error", message:"chain corruption, as a non index 0 block is without a previous_hash"})
|
|
206
|
-
}
|
|
20
|
+
let block = new Block({ content: address }, this);
|
|
21
|
+
await block.sync();
|
|
207
22
|
|
|
208
|
-
this.folder.write(data);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
add_block = (block) => {
|
|
212
23
|
this.blocks.push(block);
|
|
213
|
-
this.height++;
|
|
214
|
-
|
|
215
|
-
if (!this.physical_address.startsWith(`${this.account.physical_address}/Opcodes`) && Array.from(Object.keys(block.metadata)).length){
|
|
216
|
-
this.persist(block)
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
this.recent_block = block;
|
|
220
|
-
|
|
221
|
-
if (this.account.initiated){
|
|
222
|
-
let parent_addr = this.parent.physical_address
|
|
223
|
-
if (this.account.vm.stack.has_stack(parent_addr)){
|
|
224
|
-
this.account.vm.stack.push(parent_addr, block);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
24
|
};
|
|
228
25
|
|
|
229
|
-
|
|
230
|
-
let
|
|
26
|
+
read = async (query) => {
|
|
27
|
+
let { index } = query;
|
|
231
28
|
|
|
232
|
-
return this.
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
stringify = (str) => {
|
|
236
|
-
let obj = {};
|
|
237
|
-
|
|
238
|
-
obj.physical_address = this.physical_address;
|
|
239
|
-
obj.height = this.blocks.length;
|
|
240
|
-
obj.parent = this.parent.path;
|
|
241
|
-
obj.account = this.parent.account.name;
|
|
242
|
-
obj.name = this.name;
|
|
243
|
-
obj.datapath = this.datapath
|
|
244
|
-
obj.connections = this.connections.map((b) =>
|
|
245
|
-
b.stringify
|
|
246
|
-
? new Object({
|
|
247
|
-
chain: b.chain.hash,
|
|
248
|
-
block: { hash: b.hash, _id: b._id },
|
|
249
|
-
})
|
|
250
|
-
: b
|
|
251
|
-
);
|
|
252
|
-
obj.hash = this.hash;
|
|
253
|
-
// obj.blocks = this.blocks.map((blk) => blk.hash || blk);
|
|
29
|
+
if (index != null) return await this.get_block(index);
|
|
254
30
|
|
|
255
|
-
return
|
|
31
|
+
return await this.get_block();
|
|
256
32
|
};
|
|
257
33
|
|
|
258
|
-
|
|
259
|
-
return this.
|
|
34
|
+
get_block = async (index) => {
|
|
35
|
+
return this.blocks[index == null ? this.blocks.length - 1 : index];
|
|
260
36
|
};
|
|
261
37
|
}
|
|
262
38
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"servers":[{"port":1408,"hostname":"127.0.0.1","repo":true,"domain":"127.0.0.1:1408"},{"port":1409,"hostname":"127.0.0.1","repo":true,"domain":"127.0.0.1:1409"}]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"servers":[{"port":1408,"hostname":"127.0.0.1","repo":true,"domain":"127.0.0.1:1408"},{"port":1409,"hostname":"127.0.0.1","repo":true,"domain":"127.0.0.1:1409"}]}
|
package/Objects/Manager.js
CHANGED
|
@@ -1,316 +1,50 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import opcodes from "../opcodes";
|
|
1
|
+
import create_server from "../utils/create_server";
|
|
2
|
+
import { post_request } from "../utils/services";
|
|
4
3
|
import Account from "./Account";
|
|
5
4
|
import Blockweb from "./Blockweb";
|
|
6
5
|
import Oracle from "./Oracle";
|
|
7
|
-
import
|
|
8
|
-
import Protocol from "godprotocol/Objects/Protocol";
|
|
6
|
+
import Repository from "./Repository";
|
|
9
7
|
|
|
8
|
+
class Manager {
|
|
9
|
+
constructor(name, options) {
|
|
10
|
+
this.name = name;
|
|
11
|
+
this.options = options || {};
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
meta = meta || {};
|
|
16
|
-
|
|
17
|
-
this.compression = meta.compression;
|
|
18
|
-
this.frequency = meta.frequency
|
|
19
|
-
this.manager_id = _id("manager");
|
|
20
|
-
this.accounts = new Object();
|
|
21
|
-
this.running = 0;
|
|
22
|
-
this.logger = new Logger()
|
|
23
|
-
this.root = meta.root || __dirname;
|
|
24
|
-
this.accounts_et_programs = new Object();
|
|
25
|
-
this.web = new Blockweb(this);
|
|
26
|
-
this.oracle = new Oracle(this, {
|
|
27
|
-
compression: this.compression,
|
|
28
|
-
datastore: meta.datastore,
|
|
29
|
-
});
|
|
30
|
-
this.servers = new Array();
|
|
31
|
-
this.init_name = meta.initiator|| process.env.INITIATOR || "initiator"
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
stringify = (str) => {
|
|
35
|
-
let obj = {
|
|
36
|
-
_id: this.manager_id,
|
|
37
|
-
servers: this.servers,
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
return str ? JSON.stringify(obj) : obj;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
initiate = (meta) => {
|
|
44
|
-
this.initiator = this.add_account(
|
|
45
|
-
this.init_name,
|
|
46
|
-
meta
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
return this.initiator;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
set_track = ({account, physical_address, pointer}, slice)=>{
|
|
53
|
-
let payload = this.accounts_et_programs[account], t, track;
|
|
54
|
-
|
|
55
|
-
for(t= payload.tracks.length-1; t>=0; t--){
|
|
56
|
-
let trck = payload.tracks[t]
|
|
57
|
-
if (trck.payload.physical_address === physical_address){
|
|
58
|
-
track = trck
|
|
59
|
-
break
|
|
60
|
-
}
|
|
13
|
+
this.sync_server = options.sync_server;
|
|
14
|
+
if (this.sync_server && !this.sync_server.domain) {
|
|
15
|
+
this.sync_server.domain = `${this.sync_server.hostname}:${this.sync_server.port}`;
|
|
61
16
|
}
|
|
17
|
+
this.server = options.server;
|
|
18
|
+
this.accounts = new Object();
|
|
62
19
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
if(pointer != null){
|
|
67
|
-
track.pointer = pointer;
|
|
68
|
-
}
|
|
20
|
+
this.oracle = new Oracle(this);
|
|
21
|
+
this.web = new Blockweb(this);
|
|
69
22
|
|
|
70
|
-
|
|
23
|
+
this.repository = new Repository(this);
|
|
71
24
|
}
|
|
72
25
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
let payload = this.accounts_et_programs[account];
|
|
77
|
-
let tracks = payload.tracks
|
|
78
|
-
|
|
79
|
-
console.log(`(${payload.clocks}) Account: ${account} [tracks(${tracks.length})]`)
|
|
80
|
-
|
|
81
|
-
let curr_track = tracks.slice(-1)[0]
|
|
82
|
-
let pointer = curr_track.pointer
|
|
83
|
-
let instruction = curr_track.sequence[pointer]
|
|
84
|
-
|
|
85
|
-
console.log(`>(${curr_track.clocks}) Track [${pointer} / ${curr_track.sequence.length}] - executing '${instruction}'`)
|
|
86
|
-
lines+=2
|
|
26
|
+
sync = async () => {
|
|
27
|
+
if (this.server && !this.server.domain) {
|
|
28
|
+
this.server.domain = `${this.server.hostname}:${this.server.port}`;
|
|
87
29
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
finish_state = ()=>{
|
|
92
|
-
console.log(`All programs finished execution`)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
start_clock = () => {
|
|
96
|
-
let frequency = Number(this.frequency)|| Number(process.env.HERTS) || 10;
|
|
97
|
-
this.clock = setInterval(() => {
|
|
98
|
-
|
|
99
|
-
for (let account in this.accounts_et_programs) {
|
|
100
|
-
let payload = this.accounts_et_programs[account], spawn, tracks;
|
|
101
|
-
|
|
102
|
-
if(payload.spawn_list.length){
|
|
103
|
-
if(payload.spawn_cursor == undefined){
|
|
104
|
-
payload.spawn_cursor = 0
|
|
105
|
-
tracks = payload.tracks;
|
|
106
|
-
}else {
|
|
107
|
-
spawn = payload.spawn_list[payload.spawn_cursor]
|
|
108
|
-
tracks = payload.spawns[spawn];
|
|
109
|
-
payload.spawn_cursor ++
|
|
110
|
-
}
|
|
111
|
-
}else {
|
|
112
|
-
tracks = payload.tracks;
|
|
113
|
-
}
|
|
30
|
+
this.server_object =
|
|
31
|
+
this.server && (await create_server({ ...this.server, manager: this }));
|
|
114
32
|
|
|
115
|
-
|
|
33
|
+
this.post_request = post_request;
|
|
116
34
|
|
|
117
|
-
|
|
118
|
-
if (program.hold) continue;
|
|
119
|
-
|
|
120
|
-
let instruction = program.sequence[program.pointer];
|
|
121
|
-
program.clocks++
|
|
122
|
-
|
|
123
|
-
program.pointer++;
|
|
124
|
-
program.account.vm.execute(instruction, program);
|
|
125
|
-
program = program.account.vm.track;
|
|
126
|
-
|
|
127
|
-
if (program.pointer >= program.sequence.length) {
|
|
128
|
-
program.account.flush_buffer(program.pid);
|
|
129
|
-
|
|
130
|
-
if (program.error_stack)
|
|
131
|
-
program.account.vm.error_manager = JSON.parse(program.error_stack)
|
|
132
|
-
tracks.pop();
|
|
133
|
-
|
|
134
|
-
if(spawn){
|
|
135
|
-
if (payload.spawn_cursor >= payload.spawn_list.length){
|
|
136
|
-
payload.spawn_cursor = undefined
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
if (!tracks.length) {
|
|
141
|
-
if (spawn){
|
|
142
|
-
delete payload.spawns[spawn]
|
|
143
|
-
payload.spawn_list = payload.spawn_list.filter(s=>s!==spawn)
|
|
144
|
-
}else {
|
|
145
|
-
this.running -= 1;
|
|
146
|
-
delete this.accounts_et_programs[account];
|
|
147
|
-
program.account.chain.mine(program.account.vm);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
this.logger.log(this.state_logger)
|
|
154
|
-
if (!this.running){
|
|
155
|
-
clearInterval(this.clock)
|
|
156
|
-
this. finish_state(this.finish_state)
|
|
157
|
-
}
|
|
158
|
-
}, frequency);
|
|
35
|
+
await this.repository.sync(this.name);
|
|
159
36
|
};
|
|
160
37
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
add_account = (name, meta) => {
|
|
164
|
-
meta = meta || {};
|
|
165
|
-
if (!name) name = meta.name;
|
|
166
|
-
|
|
167
|
-
let account;
|
|
168
|
-
if (!name) {
|
|
169
|
-
account = { error: true, error_message: "Name cannot be blank" };
|
|
170
|
-
} else {
|
|
171
|
-
account = this.get_account(name);
|
|
38
|
+
add_account = async (name, options) => {
|
|
39
|
+
let account = this.accounts[name];
|
|
172
40
|
|
|
173
|
-
|
|
174
|
-
account = new Account(name, { ...meta, manager: this });
|
|
41
|
+
if (account) return account;
|
|
175
42
|
|
|
176
|
-
|
|
177
|
-
|
|
43
|
+
account = new Account(name, { ...options, manager: this });
|
|
44
|
+
await account.sync();
|
|
178
45
|
|
|
179
|
-
|
|
180
|
-
if (meta.string) account = account.stringify();
|
|
181
|
-
} else if (meta && meta.private && !account.private) {
|
|
182
|
-
account.private = true;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return meta.string ? JSON.stringify(account) : account;
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
push = (program) => {
|
|
190
|
-
let account = this.accounts_et_programs[program.account.name];
|
|
191
|
-
let tracks ;
|
|
192
|
-
|
|
193
|
-
if (account) {
|
|
194
|
-
if (program.spawn) {
|
|
195
|
-
tracks = account.spawns[program.spawn]
|
|
196
|
-
if (!tracks) {
|
|
197
|
-
tracks = []
|
|
198
|
-
account.spawns[program.spawn] = tracks;
|
|
199
|
-
}
|
|
200
|
-
account.spawn_list.push(program.spawn)
|
|
201
|
-
}else tracks= account.tracks
|
|
202
|
-
if(program.unshift){
|
|
203
|
-
delete program.unshift
|
|
204
|
-
tracks.unshift({ ...program, pointer: 0, clocks:0 });
|
|
205
|
-
} else tracks.push({ ...program, pointer: 0, clocks:0 });
|
|
206
|
-
} else {
|
|
207
|
-
tracks = [{ ...program, pointer: 0, clocks:0 }];
|
|
208
|
-
account = {
|
|
209
|
-
clocks:0,
|
|
210
|
-
spawns: {},
|
|
211
|
-
spawn_list: []
|
|
212
|
-
}
|
|
213
|
-
if (program.spawn){
|
|
214
|
-
account.spawns[program.spawn] = tracks
|
|
215
|
-
account.spawn_list.push(program.spawn)
|
|
216
|
-
}else account.tracks = tracks;
|
|
217
|
-
|
|
218
|
-
this.accounts_et_programs[program.account.name] = account;
|
|
219
|
-
|
|
220
|
-
this.running++;
|
|
221
|
-
this.running === 1 && this.start_clock();
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
get_initiator = () => this.initiator || this.initiate();
|
|
226
|
-
|
|
227
|
-
endpoint = (endpoint, payload, callback) => {
|
|
228
|
-
let account = this.get_account(payload.account);
|
|
229
|
-
if (!account && payload.account) {
|
|
230
|
-
return this.get_initiator().run_callback(callback, {
|
|
231
|
-
error: true,
|
|
232
|
-
error_message: "Account is not found",
|
|
233
|
-
payload,
|
|
234
|
-
endpoint,
|
|
235
|
-
});
|
|
236
|
-
}else account = this.get_initiator()
|
|
237
|
-
|
|
238
|
-
if (
|
|
239
|
-
!account.validate(
|
|
240
|
-
payload.program || payload.payload,
|
|
241
|
-
payload.signature,
|
|
242
|
-
callback || payload.callback
|
|
243
|
-
)
|
|
244
|
-
)
|
|
245
|
-
return;
|
|
246
|
-
|
|
247
|
-
let method = account[endpoint];
|
|
248
|
-
typeof method === "function" && method({ ...payload, callback });
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
add_server = (server) => {
|
|
252
|
-
this.servers.push(server);
|
|
46
|
+
this.accounts[name] = account;
|
|
253
47
|
};
|
|
254
|
-
|
|
255
|
-
prepare_query = query=>{
|
|
256
|
-
query = query.split('&');
|
|
257
|
-
let obj = {}
|
|
258
|
-
for (let q=0; q< query.length; q++){
|
|
259
|
-
let [prop, value] = query[q].split('=')
|
|
260
|
-
|
|
261
|
-
if (prop === 'executable'){
|
|
262
|
-
obj.executable = value;
|
|
263
|
-
}else if (prop.startsWith('arguments')){
|
|
264
|
-
if (!obj.arguments) obj.arguments = {argv: []}
|
|
265
|
-
if (prop.includes('.')){
|
|
266
|
-
prop = prop.split('.')
|
|
267
|
-
if (prop[2] ==='$'){
|
|
268
|
-
try{
|
|
269
|
-
value = JSON.parse(value)
|
|
270
|
-
}catch(e){}
|
|
271
|
-
}
|
|
272
|
-
obj.arguments.argv.push({name: prop[1], [prop[2]==='$'?'static_value':'address']: value})
|
|
273
|
-
} else {
|
|
274
|
-
obj.arguments.argv.push({address:value, position: obj.arguments.argv.length})
|
|
275
|
-
}
|
|
276
|
-
obj.arguments.argc = obj.arguments.argv.length
|
|
277
|
-
}else if (prop === 'location'){
|
|
278
|
-
obj.location = value;
|
|
279
|
-
}else if (prop === 'metadata'){
|
|
280
|
-
try {
|
|
281
|
-
obj.metadata = JSON.parse(value);
|
|
282
|
-
} catch (e) {}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
return obj;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
handle_route = ({req,res, data})=>{
|
|
290
|
-
let url = req.url;
|
|
291
|
-
let split = url.split('?')
|
|
292
|
-
let query = split[1]
|
|
293
|
-
|
|
294
|
-
let payload = data || this.prepare_query(query);
|
|
295
|
-
url = split[0].split('/').filter(a=>!!a)
|
|
296
|
-
|
|
297
|
-
if (url.length> 2){}
|
|
298
|
-
|
|
299
|
-
let account = this.get_account(data && data.account || url[1]);
|
|
300
|
-
if(!account){
|
|
301
|
-
return res.end(JSON.stringify({error:true, error_message:"Account is not found!"}))
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
if (['display', 'read_chain'].includes(payload.executable )){
|
|
305
|
-
if (!payload.metadata)payload.metadata = {}
|
|
306
|
-
payload.metadata.raw = true;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
let response = account.vm.exec(payload)
|
|
310
|
-
|
|
311
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
312
|
-
res.end(JSON.stringify(response))
|
|
313
|
-
}
|
|
314
48
|
}
|
|
315
49
|
|
|
316
50
|
export default Manager;
|