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.
Files changed (88) hide show
  1. package/Executables/callables.js +131 -0
  2. package/Executables/executables.js +230 -0
  3. package/Executables/functions/adminstrator/adm_register.js +29 -0
  4. package/Executables/functions/adminstrator/native_components.js +10 -0
  5. package/Executables/functions/adminstrator/query_selectors.js +10 -0
  6. package/Executables/functions/adminstrator/render.js +24 -0
  7. package/Executables/functions/array_register.js +161 -0
  8. package/Executables/functions/assign.js +19 -0
  9. package/Executables/functions/display.js +55 -0
  10. package/Executables/functions/folder.js +95 -0
  11. package/Executables/functions/folder_register.js +87 -0
  12. package/Executables/functions/importcheck.js +13 -0
  13. package/Executables/functions/indices.js +56 -0
  14. package/Executables/functions/len.js +22 -0
  15. package/Executables/functions/methods/array.js +272 -0
  16. package/Executables/functions/methods/string.js +197 -0
  17. package/Executables/functions/methods/twain.js +148 -0
  18. package/Executables/functions/op.js +15 -0
  19. package/Executables/functions/price_register.js +30 -0
  20. package/Executables/functions/pricing.js +26 -0
  21. package/Executables/functions/servers/account.js +81 -0
  22. package/Executables/functions/servers/account_register.js +56 -0
  23. package/Executables/functions/servers/block.js +21 -0
  24. package/Executables/functions/servers/block_register.js +18 -0
  25. package/Executables/functions/servers/chain.js +42 -0
  26. package/Executables/functions/servers/chain_register.js +28 -0
  27. package/Executables/functions/set_error_catch.js +34 -0
  28. package/Executables/functions/string_register.js +130 -0
  29. package/Executables/functions/twain_register.js +56 -0
  30. package/Executables/functions/typecast.js +43 -0
  31. package/Executables/functions/utils.js +9 -0
  32. package/Executables/stack.js +53 -0
  33. package/Index.js +2 -7
  34. package/Loader_sequence/main.js +535 -119
  35. package/Loader_sequence/repository.js +33 -0
  36. package/Objects/Account.js +220 -54
  37. package/Objects/Block.js +46 -16
  38. package/Objects/Blockweb.js +14 -0
  39. package/Objects/Chain.js +163 -16
  40. package/Objects/Fee.js +33 -0
  41. package/Objects/Filesystem.js +100 -26
  42. package/Objects/Frame.js +10 -3
  43. package/Objects/Manager.js +272 -46
  44. package/Objects/Opcodes.js +60 -10
  45. package/Objects/Oracle.js +90 -5
  46. package/Objects/Protocol.js +5 -0
  47. package/Objects/Virtual_machine.js +145 -53
  48. package/Trinity.js +20 -0
  49. package/callables.js +172 -0
  50. package/framer.js +111 -52
  51. package/opcodes/add.js +6 -6
  52. package/opcodes/indices.js +82 -0
  53. package/opcodes/jmp.js +12 -11
  54. package/opcodes/std.js +7 -2
  55. package/opcodes.js +72 -14
  56. package/package.json +6 -5
  57. package/readme.md +1 -1
  58. package/starter_scripts/constants.seq +11 -0
  59. package/utils/create_server.js +34 -27
  60. package/utils/hash.js +2 -2
  61. package/utils/logger.js +79 -0
  62. package/utils/services.js +5 -3
  63. package/utils/type_coersion.js +3 -0
  64. package/utils/vm_utils.js +292 -0
  65. package/.babelrc +0 -3
  66. package/build/Account.js +0 -162
  67. package/build/Block.js +0 -65
  68. package/build/Blockweb.js +0 -36
  69. package/build/Chain.js +0 -119
  70. package/build/Explorer.js +0 -47
  71. package/build/Filesystem.js +0 -127
  72. package/build/Frame.js +0 -52
  73. package/build/Index.js +0 -15
  74. package/build/Manager.js +0 -99
  75. package/build/Opcodes.js +0 -39
  76. package/build/Oracle.js +0 -42
  77. package/build/Virtual_machine.js +0 -128
  78. package/build/add.js +0 -41
  79. package/build/create_server.js +0 -119
  80. package/build/framer.js +0 -57
  81. package/build/functions.js +0 -82
  82. package/build/hash.js +0 -14
  83. package/build/jmp.js +0 -19
  84. package/build/main.js +0 -283
  85. package/build/opcodes.js +0 -42
  86. package/build/privacy.js +0 -27
  87. package/build/services.js +0 -50
  88. package/build/std.js +0 -11
package/Objects/Chain.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { _id } from "generalised-datastore/utils/functions";
1
2
  import { hash } from "../utils/hash";
2
3
  import Block from "./Block";
3
4
  import Explorer from "./Explorer";
@@ -9,6 +10,7 @@ class Chain extends Explorer {
9
10
  this.parent = parent;
10
11
  this.account = this.parent.account;
11
12
  this.name = name || this.parent.name;
13
+
12
14
  this.physical_address =
13
15
  this.parent.manager || !name
14
16
  ? this.parent.physical_address
@@ -16,8 +18,7 @@ class Chain extends Explorer {
16
18
 
17
19
  this.pending_children = new Array();
18
20
  this.connections = new Array();
19
-
20
- this.blocks = new Array();
21
+
21
22
  this.txs = new Array();
22
23
 
23
24
  this.height = 0;
@@ -27,7 +28,52 @@ class Chain extends Explorer {
27
28
 
28
29
  this.generate_hash();
29
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
+
30
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
+ }
48
+ }
49
+
50
+ manage_config = (init) => {
51
+ if (init) {
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()
31
77
  }
32
78
 
33
79
  append_buffer = () => {
@@ -50,8 +96,45 @@ class Chain extends Explorer {
50
96
  this.hash = hash(this.physical_address);
51
97
  };
52
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
+
53
123
  get_block = (index) => {
54
- return this.blocks[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;
55
138
  };
56
139
 
57
140
  add_tx = (instruction) => {
@@ -60,36 +143,94 @@ class Chain extends Explorer {
60
143
  this.txs.push(instruction);
61
144
  };
62
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
+
63
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
+
64
173
  let block = new Block(this);
65
174
 
66
175
  block.generate_hash();
67
- this.add_block(block);
68
-
69
176
  block.store(no_curs);
70
177
 
71
178
  block.children = this.pending_children;
72
179
  this.pending_children = new Array();
73
180
 
74
- let prev_context = vm.get_context(-2);
75
- if (prev_context) {
76
- prev_context.pending_children.push(block);
77
- prev_context.connections.push(block);
78
- }
181
+ block.timelapse = Date.now() - this.start_time;
182
+
183
+ this.add_block(block);
184
+
185
+ vm.connect_block(block)
79
186
 
80
187
  this.txs = new Array();
81
188
 
82
- block.timelapse = Date.now() - this.start_time;
189
+ this.manage_config();
83
190
 
84
191
  return block;
85
192
  };
86
193
 
194
+ persist = block=>{
195
+ let data = block.stringify();
196
+
197
+ data = this.account.manager.oracle.handle_compression({
198
+ addr: this.folder.folder_path,
199
+ data,
200
+ no_string: true,
201
+ });
202
+
203
+ if(!data.previous_hash &&data.index!==0){
204
+ console.log(data)
205
+ return this.account.vm.stderr({name:"Chain_error", message:"chain corruption, as a non index 0 block is without a previous_hash"})
206
+ }
207
+
208
+ this.folder.write(data);
209
+ }
210
+
87
211
  add_block = (block) => {
88
212
  this.blocks.push(block);
89
- this.height = this.blocks.length;
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
+ }
90
227
  };
91
228
 
92
- get_latest_block = () => this.blocks.slice(-1)[0];
229
+ get_latest_block = () => {
230
+ let blk = this.recent_block;
231
+
232
+ return this.build_block(blk);
233
+ };
93
234
 
94
235
  stringify = (str) => {
95
236
  let obj = {};
@@ -99,11 +240,17 @@ class Chain extends Explorer {
99
240
  obj.parent = this.parent.path;
100
241
  obj.account = this.parent.account.name;
101
242
  obj.name = this.name;
102
- obj.connections = this.connections.map(
103
- (b) => new Object({ chain: b.chain.hash, block: b.hash })
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
104
251
  );
105
252
  obj.hash = this.hash;
106
- obj.blocks = this.blocks.map((blk) => blk.hash);
253
+ // obj.blocks = this.blocks.map((blk) => blk.hash || blk);
107
254
 
108
255
  return str ? JSON.stringify(obj) : obj;
109
256
  };
package/Objects/Fee.js ADDED
@@ -0,0 +1,33 @@
1
+ class Fee {
2
+ constructor(chain){
3
+ this.chain = chain;
4
+
5
+ this.fees = new Object()
6
+ }
7
+
8
+ set = ({purpose, fee, validator})=>{
9
+ let fees= this.fees[purpose]
10
+ if (!fees){
11
+ fees = new Array();
12
+ this.fees[purpose] = fees;
13
+ }
14
+ fees.push({fee, validator})
15
+ }
16
+
17
+ get = (purpose)=>{
18
+ let fees = this.fees[purpose]
19
+ if (!fees)return
20
+
21
+ return fees.slice(-1)[0]
22
+ }
23
+ }
24
+
25
+ // Fee is a physical_address
26
+ // Validator is also a physical_address
27
+ // If fee address is a callable/executable
28
+ // validator runs itself using the result from the address using the payload as argument 0
29
+ // Else if fee is a datastructure,
30
+ // validator runs itself using data as argument.
31
+ // Validator must return a true, to pass.
32
+
33
+ // A chain can have multiple price purposes.
@@ -2,21 +2,24 @@ import { hash } from "../utils/hash";
2
2
 
3
3
  class Filesystem {
4
4
  constructor() {
5
+ this.root = process.env.ROOT || __dirname;
5
6
  this.base_address = process.env.BASE_ADDRESS || "Accounts";
6
7
  }
7
8
 
8
9
  parse_path = (path) => {
9
- if (!path || (path && !path.includes("/"))) return path;
10
+ if (!path || (typeof path === 'string' && (!path.includes("/") || path.includes(' ')))) return path;
10
11
 
11
12
  return this.read(path);
12
13
  };
13
14
 
14
15
  read = (datapath) => {
15
16
  if (!datapath) return null;
16
-
17
+
17
18
  let type_folder = this.account.manager.oracle.get(datapath);
18
19
 
19
- if (!type_folder) return datapath;
20
+ if (!type_folder){
21
+ return datapath;
22
+ }
20
23
 
21
24
  let content;
22
25
  if (type_folder.type === "twain") {
@@ -36,35 +39,78 @@ class Filesystem {
36
39
  return content;
37
40
  };
38
41
 
39
- save = (content, chain, just_obj) => {
40
- let addr, obj;
42
+ object_type = (obj, chain)=>{
43
+ let type = typeof obj
44
+ if (type === 'object'){
45
+ if (Array.isArray(obj)) type = 'array'
46
+ else if (!obj) type = 'void'
47
+ else type = 'twain'
48
+ }
41
49
 
42
- let type = this.get_type(chain);
50
+ if (!chain)return type;
43
51
 
44
- if (type === "array" || type === "string") {
45
- obj = [];
52
+ type = `${type[0].toUpperCase()}${type.slice(1)}`
53
+
54
+ return this.get_datatype_chain(type)
55
+ }
46
56
 
47
- for (let curs in content) {
48
- obj.splice(Number(curs) || -1, 0, content[curs]);
49
- }
50
- if (type === "string") obj = obj.join(" ");
51
- } else if (type === "twain") {
52
- obj = {};
53
- for (let curs in content) {
54
- obj[curs] = content[curs];
55
- }
56
- } else if (type === "number") {
57
- obj = Number(content["-1"]);
58
- if (isNaN(obj)) obj = -1;
59
- } else {
60
- obj = null;
57
+ get_datatype_chain = (type)=>{
58
+ let chain = this.vm.handle_chain(`${this.account.physical_address}/Datatypes/${type}`)
59
+
60
+ return chain
61
+ }
62
+
63
+ save = (content, chain, options) => {
64
+ let just_obj, pure;
65
+ if (typeof options !== 'object'){
66
+ just_obj = options
67
+ }else if(options) {
68
+ just_obj = options.just_obj
69
+ pure = options.pure
70
+ }
71
+ let addr, obj;
72
+
73
+ if (!chain){
74
+ chain = this.object_type(content, true)
61
75
  }
62
76
 
77
+ let type = typeof chain === "string" ? chain : this.get_type(chain);
78
+
79
+
80
+ if(!pure){
81
+ if(typeof content==='string'){
82
+ if(type!=="string")obj= JSON.parse(content)
83
+ else obj = content
84
+ } else{
85
+ if (type === "array" || type === "string") {
86
+ obj = [];
87
+
88
+ for (let curs in content)
89
+ obj.splice(Number(curs) || -1, 0, content[curs]);
90
+
91
+ if (type === "string") obj = obj.join(" ");
92
+ } else if (type === "twain") {
93
+ obj = {};
94
+ for (let curs in content) {
95
+ obj[curs] = content[curs];
96
+ }
97
+ } else if (type === "number") {
98
+ obj = Number(content["-1"]);
99
+ if (isNaN(obj)) obj = 0;
100
+ }else if (type === 'boolean'){
101
+ obj = content['-1']
102
+ obj = obj === 'True' ? true:false
103
+ } else {
104
+ obj = null;
105
+ }
106
+ }
107
+ }else obj = content
108
+
63
109
  if (just_obj) return obj;
64
110
 
65
111
  addr = `${chain.path}/${hash(JSON.stringify(obj))}`;
66
112
  this;
67
- if (obj != null) this.manager.oracle.write(addr, obj);
113
+ if (obj != null || type === 'void') this.manager.oracle.write(addr, obj);
68
114
 
69
115
  this.manager.oracle.set(addr, { obj, type });
70
116
 
@@ -92,13 +138,41 @@ class Filesystem {
92
138
  }
93
139
  };
94
140
 
141
+ is_datatype=chain=>{
142
+
143
+ let type = this.get_type(chain);
144
+ return !!type
145
+ }
146
+
147
+ stringify_type = (type)=>{
148
+ let val;
149
+ switch(type){
150
+ case 'string':
151
+ val = ""
152
+ break;
153
+ case 'number':
154
+ val = "0"
155
+ break;
156
+ case 'array':
157
+ val = "[]"
158
+ break;
159
+ case 'twain':
160
+ val = "{}"
161
+ break;
162
+ case 'void':
163
+ val = 'null'
164
+ break
165
+ ;
166
+ }
167
+ return val
168
+ }
169
+
95
170
  write = (arg, context) => {
96
171
  let content = context.get_buffer();
97
172
 
98
173
  if (!content || !arg) return;
99
174
 
100
175
  content[context.cursor] = arg;
101
- // console.log(`Writing: ${arg}`);
102
176
  };
103
177
 
104
178
  name_hash = () => {
@@ -116,8 +190,8 @@ class Filesystem {
116
190
 
117
191
  let fs = this.manager.oracle.fs;
118
192
 
119
- if (!fs.existsSync(chain.path)) {
120
- fs.mkdirSync(chain.path, { recursive: true });
193
+ if (!fs.existsSync(`${this.manager.root}/${chain.path}`)) {
194
+ fs.mkdirSync(`${this.manager.root}/${chain.path}`, { recursive: true });
121
195
  }
122
196
  };
123
197
 
package/Objects/Frame.js CHANGED
@@ -8,15 +8,14 @@ class Frame {
8
8
  ? array_to_nested_objects(object)
9
9
  : object;
10
10
 
11
- this.children = new Array();
12
11
  this.instructions = new Array();
13
12
  }
14
13
 
15
14
  frame = (object) => {
16
15
  let frame = new Frame(object, this);
16
+ frame.account = this.account;
17
17
 
18
18
  this.instructions.push(frame);
19
- this.children.push(frame);
20
19
 
21
20
  return frame;
22
21
  };
@@ -26,7 +25,14 @@ class Frame {
26
25
  let val = this.object[prop];
27
26
  this.instructions.push(`chain ${prop}`);
28
27
 
29
- val && this.frame(val).to_instruction();
28
+ if(val && val.__type){
29
+ let instructions = this.account.assembler.run(JSON.stringify(val.value), {instructions: true, pure: true})
30
+
31
+ this.instructions.push(...instructions)
32
+
33
+ this.instructions.push(`cursor {output}`)
34
+ this.instructions.push(`write {datapath:-1}`)
35
+ }else val && this.frame(val).to_instruction();
30
36
 
31
37
  this.instructions.push(`pop ${prop}`);
32
38
  }
@@ -40,6 +46,7 @@ class Frame {
40
46
  if (ins instanceof Frame) inss.push(...ins.flatten_instructions());
41
47
  else inss.push(ins);
42
48
  }
49
+
43
50
  return inss;
44
51
  };
45
52
  }