godprotocol 1.2.26 → 1.2.28

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,7 @@
1
1
  import Trinity from "./Objects/Trinity.js";
2
+ import Oracle from "./Objects/Oracle.js";
2
3
 
3
4
  let trinity = new Trinity(process.env.Trinity || "GodProtocol");
4
5
 
5
6
  export default trinity;
7
+ export { Oracle };
@@ -5,185 +5,215 @@ import Virtual_machine from "./Virtual_machine.js";
5
5
  import register from "godprotocol/callables/register.js";
6
6
  import { hash } from "godprotocol/utils/hash.js";
7
7
 
8
- class Account extends Utils{
9
- constructor(name, options){
10
- super()
11
-
12
- this.name = name
13
- this.options = options
14
- this.manager = options.manager
15
- this.vm = new Virtual_machine(this)
16
- this.compiler = new Compiler(this)
8
+ class Account extends Utils {
9
+ constructor(name, options) {
10
+ super();
11
+
12
+ this.name = name;
13
+ this.options = options;
14
+ this.manager = options.manager;
15
+ this.vm = new Virtual_machine(this);
16
+ this.compiler = new Compiler(this);
17
17
  }
18
18
 
19
- load = async(payload, options)=>{
20
- let {address, sequence, compiler, from_reload, _id} = payload
19
+ load = async (payload, options) => {
20
+ let { address, sequence, compiler, from_reload, _id } = payload;
21
21
 
22
- let phy = `${this.physical_address}/${address}`
23
- let chain = await this.chain.chain(phy)
22
+ let phy = `${this.physical_address}/${address}`;
23
+ let chain = await this.chain.chain(phy);
24
24
 
25
- if (compiler){
26
- !from_reload && await this.manager.oracle.write(`${phy}.air`, sequence, {account: this.physical_address, prefix: '.codes', address: phy})
27
-
28
- sequence = await this.compiler.to_sequence(sequence, address)
29
- sequence = sequence[0]
25
+ if (compiler) {
26
+ !from_reload &&
27
+ (await this.manager.oracle.write(`${phy}.air`, sequence, {
28
+ account: this.physical_address,
29
+ prefix: ".codes",
30
+ address: phy,
31
+ }));
32
+
33
+ sequence = await this.compiler.to_sequence(sequence, address);
34
+ sequence = sequence[0];
30
35
  }
31
-
32
- let res = await chain.add_config(sequence, {...options, _id})
36
+
37
+ let res = await chain.add_config(sequence, { ...options, _id });
33
38
 
34
39
  return res;
35
- }
40
+ };
36
41
 
37
- run = async(payload, socket)=>{
38
- let {address, _id, env, config, thread, callback}= payload;
42
+ run = async (payload, socket) => {
43
+ let { address, _id, env, config, thread, callback } = payload;
39
44
 
40
- if(!config){
41
- let phy = `${this.physical_address}/${address}`
42
- let chain = await this.chain.chain(phy)
45
+ if (!config) {
46
+ let phy = `${this.physical_address}/${address}`;
47
+ let chain = await this.chain.chain(phy);
43
48
 
44
- let folder = await chain.folder('.configs')
45
- config = await folder.readone(_id? {_id}: null)
49
+ let folder = await chain.folder(".configs");
50
+ config = await folder.readone(_id ? { _id } : null);
46
51
 
47
- if (!config){
48
- let code = await this.manager.oracle.read(`${phy}.air`, {account: this.physical_address, prefix: '.codes', address: phy})
49
- if (code){
50
- code = await this.compiler.to_sequence(code, address)
51
- code = code[0]
52
+ if (!config) {
53
+ let code = await this.manager.oracle.read(`${phy}.air`, {
54
+ account: this.physical_address,
55
+ prefix: ".codes",
56
+ address: phy,
57
+ });
58
+ if (code) {
59
+ code = await this.compiler.to_sequence(code, address);
60
+ code = code[0];
52
61
 
53
62
  config = code;
54
63
 
55
- await this.load({address, sequence: config, _id})
64
+ await this.load({ address, sequence: config, _id });
56
65
  }
57
66
  }
58
67
  }
59
-
60
- if (!config){
61
- return {message: 'Code not found!'}
68
+
69
+ if (!config) {
70
+ return { message: "Code not found!" };
62
71
  }
63
72
 
64
- let result = await this.vm.nodelist(config)
65
-
66
- let thread_id = await this.manager.load(result, this, {spawn: thread && thread._id, pointer: thread && thread.pointer, callback: async res=>{
67
- if (typeof callback === 'string'){
68
- if (callback === 'store' && !Array.isArray(res)){
69
- let store_fn = await this.vm.callable('store');
70
- await store_fn.callable({object: await this.vm.cloth(res)}, {vm: this.vm, chain: this.chain})
71
- callback = res.value
72
- }else {
73
- let folder = await this.ds.folder(callback)
74
- await folder.write(res, {replace: true})
73
+ let result = await this.vm.nodelist(config);
74
+
75
+ let thread_id = await this.manager.load(result, this, {
76
+ spawn: thread && thread._id,
77
+ pointer: thread && thread.pointer,
78
+ callback: async (res) => {
79
+ if (typeof callback === "string") {
80
+ if (callback === "store" && !Array.isArray(res)) {
81
+ let store_fn = await this.vm.callable("store");
82
+ await store_fn.callable(
83
+ { object: await this.vm.cloth(res) },
84
+ { vm: this.vm, chain: this.chain }
85
+ );
86
+ callback = res.value;
87
+ } else {
88
+ let folder = await this.ds.folder(callback);
89
+ await folder.write(res, { replace: true });
90
+ }
91
+ } else if (typeof callback === "function") {
92
+ await callback(res);
75
93
  }
76
- }else if (typeof callback === 'function'){
77
- await callback(res)
78
- }
79
- if (socket){
80
- // handle socket
81
- }
82
- }})
94
+ if (socket) {
95
+ // handle socket
96
+ }
97
+ },
98
+ });
83
99
 
84
- if (env){
85
- this.vm.envs[thread_id] = env
100
+ if (env) {
101
+ this.vm.envs[thread_id] = env;
86
102
  }
87
103
 
88
- return {thread_id, callback}
89
- }
90
-
91
- parse = async (payload)=>{
92
- let {address, query, retrieve, from} = payload
104
+ return { thread_id, callback };
105
+ };
93
106
 
94
- if (!address){
95
- return {
96
- message: 'Invalid payload.'
97
- }
98
- }
99
- let phy = `${this.physical_address}/${address}`
107
+ parse = async (payload) => {
108
+ let { address, query, retrieve, from } = payload;
100
109
 
101
- if (from === '.codes'){
102
- let res = await this.manager.oracle.read(`${phy}.air`, {account: this.physical_address, address: phy, prefix: from})
103
- if (res){
104
- return {result: res}
105
- }else return {message: 'Code not found'}
110
+ if (!address) {
111
+ return {
112
+ message: "Invalid payload.",
113
+ };
106
114
  }
107
- else if (from === 'thread'){
115
+ let phy = `${this.physical_address}/${address}`;
116
+
117
+ if (from === ".codes") {
118
+ let res = await this.manager.oracle.read(`${phy}.air`, {
119
+ account: this.physical_address,
120
+ address: phy,
121
+ prefix: from,
122
+ });
123
+ if (res) {
124
+ return { result: res };
125
+ } else return { message: "Code not found" };
126
+ } else if (from === "thread") {
108
127
  // is thread_id;
109
- let thread_info = this.manager.accounts_pairs[this.name]
110
- let thread = thread_info.threads[address]
111
-
112
- return thread ? {
113
- _id: thread._id,
114
- status: thread.status,
115
- spawn: thread.spawn,
116
- callback: thread.callback,
117
- pointer: thread.pointer,
118
- total: thread.bullets.length
119
- } : {}
128
+ let thread_info = this.manager.accounts_pairs[this.name];
129
+ let thread = thread_info.threads[address];
130
+
131
+ return thread
132
+ ? {
133
+ _id: thread._id,
134
+ status: thread.status,
135
+ spawn: thread.spawn,
136
+ callback: thread.callback,
137
+ pointer: thread.pointer,
138
+ total: thread.bullets.length,
139
+ }
140
+ : {};
120
141
  }
121
142
 
122
- let folder = await this.ds.folder(phy, from === 'ram' ? null : {account: this.physical_address})
123
-
124
- let result = await folder.readone(query.query, {...query.options})
143
+ let folder = await this.ds.folder(
144
+ phy,
145
+ from === "ram" ? null : { account: this.physical_address }
146
+ );
147
+
148
+ let result = await folder.readone(query.query, { ...query.options });
125
149
  if (!result) return result;
126
-
127
- if (from !== 'ram'){
128
- if (this.vm.datatypes.includes(result.type)){
129
- let obj = await this.vm.retrieve(result, true)
130
- if (retrieve){
131
- result = await obj.to_address()
132
- } else result.literal = await (obj).literal()
133
- }else if (retrieve){
134
- result = await this.vm.retrieve(result)
150
+
151
+ if (from !== "ram") {
152
+ if (this.vm.datatypes.includes(result.type)) {
153
+ let obj = await this.vm.retrieve(result, true);
154
+ if (retrieve) {
155
+ result = await obj.to_address();
156
+ } else result.literal = await obj.literal();
157
+ } else if (retrieve) {
158
+ result = await this.vm.retrieve(result);
135
159
  }
136
160
  } else {
137
- if (this.vm.datatypes.includes(result.type)){
138
- if (retrieve){
139
- result = await (await this.vm.cloth_content(result)).to_address()
140
- }else result.literal = await (await this.vm.cloth_content(result)).literal()
141
- }else if (retrieve){
142
- result = await this.vm.cloth_content(result)
161
+ if (this.vm.datatypes.includes(result.type)) {
162
+ if (retrieve) {
163
+ result = await (await this.vm.cloth_content(result)).to_address();
164
+ } else
165
+ result.literal = await (
166
+ await this.vm.cloth_content(result)
167
+ ).literal();
168
+ } else if (retrieve) {
169
+ result = await this.vm.cloth_content(result);
143
170
  }
144
171
  }
145
172
 
146
- return result
147
- }
173
+ return result;
174
+ };
148
175
 
149
- verify = async(password)=>{
150
- let servers = await this.manager.oracle.get_servers(this.physical_address)
151
-
152
- if(!servers.length) servers = await this.manager.oracle.update_servers(this.physical_address)
176
+ verify = async (password) => {
177
+ let passes = await this.ds.folder(`${this.physical_address}/__passwords`, {
178
+ account: this.physical_address,
179
+ });
153
180
 
154
- let passes = await this.ds.folder(`${this.physical_address}/__passwords`, {account: this.physical_address})
181
+ let pass = await passes.readone(),
182
+ passed = false;
155
183
 
156
- let pass = await passes.readone()
157
-
158
- if (pass){
159
- let passed = pass && pass.key === hash(password)
160
-
161
- passed && await this.manager.oracle.update_servers(this.physical_address)
162
- return passed
163
- }else await passes.write({key: hash(password)})
184
+ if (pass) {
185
+ passed = pass && pass.key === hash(password);
186
+ } else {
187
+ passed = await passes.write({ key: hash(password) });
188
+ }
164
189
 
165
- return true;
166
- }
167
-
168
- sync = async()=>{
169
- this.physical_address = `${"Accounts"}/${this.name}`
170
- this.chain = new Chain(`${this.physical_address}`, this)
171
- this.ds = this.manager.ds
190
+ this.servers =
191
+ passed &&
192
+ (await this.manager.oracle.update_servers(this.physical_address));
193
+
194
+ return passed;
195
+ };
172
196
 
173
- let pass = this.options.password, res = true
197
+ sync = async () => {
198
+ this.physical_address = `${"Accounts"}/${this.name}`;
199
+ this.chain = new Chain(`${this.physical_address}`, this);
200
+ this.ds = this.manager.ds;
201
+
202
+ let pass = this.options.password,
203
+ res = true;
174
204
 
175
205
  if (pass) {
176
- res = await this.verify(pass)
206
+ res = await this.verify(pass);
177
207
  }
178
208
 
179
- await register(this)
209
+ await register(this);
180
210
 
181
- if (res){
182
- console.log(`Account:${this.name} initiation successful!`)
183
- } else console.log(`Account:${this.name} initiation failed!`)
211
+ if (res) {
212
+ console.log(`Account:${this.name} initiation successful!`);
213
+ } else console.log(`Account:${this.name} initiation failed!`);
184
214
 
185
215
  return res;
186
- }
216
+ };
187
217
  }
188
218
 
189
219
  export default Account;
@@ -372,22 +372,20 @@ class Manager {
372
372
  app: this.options.app,
373
373
  });
374
374
 
375
- this.handler = (req, res, cb) => {
375
+ this.handler = async (req, res, cb) => {
376
376
  handle(req, res);
377
377
 
378
378
  if (this.options.oracle && !this.served) {
379
379
  this.served = true;
380
380
  this.oracle = this.options.oracle;
381
- this.oracle.sync(this).then(async () => {
382
- if (this.options.init_account) {
383
- await this.add_account(this.options.init_account.name, {
384
- password: this.options.init_account.password,
385
- init: true,
386
- });
387
-
388
- cb && cb();
389
- }
390
- });
381
+ if (this.options.init_account) {
382
+ await this.add_account(this.options.init_account.name, {
383
+ password: this.options.init_account.password,
384
+ init: true,
385
+ });
386
+
387
+ cb && cb();
388
+ }
391
389
  }
392
390
  };
393
391
  }
package/Objects/Oracle.js CHANGED
@@ -3,15 +3,14 @@ import { post_request } from "../utils/services.js";
3
3
  class Oracle_client {
4
4
  constructor(server) {
5
5
  this.server = server;
6
+
7
+ this.repo_cache = new Object();
6
8
  }
7
9
 
8
- add_repo = async (repo, filter) => {
10
+ add_repo = async (repo) => {
9
11
  await this.fetch({
10
12
  method: "add_repo",
11
- args: {
12
- repo,
13
- filter,
14
- },
13
+ args: repo,
15
14
  });
16
15
  };
17
16
 
@@ -23,7 +22,9 @@ class Oracle_client {
23
22
  };
24
23
 
25
24
  read = async (path) => {
26
- let repo = this.repo_cache[path];
25
+ let repo = this.repo_cache[path],
26
+ content;
27
+
27
28
  if (repo) {
28
29
  content = await repo.read(path);
29
30
  }
@@ -42,21 +43,40 @@ class Oracle_client {
42
43
  return content;
43
44
  };
44
45
 
46
+ server_match = (s1, s2) => s1.hostname === s2.hostname && s1.port === s2.port;
47
+
48
+ update_servers = async (account) => {
49
+ let addr = `${account}/.servers`;
50
+
51
+ let servers = await this.read(addr);
52
+ servers = servers ? JSON.parse(servers) : [];
53
+ if (!servers.find((s) => this.server_match(s, this.client))) {
54
+ servers.push(this.client);
55
+ await this.write(addr, JSON.stringify(servers));
56
+ }
57
+ return servers;
58
+ };
59
+
45
60
  fetch = async (payload) => {
61
+ let headers;
46
62
  if (payload.method !== "authenticate" && !this.auth_token) {
47
63
  await this.sync(this.client);
64
+ } else if (payload.method !== "authenticate") {
65
+ headers = {
66
+ authorisation: this.auth_token,
67
+ };
48
68
  }
69
+
49
70
  try {
50
71
  let resp = await post_request({
51
72
  options: {
52
73
  ...this.server,
53
74
  path: `/oracle/${payload.method}`,
54
- headers: {
55
- authorisation: this.auth_token,
56
- },
75
+ headers,
57
76
  },
58
77
  data: JSON.stringify(payload.args),
59
78
  });
79
+
60
80
  return resp || null;
61
81
  } catch (e) {
62
82
  console.warn("fetch error:", e.message || e);
@@ -81,7 +101,7 @@ class Oracle_client {
81
101
  };
82
102
 
83
103
  cloth_repo = async (repo) => {
84
- return await this.Repos.cloth_repo(repo);
104
+ return this.Repos && (await this.Repos.cloth_repo(repo));
85
105
  };
86
106
  }
87
107
 
package/README.md CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "1.2.26",
3
+ "version": "1.2.28",
4
4
  "description": "A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.",
5
5
  "main": "Index.js",
6
6
  "type": "module",