From 10cceb851b60e090f60db4a4a0b1542805122de9 Mon Sep 17 00:00:00 2001 From: Francisco Tobar Date: Tue, 10 Oct 2023 16:38:03 -0600 Subject: [PATCH] refactor: server initialization --- docs/architecture.md | 1 - etc/js/env-config.js | 26 ------- infrastructure/zk/src/docker.ts | 3 - infrastructure/zk/src/run/forced-exit.ts | 99 +++++++++++++++--------- infrastructure/zk/src/run/run.ts | 5 -- infrastructure/zk/src/server.ts | 14 +++- 6 files changed, 74 insertions(+), 74 deletions(-) delete mode 100644 etc/js/env-config.js diff --git a/docs/architecture.md b/docs/architecture.md index 6feb5a9636..c878b1111e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -83,7 +83,6 @@ This section provides an overview on folders / sub-projects that exist in this r - `/docker`: Dockerfiles used for development of zkSync and for packaging zkSync for a production environment. - `/etc`: Configration files. - `/env`: `.env` files that contain environment variables for different configuration of zkSync Server / Prover. - - `/js`: Configuration files for JavaScript applications (such as Explorer). - `/tokens`: Configuration of supported Rootstock ERC-20 tokens. - `/infrastructure`: Application that aren't naturally a part of zkSync core, but are related to it. - `/keys`: Verification keys for `circuit` module. diff --git a/etc/js/env-config.js b/etc/js/env-config.js deleted file mode 100644 index 090bc8a97a..0000000000 --- a/etc/js/env-config.js +++ /dev/null @@ -1,26 +0,0 @@ -export default { - 'http://localhost': { - API_SERVER: 'https://localhost:3001', - WALLET_ADDRESS: 'http://localhost:3000', - EXPLORER: 'http://localhost:7001', - ETH_NETWORK: 'localhost', - WS_API_ADDR: 'ws://localhost:3031', - HTTP_RPC_API_ADDR: 'http://localhost:3030' - }, - 'https://explorer.dev.aggregation.rifcomputing.net': { - API_SERVER: 'https://dev.aggregation.rifcomputing.net:3029', - WALLET_ADDRESS: 'https://wallet.dev.aggregation.rifcomputing.net', - EXPLORER: 'https://explorer.testnet.rsk.co', - ETH_NETWORK: 'testnet', - WS_API_ADDR: 'https://dev.aggregation.rifcomputing.net:3031', - HTTP_RPC_API_ADDR: 'https://dev.aggregation.rifcomputing.net:3030' - }, - 'https://explorer.aggregation.rifcomputing.net': { - API_SERVER: 'https://aggregation.rifcomputing.net:3029', - WALLET_ADDRESS: 'https://wallet.aggregation.rifcomputing.net', - EXPLORER: 'https://explorer.rsk.co', - ETH_NETWORK: 'rsk_mainnet', - WS_API_ADDR: 'https://aggregation.rifcomputing.net:3031', - HTTP_RPC_API_ADDR: 'https://aggregation.rifcomputing.net:3030' - } -}[`${location.protocol}//${location.hostname}`]; diff --git a/infrastructure/zk/src/docker.ts b/infrastructure/zk/src/docker.ts index 3900fb5998..1f27ff9ac2 100644 --- a/infrastructure/zk/src/docker.ts +++ b/infrastructure/zk/src/docker.ts @@ -36,9 +36,6 @@ async function dockerCommand(command: 'push' | 'build', image: string, tag: stri } async function _build(image: string, tag: string) { - if (image == 'nginx') { - await utils.spawn('yarn explorer build'); - } if (image == 'server' || image == 'prover') { await contract.build(); } diff --git a/infrastructure/zk/src/run/forced-exit.ts b/infrastructure/zk/src/run/forced-exit.ts index 4e9b059762..a02e1aaf47 100644 --- a/infrastructure/zk/src/run/forced-exit.ts +++ b/infrastructure/zk/src/run/forced-exit.ts @@ -1,7 +1,7 @@ import { Command } from 'commander'; import fetch from 'node-fetch'; -import * as utils from '../utils'; import { BigNumber, BigNumberish, ethers } from 'ethers'; +import * as utils from '../utils'; type State = { balances: { @@ -19,28 +19,36 @@ type RpcAccountState = { verified: State; }; -async function checkForcedExitSenderAccountBalance(nodeUrl: string, nodeType: 'REST' | 'JSONRPC') { +const SYNC = 'sync:0000000000000000000000000000000000000000'; + +async function isForcedExitSenderAccountReady(nodeUrl: string, nodeType: string): Promise { + if (nodeType !== 'REST' && nodeType !== 'JSONRPC') { + console.log('Node type must be either REST or JSONRPC'); + return false; + } + const forcedExitAccount = process.env.FORCED_EXIT_REQUESTS_SENDER_ACCOUNT_ADDRESS as string; - let state: State | undefined; - if (nodeType === 'REST') { - ({ finalized: state } = await processRestRequest(nodeUrl, forcedExitAccount)); - } else { - ({ verified: state } = await processJsonRpcRequest(nodeUrl, forcedExitAccount)); + const state = + nodeType === 'REST' + ? (await processRestRequest(nodeUrl, forcedExitAccount)).finalized + : (await processJsonRpcRequest(nodeUrl, forcedExitAccount)).verified; + + if (state?.pubKeyHash !== SYNC) { + console.log('Forced exit sender account is ready'); + return true; } - if (!state || state.pubKeyHash === 'sync:0000000000000000000000000000000000000000') { - if (state?.balances['RBTC']) { - const balance = BigNumber.from(state.balances['RBTC']); - if (!balance.isZero()) { - console.log(`Forced exit sender account balance is ${balance.toString()} RBTC`); - console.log('Wait until the preparation of the forced exit sender account is completed'); - } + if (state?.balances['RBTC']) { + const balance = BigNumber.from(state.balances['RBTC']); + if (!balance.isZero()) { + console.log(`Forced exit sender account balance is ${balance.toString()} RBTC`); + console.log('Wait until the preparation of the forced exit sender account is completed'); + return true; } - console.log('Forced exit sender account balance is not ready'); - return; } - console.log('Forced exit sender account balance is ready'); + console.log('Forced exit sender account is not ready'); + return false; } async function processRestRequest(nodeUrl: string, forcedExitAccount: string): Promise { @@ -70,22 +78,30 @@ async function processJsonRpcRequest(nodeUrl: string, forcedExitAccount: string) return result; } -async function depositToForcedExitSenderAccount( +async function prepareForcedExitSenderAccount( l1Address: string, privateKey: string, amount: string, - mnemonic: boolean -) { - const forcedExitAccount = process.env.FORCED_EXIT_REQUESTS_SENDER_ACCOUNT_ADDRESS as string; + nodeUrl: string, + nodeType: string +): Promise { + if (await isForcedExitSenderAccountReady(nodeUrl, nodeType)) { + return; + } + + await depositToForcedExitSenderAccount(l1Address, privateKey, amount); +} + +export async function depositToForcedExitSenderAccount(l1Address: string, privateKey: string, amount: string) { + console.log('Depositing to the forced exit sender account sender'); + const provider = new ethers.providers.JsonRpcProvider( process.env.FORCED_EXIT_REQUESTS_WEB3_URL ?? process.env.ETH_CLIENT_WEB3_URL ); - const wallet = mnemonic - ? ethers.Wallet.fromMnemonic(privateKey).connect(provider) - : new ethers.Wallet(privateKey).connect(provider); + const wallet = new ethers.Wallet(privateKey, provider); if (l1Address.toLowerCase() !== wallet.address.toLowerCase()) { - console.log('L1 address does not match the provided private key or mnemonic'); + console.log('L1 address does not match the provided private key'); return; } @@ -95,35 +111,42 @@ async function depositToForcedExitSenderAccount( wallet ); + const forcedExitAccount = process.env.FORCED_EXIT_REQUESTS_SENDER_ACCOUNT_ADDRESS as string; const depositTransaction = (await mainZkSyncContract.depositRBTC(forcedExitAccount, { value: ethers.utils.parseEther(amount) })) as ethers.ContractTransaction; + console.log(`Deposit transaction hash: ${depositTransaction.hash}`); + await depositTransaction.wait(); console.log('Deposit to the forced exit sender account has been successfully completed'); } -export const command = new Command('forced-exit').description('prepare forced exit sender account'); +export const command = new Command('forced-exit') + .description('prepare forced exit sender account') + .requiredOption('-n, --nodeUrl ', 'Node url') + .requiredOption('-t, --nodeType ', 'Node type (REST or JSONRPC)'); command .command('check') .description('check forced exit sender account balance') - .arguments(' ') - .action(async (nodeUrl: string, nodeType: string) => { - if (nodeType !== 'REST' && nodeType !== 'JSONRPC') { - console.log('Node type must be either REST or JSONRPC'); - return; - } - await checkForcedExitSenderAccountBalance(nodeUrl, nodeType); + .action(async (cmd: Command) => { + const { + parent: { nodeUrl, nodeType } + } = cmd; + await isForcedExitSenderAccountReady(nodeUrl, nodeType); }); command .command('prepare') - .description('deposit to forced exit sender account') - .arguments(' [amount]') - .option('-m --mnemonic', 'Is mnemonic') + .description('deposit to forced exit sender account if necessary') + .arguments('') + .arguments('') + .arguments('[amount]') .action(async (l1Address: string, privateKey: string, amount = '0.0001', cmd: Command) => { - const { mnemonic } = cmd; - await depositToForcedExitSenderAccount(l1Address, privateKey, amount, !!mnemonic); + const { + parent: { nodeUrl, nodeType } + } = cmd; + await prepareForcedExitSenderAccount(l1Address, privateKey, amount, nodeUrl, nodeType); }); diff --git a/infrastructure/zk/src/run/run.ts b/infrastructure/zk/src/run/run.ts index 035afd1ede..7bce375afa 100644 --- a/infrastructure/zk/src/run/run.ts +++ b/infrastructure/zk/src/run/run.ts @@ -143,10 +143,6 @@ export async function revertReason(txHash: string, web3url?: string) { await utils.spawn(`yarn contracts ts-node scripts/revert-reason.ts ${txHash} ${web3url || ''}`); } -export async function explorer() { - await utils.spawn('yarn explorer serve'); -} - export async function exitProof(...args: string[]) { await utils.spawn(`cargo run --example generate_exit_proof --release -- ${args.join(' ')}`); } @@ -245,7 +241,6 @@ export const command = new Command('run') .addCommand(forcedExit.command); command.command('test-accounts').description('print rootstock test accounts').action(testAccounts); -command.command('explorer').description('run zksync explorer locally').action(explorer); command .command('yarn') .description('install all JS dependencies') diff --git a/infrastructure/zk/src/server.ts b/infrastructure/zk/src/server.ts index 2c2f0e1cce..3791f6ffa3 100644 --- a/infrastructure/zk/src/server.ts +++ b/infrastructure/zk/src/server.ts @@ -4,8 +4,10 @@ import * as env from './env'; import fs from 'fs'; import * as db from './db/db'; import * as docker from './docker'; +import * as forcedExit from './run/forced-exit'; export async function core(withDocker = false) { + await prepareForcedExitAccountRequest(); if (withDocker) { await docker.deployUp('server-core'); return; @@ -37,12 +39,22 @@ export async function apiNode(withDocker = false) { } export async function server(withDocker = false) { + await prepareForcedExitAccountRequest(); if (withDocker) { await docker.deployUp('server'); return; } - await utils.spawn('cargo run --bin zksync_server --release'); + await utils.spawn('cargo run --bin zksync_server --release&'); +} + +async function prepareForcedExitAccountRequest() { + if (process.env.ZKSYNC_ENV === 'dev') { + const address = '0x09a1eda29f664ac8f68106f6567276df0c65d859'; + const privateKey = '0x082f57b8084286a079aeb9f2d0e17e565ced44a2cb9ce4844e6d4b9d89f3f595'; + const amount = '0.001'; + await forcedExit.depositToForcedExitSenderAccount(address, privateKey, amount); + } } export async function genesis(withDocker = false) {