forked from ainblockchain/ain-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
constants.js
497 lines (463 loc) · 13.4 KB
/
constants.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
const fs = require('fs');
const path = require('path');
const moment = require('moment');
const {
ConsensusConsts,
ConsensusDbPaths,
} = require('./consensus/constants');
const ChainUtil = require('./chain-util');
const DEFAULT_GENESIS_CONFIGS_DIR = 'blockchain';
const CUSTOM_GENESIS_CONFIGS_DIR = process.env.GENESIS_CONFIGS_DIR ?
process.env.GENESIS_CONFIGS_DIR : null;
const BLOCKCHAINS_DIR = path.resolve(__dirname, 'blockchain/blockchains');
const PROTOCOL_VERSIONS = path.resolve(__dirname, 'client/protocol_versions.json');
const DEBUG = process.env.DEBUG ? process.env.DEBUG.toLowerCase().startsWith('t') : false;
const MAX_TX_BYTES = 10000;
const TRANSACTION_POOL_TIME_OUT_MS = moment.duration(1, 'hours').as('milliseconds');
const TRANSACTION_TRACKER_TIME_OUT_MS = moment.duration(24, 'hours').as('milliseconds');
// TODO (lia): Check network id in all messages
const NETWORK_ID = process.env.NETWORK_ID || 'Testnet';
// HOSTING_ENV is a variable used in extracting the ip address of the host machine,
// of which value could be either 'local', 'default', or 'gcp'.
const HOSTING_ENV = process.env.HOSTING_ENV || 'default';
const COMCOM_HOST_EXTERNAL_IP = process.env.COMCOM_HOST_EXTERNAL_IP ?
process.env.COMCOM_HOST_EXTERNAL_IP : '';
const COMCOM_HOST_INTERNAL_IP_MAP = {
'aincom1': '192.168.1.13',
'aincom2': '192.168.1.14',
}
const ACCOUNT_INDEX = process.env.ACCOUNT_INDEX || null;
const TRACKER_WS_ADDR = process.env.TRACKER_WS_ADDR || 'ws://localhost:5000';
const PORT = process.env.PORT || getPortNumber(8080, 8081);
const P2P_PORT = process.env.P2P_PORT || getPortNumber(5000, 5001);
const HASH_DELIMITER = '#';
const MAX_SHARD_REPORT = 100;
const LIGHTWEIGHT = process.env.LIGHTWEIGHT ? process.env.LIGHTWEIGHT.toLowerCase().startsWith('t') : false;
function getPortNumber(defaultValue, baseValue) {
if (HOSTING_ENV == 'local') {
return Number(baseValue) + (ACCOUNT_INDEX !== null ? Number(ACCOUNT_INDEX) : 0);
}
return defaultValue;
}
/**
* Message types for communication between nodes
* @enum {string}
*/
const MessageTypes = {
TRANSACTION: 'transaction',
CHAIN_SUBSECTION: 'chain_subsection',
CHAIN_SUBSECTION_REQUEST: 'chain_subsection_request',
CONSENSUS: 'consensus',
HEARTBEAT: 'heartbeat'
};
/**
* Predefined database paths
* @enum {string}
*/
// TODO (lia): Pick one convention: full-paths (e.g. /deposit/consensus) or keys (e.g. token)
const PredefinedDbPaths = {
// Roots
OWNERS_ROOT: 'owners',
RULES_ROOT: 'rules',
FUNCTIONS_ROOT: 'functions',
VALUES_ROOT: 'values',
// Consensus
CONSENSUS: 'consensus',
// Token
TOKEN: 'token',
TOKEN_NAME: 'name',
TOKEN_SYMBOL: 'symbol',
TOKEN_TOTAL_SUPPLY: 'total_supply',
// Accounts & Transfer
ACCOUNTS: 'accounts',
BALANCE: 'balance',
TRANSFER: 'transfer',
TRANSFER_VALUE: 'value',
TRANSFER_RESULT: 'result',
// Deposit & Withdraw
DEPOSIT: 'deposit',
DEPOSIT_ACCOUNTS: 'deposit_accounts',
DEPOSIT_CONFIG: 'config',
DEPOSIT_CREATED_AT: 'created_at',
DEPOSIT_EXPIRE_AT: 'expire_at',
DEPOSIT_LOCKUP_DURATION: 'lockup_duration',
DEPOSIT_RESULT: 'result',
DEPOSIT_VALUE: 'value',
WITHDRAW: 'withdraw',
WITHDRAW_CREATED_AT: 'created_at',
WITHDRAW_RESULT: 'result',
WITHDRAW_VALUE: 'value',
DEPOSIT_ACCOUNTS_CONSENSUS: 'deposit_accounts/consensus',
DEPOSIT_CONSENSUS: 'deposit/consensus',
WITHDRAW_CONSENSUS: 'withdraw/consensus',
// Remote transaction action
REMOTE_TX_ACTION_RESULT: 'result',
// Sharding
SHARDING: 'sharding',
SHARDING_CONFIG: 'config',
SHARDING_SHARD: 'shard',
// Check-in & Check-out
CHECKIN: 'checkin',
CHECKIN_REQUEST: 'request',
CHECKIN_PAYLOAD: 'payload',
CHECKIN_PARENT_FINALIZE: 'parent_finalize',
CHECKOUT: 'checkout',
};
/**
* Properties of token configs
* @enum {string}
*/
const TokenProperties = {
NAME: 'name',
SYMBOL: 'symbol',
TOTAL_SUPPLY: 'total_supply',
};
/**
* Properties of account configs
* @enum {string}
*/
const AccountProperties = {
ADDRESS: 'address',
OTHERS: 'others',
OWNER: 'owner',
PRIVATE_KEY: 'private_key',
PUBLIC_KEY: 'public_key',
SHARES: 'shares',
TIMESTAMP: 'timestamp',
};
/**
* Properties of owner configs
* @enum {string}
*/
const OwnerProperties = {
ANYONE: '*',
BRANCH_OWNER: 'branch_owner',
OWNER: '.owner',
OWNERS: 'owners',
WRITE_FUNCTION: 'write_function',
WRITE_OWNER: 'write_owner',
WRITE_RULE: 'write_rule',
};
/**
* Properties of rule configs
* @enum {string}
*/
const RuleProperties = {
WRITE: '.write',
};
/**
* Properties of function configs
* @enum {string}
*/
const FunctionProperties = {
EVENT_LISTENER: 'event_listener',
FUNCTION: '.function',
FUNCTION_ID: 'function_id',
FUNCTION_TYPE: 'function_type',
SERVICE_NAME: 'service_name',
};
/**
* Types of functions
* @enum {string}
*/
const FunctionTypes = {
NATIVE: 'NATIVE',
REST: 'REST',
};
/**
* Properties of proof configs
* @enum {string}
*/
const ProofProperties = {
PROOF_HASH: '.proof_hash',
};
/**
* IDs of native functions
* @enum {string}
*/
const NativeFunctionIds = {
DEPOSIT: '_deposit',
TRANSFER: '_transfer',
WITHDRAW: '_withdraw',
UPDATE_LATEST_SHARD_REPORT: '_updateLatestShardReport',
OPEN_CHECKIN: '_openCheckin',
CLOSE_CHECKIN: '_closeCheckin',
};
/**
* Properties of sharding configs
* @enum {string}
*/
const ShardingProperties = {
LATEST: 'latest',
PARENT_CHAIN_POC: 'parent_chain_poc',
PROOF_HASH: 'proof_hash',
PROOF_HASH_MAP: 'proof_hash_map',
REPORTING_PERIOD: 'reporting_period',
SHARD: '.shard',
SHARD_OWNER: 'shard_owner',
SHARD_REPORTER: 'shard_reporter',
SHARDING_ENABLED: 'sharding_enabled',
SHARDING_PATH: 'sharding_path',
SHARDING_PROTOCOL: 'sharding_protocol',
TOKEN_EXCH_SCHEME: 'token_exchange_scheme',
TOKEN_EXCH_RATE: 'token_exchange_rate',
};
/**
* Sharding protocols
* @enum {string}
*/
const ShardingProtocols = {
NONE: 'NONE',
POA: 'POA',
};
/**
* Token exchange schemes
* @enum {string}
*/
const TokenExchangeSchemes = {
NONE: 'NONE',
FIXED: 'FIXED',
};
/**
* Types of read database operations
* @enum {string}
*/
const ReadDbOperations = {
GET_VALUE: 'GET_VALUE',
GET_FUNCTION: 'GET_FUNCTION',
GET_RULE: 'GET_RULE',
GET_OWNER: 'GET_OWNER',
GET_PROOF: 'GET_PROOF',
MATCH_FUNCTION: 'MATCH_FUNCTION',
MATCH_RULE: 'MATCH_RULE',
MATCH_OWNER: 'MATCH_OWNER',
EVAL_RULE: 'EVAL_RULE',
EVAL_OWNER: 'EVAL_OWNER',
GET: 'GET',
};
/**
* Types of write database operations
* @enum {string}
*/
const WriteDbOperations = {
SET_VALUE: 'SET_VALUE',
INC_VALUE: 'INC_VALUE',
DEC_VALUE: 'DEC_VALUE',
SET_FUNCTION: 'SET_FUNCTION',
SET_RULE: 'SET_RULE',
SET_OWNER: 'SET_OWNER',
SET: 'SET',
};
/**
* Function result code
* @enum {string}
*/
const FunctionResultCode = {
SUCCESS: 'SUCCESS',
FAILURE: 'FAILURE',
INSUFFICIENT_BALANCE: 'INSUFFICIENT_BALANCE',
IN_LOCKUP_PERIOD: 'IN_LOCKUP_PERIOD',
};
/**
* Constant values for transactionTracker
* @enum {string}
*/
const TransactionStatus = {
BLOCK_STATUS: 'BLOCK',
POOL_STATUS: 'POOL',
TIMEOUT_STATUS: 'TIMEOUT',
FAIL_STATUS: 'FAIL'
};
/**
* Default values
*/
const DefaultValues = {
DEPOSIT_LOCKUP_DURATION_MS: 2592000000 // 30 days
}
const GenesisToken = getGenesisConfig('genesis_token.json');
const GenesisAccounts = getGenesisConfig('genesis_accounts.json');
const GenesisSharding = getGenesisSharding();
const GenesisWhitelist = getGenesisWhitelist();
const GenesisValues = getGenesisValues();
const GenesisFunctions = getGenesisFunctions();
const GenesisRules = getGenesisRules();
const GenesisOwners = getGenesisOwners();
function getGenesisConfig(filename, additionalEnv) {
let config = null;
if (CUSTOM_GENESIS_CONFIGS_DIR) {
const configPath = path.resolve(__dirname, CUSTOM_GENESIS_CONFIGS_DIR, filename);
if (fs.existsSync(configPath)) {
config = JSON.parse(fs.readFileSync(configPath));
}
}
if (!config) {
const configPath = path.resolve(__dirname, DEFAULT_GENESIS_CONFIGS_DIR, filename);
if (fs.existsSync(configPath)) {
config = JSON.parse(fs.readFileSync(configPath));
} else {
throw Error(`Missing genesis config file: ${configPath}`);
}
}
if (additionalEnv) {
const parts = additionalEnv.split(':');
const dbPath = parts[0];
const additionalFilePath = path.resolve(__dirname, parts[1])
if (fs.existsSync(additionalFilePath)) {
const additionalConfig = JSON.parse(fs.readFileSync(additionalFilePath));
ChainUtil.setJsObject(config, [dbPath], additionalConfig);
} else {
throw Error(`Missing additional genesis config file: ${additionalFilePath}`);
}
}
return config;
}
function getGenesisSharding() {
const config = getGenesisConfig('genesis_sharding.json');
if (config[ShardingProperties.SHARDING_PROTOCOL] === ShardingProtocols.POA) {
const ownerAddress = ChainUtil.getJsObject(
GenesisAccounts, [AccountProperties.OWNER, AccountProperties.ADDRESS]);
const reporterAddress =
GenesisAccounts[AccountProperties.OTHERS][0][AccountProperties.ADDRESS];
ChainUtil.setJsObject(config, [ShardingProperties.SHARD_OWNER], ownerAddress);
ChainUtil.setJsObject(config, [ShardingProperties.SHARD_REPORTER], reporterAddress);
}
return config;
}
// TODO(lia): Increase this list to 10.
function getGenesisWhitelist() {
const whitelist = {};
for (let i = 0; i < ConsensusConsts.INITIAL_NUM_VALIDATORS; i++) {
const accountAddress = GenesisAccounts[AccountProperties.OTHERS][i][AccountProperties.ADDRESS];
ChainUtil.setJsObject(
whitelist, [accountAddress], ConsensusConsts.INITIAL_STAKE);
}
return whitelist;
}
function getGenesisValues() {
const values = {};
ChainUtil.setJsObject(values, [PredefinedDbPaths.TOKEN], GenesisToken);
const ownerAddress = ChainUtil.getJsObject(
GenesisAccounts, [AccountProperties.OWNER, AccountProperties.ADDRESS]);
ChainUtil.setJsObject(
values,
[PredefinedDbPaths.ACCOUNTS, ownerAddress, PredefinedDbPaths.BALANCE],
GenesisToken[TokenProperties.TOTAL_SUPPLY]);
ChainUtil.setJsObject(
values, [PredefinedDbPaths.SHARDING, PredefinedDbPaths.SHARDING_CONFIG], GenesisSharding);
ChainUtil.setJsObject(
values, [ConsensusDbPaths.CONSENSUS, ConsensusDbPaths.WHITELIST], GenesisWhitelist);
return values;
}
function getGenesisFunctions() {
const functions = getGenesisConfig('genesis_functions.json', process.env.ADDITIONAL_FUNCTIONS);
return functions;
}
function getGenesisRules() {
const rules = getGenesisConfig('genesis_rules.json', process.env.ADDITIONAL_RULES);
if (GenesisSharding[ShardingProperties.SHARDING_PROTOCOL] !== ShardingProtocols.NONE) {
ChainUtil.setJsObject(
rules, [PredefinedDbPaths.SHARDING, PredefinedDbPaths.SHARDING_CONFIG], getShardingRule());
}
ChainUtil.setJsObject(
rules, [ConsensusDbPaths.CONSENSUS, ConsensusDbPaths.WHITELIST], getWhitelistRule());
return rules;
}
function getGenesisOwners() {
const owners = getGenesisConfig('genesis_owners.json', process.env.ADDITIONAL_OWNERS);
if (GenesisSharding[ShardingProperties.SHARDING_PROTOCOL] !== ShardingProtocols.NONE) {
ChainUtil.setJsObject(
owners, [PredefinedDbPaths.SHARDING, PredefinedDbPaths.SHARDING_CONFIG],
getShardingOwner());
}
ChainUtil.setJsObject(
owners, [ConsensusDbPaths.CONSENSUS, ConsensusDbPaths.WHITELIST], getWhitelistOwner());
return owners;
}
function getShardingRule() {
const ownerAddress =
ChainUtil.getJsObject(GenesisAccounts, [AccountProperties.OWNER, AccountProperties.ADDRESS]);
return {
[RuleProperties.WRITE]: `auth === '${ownerAddress}'`,
};
}
function getWhitelistRule() {
const ownerAddress =
ChainUtil.getJsObject(GenesisAccounts, [AccountProperties.OWNER, AccountProperties.ADDRESS]);
return {
[RuleProperties.WRITE]: `auth === '${ownerAddress}'`,
};
}
function getShardingOwner() {
return {
[OwnerProperties.OWNER]: {
[OwnerProperties.OWNERS]: {
[GenesisAccounts.owner.address]: buildOwnerPermissions(false, true, true, true),
[OwnerProperties.ANYONE]: buildOwnerPermissions(false, false, false, false),
}
}
};
}
function getWhitelistOwner() {
return {
[OwnerProperties.OWNER]: {
[OwnerProperties.OWNERS]: {
[GenesisAccounts.owner.address]: buildOwnerPermissions(false, true, true, true),
[OwnerProperties.ANYONE]: buildOwnerPermissions(false, false, false, false),
}
}
};
}
function buildOwnerPermissions(branchOwner, writeFunction, writeOwner, writeRule) {
return {
[OwnerProperties.BRANCH_OWNER]: branchOwner,
[OwnerProperties.WRITE_FUNCTION]: writeFunction,
[OwnerProperties.WRITE_OWNER]: writeOwner,
[OwnerProperties.WRITE_RULE]: writeRule
};
}
module.exports = {
BLOCKCHAINS_DIR,
PROTOCOL_VERSIONS,
DEBUG,
MAX_TX_BYTES,
TRANSACTION_POOL_TIME_OUT_MS,
TRANSACTION_TRACKER_TIME_OUT_MS,
NETWORK_ID,
HOSTING_ENV,
COMCOM_HOST_EXTERNAL_IP,
COMCOM_HOST_INTERNAL_IP_MAP,
ACCOUNT_INDEX,
PORT,
P2P_PORT,
TRACKER_WS_ADDR,
MAX_SHARD_REPORT,
LIGHTWEIGHT,
MessageTypes,
PredefinedDbPaths,
TokenProperties,
AccountProperties,
OwnerProperties,
RuleProperties,
FunctionProperties,
FunctionTypes,
FunctionResultCode,
ProofProperties,
NativeFunctionIds,
ShardingProperties,
ShardingProtocols,
TokenExchangeSchemes,
ReadDbOperations,
WriteDbOperations,
TransactionStatus,
DefaultValues,
GenesisToken,
GenesisAccounts,
GenesisSharding,
GenesisWhitelist,
GenesisValues,
GenesisFunctions,
GenesisRules,
GenesisOwners,
HASH_DELIMITER,
buildOwnerPermissions,
};