godprotocol 1.0.7991 → 1.1.0
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/Executables/callables.js +131 -0
- package/Executables/executables.js +230 -0
- package/Executables/functions/adminstrator/adm_register.js +29 -0
- package/Executables/functions/adminstrator/native_components.js +10 -0
- package/Executables/functions/adminstrator/query_selectors.js +10 -0
- package/Executables/functions/adminstrator/render.js +24 -0
- package/Executables/functions/array_register.js +161 -0
- package/Executables/functions/assign.js +19 -0
- package/Executables/functions/display.js +55 -0
- package/Executables/functions/folder.js +95 -0
- package/Executables/functions/folder_register.js +87 -0
- package/Executables/functions/importcheck.js +13 -0
- package/Executables/functions/indices.js +56 -0
- package/Executables/functions/len.js +22 -0
- package/Executables/functions/methods/array.js +272 -0
- package/Executables/functions/methods/string.js +197 -0
- package/Executables/functions/methods/twain.js +148 -0
- package/Executables/functions/op.js +15 -0
- package/Executables/functions/price_register.js +30 -0
- package/Executables/functions/pricing.js +26 -0
- package/Executables/functions/servers/account.js +81 -0
- package/Executables/functions/servers/account_register.js +56 -0
- package/Executables/functions/servers/block.js +21 -0
- package/Executables/functions/servers/block_register.js +18 -0
- package/Executables/functions/servers/chain.js +42 -0
- package/Executables/functions/servers/chain_register.js +28 -0
- package/Executables/functions/set_error_catch.js +34 -0
- package/Executables/functions/string_register.js +130 -0
- package/Executables/functions/twain_register.js +56 -0
- package/Executables/functions/typecast.js +43 -0
- package/Executables/functions/utils.js +9 -0
- package/Executables/stack.js +53 -0
- package/Index.js +2 -7
- package/Loader_sequence/main.js +535 -119
- package/Loader_sequence/repository.js +33 -0
- package/Objects/Account.js +220 -54
- package/Objects/Block.js +46 -16
- package/Objects/Blockweb.js +14 -0
- package/Objects/Chain.js +163 -16
- package/Objects/Fee.js +33 -0
- package/Objects/Filesystem.js +100 -26
- package/Objects/Frame.js +10 -3
- package/Objects/Manager.js +272 -46
- package/Objects/Opcodes.js +60 -10
- package/Objects/Oracle.js +90 -5
- package/Objects/Protocol.js +5 -0
- package/Objects/Virtual_machine.js +145 -53
- package/Trinity.js +20 -0
- package/callables.js +172 -0
- package/framer.js +111 -52
- package/opcodes/add.js +6 -6
- package/opcodes/indices.js +82 -0
- package/opcodes/jmp.js +12 -11
- package/opcodes/std.js +7 -2
- package/opcodes.js +72 -14
- package/package.json +6 -5
- package/readme.md +1 -1
- package/starter_scripts/constants.seq +11 -0
- package/utils/create_server.js +34 -27
- package/utils/hash.js +2 -2
- package/utils/logger.js +79 -0
- package/utils/services.js +5 -3
- package/utils/type_coersion.js +3 -0
- package/utils/vm_utils.js +292 -0
- package/.babelrc +0 -3
- package/build/Account.js +0 -162
- package/build/Block.js +0 -65
- package/build/Blockweb.js +0 -36
- package/build/Chain.js +0 -119
- package/build/Explorer.js +0 -47
- package/build/Filesystem.js +0 -127
- package/build/Frame.js +0 -52
- package/build/Index.js +0 -15
- package/build/Manager.js +0 -99
- package/build/Opcodes.js +0 -39
- package/build/Oracle.js +0 -42
- package/build/Virtual_machine.js +0 -128
- package/build/add.js +0 -41
- package/build/create_server.js +0 -119
- package/build/framer.js +0 -57
- package/build/functions.js +0 -82
- package/build/hash.js +0 -14
- package/build/jmp.js +0 -19
- package/build/main.js +0 -283
- package/build/opcodes.js +0 -42
- package/build/privacy.js +0 -27
- package/build/services.js +0 -50
- package/build/std.js +0 -11
package/Objects/Manager.js
CHANGED
|
@@ -1,90 +1,316 @@
|
|
|
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";
|
|
4
5
|
import Blockweb from "./Blockweb";
|
|
5
6
|
import Oracle from "./Oracle";
|
|
7
|
+
import Logger from "../utils/logger";
|
|
8
|
+
import Protocol from "godprotocol/Objects/Protocol";
|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
|
|
10
|
+
|
|
11
|
+
class Manager extends Protocol{
|
|
12
|
+
constructor(meta) {
|
|
13
|
+
super()
|
|
14
|
+
|
|
15
|
+
meta = meta || {};
|
|
16
|
+
|
|
17
|
+
this.compression = meta.compression;
|
|
18
|
+
this.frequency = meta.frequency
|
|
19
|
+
this.manager_id = _id("manager");
|
|
9
20
|
this.accounts = new Object();
|
|
10
21
|
this.running = 0;
|
|
11
|
-
this.
|
|
22
|
+
this.logger = new Logger()
|
|
23
|
+
this.root = meta.root || __dirname;
|
|
24
|
+
this.accounts_et_programs = new Object();
|
|
12
25
|
this.web = new Blockweb(this);
|
|
13
|
-
this.oracle = new Oracle(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
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if(slice && track){
|
|
64
|
+
payload.tracks.splice(t+1)
|
|
65
|
+
}
|
|
66
|
+
if(pointer != null){
|
|
67
|
+
track.pointer = pointer;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return track
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
state_logger = ()=>{
|
|
74
|
+
let lines=0
|
|
75
|
+
for (let account in this.accounts_et_programs){
|
|
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
|
|
87
|
+
}
|
|
88
|
+
return lines
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
finish_state = ()=>{
|
|
92
|
+
console.log(`All programs finished execution`)
|
|
14
93
|
}
|
|
15
94
|
|
|
16
95
|
start_clock = () => {
|
|
17
|
-
let frequency = Number(process.env.HERTS) || 10;
|
|
96
|
+
let frequency = Number(this.frequency)|| Number(process.env.HERTS) || 10;
|
|
18
97
|
this.clock = setInterval(() => {
|
|
19
|
-
|
|
20
|
-
|
|
98
|
+
|
|
99
|
+
for (let account in this.accounts_et_programs) {
|
|
100
|
+
let payload = this.accounts_et_programs[account], spawn, tracks;
|
|
21
101
|
|
|
22
|
-
|
|
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
|
+
}
|
|
23
114
|
|
|
24
|
-
|
|
25
|
-
track.pointer++;
|
|
115
|
+
let program = tracks.slice(-1)[0];
|
|
26
116
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
track.account.chain.mine(track.account.vm);
|
|
30
|
-
track.account.flush_buffer(track.pids.splice(-1)[0]);
|
|
117
|
+
payload.clocks ++
|
|
118
|
+
if (program.hold) continue;
|
|
31
119
|
|
|
32
|
-
|
|
33
|
-
|
|
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();
|
|
34
133
|
|
|
35
|
-
|
|
36
|
-
|
|
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
|
+
|
|
37
150
|
}
|
|
38
151
|
}
|
|
39
152
|
|
|
40
|
-
|
|
153
|
+
this.logger.log(this.state_logger)
|
|
154
|
+
if (!this.running){
|
|
155
|
+
clearInterval(this.clock)
|
|
156
|
+
this. finish_state(this.finish_state)
|
|
157
|
+
}
|
|
41
158
|
}, frequency);
|
|
42
159
|
};
|
|
43
160
|
|
|
44
161
|
get_account = (name) => this.accounts[name];
|
|
45
162
|
|
|
46
163
|
add_account = (name, meta) => {
|
|
47
|
-
|
|
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);
|
|
172
|
+
|
|
173
|
+
if (!account) {
|
|
174
|
+
account = new Account(name, { ...meta, manager: this });
|
|
48
175
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
framer(account);
|
|
52
|
-
opcodes(account);
|
|
176
|
+
opcodes(account);
|
|
177
|
+
framer(account, meta.on_framed);
|
|
53
178
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
account.private
|
|
179
|
+
this.accounts[account.name] = account;
|
|
180
|
+
if (meta.string) account = account.stringify();
|
|
181
|
+
} else if (meta && meta.private && !account.private) {
|
|
182
|
+
account.private = true;
|
|
183
|
+
}
|
|
57
184
|
}
|
|
58
185
|
|
|
59
|
-
return account;
|
|
186
|
+
return meta.string ? JSON.stringify(account) : account;
|
|
60
187
|
};
|
|
61
188
|
|
|
62
189
|
push = (program) => {
|
|
63
|
-
let
|
|
64
|
-
|
|
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);
|
|
190
|
+
let account = this.accounts_et_programs[program.account.name];
|
|
191
|
+
let tracks ;
|
|
69
192
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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 });
|
|
75
206
|
} else {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
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;
|
|
83
219
|
|
|
84
220
|
this.running++;
|
|
85
221
|
this.running === 1 && this.start_clock();
|
|
86
222
|
}
|
|
87
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);
|
|
253
|
+
};
|
|
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
|
+
}
|
|
88
314
|
}
|
|
89
315
|
|
|
90
316
|
export default Manager;
|
package/Objects/Opcodes.js
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Vm_utils from "../utils/vm_utils";
|
|
2
|
+
import { obj } from "../framer";
|
|
2
3
|
|
|
3
|
-
class Opcodes {
|
|
4
|
+
class Opcodes extends Vm_utils {
|
|
4
5
|
constructor() {
|
|
6
|
+
super()
|
|
7
|
+
|
|
5
8
|
this.opcodes = new Object();
|
|
6
9
|
}
|
|
7
10
|
|
|
8
11
|
set = (opcode, circuit) => {
|
|
9
12
|
this.opcodes[opcode] = circuit;
|
|
13
|
+
obj.Opcodes[opcode] = null;
|
|
14
|
+
|
|
10
15
|
this.account.assembler.opcodes.push(opcode);
|
|
11
16
|
};
|
|
12
17
|
|
|
@@ -17,20 +22,65 @@ class Opcodes {
|
|
|
17
22
|
|
|
18
23
|
let buf = context.get_buffer();
|
|
19
24
|
|
|
20
|
-
let args =
|
|
25
|
+
let args =
|
|
26
|
+
context.account.read(buf && buf.inputs) ||
|
|
27
|
+
this.account.stdin.splice(-1)[0];
|
|
21
28
|
|
|
22
|
-
|
|
29
|
+
try{
|
|
30
|
+
let res = circ(args, this);
|
|
23
31
|
|
|
24
|
-
|
|
32
|
+
this.stdin(this.static_cast(res));
|
|
33
|
+
} catch(e){
|
|
34
|
+
console.log(e)
|
|
35
|
+
this.stderr(`${JSON.stringify({circuit:opcode,args, error_message:e.message})}`)
|
|
36
|
+
}
|
|
25
37
|
};
|
|
26
38
|
|
|
27
|
-
stdin = (object, cb) => {
|
|
28
|
-
|
|
39
|
+
stdin = (object, cb, options) => {
|
|
40
|
+
options = options || {}
|
|
41
|
+
let full_obj = object
|
|
42
|
+
object = object.static_value
|
|
43
|
+
|
|
44
|
+
if (object == null) {
|
|
45
|
+
this.flags.void = true;
|
|
46
|
+
object = null
|
|
47
|
+
} else this.flags.void = false;
|
|
48
|
+
|
|
49
|
+
this.flags.zero = !object;
|
|
50
|
+
if (typeof object === "number") this.flags.neg = object < 0;
|
|
51
|
+
|
|
52
|
+
let code_string;
|
|
53
|
+
|
|
54
|
+
if (cb && cb.payload && cb.payload.stdin)
|
|
55
|
+
options.address = cb.payload.stdin;
|
|
56
|
+
|
|
57
|
+
if (!options.address){
|
|
58
|
+
let contx_2 = this.get_context(-2)
|
|
59
|
+
if (full_obj&& typeof full_obj === 'object'){
|
|
60
|
+
if(contx_2){
|
|
61
|
+
full_obj.__address__ = contx_2.physical_address
|
|
62
|
+
}else full_obj .__address__ = this.get_context().physical_address
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (typeof full_obj === 'string'){
|
|
67
|
+
code_string = full_obj
|
|
68
|
+
} else if (full_obj.type){
|
|
69
|
+
if (full_obj.type === 'boolean'){
|
|
70
|
+
code_string = `{"type":"boolean", "static_value":${full_obj.static_value?'True':'False'},"__address__":"${full_obj.__address__}"}`
|
|
71
|
+
}else code_string = JSON.stringify(full_obj)
|
|
72
|
+
}else code_string= typeof object !== "string" ?object ==null?'Void': JSON.stringify(object) : `"${object}"`;
|
|
29
73
|
|
|
30
|
-
|
|
74
|
+
if (options.address){
|
|
75
|
+
let addr = options.address;
|
|
76
|
+
if(!addr.startsWith('@')){
|
|
77
|
+
addr = `@/${addr.split('/').slice(2).join('/')}`
|
|
78
|
+
}
|
|
31
79
|
|
|
32
|
-
|
|
33
|
-
|
|
80
|
+
code_string = `>${addr} ${code_string}`
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
this.account.assembler.run(code_string, { cb, pure: true });
|
|
34
84
|
};
|
|
35
85
|
}
|
|
36
86
|
|
package/Objects/Oracle.js
CHANGED
|
@@ -1,12 +1,29 @@
|
|
|
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(
|
|
14
|
+
meta.datastore || process.env.DATASTORE || "godprotocol"
|
|
15
|
+
).sync({
|
|
16
|
+
manager: this.mgr,
|
|
17
|
+
});
|
|
8
18
|
}
|
|
9
19
|
|
|
20
|
+
get_folder = (physical_address, options) => {
|
|
21
|
+
if (typeof options !== "object") {
|
|
22
|
+
options = { no_new: options ? false : true };
|
|
23
|
+
}
|
|
24
|
+
return this.gds.folder(this.hash(physical_address));
|
|
25
|
+
};
|
|
26
|
+
|
|
10
27
|
set = (path, { type, obj }) => {
|
|
11
28
|
this.datapaths[path] = {
|
|
12
29
|
obj,
|
|
@@ -14,12 +31,80 @@ class Oracle {
|
|
|
14
31
|
};
|
|
15
32
|
};
|
|
16
33
|
|
|
17
|
-
get = (path) =>
|
|
34
|
+
get = (path) => {
|
|
35
|
+
let obj = this.datapaths[path];
|
|
36
|
+
|
|
37
|
+
if (!obj) {
|
|
38
|
+
try {
|
|
39
|
+
obj = this.fs.readFileSync(`${this.mgr.root}/${path}`, {encoding:'utf-8'});
|
|
40
|
+
obj = JSON.parse(obj);
|
|
41
|
+
let type = obj && obj.payload && typeof obj.payload;
|
|
42
|
+
obj = obj && obj.payload;
|
|
43
|
+
type =
|
|
44
|
+
type === "object" ? (Array.isArray(obj) ? "array" : "twain") : type;
|
|
45
|
+
|
|
46
|
+
obj = { type, obj };
|
|
47
|
+
this.set(path, obj);
|
|
48
|
+
} catch (e) {
|
|
49
|
+
// console.log(e.message, 'Error thrown?')
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return obj;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
hash = (data, alg) => {
|
|
57
|
+
data = typeof data !== "string" ? JSON.stringify(data) : data;
|
|
58
|
+
return hash(data, alg);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
handle_compression = ({ addr, data, no_string }) => {
|
|
62
|
+
if (this.compression) {
|
|
63
|
+
let res = this.compression({ addr, data, no_string });
|
|
64
|
+
if (res) data = res.data;
|
|
65
|
+
}
|
|
66
|
+
if (!no_string && (typeof addr !== "string" || typeof data !== "string"))
|
|
67
|
+
return;
|
|
68
|
+
|
|
69
|
+
return data;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
write = (address, payload) => {
|
|
73
|
+
payload = JSON.stringify({ payload });
|
|
74
|
+
let data = this.handle_compression({
|
|
75
|
+
addr: address,
|
|
76
|
+
data: payload,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
this.fs.writeFileSync(`${this.mgr.root}/${address}`, data, {
|
|
80
|
+
encoding: "utf-8",
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
fetch = async (payload, callback) => {
|
|
85
|
+
let { physical_address, config, remote, query } = payload,
|
|
86
|
+
result;
|
|
87
|
+
|
|
88
|
+
let folder = this.get_folder(physical_address);
|
|
89
|
+
|
|
90
|
+
if (config) {
|
|
91
|
+
result = folder.config.stringify();
|
|
92
|
+
folder.add_remote(remote);
|
|
93
|
+
} else {
|
|
94
|
+
if (!folder.check_remote(query.operation)) {
|
|
95
|
+
result = {
|
|
96
|
+
error: true,
|
|
97
|
+
error_message: "Query operation cannot be performed remotely.",
|
|
98
|
+
};
|
|
99
|
+
} else {
|
|
100
|
+
let op = folder[query.operation];
|
|
18
101
|
|
|
19
|
-
|
|
20
|
-
|
|
102
|
+
result = await op(query.query, query.options);
|
|
103
|
+
folder.remote_stuff(remote, result);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
21
106
|
|
|
22
|
-
|
|
107
|
+
callback(result);
|
|
23
108
|
};
|
|
24
109
|
}
|
|
25
110
|
|