Skip to content

Commit 6220421

Browse files
authored
Merge pull request #839 from EYBlockchain/liju.jose/refactor-config-and-consts
refactor done seprated constants from config
2 parents 8a8971b + 089ee94 commit 6220421

Some content is hidden

Large Commits have some content hidden by default. Use the searcx below for content that may be hidden.

54 files changed

+157
-141
lines changed

‎common-files/classes/timber.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ A class for timber-like merkle trees.
77

88
import config from 'config';
99
import utils from '../utils/crypto/merkle-tree/utils.mjs';
10+
import constants from '../constants/index.mjs';
1011

11-
const { TIMBER_HEIGHT, HASH_TYPE, ZERO } = config;
12+
const { TIMBER_HEIGHT, HASH_TYPE } = config;
13+
const { ZERO } = constants;
1214

1315
/**
1416
Helper functions for use in the Timber class, defined here before use

‎common-files/classes/transaction.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
/**
55
An optimistic Transaction class
66
*/
7-
import config from 'config';
87
import gen from 'general-number';
98
import Web3 from '../utils/web3.mjs';
109
import { compressProof } from '../utils/curve-maths/curves.mjs';
10+
import constants from '../constants/index.mjs';
1111

1212
const { generalise } = gen;
1313

1414
const TOKEN_TYPES = { ERC20: 0, ERC721: 1, ERC1155: 2 };
15-
const { TRANSACTION_TYPES } = config;
15+
const { TRANSACTION_TYPES } = constants;
1616

1717
// function to compute the keccak hash of a transaction
1818
function keccak(preimage) {

‎common-files/constants/constants.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"PROPOSERS_CONTRACT_NAME": "Proposers",
3+
"SHIELD_CONTRACT_NAME": "Shield",
4+
"CHALLENGES_CONTRACT_NAME": "Challenges",
5+
"STATE_CONTRACT_NAME": "State",
6+
"BLOCK_PROPOSED_EVENT_NAME": "BlockProposed",
7+
"ZKP_KEY_LENGTH": 32,
8+
"NODE_HASHLENGTH": 32,
9+
"ZERO": "0x0000000000000000000000000000000000000000000000000000000000000000",
10+
"BLOCK_TYPES": "(uint48,address,bytes32,uint256,bytes32,bytes32)",
11+
"TRANSACTION_TYPES":
12+
"(uint112,uint64[2],uint8,uint8,bytes32,bytes32,bytes32,bytes32[2],bytes32[2],bytes32[2],uint[4])",
13+
"PROPOSE_BLOCK_TYPES": [
14+
"(uint48,address,bytes32,uint256,bytes32,bytes32)",
15+
"(uint112,uint64[2],uint8,uint8,bytes32,bytes32,bytes32,bytes32[2],bytes32[2],bytes32[2],uint[4])[]"
16+
],
17+
"SUBMIT_TRANSACTION_TYPES":
18+
"(uint112,uint64[2],uint8,uint8,bytes32,bytes32,bytes32,bytes32[2],bytes32[2],bytes32[2],uint[4])"
19+
}

‎common-files/constants/index.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* ignore unused exports */
2+
3+
import { createRequire } from 'module';
4+
5+
const require = createRequire(import.meta.url);
6+
const constants = require('./constants.json');
7+
8+
export default constants;

‎config/default.js

+14-30
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,23 @@ module.exports = {
3939
PEERS_COLLECTION: 'peers',
4040
TIMBER_COLLECTION: 'timber',
4141
CIRCUIT_COLLECTION: 'circuit_storage',
42+
KEYS_COLLECTION: 'keys',
4243
CONTRACT_ARTIFACTS: '/app/build/contracts',
43-
PROPOSERS_CONTRACT_NAME: 'Proposers',
44-
SHIELD_CONTRACT_NAME: 'Shield',
45-
CHALLENGES_CONTRACT_NAME: 'Challenges',
46-
STATE_CONTRACT_NAME: 'State',
44+
EXCLUDE_DIRS: 'common',
45+
PROOF_QUEUE: 'generate-proof',
46+
MAX_QUEUE: 5,
47+
TIMBER_HEIGHT: 32,
48+
TXHASH_TREE_HEIGHT: 5,
49+
CONFIRMATION_POLL_TIME: 1000,
50+
CONFIRMATIONS: 12,
51+
DEFAULT_ACCOUNT_NUM: 10,
52+
HASH_TYPE: 'poseidon',
53+
TXHASH_TREE_HASH_TYPE: 'keccak256',
4754
STATE_GENESIS_BLOCK: process.env.STATE_GENESIS_BLOCK,
48-
BLOCK_PROPOSED_EVENT_NAME: 'BlockProposed',
4955
CIRCUITS_HOME: process.env.CIRCUITS_HOME || '/app/circuits/',
5056
ALWAYS_DO_TRUSTED_SETUP: process.env.ALWAYS_DO_TRUSTED_SETUP || false,
51-
EXCLUDE_DIRS: 'common', // don't setup files with this in their path
5257
LOG_LEVEL: process.env.LOG_LEVEL || 'debug',
5358
MONGO_URL: process.env.MONGO_URL || 'mongodb://localhost:27017/',
54-
ZKP_KEY_LENGTH: 32, // use a 32 byte key length for SHA compatibility
55-
CONFIRMATION_POLL_TIME: 1000, // time to wait before querying the blockchain (ms). Must be << block interval
56-
CONFIRMATIONS: 12, // number of confirmations to wait before accepting a transaction
5759
PROTOCOL: 'http://', // connect to zokrates microservice like this
5860
WEBSOCKET_PORT: process.env.WEBSOCKET_PORT || 8080,
5961
WEBSOCKET_PING_TIME: 15000,
@@ -97,33 +99,18 @@ module.exports = {
9799
PROVING_SCHEME: process.env.PROVING_SCHEME || 'g16',
98100
BACKEND: process.env.BACKEND || 'bellman',
99101
CURVE: process.env.CURVE || 'bn128',
100-
PROOF_QUEUE: 'generate-proof',
101-
BN128_GROUP_ORDER: 21888242871839275222246405745257275088548364400416034343698204186575808495617n,
102-
BN128_PRIME_FIELD: 21888242871839275222246405745257275088696311157297823662689037894645226208583n,
102+
103103
TRANSACTIONS_PER_BLOCK: Number(process.env.TRANSACTIONS_PER_BLOCK) || 2,
104-
BLOCK_TYPES: '(uint48,address,bytes32,uint256,bytes32,bytes32)',
105-
TRANSACTION_TYPES:
106-
'(uint112,uint64[2],uint8,uint8,bytes32,bytes32,bytes32,bytes32[2],bytes32[2],bytes32[2],uint[4])',
107-
PROPOSE_BLOCK_TYPES: [
108-
'(uint48,address,bytes32,uint256,bytes32,bytes32)',
109-
'(uint112,uint64[2],uint8,uint8,bytes32,bytes32,bytes32,bytes32[2],bytes32[2],bytes32[2],uint[4])[]',
110-
], // used to encode/decode proposeBlock signature
111-
SUBMIT_TRANSACTION_TYPES:
112-
'(uint112,uint64[2],uint8,uint8,bytes32,bytes32,bytes32,bytes32[2],bytes32[2],bytes32[2],uint[4])',
113104
RETRIES: Number(process.env.AUTOSTART_RETRIES) || 50,
114-
NODE_HASHLENGTH: 32,
115-
ZERO: '0x0000000000000000000000000000000000000000000000000000000000000000',
116-
HASH_TYPE: 'poseidon',
117-
TXHASH_TREE_HASH_TYPE: 'keccak256',
118105
USE_STUBS: process.env.USE_STUBS === 'true',
119106
VK_IDS: { deposit: 0, single_transfer: 1, double_transfer: 2, withdraw: 3 }, // used as an enum to mirror the Shield contracts enum for vk types. The keys of this object must correspond to a 'folderpath' (the .zok file without the '.zok' bit)
120-
TIMBER_HEIGHT: 32,
121-
TXHASH_TREE_HEIGHT: 5,
122107
MAX_PUBLIC_VALUES: {
123108
ERCADDRESS: 2n ** 161n - 1n,
124109
COMMITMENT: 2n ** 249n - 1n,
125110
NULLIFIER: 2n ** 249n - 1n,
126111
},
112+
BN128_GROUP_ORDER: 21888242871839275222246405745257275088548364400416034343698204186575808495617n,
113+
BN128_PRIME_FIELD: 21888242871839275222246405745257275088696311157297823662689037894645226208583n,
127114
// the various parameters needed to describe the Babyjubjub curve that we use for El-Gamal
128115
// BABYJUBJUB
129116
// Montgomery EC form is y^2 = x^3 + Ax^2 + Bx
@@ -147,7 +134,6 @@ module.exports = {
147134
ELLIGATOR2: {
148135
U: BigInt(5), // non square in Fp
149136
},
150-
MAX_QUEUE: 5,
151137
MPC: {
152138
MPC_PARAMS_URL:
153139
'https://nightfallv3-proving-files.s3.eu-west-1.amazonaws.com/phase2/mpc_params',
@@ -452,8 +438,6 @@ module.exports = {
452438
eventWsUrl:
453439
process.env.LOCAL_PROPOSER === 'true' ? process.env.LOCAL_WS_URL : process.env.PROPOSER_WS_URL,
454440

455-
KEYS_COLLECTION: 'keys',
456-
DEFAULT_ACCOUNT_NUM: 10,
457441
AWS: {
458442
s3Bucket: configureAWSBucket(),
459443
circuitFiles: parseCircuitFilesPath(),

‎nightfall-administrator/src/services/contract-transactions.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import config from 'config';
22
import { waitForContract, web3 } from '../../../common-files/utils/contract.mjs';
33
import logger from '../../../common-files/utils/logger.mjs';
4+
import constants from '../../../common-files/constants/index.mjs';
45
import { addMultiSigSignature, getTokenAddress } from './helpers.mjs';
56

6-
const { RESTRICTIONS, SHIELD_CONTRACT_NAME } = config;
7+
const { RESTRICTIONS } = config;
8+
const { SHIELD_CONTRACT_NAME } = constants;
79
const pausables = ['State', 'Shield'];
810

911
/**

‎nightfall-client/src/event-handlers/block-proposed.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import config from 'config';
22
import logger from 'common-files/utils/logger.mjs';
33
import Timber from 'common-files/classes/timber.mjs';
44
import getTimeByBlock from 'common-files/utils/block-info.mjs';
5+
import constants from 'common-files/constants/index.mjs';
56
import {
67
markNullifiedOnChain,
78
markOnChain,
@@ -22,7 +23,8 @@ import {
2223
} from '../services/database.mjs';
2324
import { decryptCommitment } from '../services/commitment-sync.mjs';
2425

25-
const { ZERO, HASH_TYPE, TIMBER_HEIGHT, TXHASH_TREE_HASH_TYPE, TXHASH_TREE_HEIGHT } = config;
26+
const { TIMBER_HEIGHT, TXHASH_TREE_HEIGHT, HASH_TYPE, TXHASH_TREE_HASH_TYPE } = config;
27+
const { ZERO } = constants;
2628

2729
/**
2830
This handler runs whenever a BlockProposed event is emitted by the blockchain

‎nightfall-client/src/event-handlers/subscribe.mjs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
*/
66
import config from 'config';
77
import { getContractInstance, getContractAddress } from 'common-files/utils/contract.mjs';
8+
import constants from 'common-files/constants/index.mjs';
89
import logger from 'common-files/utils/logger.mjs';
910

10-
const { STATE_CONTRACT_NAME, RETRIES } = config;
11-
11+
const { RETRIES } = config;
12+
const { STATE_CONTRACT_NAME } = constants;
1213
/**
1314
* Function that tries to get a (named) contract instance and, if it fails, will
1415
* retry after 3 seconds. After RETRIES attempts, it will give up and throw.

‎nightfall-client/src/services/commitment-sync.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ commitmentsync services to decrypt commitments from transaction blockproposed ev
33
or use clientCommitmentSync to decrypt when new zkpPrivateKey is received.
44
*/
55

6-
import config from 'config';
76
import logger from 'common-files/utils/logger.mjs';
87
import { generalise } from 'general-number';
98
import { edwardsDecompress } from 'common-files/utils/curve-maths/curves.mjs';
9+
import constants from 'common-files/constants/index.mjs';
1010
import { getAllTransactions } from './database.mjs';
1111
import { countCommitments, storeCommitment } from './commitment-storage.mjs';
1212
import { decrypt, packSecrets } from './kem-dem.mjs';
1313
import { ZkpKeys } from './keys.mjs';
1414
import Commitment from '../classes/commitment.mjs';
1515

16-
const { ZERO } = config;
16+
const { ZERO } = constants;
1717

1818
/**
1919
decrypt commitments for a transaction given zkpPrivateKeys and nullifierKeys.

‎nightfall-client/src/services/database.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const {
1616
TIMBER_COLLECTION,
1717
SUBMITTED_BLOCKS_COLLECTION,
1818
TRANSACTIONS_COLLECTION,
19-
HASH_TYPE,
2019
TIMBER_HEIGHT,
20+
HASH_TYPE,
2121
} = config;
2222

2323
/**

‎nightfall-client/src/services/deposit.mjs

+4-9
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,14 @@ import gen from 'general-number';
1212
import { randValueLT } from 'common-files/utils/crypto/crypto-random.mjs';
1313
import { getContractInstance } from 'common-files/utils/contract.mjs';
1414
import logger from 'common-files/utils/logger.mjs';
15+
import constants from 'common-files/constants/index.mjs';
1516
import { Commitment, Transaction } from '../classes/index.mjs';
1617
import { storeCommitment } from './commitment-storage.mjs';
1718
import { ZkpKeys } from './keys.mjs';
1819

19-
const {
20-
ZOKRATES_WORKER_HOST,
21-
SHIELD_CONTRACT_NAME,
22-
PROVING_SCHEME,
23-
BACKEND,
24-
PROTOCOL,
25-
USE_STUBS,
26-
BN128_GROUP_ORDER,
27-
} = config;
20+
const { ZOKRATES_WORKER_HOST, PROVING_SCHEME, BACKEND, PROTOCOL, USE_STUBS, BN128_GROUP_ORDER } =
21+
config;
22+
const { SHIELD_CONTRACT_NAME } = constants;
2823
const { generalise } = gen;
2924

3025
async function deposit(items) {

‎nightfall-client/src/services/finalise-withdrawal.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
Module to endable withdrawal of funds from the Shield contract to the user's
33
address.
44
*/
5-
import config from 'config';
65
import { getContractInstance } from 'common-files/utils/contract.mjs';
6+
import constants from 'common-files/constants/index.mjs';
77
import { Transaction } from '../classes/index.mjs';
88
import { getTransactionByTransactionHash, getBlockByTransactionHash } from './database.mjs';
99

10-
const { SHIELD_CONTRACT_NAME } = config;
10+
const { SHIELD_CONTRACT_NAME } = constants;
1111

1212
// TODO move classes to their own folder so this is not needed (it's already a
1313
// static function in the Block class)

‎nightfall-client/src/services/instant-withdrawal.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* This module enables setting instant withdrawals fees
33
*/
44

5-
import config from 'config';
65
import { getContractInstance } from 'common-files/utils/contract.mjs';
6+
import constants from 'common-files/constants/index.mjs';
77
import { Transaction } from '../classes/index.mjs';
88
import { buildSolidityStruct } from './finalise-withdrawal.mjs';
99
import { getTransactionByTransactionHash, getBlockByTransactionHash } from './database.mjs';
1010

11-
const { SHIELD_CONTRACT_NAME } = config;
11+
const { SHIELD_CONTRACT_NAME } = constants;
1212

1313
const setInstantWithdrawl = async ({ transactionHash }) => {
1414
const block = await getBlockByTransactionHash(transactionHash);

‎nightfall-client/src/services/peers.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
22
* This module discovers and manages optimistic peers for direct transfers
33
*/
4-
import config from 'config';
54
import { getContractInstance } from 'common-files/utils/contract.mjs';
5+
import constants from 'common-files/constants/index.mjs';
66

7-
const { STATE_CONTRACT_NAME } = config;
7+
const { STATE_CONTRACT_NAME } = constants;
88

99
/* Retrieve N next proposers (eth address + URL). If N is not defined, retrieve all */
1010
const getProposersUrl = async N => {

‎nightfall-client/src/services/process-calldata.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ Function to retreive calldata associated with a blockchain event.
33
This is used, rather than re-emmiting the calldata in the event because it's
44
much cheaper, although the offchain part is more complex.
55
*/
6-
import config from 'config';
76
import Web3 from 'common-files/utils/web3.mjs';
87
import Transaction from 'common-files/classes/transaction.mjs';
98
import { decompressProof } from 'common-files/utils/curve-maths/curves.mjs';
9+
import constants from 'common-files/constants/index.mjs';
1010

11-
const { PROPOSE_BLOCK_TYPES } = config;
11+
const { PROPOSE_BLOCK_TYPES } = constants;
1212

1313
async function getProposeBlockCalldata(eventData) {
1414
const web3 = Web3.connection();

‎nightfall-client/src/services/state-sync.mjs

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ import logger from 'common-files/utils/logger.mjs';
1010
import mongo from 'common-files/utils/mongo.mjs';
1111
import downloadFile from 'common-files/utils/httputils.mjs';
1212
import { unpauseQueue } from 'common-files/utils/event-queue.mjs';
13+
import constants from 'common-files/constants/index.mjs';
1314
import { waitForContract } from '../event-handlers/subscribe.mjs';
1415
import blockProposedEventHandler from '../event-handlers/block-proposed.mjs';
1516
import rollbackEventHandler from '../event-handlers/rollback.mjs';
1617

18+
const { STATE_CONTRACT_NAME } = constants;
1719
const {
1820
MONGO_URL,
1921
COMMITMENTS_DB,
2022
COMMITMENTS_COLLECTION,
21-
STATE_CONTRACT_NAME,
23+
CONTRACT_ARTIFACTS,
2224
STATE_GENESIS_BLOCK,
2325
DEPLOYMENT_FILES_URL: { DEFAULT_CONTRACT_FILES_URL },
2426
} = config;
@@ -78,8 +80,8 @@ const checkContractsABI = async () => {
7880
const res = await axios.get(url); // get all json abi contracts
7981
const files = res.data.split('\n');
8082

81-
if (!fs.existsSync(`${config.CONTRACT_ARTIFACTS}`)) {
82-
fs.mkdirSync(`${config.CONTRACT_ARTIFACTS}`);
83+
if (!fs.existsSync(`${CONTRACT_ARTIFACTS}`)) {
84+
fs.mkdirSync(`${CONTRACT_ARTIFACTS}`);
8385
}
8486

8587
logger.info(`Downloading contracts from ${url}...`);
@@ -90,7 +92,7 @@ const checkContractsABI = async () => {
9092
try {
9193
await downloadFile(
9294
`${baseUrl}/build/contracts/${f.split(' ')[1]}`,
93-
`${config.CONTRACT_ARTIFACTS}/${f.split(' ')[1]}`,
95+
`${CONTRACT_ARTIFACTS}/${f.split(' ')[1]}`,
9496
);
9597
} catch (e) {
9698
console.error(`ERROR downloading ${f.split(' ')[1]}`);

‎nightfall-client/src/services/transfer.mjs

+4-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { randValueLT } from 'common-files/utils/crypto/crypto-random.mjs';
1212
import { getContractInstance } from 'common-files/utils/contract.mjs';
1313
import logger from 'common-files/utils/logger.mjs';
1414
import { edwardsCompress } from 'common-files/utils/curve-maths/curves.mjs';
15+
import constants from 'common-files/constants/index.mjs';
1516
import { Nullifier, Commitment, Transaction } from '../classes/index.mjs';
1617
import {
1718
findUsableCommitmentsMutex,
@@ -24,15 +25,9 @@ import getProposersUrl from './peers.mjs';
2425
import { ZkpKeys } from './keys.mjs';
2526
import { encrypt, genEphemeralKeys, packSecrets } from './kem-dem.mjs';
2627

27-
const {
28-
BN128_GROUP_ORDER,
29-
ZOKRATES_WORKER_HOST,
30-
PROVING_SCHEME,
31-
BACKEND,
32-
SHIELD_CONTRACT_NAME,
33-
PROTOCOL,
34-
USE_STUBS,
35-
} = config;
28+
const { BN128_GROUP_ORDER, ZOKRATES_WORKER_HOST, PROVING_SCHEME, BACKEND, PROTOCOL, USE_STUBS } =
29+
config;
30+
const { SHIELD_CONTRACT_NAME } = constants;
3631
const { generalise, GN } = gen;
3732

3833
const NEXT_N_PROPOSERS = 3;

‎nightfall-client/src/services/valid-withdrawal.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
Module to endable withdrawal of funds from the Shield contract to the user's
33
address.
44
*/
5-
import config from 'config';
65
import { getContractInstance } from 'common-files/utils/contract.mjs';
6+
import constants from 'common-files/constants/index.mjs';
77
import { Transaction } from '../classes/index.mjs';
88
// eslint-disable-next-line import/no-cycle
99
import { buildSolidityStruct } from './finalise-withdrawal.mjs';
1010

11-
const { SHIELD_CONTRACT_NAME } = config;
11+
const { SHIELD_CONTRACT_NAME } = constants;
1212

1313
// eslint-disable-next-line import/prefer-default-export
1414
export async function isValidWithdrawal({ block, transaction, index, siblingPath }) {

0 commit comments

Comments
 (0)