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
PrevPrevious commit
fix: refactored constants and config
  • Loading branch information
@LijuJoseJJ
LijuJoseJJ committedAug 4, 2022
commit 089ee94ccb96b0d3648332c6ee2cd6d0d22d2435
4 changes: 2 additions & 2 deletions common-files/classes/timber.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,8 +9,8 @@ import config from 'config';
import utils from '../utils/crypto/merkle-tree/utils.mjs';
import constants from '../constants/index.mjs';

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

/**
Helper functions for use in the Timber class, defined here before use
Expand Down
9 changes: 0 additions & 9 deletions common-files/constants/constants.json
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
{
"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,
"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])",
Expand Down
3 changes: 1 addition & 2 deletions common-files/utils/contract.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,6 @@

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@@ -13,7 +12,7 @@ export const web3 = Web3.connection();
const options = config.WEB3_OPTIONS;

export const contractPath = contractName => {
return `${constants.CONTRACT_ARTIFACTS}/${contractName}.json`;
return `${config.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 } = constants;
const { MAX_QUEUE, CONFIRMATION_POLL_TIME, CONFIRMATIONS } = config;
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
9 changes: 9 additions & 0 deletions config/default.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,8 +40,17 @@ module.exports = {
TIMBER_COLLECTION: 'timber',
CIRCUIT_COLLECTION: 'circuit_storage',
KEYS_COLLECTION: 'keys',
CONTRACT_ARTIFACTS: '/app/build/contracts',
EXCLUDE_DIRS: 'common',
PROOF_QUEUE: 'generate-proof',
MAX_QUEUE: 5,
TIMBER_HEIGHT: 32,
TXHASH_TREE_HEIGHT: 5,
CONFIRMATION_POLL_TIME: 1000,
CONFIRMATIONS: 12,
DEFAULT_ACCOUNT_NUM: 10,
HASH_TYPE: 'poseidon',
TXHASH_TREE_HASH_TYPE: 'keccak256',
STATE_GENESIS_BLOCK: process.env.STATE_GENESIS_BLOCK,
CIRCUITS_HOME: process.env.CIRCUITS_HOME || '/app/circuits/',
ALWAYS_DO_TRUSTED_SETUP: process.env.ALWAYS_DO_TRUSTED_SETUP || false,
Expand Down
4 changes: 2 additions & 2 deletions nightfall-client/src/event-handlers/block-proposed.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,8 +23,8 @@ import {
} from '../services/database.mjs';
import { decryptCommitment } from '../services/commitment-sync.mjs';

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

/**
This handler runs whenever a BlockProposed event is emitted by the blockchain
Expand Down
3 changes: 1 addition & 2 deletions nightfall-client/src/services/database.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,16 +9,15 @@ 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 { HASH_TYPE } = constants;
const {
MONGO_URL,
COMMITMENTS_DB,
TIMBER_COLLECTION,
SUBMITTED_BLOCKS_COLLECTION,
TRANSACTIONS_COLLECTION,
TIMBER_HEIGHT,
HASH_TYPE,
} = config;

/**
Expand Down
3 changes: 2 additions & 1 deletion nightfall-client/src/services/state-sync.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,11 +15,12 @@ import { waitForContract } from '../event-handlers/subscribe.mjs';
import blockProposedEventHandler from '../event-handlers/block-proposed.mjs';
import rollbackEventHandler from '../event-handlers/rollback.mjs';

const { STATE_CONTRACT_NAME, CONTRACT_ARTIFACTS } = constants;
const { STATE_CONTRACT_NAME } = constants;
const {
MONGO_URL,
COMMITMENTS_DB,
COMMITMENTS_COLLECTION,
CONTRACT_ARTIFACTS,
STATE_GENESIS_BLOCK,
DEPLOYMENT_FILES_URL: { DEFAULT_CONTRACT_FILES_URL },
} = config;
Expand Down
3 changes: 1 addition & 2 deletions nightfall-deployer/src/circuit-setup.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,6 @@ import fs from 'fs';
import path from 'path';
import logger from 'common-files/utils/logger.mjs';
import Web3 from 'common-files/utils/web3.mjs';
import constants from 'common-files/constants/index.mjs';
import { waitForContract, getContractAddress } from 'common-files/utils/contract.mjs';

const fsPromises = fs.promises;
Expand DownExpand Up@@ -44,7 +43,7 @@ async function waitForZokrates() {

async function walk(dir) {
let files = await fsPromises.readdir(dir);
files = files.filter(file => !file.includes(constants.EXCLUDE_DIRS)); // remove common dir
files = files.filter(file => !file.includes(config.EXCLUDE_DIRS)); // remove common dir
files = await Promise.all(
files.map(async file => {
const filePath = path.join(dir, file);
Expand Down
4 changes: 2 additions & 2 deletions nightfall-optimist/src/classes/block.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,8 +7,8 @@ import Web3 from 'common-files/utils/web3.mjs';
import constants from 'common-files/constants/index.mjs';
import { getLatestBlockInfo, getTreeByBlockNumberL2 } from '../services/database.mjs';

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

/**
This Block class does not have the Block components that are computed on-chain.
Expand Down
4 changes: 2 additions & 2 deletions nightfall-optimist/src/event-handlers/block-proposed.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,8 +21,8 @@ import { getProposeBlockCalldata } from '../services/process-calldata.mjs';
import { increaseBlockInvalidCounter } from '../services/debug-counters.mjs';
import transactionSubmittedEventHandler from './transaction-submitted.mjs';

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

let ws;

Expand Down
5 changes: 2 additions & 3 deletions nightfall-optimist/src/routes/proposer.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,9 +25,8 @@ import transactionSubmittedEventHandler from '../event-handlers/transaction-subm
import getProposers from '../services/proposer.mjs';

const router = express.Router();
const { TIMBER_HEIGHT } = config;
const { STATE_CONTRACT_NAME, PROPOSERS_CONTRACT_NAME, SHIELD_CONTRACT_NAME, ZERO, HASH_TYPE } =
constants;
const { TIMBER_HEIGHT, HASH_TYPE } = config;
const { STATE_CONTRACT_NAME, PROPOSERS_CONTRACT_NAME, SHIELD_CONTRACT_NAME, ZERO } = constants;

let proposer;
export function setProposer(p) {
Expand Down
3 changes: 2 additions & 1 deletion nightfall-optimist/src/services/database.mjs
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,7 +9,7 @@ import mongo from 'common-files/utils/mongo.mjs';
import Timber from 'common-files/classes/timber.mjs';
import constants from 'common-files/constants/index.mjs';

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

const {
MONGO_URL,
Expand All@@ -23,6 +23,7 @@ const {
COMMIT_COLLECTION,
TIMBER_COLLECTION,
TIMBER_HEIGHT,
HASH_TYPE,
} = config;

/**
Expand Down
4 changes: 2 additions & 2 deletions wallet/src/common-files/classes/timber.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,8 +6,8 @@ A class for timber-like merkle trees.
*/
import utils from '../utils/crypto/merkle-tree/utils';

const { TIMBER_HEIGHT } = global.config;
const { HASH_TYPE, ZERO } = global.nightfallConstants;
const { TIMBER_HEIGHT, HASH_TYPE } = global.config;
const { ZERO } = global.nightfallConstants;

/**
Helper functions for use in the Timber class, defined here before use
Expand Down
2 changes: 1 addition & 1 deletion wallet/src/common-files/utils/event-queue.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,7 +20,7 @@ and catch these removals, processing them appropriately.
import Queue from 'queue';
import logger from './logger';

const { MAX_QUEUE } = global.nightfallConstants;
const { MAX_QUEUE } = global.config;
const fastQueue = new Queue({ autostart: true, concurrency: 1 });
const slowQueue = new Queue({ autostart: true, concurrency: 1 });
export const queues = [fastQueue, slowQueue];
Expand Down
4 changes: 2 additions & 2 deletions wallet/src/nightfall-browser/event-handlers/block-proposed.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,8 +24,8 @@ import {
updateTransactionTime,
} from '../services/database';

const { TIMBER_HEIGHT, TXHASH_TREE_HEIGHT } = global.config;
const { ZERO, HASH_TYPE, TXHASH_TREE_HASH_TYPE } = global.nightfallConstants;
const { TIMBER_HEIGHT, TXHASH_TREE_HEIGHT, HASH_TYPE, TXHASH_TREE_HASH_TYPE } = global.config;
const { ZERO } = global.nightfallConstants;

/**
This handler runs whenever a BlockProposed event is emitted by the blockchain
Expand Down
2 changes: 1 addition & 1 deletion wallet/src/nightfall-browser/services/database.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,8 +16,8 @@ const {
KEYS_COLLECTION,
CIRCUIT_COLLECTION,
TIMBER_HEIGHT,
HASH_TYPE,
} = global.config;
const { HASH_TYPE } = global.nightfallConstants;

// This needs to have better indexDB performance.

Expand Down
2 changes: 1 addition & 1 deletion wallet/src/views/wallet/index.jsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,7 +33,7 @@ import checkMarkCross from '../../assets/lottie/check-mark-cross.json';

const supportedTokens = importTokens();

const { DEFAULT_ACCOUNT_NUM } = global.nightfallConstants;
const { DEFAULT_ACCOUNT_NUM } = global.config;

const { ethereum } = global;

Expand Down