-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add localnet command #16
Merged
Merged
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4ca6b76
chore(security): add workflow for leaked secrets monitoring
otani88 eeb45ec
Implement localnet subcommand
jpcenteno 68a67ad
Use import instead of require to get type definitons.
jpcenteno a4c8d71
Cleanup
jpcenteno 1a09eae
Point local-setup repo to our branch. Interface with docker-compose d…
jpcenteno cebc398
Merge pull request #2 from lambdaclass/feat-local-node
mationorato a8d342c
Add `localnet` subcommand to the README
jpcenteno 1acd846
Merge pull request #5 from lambdaclass/document-localnet-subcommand
mationorato 3973ee2
update docker compose from v1 to v2
mationorato d90245b
Update local-setup repo url to matter-labs main branch
mationorato 5940293
update REPO_URL and REPO_BRANCH in ts file
mationorato 4d44048
Merge branch 'upstream:main' into fix-merge-conflicts-with-matterlabs…
jpcenteno 55b145c
Update help
jpcenteno 860f415
Merge pull request #7 from lambdaclass/fix-merge-conflicts-with-matte…
jpcenteno edddec3
Update README.md
mationorato 40b8aa8
Document why repoDirectory is using XDG_STATE_HOME
jpcenteno c58b62b
Remove new bin/ files introduced from our fork
jpcenteno f2a9ecf
Fix pkg version
jpcenteno 724a224
Replace zksync 2.0 with Era for branding consistency
jpcenteno 4dd22b2
Merge branch 'address-matterlabs-review-comments'
jpcenteno fcd11b1
Merge branch 'main' into main
jpcenteno 21ea0bb
Update package lock
jpcenteno fbd18ed
Revert "Update package lock"
jpcenteno ec6a3e8
Fix messing newline at end of file
jpcenteno 04eaa91
Address CI messages
jpcenteno cb5a4ba
Export localnet help function
jpcenteno 38c14a3
Merge remote-tracking branch 'upstream/main'
jpcenteno 7ffb3ea
fix: refactor localnet module to follow new project structure
jpcenteno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { execSync, ExecSyncOptions } from 'child_process'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import * as os from 'os'; | ||
|
||
const REPO_URL: string = "https://github.com/matter-labs/local-setup.git"; | ||
const REPO_BRANCH: string = "main" | ||
|
||
// --------------------------------------------------------------------------------------- | ||
// Utilities | ||
// --------------------------------------------------------------------------------------- | ||
|
||
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(); | ||
} | ||
|
||
/** | ||
* Returns the path where the `zksync-cli/local-setup` repository, used | ||
* internally to manage localnet deployments, should be located. | ||
* | ||
* **Why follow the XDG Base Directory Specification?** | ||
* | ||
* This function follows the XDG Base Directory Specification | ||
* (https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) | ||
* to determine the parent directory location: | ||
* | ||
* The XDG Base Directory Specification is widely accepted as a standard. | ||
* The decision to place the files under `$XDG_STATE_HOME` is based on considering the | ||
* presence or absence of this repository as part of the CLI tool's state. | ||
* | ||
* Alternative locations within the XDG Base Directory Specification were | ||
* considered and ruled out for the following reasons: | ||
* | ||
* - `$XDG_DATA_HOME` was not chosen because these files aren't user-specific | ||
* data files. | ||
* | ||
* - `$XDG_CACHE_HOME` was not chosen because these files aren't considered | ||
* non-essential cached data files. | ||
* | ||
* @returns {string} The path where the `zksync-cli/local-setup` repository should be | ||
* placed. | ||
*/ | ||
function repoDirectory(): string { | ||
// From the XDG Base Directory Specification: | ||
// `$XDG_STATE_HOME` defines the base directory relative to which user-specific state files should be stored. If `$XDG_STATE_HOME` is either not set or empty, a default equal to `$HOME/.local/state` should be used. | ||
const xdgStateHome = process.env.XDG_STATE_HOME || path.join(os.homedir(), ".local/state"); | ||
mationorato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return path.join(xdgStateHome, "zksync-cli/local-setup"); | ||
} | ||
|
||
function isRepoCloned(): boolean { | ||
return fs.existsSync(repoDirectory()); | ||
} | ||
|
||
function cloneRepo() { | ||
const parentDirectory = path.join(repoDirectory(), ".."); | ||
runCommand(`mkdir -p '${parentDirectory}'`, { cwd: "/" }); | ||
const options: ExecSyncOptions = { cwd: parentDirectory }; | ||
runCommand(`git clone --branch '${REPO_BRANCH}' '${REPO_URL}'`, options); | ||
} | ||
|
||
function setUp() { | ||
cloneRepo(); | ||
} | ||
|
||
// --------------------------------------------------------------------------------------- | ||
// Localnet operations | ||
// --------------------------------------------------------------------------------------- | ||
|
||
function logs(): number { | ||
const options: ExecSyncOptions = { stdio: 'inherit' }; | ||
runCommand("docker compose logs --follow", options); | ||
return 0; | ||
} | ||
|
||
function up(): number { | ||
if (! isRepoCloned()) { | ||
setUp(); | ||
} | ||
runCommand("docker compose up --detach"); | ||
return 0; | ||
} | ||
|
||
function down(): number { | ||
runCommand("docker compose down --volumes"); | ||
return 0; | ||
} | ||
|
||
function start(): number { | ||
runCommand("docker compose start"); | ||
return 0; | ||
} | ||
|
||
function stop(): number { | ||
runCommand("docker compose stop"); | ||
return 0; | ||
} | ||
|
||
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 <operation>"); | ||
console.log(""); | ||
console.log("Manage local L1 and L2 chains"); | ||
console.log(""); | ||
console.log("Available operations"); | ||
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'); | ||
return 0; | ||
} | ||
|
||
function handleUndefinedOperation(): number { | ||
console.error("No operation provided"); | ||
help(); | ||
return 1; | ||
} | ||
|
||
function handleInvalidOperation(operationName: string): number { | ||
const validOperationNames = Array.from(operationHandlers.keys()); | ||
console.error('Invalid operation: ', operationName); | ||
help(); | ||
return 1; | ||
} | ||
|
||
const operationHandlers = new Map<string | undefined, () => number>([ | ||
['up', up], | ||
['down', down], | ||
['start', start], | ||
['stop', stop], | ||
['logs', logs], | ||
['help', help], | ||
['wallets', wallets], | ||
[undefined, handleUndefinedOperation], | ||
]); | ||
|
||
export default async function (operationName: string | undefined) { | ||
const handler = operationHandlers.get(operationName) || (() => handleInvalidOperation(operationName!)); | ||
process.exit(handler()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤