From 4ca6b76caf572a01b6c281623af1a6a50214dcc7 Mon Sep 17 00:00:00 2001 From: Maksym Date: Mon, 20 Feb 2023 18:40:46 +0200 Subject: [PATCH 1/5] chore(security): add workflow for leaked secrets monitoring --- .github/workflows/secrets_scanner.yaml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .github/workflows/secrets_scanner.yaml diff --git a/.github/workflows/secrets_scanner.yaml b/.github/workflows/secrets_scanner.yaml new file mode 100644 index 00000000..54054cf7 --- /dev/null +++ b/.github/workflows/secrets_scanner.yaml @@ -0,0 +1,17 @@ +name: Leaked Secrets Scan +on: [pull_request] +jobs: + TruffleHog: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3 + with: + fetch-depth: 0 + - name: TruffleHog OSS + uses: trufflesecurity/trufflehog@0c66d30c1f4075cee1aada2e1ab46dabb1b0071a + with: + path: ./ + base: ${{ github.event.repository.default_branch }} + head: HEAD + extra_args: --debug --only-verified From eeb45ecfbc5d7adef8a41e089bb6c3d3441401ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20P=2E=20Centeno?= Date: Wed, 3 May 2023 13:40:09 -0300 Subject: [PATCH 2/5] Implement localnet subcommand --- bin/index.js | 7 ++- bin/localnet.d.ts | 1 + bin/localnet.js | 113 ++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 7 ++- src/localnet.ts | 117 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 243 insertions(+), 2 deletions(-) create mode 100644 bin/localnet.d.ts create mode 100644 bin/localnet.js create mode 100644 src/localnet.ts diff --git a/bin/index.js b/bin/index.js index fb6ebf69..7cf7351d 100755 --- a/bin/index.js +++ b/bin/index.js @@ -12,7 +12,8 @@ const figlet_1 = __importDefault(require("figlet")); const create_1 = __importDefault(require("./create")); const deposit_1 = __importDefault(require("./deposit")); const withdraw_1 = __importDefault(require("./withdraw")); -const availableOptions = ['create', 'deposit', 'withdraw']; +const localnet_1 = __importDefault(require("./localnet")); +const availableOptions = ['create', 'deposit', 'withdraw', 'localnet']; // second argument should be the selected option const option = process.argv[2]; if (!availableOptions.includes(option)) { @@ -33,4 +34,8 @@ switch (option) { case 'withdraw': (0, withdraw_1.default)(); break; + case 'localnet': + const subcommandName = process.argv[3] || undefined; + (0, localnet_1.default)(subcommandName); + break; } diff --git a/bin/localnet.d.ts b/bin/localnet.d.ts new file mode 100644 index 00000000..0eb26515 --- /dev/null +++ b/bin/localnet.d.ts @@ -0,0 +1 @@ +export default function (operationName: string | undefined): Promise; diff --git a/bin/localnet.js b/bin/localnet.js new file mode 100644 index 00000000..97320c23 --- /dev/null +++ b/bin/localnet.js @@ -0,0 +1,113 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const { execSync } = require('child_process'); +const path = require('path'); +const fs = require('fs'); +/** + * Runs CLI commands + * @param {*} command String command to run + */ +const runCommand = (command) => { + try { + // runs given command and prints its output to console + execSync(`${command}`, { stdio: 'inherit' }); + } + catch (error) { + console.error('Failed to run command: ', error); + return false; + } + return true; +}; +function localSetupRepoPath() { + const defaultXDGStateHome = path.join(process.env.HOME, ".local", "state"); + const xdgStateHome = process.env["XDG_STATE_HOME"] || defaultXDGStateHome; + return path.join(xdgStateHome, "zksync-cli", "local-setup"); +} +function localSetupRepoExists() { + return fs.existsSync(localSetupRepoPath()); +} +function cloneLocalSetupRepo() { + const repoParentDir = path.join(localSetupRepoPath(), ".."); + runCommand(`mkdir -p "${repoParentDir}"`); + runCommand(`cd "${repoParentDir}" && git clone https://github.com/matter-labs/local-setup.git`); +} +function createLocalSetupStartInBackgroundScript() { + runCommand(`cd "${localSetupRepoPath()}" && sed 's/^docker-compose up$/docker-compose up --detach/' start.sh > start-background.sh && chmod +x start-background.sh`); +} +function handleStartOperation() { + const repoPath = localSetupRepoPath(); + if (!localSetupRepoExists()) { + cloneLocalSetupRepo(); + } + createLocalSetupStartInBackgroundScript(); + runCommand(`cd "${localSetupRepoPath()}" && ./start-background.sh`); + return 0; +} +function handleDownOperation() { + runCommand(`cd "${localSetupRepoPath()}" && docker-compose down`); + return 0; +} +function handleLogsOperation() { + runCommand(`cd "${localSetupRepoPath()}" && docker-compose logs --follow`); + return 0; +} +function handleClearOperation() { + runCommand(`cd "${localSetupRepoPath()}" && ./clear.sh`); + return 0; +} +function handleHelpOperation() { + console.log("USAGE: zksync-cli localnet "); + console.log(""); + console.log("Manage local L1 and L2 chains"); + console.log(""); + console.log("Available operations"); + console.log(' start -- Start L1 and L2 localnets'); + console.log(' down -- Stop L1 and L2 localnets'); + console.log(' clear -- Reset the localnet state'); + console.log(' logs -- Display logs'); + console.log(' help -- Display this message and quit'); + console.log(' wallets -- Display seeded wallet keys'); + return 0; +} +function handleUndefinedOperation() { + console.error("No operation provided"); + handleHelpOperation(); + return 1; +} +function handleWalletsOperation() { + const rawJSON = fs.readFileSync(path.join(localSetupRepoPath(), "rich-wallets.json")); + const wallets = JSON.parse(rawJSON); + console.log(wallets); + return 0; +} +function handleInvalidOperation(operationName) { + const validOperationNames = Array.from(operationHandlers.keys()); + console.error('Invalid operation: ', operationName); + handleHelpOperation(); + return 1; +} +const operationHandlers = new Map([ + ['start', handleStartOperation], + ['down', handleDownOperation], + ['logs', handleLogsOperation], + ['help', handleHelpOperation], + ['wallets', handleWalletsOperation], + ['clear', handleClearOperation], + [undefined, handleUndefinedOperation], +]); +function default_1(operationName) { + return __awaiter(this, void 0, void 0, function* () { + const handler = operationHandlers.get(operationName) || (() => handleInvalidOperation(operationName)); + process.exit(handler()); + }); +} +exports.default = default_1; diff --git a/src/index.ts b/src/index.ts index efb6de6d..36f23ad3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,8 +10,9 @@ import figlet from 'figlet'; import create from './create'; import deposit from './deposit'; import withdraw from './withdraw'; +import localnet from './localnet'; -const availableOptions: string[] = ['create', 'deposit', 'withdraw']; +const availableOptions: string[] = ['create', 'deposit', 'withdraw', 'localnet']; // second argument should be the selected option const option: string = process.argv[2]; @@ -43,4 +44,8 @@ switch (option) { case 'withdraw': withdraw(); break; + case 'localnet': + const subcommandName = process.argv[3] || undefined; + localnet(subcommandName); + break; } diff --git a/src/localnet.ts b/src/localnet.ts new file mode 100644 index 00000000..85cee3db --- /dev/null +++ b/src/localnet.ts @@ -0,0 +1,117 @@ +const { execSync } = require('child_process'); +const path = require('path'); +const fs = require('fs'); + +/** + * Runs CLI commands + * @param {*} command String command to run + */ +const runCommand = (command: string) => { + try { + // runs given command and prints its output to console + execSync(`${command}`, { stdio: 'inherit' }); + } catch (error) { + console.error('Failed to run command: ', error); + return false; + } + return true; +}; + +function localSetupRepoPath(): string { + const defaultXDGStateHome = path.join(process.env.HOME!, ".local", "state"); + const xdgStateHome = process.env["XDG_STATE_HOME"] || defaultXDGStateHome; + return path.join(xdgStateHome, "zksync-cli", "local-setup"); +} + +function localSetupRepoExists(): boolean { + return fs.existsSync(localSetupRepoPath()); +} + +function cloneLocalSetupRepo() { + const repoParentDir = path.join(localSetupRepoPath(), ".."); + runCommand(`mkdir -p "${repoParentDir}"`); + runCommand(`cd "${repoParentDir}" && git clone https://github.com/matter-labs/local-setup.git`); +} + +function createLocalSetupStartInBackgroundScript() { + runCommand(`cd "${localSetupRepoPath()}" && sed 's/^docker-compose up$/docker-compose up --detach/' start.sh > start-background.sh && chmod +x start-background.sh`); +} + +function handleStartOperation(): number { + const repoPath = localSetupRepoPath(); + + if (!localSetupRepoExists()) { + cloneLocalSetupRepo(); + } + + createLocalSetupStartInBackgroundScript(); + + runCommand(`cd "${localSetupRepoPath()}" && ./start-background.sh`); + + return 0; +} + +function handleDownOperation(): number { + runCommand(`cd "${localSetupRepoPath()}" && docker-compose down`); + return 0; +} + +function handleLogsOperation(): number { + runCommand(`cd "${localSetupRepoPath()}" && docker-compose logs --follow`); + return 0; +} + +function handleClearOperation(): number { + runCommand(`cd "${localSetupRepoPath()}" && ./clear.sh`); + return 0; +} + +function handleHelpOperation(): number { + console.log("USAGE: zksync-cli localnet "); + console.log(""); + console.log("Manage local L1 and L2 chains"); + console.log(""); + console.log("Available operations"); + console.log(' start -- Start L1 and L2 localnets'); + console.log(' down -- Stop L1 and L2 localnets'); + console.log(' clear -- Reset the localnet state'); + console.log(' logs -- Display logs'); + console.log(' help -- Display this message and quit'); + console.log(' wallets -- Display seeded wallet keys'); + return 0; +} + +function handleUndefinedOperation(): number { + console.error("No operation provided"); + handleHelpOperation(); + return 1; +} + +function handleWalletsOperation(): number { + const rawJSON = fs.readFileSync(path.join(localSetupRepoPath(), "rich-wallets.json")); + const wallets = JSON.parse(rawJSON); + console.log(wallets); + return 0; +} + +function handleInvalidOperation(operationName: string): number { + const validOperationNames = Array.from(operationHandlers.keys()); + console.error('Invalid operation: ', operationName); + handleHelpOperation(); + return 1; +} + +const operationHandlers = new Map number>([ + ['start', handleStartOperation], + ['down', handleDownOperation], + ['logs', handleLogsOperation], + ['help', handleHelpOperation], + ['wallets', handleWalletsOperation], + ['clear', handleClearOperation], + [undefined, handleUndefinedOperation], +]); + +export default async function (operationName: string | undefined) { + const handler = operationHandlers.get(operationName) || (() => handleInvalidOperation(operationName!)); + process.exit(handler()); +} From 68a67ad28d33b4ceeb0ada9fd01aa12c0836ef4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20P=2E=20Centeno?= Date: Thu, 4 May 2023 18:15:20 -0300 Subject: [PATCH 3/5] Use import instead of require to get type definitons. --- src/localnet.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/localnet.ts b/src/localnet.ts index 85cee3db..fdb45f77 100644 --- a/src/localnet.ts +++ b/src/localnet.ts @@ -1,6 +1,6 @@ -const { execSync } = require('child_process'); -const path = require('path'); -const fs = require('fs'); +import { execSync } from 'child_process'; +import * as path from 'path'; +import * as fs from 'fs'; /** * Runs CLI commands From a4c8d71664f4aaec089aa277092f8c7a54d2709e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20P=2E=20Centeno?= Date: Thu, 4 May 2023 19:37:44 -0300 Subject: [PATCH 4/5] Cleanup --- bin/localnet.js | 143 ++++++++++++++++++++++++++++-------------------- src/localnet.ts | 123 +++++++++++++++++++++-------------------- 2 files changed, 149 insertions(+), 117 deletions(-) diff --git a/bin/localnet.js b/bin/localnet.js index 97320c23..48695a69 100644 --- a/bin/localnet.js +++ b/bin/localnet.js @@ -1,4 +1,27 @@ "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -9,68 +32,78 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); -const { execSync } = require('child_process'); -const path = require('path'); -const fs = require('fs'); -/** - * Runs CLI commands - * @param {*} command String command to run - */ -const runCommand = (command) => { - try { - // runs given command and prints its output to console - execSync(`${command}`, { stdio: 'inherit' }); - } - catch (error) { - console.error('Failed to run command: ', error); - return false; - } - return true; -}; -function localSetupRepoPath() { - const defaultXDGStateHome = path.join(process.env.HOME, ".local", "state"); - const xdgStateHome = process.env["XDG_STATE_HOME"] || defaultXDGStateHome; - return path.join(xdgStateHome, "zksync-cli", "local-setup"); +const child_process_1 = require("child_process"); +const path = __importStar(require("path")); +const fs = __importStar(require("fs")); +const os = __importStar(require("os")); +// --------------------------------------------------------------------------------------- +// Utilities +// --------------------------------------------------------------------------------------- +function runCommand(command, options) { + const defaultOptions = { cwd: repoDirectory(), encoding: 'utf-8' }; + const unifiedOptions = Object.assign(Object.assign({}, defaultOptions), options); + return (0, child_process_1.execSync)(command, unifiedOptions).toString(); +} +function repoDirectory() { + const xdgStateHome = process.env.XDG_STATE_HOME || path.join(os.homedir(), ".local/state"); + return path.join(xdgStateHome, "zksync-cli/local-setup"); } -function localSetupRepoExists() { - return fs.existsSync(localSetupRepoPath()); +function isRepoCloned() { + return fs.existsSync(repoDirectory()); } -function cloneLocalSetupRepo() { - const repoParentDir = path.join(localSetupRepoPath(), ".."); - runCommand(`mkdir -p "${repoParentDir}"`); - runCommand(`cd "${repoParentDir}" && git clone https://github.com/matter-labs/local-setup.git`); +function cloneRepo() { + const parentDirectory = path.join(repoDirectory(), ".."); + runCommand(`mkdir -p "${parentDirectory}"`); + const options = { cwd: parentDirectory }; + runCommand("git clone https://github.com/matter-labs/local-setup.git", options); } -function createLocalSetupStartInBackgroundScript() { - runCommand(`cd "${localSetupRepoPath()}" && sed 's/^docker-compose up$/docker-compose up --detach/' start.sh > start-background.sh && chmod +x start-background.sh`); +function createStartInBackgroundScript() { + runCommand("sed 's/^docker-compose up$/docker-compose up --detach/' start.sh > start-background.sh"); + runCommand("chmod +x start-background.sh"); } -function handleStartOperation() { - const repoPath = localSetupRepoPath(); - if (!localSetupRepoExists()) { - cloneLocalSetupRepo(); +function setUp() { + cloneRepo(); + createStartInBackgroundScript(); +} +// --------------------------------------------------------------------------------------- +// Localnet operations +// --------------------------------------------------------------------------------------- +function logs() { + const options = { stdio: 'inherit' }; + runCommand("docker-compose logs --follow", options); + return 0; +} +function up() { + if (!isRepoCloned()) { + setUp(); } - createLocalSetupStartInBackgroundScript(); - runCommand(`cd "${localSetupRepoPath()}" && ./start-background.sh`); + runCommand("./start-background.sh"); return 0; } -function handleDownOperation() { - runCommand(`cd "${localSetupRepoPath()}" && docker-compose down`); +function down() { + runCommand("docker-compose down"); return 0; } -function handleLogsOperation() { - runCommand(`cd "${localSetupRepoPath()}" && docker-compose logs --follow`); +function clear() { + runCommand("./clear.sh"); return 0; } -function handleClearOperation() { - runCommand(`cd "${localSetupRepoPath()}" && ./clear.sh`); +function wallets() { + const rawJSON = fs.readFileSync(path.join(repoDirectory(), "rich-wallets.json")).toString(); + const wallets = JSON.parse(rawJSON); + console.log(wallets); return 0; } -function handleHelpOperation() { +// --------------------------------------------------------------------------------------- +// Command handling +// --------------------------------------------------------------------------------------- +function help() { console.log("USAGE: zksync-cli localnet "); console.log(""); console.log("Manage local L1 and L2 chains"); console.log(""); console.log("Available operations"); - console.log(' start -- Start L1 and L2 localnets'); + console.log(' up -- Start L1 and L2 localnets'); console.log(' down -- Stop L1 and L2 localnets'); console.log(' clear -- Reset the localnet state'); console.log(' logs -- Display logs'); @@ -80,28 +113,22 @@ function handleHelpOperation() { } function handleUndefinedOperation() { console.error("No operation provided"); - handleHelpOperation(); + help(); return 1; } -function handleWalletsOperation() { - const rawJSON = fs.readFileSync(path.join(localSetupRepoPath(), "rich-wallets.json")); - const wallets = JSON.parse(rawJSON); - console.log(wallets); - return 0; -} function handleInvalidOperation(operationName) { const validOperationNames = Array.from(operationHandlers.keys()); console.error('Invalid operation: ', operationName); - handleHelpOperation(); + help(); return 1; } const operationHandlers = new Map([ - ['start', handleStartOperation], - ['down', handleDownOperation], - ['logs', handleLogsOperation], - ['help', handleHelpOperation], - ['wallets', handleWalletsOperation], - ['clear', handleClearOperation], + ['up', up], + ['down', down], + ['logs', logs], + ['help', help], + ['wallets', wallets], + ['clear', clear], [undefined, handleUndefinedOperation], ]); function default_1(operationName) { diff --git a/src/localnet.ts b/src/localnet.ts index fdb45f77..b91d9519 100644 --- a/src/localnet.ts +++ b/src/localnet.ts @@ -1,78 +1,90 @@ -import { execSync } from 'child_process'; +import { execSync, ExecSyncOptions } from 'child_process'; import * as path from 'path'; import * as fs from 'fs'; +import * as os from 'os'; -/** - * Runs CLI commands - * @param {*} command String command to run - */ -const runCommand = (command: string) => { - try { - // runs given command and prints its output to console - execSync(`${command}`, { stdio: 'inherit' }); - } catch (error) { - console.error('Failed to run command: ', error); - return false; - } - return true; -}; - -function localSetupRepoPath(): string { - const defaultXDGStateHome = path.join(process.env.HOME!, ".local", "state"); - const xdgStateHome = process.env["XDG_STATE_HOME"] || defaultXDGStateHome; - return path.join(xdgStateHome, "zksync-cli", "local-setup"); -} +// --------------------------------------------------------------------------------------- +// Utilities +// --------------------------------------------------------------------------------------- -function localSetupRepoExists(): boolean { - return fs.existsSync(localSetupRepoPath()); +function runCommand(command: string, options?: ExecSyncOptions): string { + const defaultOptions: ExecSyncOptions = { cwd: repoDirectory(), encoding: 'utf-8' }; + const unifiedOptions: ExecSyncOptions = {...defaultOptions, ...options}; + return execSync(command, unifiedOptions).toString(); } -function cloneLocalSetupRepo() { - const repoParentDir = path.join(localSetupRepoPath(), ".."); - runCommand(`mkdir -p "${repoParentDir}"`); - runCommand(`cd "${repoParentDir}" && git clone https://github.com/matter-labs/local-setup.git`); +function repoDirectory(): string { + const xdgStateHome = process.env.XDG_STATE_HOME || path.join(os.homedir(), ".local/state"); + return path.join(xdgStateHome, "zksync-cli/local-setup"); } -function createLocalSetupStartInBackgroundScript() { - runCommand(`cd "${localSetupRepoPath()}" && sed 's/^docker-compose up$/docker-compose up --detach/' start.sh > start-background.sh && chmod +x start-background.sh`); +function isRepoCloned(): boolean { + return fs.existsSync(repoDirectory()); } -function handleStartOperation(): number { - const repoPath = localSetupRepoPath(); +function cloneRepo() { + const parentDirectory = path.join(repoDirectory(), ".."); + runCommand(`mkdir -p "${parentDirectory}"`); + const options: ExecSyncOptions = { cwd: parentDirectory }; + runCommand("git clone https://github.com/matter-labs/local-setup.git", options); +} - if (!localSetupRepoExists()) { - cloneLocalSetupRepo(); - } +function createStartInBackgroundScript() { + runCommand("sed 's/^docker-compose up$/docker-compose up --detach/' start.sh > start-background.sh"); + runCommand("chmod +x start-background.sh"); +} - createLocalSetupStartInBackgroundScript(); +function setUp() { + cloneRepo(); + createStartInBackgroundScript(); +} - runCommand(`cd "${localSetupRepoPath()}" && ./start-background.sh`); +// --------------------------------------------------------------------------------------- +// Localnet operations +// --------------------------------------------------------------------------------------- +function logs(): number { + const options: ExecSyncOptions = { stdio: 'inherit' }; + runCommand("docker-compose logs --follow", options); return 0; } -function handleDownOperation(): number { - runCommand(`cd "${localSetupRepoPath()}" && docker-compose down`); +function up(): number { + if (! isRepoCloned()) { + setUp(); + } + runCommand("./start-background.sh") return 0; } -function handleLogsOperation(): number { - runCommand(`cd "${localSetupRepoPath()}" && docker-compose logs --follow`); +function down(): number { + runCommand("docker-compose down"); return 0; } -function handleClearOperation(): number { - runCommand(`cd "${localSetupRepoPath()}" && ./clear.sh`); +function clear(): number { + runCommand("./clear.sh") return 0; } -function handleHelpOperation(): number { +function wallets(): number { + const rawJSON = fs.readFileSync(path.join(repoDirectory(), "rich-wallets.json")).toString(); + const wallets = JSON.parse(rawJSON); + console.log(wallets); + return 0; +} + +// --------------------------------------------------------------------------------------- +// Command handling +// --------------------------------------------------------------------------------------- + +function help(): number { console.log("USAGE: zksync-cli localnet "); console.log(""); console.log("Manage local L1 and L2 chains"); console.log(""); console.log("Available operations"); - console.log(' start -- Start L1 and L2 localnets'); + console.log(' up -- Start L1 and L2 localnets'); console.log(' down -- Stop L1 and L2 localnets'); console.log(' clear -- Reset the localnet state'); console.log(' logs -- Display logs'); @@ -83,31 +95,24 @@ function handleHelpOperation(): number { function handleUndefinedOperation(): number { console.error("No operation provided"); - handleHelpOperation(); + help(); return 1; } -function handleWalletsOperation(): number { - const rawJSON = fs.readFileSync(path.join(localSetupRepoPath(), "rich-wallets.json")); - const wallets = JSON.parse(rawJSON); - console.log(wallets); - return 0; -} - function handleInvalidOperation(operationName: string): number { const validOperationNames = Array.from(operationHandlers.keys()); console.error('Invalid operation: ', operationName); - handleHelpOperation(); + help(); return 1; } const operationHandlers = new Map number>([ - ['start', handleStartOperation], - ['down', handleDownOperation], - ['logs', handleLogsOperation], - ['help', handleHelpOperation], - ['wallets', handleWalletsOperation], - ['clear', handleClearOperation], + ['up', up], + ['down', down], + ['logs', logs], + ['help', help], + ['wallets', wallets], + ['clear', clear], [undefined, handleUndefinedOperation], ]); From 1a09eae07cda21d799fa8f6cad51d005d8f74182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20P=2E=20Centeno?= Date: Thu, 11 May 2023 17:56:23 -0300 Subject: [PATCH 5/5] Point local-setup repo to our branch. Interface with docker-compose directly --- bin/localnet.js | 33 ++++++++++++++++++--------------- src/localnet.ts | 36 ++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/bin/localnet.js b/bin/localnet.js index 48695a69..28ddf871 100644 --- a/bin/localnet.js +++ b/bin/localnet.js @@ -36,6 +36,8 @@ const child_process_1 = require("child_process"); const path = __importStar(require("path")); const fs = __importStar(require("fs")); const os = __importStar(require("os")); +const REPO_URL = "git@github.com:lambdaclass/local-setup.git"; +const REPO_BRANCH = "feature-volumes"; // --------------------------------------------------------------------------------------- // Utilities // --------------------------------------------------------------------------------------- @@ -53,17 +55,12 @@ function isRepoCloned() { } function cloneRepo() { const parentDirectory = path.join(repoDirectory(), ".."); - runCommand(`mkdir -p "${parentDirectory}"`); + runCommand(`mkdir -p '${parentDirectory}'`, { cwd: "/" }); const options = { cwd: parentDirectory }; - runCommand("git clone https://github.com/matter-labs/local-setup.git", options); -} -function createStartInBackgroundScript() { - runCommand("sed 's/^docker-compose up$/docker-compose up --detach/' start.sh > start-background.sh"); - runCommand("chmod +x start-background.sh"); + runCommand(`git clone --branch '${REPO_BRANCH}' '${REPO_URL}'`, options); } function setUp() { cloneRepo(); - createStartInBackgroundScript(); } // --------------------------------------------------------------------------------------- // Localnet operations @@ -77,15 +74,19 @@ function up() { if (!isRepoCloned()) { setUp(); } - runCommand("./start-background.sh"); + runCommand("docker-compose up --detach"); return 0; } function down() { - runCommand("docker-compose down"); + runCommand("docker-compose down --volumes"); + return 0; +} +function start() { + runCommand("docker-compose start"); return 0; } -function clear() { - runCommand("./clear.sh"); +function stop() { + runCommand("docker-compose stop"); return 0; } function wallets() { @@ -103,9 +104,10 @@ function help() { console.log("Manage local L1 and L2 chains"); console.log(""); console.log("Available operations"); - console.log(' up -- Start L1 and L2 localnets'); - console.log(' down -- Stop L1 and L2 localnets'); - console.log(' clear -- Reset the localnet state'); + console.log(' up -- Bootstrap L1 and L2 localnets'); + console.log(' down -- clear L1 and L2 localnets'); + console.log(' start -- start L1 and L2 localnets'); + console.log(' stop -- stop L1 and L2 localnets'); console.log(' logs -- Display logs'); console.log(' help -- Display this message and quit'); console.log(' wallets -- Display seeded wallet keys'); @@ -125,10 +127,11 @@ function handleInvalidOperation(operationName) { const operationHandlers = new Map([ ['up', up], ['down', down], + ['start', start], + ['stop', stop], ['logs', logs], ['help', help], ['wallets', wallets], - ['clear', clear], [undefined, handleUndefinedOperation], ]); function default_1(operationName) { diff --git a/src/localnet.ts b/src/localnet.ts index b91d9519..b15252d3 100644 --- a/src/localnet.ts +++ b/src/localnet.ts @@ -3,6 +3,9 @@ import * as path from 'path'; import * as fs from 'fs'; import * as os from 'os'; +const REPO_URL: string = "git@github.com:lambdaclass/local-setup.git"; +const REPO_BRANCH: string = "feature-volumes" + // --------------------------------------------------------------------------------------- // Utilities // --------------------------------------------------------------------------------------- @@ -24,19 +27,13 @@ function isRepoCloned(): boolean { function cloneRepo() { const parentDirectory = path.join(repoDirectory(), ".."); - runCommand(`mkdir -p "${parentDirectory}"`); + runCommand(`mkdir -p '${parentDirectory}'`, { cwd: "/" }); const options: ExecSyncOptions = { cwd: parentDirectory }; - runCommand("git clone https://github.com/matter-labs/local-setup.git", options); + runCommand(`git clone --branch '${REPO_BRANCH}' '${REPO_URL}'`, options); } -function createStartInBackgroundScript() { - runCommand("sed 's/^docker-compose up$/docker-compose up --detach/' start.sh > start-background.sh"); - runCommand("chmod +x start-background.sh"); -} - function setUp() { cloneRepo(); - createStartInBackgroundScript(); } // --------------------------------------------------------------------------------------- @@ -53,17 +50,22 @@ function up(): number { if (! isRepoCloned()) { setUp(); } - runCommand("./start-background.sh") + runCommand("docker-compose up --detach"); return 0; } function down(): number { - runCommand("docker-compose down"); + runCommand("docker-compose down --volumes"); + return 0; +} + +function start(): number { + runCommand("docker-compose start"); return 0; } -function clear(): number { - runCommand("./clear.sh") +function stop(): number { + runCommand("docker-compose stop"); return 0; } @@ -84,9 +86,10 @@ function help(): number { console.log("Manage local L1 and L2 chains"); console.log(""); console.log("Available operations"); - console.log(' up -- Start L1 and L2 localnets'); - console.log(' down -- Stop L1 and L2 localnets'); - console.log(' clear -- Reset the localnet state'); + console.log(' up -- Bootstrap L1 and L2 localnets'); + console.log(' down -- clear L1 and L2 localnets'); + console.log(' start -- start L1 and L2 localnets'); + console.log(' stop -- stop L1 and L2 localnets'); console.log(' logs -- Display logs'); console.log(' help -- Display this message and quit'); console.log(' wallets -- Display seeded wallet keys'); @@ -109,10 +112,11 @@ function handleInvalidOperation(operationName: string): number { const operationHandlers = new Map number>([ ['up', up], ['down', down], + ['start', start], + ['stop', stop], ['logs', logs], ['help', help], ['wallets', wallets], - ['clear', clear], [undefined, handleUndefinedOperation], ]);