Skip to content

Commit

Permalink
refactor: server initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscotobar committed Oct 10, 2023
1 parent 0ceb814 commit 10cceb8
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 74 deletions.
1 change: 0 additions & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 0 additions & 26 deletions etc/js/env-config.js

This file was deleted.

3 changes: 0 additions & 3 deletions infrastructure/zk/src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
99 changes: 61 additions & 38 deletions infrastructure/zk/src/run/forced-exit.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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<boolean> {
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<RestAccountState> {
Expand Down Expand Up @@ -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<void> {
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;
}

Expand All @@ -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 <nodeUrl>', 'Node url')
.requiredOption('-t, --nodeType <nodeType>', 'Node type (REST or JSONRPC)');

command
.command('check')
.description('check forced exit sender account balance')
.arguments('<nodeUrl> <nodeType>')
.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('<l1Address> <privateKey> [amount]')
.option('-m --mnemonic', 'Is mnemonic')
.description('deposit to forced exit sender account if necessary')
.arguments('<l1Address>')
.arguments('<privateKey>')
.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);
});
5 changes: 0 additions & 5 deletions infrastructure/zk/src/run/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ')}`);
}
Expand Down Expand Up @@ -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')
Expand Down
14 changes: 13 additions & 1 deletion infrastructure/zk/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 10cceb8

Please sign in to comment.