Skip to content

refactor done seprated constants from config #839

New issue

Have a question about this project? Sign up for a free account to open an issue and contact its maintainers and the community.

By clicking “Sign up for ”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on ? Sign in to your account

Merged
merged 8 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: refactor done seprated constants from config
  • Loading branch information
@LijuJoseJJ
LijuJoseJJ committedAug 2, 2022
commit 73f8d2e0f9e3ea8c495da1edc6ac10effb791aac
4 changes: 2 additions & 2 deletions common-files/classes/timber.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,10 +5,10 @@
A class for timber-like merkle trees.
*/

import config from 'config';
import utils from '../utils/crypto/merkle-tree/utils.mjs';
import constants from '../constants/index.mjs';

const { TIMBER_HEIGHT, HASH_TYPE, ZERO } = config;
const { TIMBER_HEIGHT, HASH_TYPE, ZERO } = constants;

/**
Helper functions for use in the Timber class, defined here before use
Expand Down
4 changes: 2 additions & 2 deletions common-files/classes/transaction.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,15 +4,15 @@
/**
An optimistic Transaction class
*/
import config from 'config';
import gen from 'general-number';
import Web3 from '../utils/web3.mjs';
import { compressProof } from '../utils/curve-maths/curves.mjs';
import constants from '../constants/index.mjs';

const { generalise } = gen;

const TOKEN_TYPES = { ERC20: 0, ERC721: 1, ERC1155: 2 };
const { TRANSACTION_TYPES } = config;
const { TRANSACTION_TYPES } = constants;

// function to compute the keccak hash of a transaction
function keccak(preimage) {
Expand Down
45 changes: 45 additions & 0 deletions common-files/constants/constants.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
{
"COMMITMENTS_DB": "nightfall_commitments",
"OPTIMIST_DB": "optimist_data",
"PROPOSER_COLLECTION": "proposers",
"CHALLENGER_COLLECTION": "challengers",
"TRANSACTIONS_COLLECTION": "transactions",
"SUBMITTED_BLOCKS_COLLECTION": "blocks",
"INVALID_BLOCKS_COLLECTION": "invalid_blocks",
"NULLIFIER_COLLECTION": "nullifiers",
"COMMIT_COLLECTION": "commits",
"WALLETS_COLLECTION": "wallets",
"COMMITMENTS_COLLECTION": "commitments",
"PEERS_COLLECTION": "peers",
"TIMBER_COLLECTION": "timber",
"CIRCUIT_COLLECTION": "circuit_storage",
"KEYS_COLLECTION": "keys",
"CONTRACT_ARTIFACTS": "/app/build/contracts",
"PROPOSERS_CONTRACT_NAME": "Proposers",
"SHIELD_CONTRACT_NAME": "Shield",
"CHALLENGES_CONTRACT_NAME": "Challenges",
"STATE_CONTRACT_NAME": "State",
"BLOCK_PROPOSED_EVENT_NAME": "BlockProposed",
"EXCLUDE_DIRS": "common",
"PROOF_QUEUE": "generate-proof",
"MAX_QUEUE": 5,
"ZKP_KEY_LENGTH": 32,
"CONFIRMATION_POLL_TIME": 1000,
"CONFIRMATIONS": 12,
"NODE_HASHLENGTH": 32,
"TIMBER_HEIGHT": 32,
"TXHASH_TREE_HEIGHT": 5,
"DEFAULT_ACCOUNT_NUM": 10,
"ZERO": "0x0000000000000000000000000000000000000000000000000000000000000000",
"HASH_TYPE": "poseidon",
"TXHASH_TREE_HASH_TYPE": "keccak256",
"BLOCK_TYPES": "(uint48,address,bytes32,uint256,bytes32,bytes32)",
"TRANSACTION_TYPES":
"(uint112,uint64[2],uint8,uint8,bytes32,bytes32,bytes32,bytes32[2],bytes32[2],bytes32[2],uint[4])",
"PROPOSE_BLOCK_TYPES": [
"(uint48,address,bytes32,uint256,bytes32,bytes32)",
"(uint112,uint64[2],uint8,uint8,bytes32,bytes32,bytes32,bytes32[2],bytes32[2],bytes32[2],uint[4])[]"
],
"SUBMIT_TRANSACTION_TYPES":
"(uint112,uint64[2],uint8,uint8,bytes32,bytes32,bytes32,bytes32[2],bytes32[2],bytes32[2],uint[4])"
}
8 changes: 8 additions & 0 deletions common-files/constants/index.mjs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
/* ignore unused exports */

import { createRequire } from 'module';

const require = createRequire(import.meta.url);
const constants = require('./constants.json');

export default constants;
3 changes: 2 additions & 1 deletion common-files/utils/contract.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@

import fs from 'fs';
import config from 'config';
import constants from '../constants/index.mjs';

import Web3 from './web3.mjs';
import logger from './logger.mjs';
Expand All@@ -12,7 +13,7 @@ export const web3 = Web3.connection();
const options = config.WEB3_OPTIONS;

export const contractPath = contractName => {
return `${config.CONTRACT_ARTIFACTS}/${contractName}.json`;
return `${constants.CONTRACT_ARTIFACTS}/${contractName}.json`;
};

export async function getContractInterface(contractName) {
Expand Down
4 changes: 2 additions & 2 deletions common-files/utils/event-queue.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,11 +20,11 @@ event with its `removed` property set to true. In the code below, we look out fo
and catch these removals, processing them appropriately.
*/
import Queue from 'queue';
import config from 'config';
import logger from 'common-files/utils/logger.mjs';
import { web3 } from 'common-files/utils/contract.mjs';
import constants from '../constants/index.mjs';

const { MAX_QUEUE, CONFIRMATION_POLL_TIME, CONFIRMATIONS } = config;
const { MAX_QUEUE, CONFIRMATION_POLL_TIME, CONFIRMATIONS } = constants;
const fastQueue = new Queue({ autostart: false, concurrency: 1 });
const slowQueue = new Queue({ autostart: false, concurrency: 1 });
const removed = {}; // singleton holding transaction hashes of any removed events
Expand Down
92 changes: 49 additions & 43 deletions config/default.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,35 +25,11 @@ BigInt..toJSON = function () {
};

module.exports = {
COMMITMENTS_DB: 'nightfall_commitments',
OPTIMIST_DB: 'optimist_data',
PROPOSER_COLLECTION: 'proposers',
CHALLENGER_COLLECTION: 'challengers',
TRANSACTIONS_COLLECTION: 'transactions',
SUBMITTED_BLOCKS_COLLECTION: 'blocks',
INVALID_BLOCKS_COLLECTION: 'invalid_blocks',
NULLIFIER_COLLECTION: 'nullifiers',
COMMIT_COLLECTION: 'commits',
WALLETS_COLLECTION: 'wallets',
COMMITMENTS_COLLECTION: 'commitments',
PEERS_COLLECTION: 'peers',
TIMBER_COLLECTION: 'timber',
CIRCUIT_COLLECTION: 'circuit_storage',
CONTRACT_ARTIFACTS: '/app/build/contracts',
PROPOSERS_CONTRACT_NAME: 'Proposers',
SHIELD_CONTRACT_NAME: 'Shield',
CHALLENGES_CONTRACT_NAME: 'Challenges',
STATE_CONTRACT_NAME: 'State',
STATE_GENESIS_BLOCK: process.env.STATE_GENESIS_BLOCK,
BLOCK_PROPOSED_EVENT_NAME: 'BlockProposed',
CIRCUITS_HOME: process.env.CIRCUITS_HOME || '/app/circuits/',
ALWAYS_DO_TRUSTED_SETUP: process.env.ALWAYS_DO_TRUSTED_SETUP || false,
EXCLUDE_DIRS: 'common', // don't setup files with this in their path
LOG_LEVEL: process.env.LOG_LEVEL || 'debug',
MONGO_URL: process.env.MONGO_URL || 'mongodb://localhost:27017/',
ZKP_KEY_LENGTH: 32, // use a 32 byte key length for SHA compatibility
CONFIRMATION_POLL_TIME: 1000, // time to wait before querying the blockchain (ms). Must be << block interval
CONFIRMATIONS: 12, // number of confirmations to wait before accepting a transaction
PROTOCOL: 'http://', // connect to zokrates microservice like this
WEBSOCKET_PORT: process.env.WEBSOCKET_PORT || 8080,
WEBSOCKET_PING_TIME: 15000,
Expand DownExpand Up@@ -97,33 +73,18 @@ module.exports = {
PROVING_SCHEME: process.env.PROVING_SCHEME || 'g16',
BACKEND: process.env.BACKEND || 'bellman',
CURVE: process.env.CURVE || 'bn128',
PROOF_QUEUE: 'generate-proof',
BN128_GROUP_ORDER: 21888242871839275222246405745257275088548364400416034343698204186575808495617n,
BN128_PRIME_FIELD: 21888242871839275222246405745257275088696311157297823662689037894645226208583n,

TRANSACTIONS_PER_BLOCK: Number(process.env.TRANSACTIONS_PER_BLOCK) || 2,
BLOCK_TYPES: '(uint48,address,bytes32,uint256,bytes32,bytes32)',
TRANSACTION_TYPES:
'(uint112,uint64[2],uint8,uint8,bytes32,bytes32,bytes32,bytes32[2],bytes32[2],bytes32[2],uint[4])',
PROPOSE_BLOCK_TYPES: [
'(uint48,address,bytes32,uint256,bytes32,bytes32)',
'(uint112,uint64[2],uint8,uint8,bytes32,bytes32,bytes32,bytes32[2],bytes32[2],bytes32[2],uint[4])[]',
], // used to encode/decode proposeBlock signature
SUBMIT_TRANSACTION_TYPES:
'(uint112,uint64[2],uint8,uint8,bytes32,bytes32,bytes32,bytes32[2],bytes32[2],bytes32[2],uint[4])',
RETRIES: Number(process.env.AUTOSTART_RETRIES) || 50,
NODE_HASHLENGTH: 32,
ZERO: '0x0000000000000000000000000000000000000000000000000000000000000000',
HASH_TYPE: 'poseidon',
TXHASH_TREE_HASH_TYPE: 'keccak256',
USE_STUBS: process.env.USE_STUBS === 'true',
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)
TIMBER_HEIGHT: 32,
TXHASH_TREE_HEIGHT: 5,
MAX_PUBLIC_VALUES: {
ERCADDRESS: 2n ** 161n - 1n,
COMMITMENT: 2n ** 249n - 1n,
NULLIFIER: 2n ** 249n - 1n,
},
BN128_GROUP_ORDER: 21888242871839275222246405745257275088548364400416034343698204186575808495617n,
BN128_PRIME_FIELD: 21888242871839275222246405745257275088696311157297823662689037894645226208583n,
// the various parameters needed to describe the Babyjubjub curve that we use for El-Gamal
// BABYJUBJUB
// Montgomery EC form is y^2 = x^3 + Ax^2 + Bx
Expand All@@ -147,7 +108,6 @@ module.exports = {
ELLIGATOR2: {
U: BigInt(5), // non square in Fp
},
MAX_QUEUE: 5,
MPC: {
MPC_PARAMS_URL:
'https://nightfallv3-proving-files.s3.eu-west-1.amazonaws.com/phase2/mpc_params',
Expand DownExpand Up@@ -452,8 +412,54 @@ module.exports = {
eventWsUrl:
process.env.LOCAL_PROPOSER === 'true' ? process.env.LOCAL_WS_URL : process.env.PROPOSER_WS_URL,

<<<<<<< HEAD
KEYS_COLLECTION: 'keys',
DEFAULT_ACCOUNT_NUM: 10,
=======
circuitsAWSFiles: {
deposit_stub: {
abi: 'circuits/deposit_stub/artifacts/deposit_stub-abi.json',
program: 'circuits/deposit_stub/artifacts/deposit_stub-program',
pk: 'circuits/deposit_stub/keypair/deposit_stub_pk.key',
},
withdraw_stub: {
abi: 'circuits/withdraw_stub/artifacts/withdraw_stub-abi.json',
program: 'circuits/withdraw_stub/artifacts/withdraw_stub-program',
pk: 'circuits/withdraw_stub/keypair/withdraw_stub_pk.key',
},
single_transfer_stub: {
abi: 'circuits/single_transfer_stub/artifacts/single_transfer_stub-abi.json',
program: 'circuits/single_transfer_stub/artifacts/single_transfer_stub-program',
pk: 'circuits/single_transfer_stub/keypair/single_transfer_stub_pk.key',
},
double_transfer_stub: {
abi: 'circuits/double_transfer_stub/artifacts/double_transfer_stub-abi.json',
program: 'circuits/double_transfer_stub/artifacts/double_transfer_stub-program',
pk: 'circuits/double_transfer_stub/keypair/double_transfer_stub_pk.key',
},
deposit: {
abi: 'circuits/deposit/artifacts/deposit-abi.json',
program: 'circuits/deposit/artifacts/deposit-program',
pk: 'circuits/deposit/keypair/deposit_pk.key',
},
withdraw: {
abi: 'circuits/withdraw/artifacts/withdraw-abi.json',
program: 'circuits/withdraw/artifacts/withdraw-program',
pk: 'circuits/withdraw/keypair/withdraw_pk.key',
},
single_transfer: {
abi: 'circuits/single_transfer/artifacts/single_transfer-abi.json',
program: 'circuits/single_transfer/artifacts/single_transfer-program',
pk: 'circuits/single_transfer/keypair/single_transfer_pk.key',
},
double_transfer: {
abi: 'circuits/double_transfer/artifacts/double_transfer-abi.json',
program: 'circuits/double_transfer/artifacts/double_transfer-program',
pk: 'circuits/double_transfer/keypair/double_transfer_pk.key',
},
},

>>>>>>> fix: refactor done seprated constants from config
AWS: {
s3Bucket: configureAWSBucket(),
circuitFiles: parseCircuitFilesPath(),
Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
import config from 'config';
import { waitForContract, web3 } from '../../../common-files/utils/contract.mjs';
import logger from '../../../common-files/utils/logger.mjs';
import constants from '../../../common-files/constants/index.mjs';
import { addMultiSigSignature, getTokenAddress } from './helpers.mjs';

const { RESTRICTIONS, SHIELD_CONTRACT_NAME } = config;
const { RESTRICTIONS } = config;
const { SHIELD_CONTRACT_NAME } = constants;
const pausables = ['State', 'Shield'];

/**
Expand Down
4 changes: 2 additions & 2 deletions nightfall-client/src/event-handlers/block-proposed.mjs
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
import config from 'config';
import logger from 'common-files/utils/logger.mjs';
import Timber from 'common-files/classes/timber.mjs';
import getTimeByBlock from 'common-files/utils/block-info.mjs';
import constants from 'common-files/constants/index.mjs';
import {
markNullifiedOnChain,
markOnChain,
Expand All@@ -22,7 +22,7 @@ import {
} from '../services/database.mjs';
import { decryptCommitment } from '../services/commitment-sync.mjs';

const { ZERO, HASH_TYPE, TIMBER_HEIGHT, TXHASH_TREE_HASH_TYPE, TXHASH_TREE_HEIGHT } = config;
const { ZERO, HASH_TYPE, TIMBER_HEIGHT, TXHASH_TREE_HASH_TYPE, TXHASH_TREE_HEIGHT } = constants;

/**
This handler runs whenever a BlockProposed event is emitted by the blockchain
Expand Down
5 changes: 3 additions & 2 deletions nightfall-client/src/event-handlers/subscribe.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,10 +5,11 @@
*/
import config from 'config';
import { getContractInstance, getContractAddress } from 'common-files/utils/contract.mjs';
import constants from 'common-files/constants/index.mjs';
import logger from 'common-files/utils/logger.mjs';

const { STATE_CONTRACT_NAME, RETRIES } = config;

const { RETRIES } = config;
const { STATE_CONTRACT_NAME } = constants;
/**
* Function that tries to get a (named) contract instance and, if it fails, will
* retry after 3 seconds. After RETRIES attempts, it will give up and throw.
Expand Down
4 changes: 3 additions & 1 deletion nightfall-client/src/services/commitment-storage.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ import { Mutex } from 'async-mutex';
import gen from 'general-number';
import mongo from 'common-files/utils/mongo.mjs';
import logger from 'common-files/utils/logger.mjs';
import constants from 'common-files/constants/index.mjs';
import { Commitment, Nullifier } from '../classes/index.mjs';
// eslint-disable-next-line import/no-cycle
import { isValidWithdrawal } from './valid-withdrawal.mjs';
Expand All@@ -16,7 +17,8 @@ import {
getTransactionHashSiblingInfo,
} from './database.mjs';

const { MONGO_URL, COMMITMENTS_DB, COMMITMENTS_COLLECTION } = config;
const { COMMITMENTS_DB, COMMITMENTS_COLLECTION } = constants;
const { MONGO_URL } = config;
const { generalise } = gen;
const mutex = new Mutex();

Expand Down
4 changes: 2 additions & 2 deletions nightfall-client/src/services/commitment-sync.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,17 +3,17 @@ commitmentsync services to decrypt commitments from transaction blockproposed ev
or use clientCommitmentSync to decrypt when new zkpPrivateKey is received.
*/

import config from 'config';
import logger from 'common-files/utils/logger.mjs';
import { generalise } from 'general-number';
import { edwardsDecompress } from 'common-files/utils/curve-maths/curves.mjs';
import constants from 'common-files/constants/index.mjs';
import { getAllTransactions } from './database.mjs';
import { countCommitments, storeCommitment } from './commitment-storage.mjs';
import { decrypt, packSecrets } from './kem-dem.mjs';
import { ZkpKeys } from './keys.mjs';
import Commitment from '../classes/commitment.mjs';

const { ZERO } = config;
const { ZERO } = constants;

/**
decrypt commitments for a transaction given zkpPrivateKeys and nullifierKeys.
Expand Down
5 changes: 3 additions & 2 deletions nightfall-client/src/services/database.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,16 +9,17 @@ import config from 'config';
import mongo from 'common-files/utils/mongo.mjs';
import Timber from 'common-files/classes/timber.mjs';
import logger from 'common-files/utils/logger.mjs';
import constants from 'common-files/constants/index.mjs';

const {
MONGO_URL,
COMMITMENTS_DB,
TIMBER_COLLECTION,
SUBMITTED_BLOCKS_COLLECTION,
TRANSACTIONS_COLLECTION,
HASH_TYPE,
TIMBER_HEIGHT,
} = config;
} = constants;
const { MONGO_URL } = config;

/**
Timber functions
Expand Down
13 changes: 4 additions & 9 deletions nightfall-client/src/services/deposit.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,19 +12,14 @@ import gen from 'general-number';
import { randValueLT } from 'common-files/utils/crypto/crypto-random.mjs';
import { getContractInstance } from 'common-files/utils/contract.mjs';
import logger from 'common-files/utils/logger.mjs';
import constants from 'common-files/constants/index.mjs';
import { Commitment, Transaction } from '../classes/index.mjs';
import { storeCommitment } from './commitment-storage.mjs';
import { ZkpKeys } from './keys.mjs';

const {
ZOKRATES_WORKER_HOST,
SHIELD_CONTRACT_NAME,
PROVING_SCHEME,
BACKEND,
PROTOCOL,
USE_STUBS,
BN128_GROUP_ORDER,
} = config;
const { ZOKRATES_WORKER_HOST, PROVING_SCHEME, BACKEND, PROTOCOL, USE_STUBS, BN128_GROUP_ORDER } =
config;
const { SHIELD_CONTRACT_NAME } = constants;
const { generalise } = gen;

async function deposit(items) {
Expand Down
4 changes: 2 additions & 2 deletions nightfall-client/src/services/finalise-withdrawal.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,12 +2,12 @@
Module to endable withdrawal of funds from the Shield contract to the user's
address.
*/
import config from 'config';
import { getContractInstance } from 'common-files/utils/contract.mjs';
import constants from 'common-files/constants/index.mjs';
import { Transaction } from '../classes/index.mjs';
import { getTransactionByTransactionHash, getBlockByTransactionHash } from './database.mjs';

const { SHIELD_CONTRACT_NAME } = config;
const { SHIELD_CONTRACT_NAME } = constants;

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