Skip to content
This repository has been archived by the owner on Feb 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1320 from matter-labs/deniallugo-zks-364-issues-w…
Browse files Browse the repository at this point in the history
…ith-toml-configs

 issues with toml configs
  • Loading branch information
popzxc authored Jan 14, 2021
2 parents 27ef734 + b027e41 commit 426176f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 14 deletions.
7 changes: 5 additions & 2 deletions core/lib/config/src/configs/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ pub struct DBConfig {
impl DBConfig {
pub fn from_env() -> Self {
Self {
pool_size: env!("DB_POOL_SIZE").parse().unwrap(),
url: env!("DATABASE_URL").to_string(),
pool_size: std::env::var("DB_POOL_SIZE")
.expect("DB_POOL_SIZE is set")
.parse()
.unwrap(),
url: std::env::var("DATABASE_URL").expect("DATABASE_URL is set"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/zk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"ethers": "^5.0.18",
"node-fetch": "^2.6.1",
"tabtab": "^3.0.2",
"toml": "^3.0.0"
"@iarna/toml": "^2.2.5"
},
"devDependencies": {
"@types/node": "^14.6.1",
Expand Down
5 changes: 2 additions & 3 deletions infrastructure/zk/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command } from 'commander';
import * as toml from 'toml';
import * as toml from '@iarna/toml';
import * as fs from 'fs';
import * as path from 'path';
import deepExtend from 'deep-extend';
Expand Down Expand Up @@ -46,8 +46,7 @@ async function loadConfig(environment: string, configName: string) {
const configPath = getConfigPath(environment, configName);
const fileContents = await fs.promises.readFile(configPath);
try {
const tomlData = toml.parse(fileContents.toString());
return tomlData;
return toml.parse(fileContents.toString());
} catch (e) {
console.error(
`<${environment}/${configName}> load failed: Parsing error on line ${e.line} column ${e.column}: ${e.message}`
Expand Down
1 change: 1 addition & 0 deletions infrastructure/zk/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export async function deploy() {
const matches = deployLog.match(pattern);
if (matches !== null) {
env.modify(envVar, matches[0]);
env.modify_contracts_toml(envVar, matches[0]);
}
}
}
Expand Down
36 changes: 33 additions & 3 deletions infrastructure/zk/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import fs from 'fs';
import dotenv from 'dotenv';
import * as utils from './utils';
import * as config from './config';
import * as toml from '@iarna/toml';

export function get() {
fs.readdirSync('etc/env').forEach((file) => {
if (file.endsWith('.bak') || file.endsWith('example') || file == 'current') {
if (!file.endsWith('.env')) {
return;
}

const env = file.replace(/\..*$/, '');
if (env == process.env.ZKSYNC_ENV) {
console.log(' * ' + env);
Expand All @@ -29,11 +31,17 @@ export async function gitHooks() {

export function set(env: string) {
const envFile = `etc/env/${env}.env`;
const envDir = `etc/env/${env}`;
if (!fs.existsSync(envFile)) {
throw new Error(envFile + ' not found');
}
if (!fs.existsSync(envDir)) {
throw new Error(envFile + ' not found');
}

fs.writeFileSync('etc/env/current', env);
process.env.ENV_FILE = envFile;
process.env.ENV_DIR = envDir;
process.env.ZKSYNC_ENV = env;
get();
}
Expand All @@ -54,14 +62,25 @@ export async function load() {
const zksyncEnv =
process.env.ZKSYNC_ENV || (fs.existsSync(current) ? fs.readFileSync(current).toString().trim() : 'dev');
const envFile = `etc/env/${zksyncEnv}.env`;
if (zksyncEnv == 'dev' && !fs.existsSync('etc/env/dev.env')) {
await config.compileConfig();
const envDir = `etc/env/${zksyncEnv}`;
if (zksyncEnv == 'dev') {
/// If there no folder with toml files we should delete the old dev.env and regenerate toml files and
if (!fs.existsSync('etc/env/dev')) {
if (fs.existsSync('etc/env/dev.env')) {
fs.rmSync('etc/env/dev.env');
}
}

if (!fs.existsSync('etc/env/dev.env')) {
await config.compileConfig();
}
}
if (!fs.existsSync(envFile)) {
throw new Error('ZkSync config file not found: ' + envFile);
}
process.env.ZKSYNC_ENV = zksyncEnv;
process.env.ENV_FILE = envFile;
process.env.ENV_DIR = envDir;
dotenv.config({ path: envFile });
}

Expand All @@ -74,6 +93,17 @@ export function modify(variable: string, assignedVariable: string) {
reload();
}

export function modify_contracts_toml(variable: string, assignedVariable: string) {
const toml_file = `${process.env.ENV_DIR}/contracts.toml`;
const source = fs.readFileSync(toml_file).toString();
const toml_res = toml.parse(source);
const trimmed_variable = variable.replace('CONTRACTS_', '');
const trimmed_value = assignedVariable.split('=');
// @ts-ignore
toml_res['contracts'][trimmed_variable] = trimmed_value[1];
fs.writeFileSync(toml_file, toml.stringify(toml_res));
}

export const command = new Command('env')
.arguments('[env_name]')
.description('get or set zksync environment')
Expand Down
1 change: 1 addition & 0 deletions infrastructure/zk/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function genesis() {
fs.mkdirSync(`logs/${label}`, { recursive: true });
fs.copyFileSync('genesis.log', `logs/${label}/genesis.log`);
env.modify('CONTRACTS_GENESIS_ROOT', genesisRoot);
env.modify_contracts_toml('CONTRACTS_GENESIS_ROOT', genesisRoot);
}

export const command = new Command('server')
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,11 @@
dependencies:
"@hapi/hoek" "^8.3.0"

"@iarna/toml@^2.2.5":
version "2.2.5"
resolved "https://registry.yarnpkg.com/@iarna/toml/-/toml-2.2.5.tgz#b32366c89b43c6f8cefbdefac778b9c828e3ba8c"
integrity sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==

"@intervolga/optimize-cssnano-plugin@^1.0.5":
version "1.0.6"
resolved "https://registry.yarnpkg.com/@intervolga/optimize-cssnano-plugin/-/optimize-cssnano-plugin-1.0.6.tgz#be7c7846128b88f6a9b1d1261a0ad06eb5c0fdf8"
Expand Down Expand Up @@ -12612,11 +12617,6 @@ [email protected]:
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553"
integrity sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==

toml@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/toml/-/toml-3.0.0.tgz#342160f1af1904ec9d204d03a5d61222d762c5ee"
integrity sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==

toposort@^1.0.0:
version "1.0.7"
resolved "https://registry.yarnpkg.com/toposort/-/toposort-1.0.7.tgz#2e68442d9f64ec720b8cc89e6443ac6caa950029"
Expand Down

0 comments on commit 426176f

Please sign in to comment.