-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add cli module * add aptoscli npm package and use it in integration tests * disable localnet process for sdk tests on ci * clean up code and update readme * fix lint * address comments
- Loading branch information
Showing
10 changed files
with
144 additions
and
17 deletions.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./localNode"; |
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,105 @@ | ||
import { ChildProcessWithoutNullStreams, spawn } from "child_process"; | ||
import kill from "tree-kill"; | ||
import { sleep } from "../utils/helpers"; | ||
|
||
export class LocalNode { | ||
readonly MAXIMUM_WAIT_TIME_SEC = 30; | ||
|
||
readonly READINESS_ENDPOINT = "http://127.0.0.1:8070/"; | ||
|
||
process: ChildProcessWithoutNullStreams | null = null; | ||
|
||
/** | ||
* kills all the descendent processes | ||
* of the node process, including the node process itself | ||
*/ | ||
stop() { | ||
if (!this.process?.pid) return; | ||
kill(this.process.pid); | ||
} | ||
|
||
/** | ||
* Runs a local testnet and waits for process to be up. | ||
* | ||
* If local node process is already up it returns and does | ||
* not start the process | ||
*/ | ||
async run() { | ||
const nodeIsUp = await this.checkIfProcessIsUp(); | ||
if (nodeIsUp) { | ||
return; | ||
} | ||
this.start(); | ||
await this.waitUntilProcessIsUp(); | ||
} | ||
|
||
/** | ||
* Starts the local testnet by running the aptos node run-local-testnet command | ||
*/ | ||
start() { | ||
const cliCommand = "npx"; | ||
const cliArgs = ["aptos", "node", "run-local-testnet", "--force-restart", "--assume-yes", "--with-indexer-api"]; | ||
|
||
const childProcess = spawn(cliCommand, cliArgs); | ||
this.process = childProcess; | ||
|
||
childProcess.stderr?.on("data", (data: any) => { | ||
const str = data.toString(); | ||
// Print local node output log | ||
// eslint-disable-next-line no-console | ||
console.log(str); | ||
}); | ||
|
||
childProcess.stdout?.on("data", (data: any) => { | ||
const str = data.toString(); | ||
// Print local node output log | ||
// eslint-disable-next-line no-console | ||
console.log(str); | ||
}); | ||
} | ||
|
||
/** | ||
* Waits for the local testnet process to be up | ||
* | ||
* @returns Promise<boolean> | ||
*/ | ||
async waitUntilProcessIsUp(): Promise<boolean> { | ||
let operational = await this.checkIfProcessIsUp(); | ||
const start = Date.now() / 1000; | ||
let last = start; | ||
|
||
while (!operational && start + this.MAXIMUM_WAIT_TIME_SEC > last) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await sleep(1000); | ||
// eslint-disable-next-line no-await-in-loop | ||
operational = await this.checkIfProcessIsUp(); | ||
last = Date.now() / 1000; | ||
} | ||
|
||
// If we are here it means something blocks the process to start. | ||
// Might worth checking if another process is running on port 8080 | ||
if (!operational) { | ||
throw new Error("Process failed to start"); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Checks if the local testnet is up | ||
* | ||
* @returns Promise<boolean> | ||
*/ | ||
async checkIfProcessIsUp(): Promise<boolean> { | ||
try { | ||
// Query readiness endpoint | ||
const data = await fetch(this.READINESS_ENDPOINT); | ||
if (data.status === 200) { | ||
return true; | ||
} | ||
return false; | ||
} catch (err: any) { | ||
return false; | ||
} | ||
} | ||
} |
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,11 @@ | ||
module.exports = async function () { | ||
// Check if the current local node process is | ||
// from within the sdk node environment | ||
if (globalThis.__LOCAL_NODE__.process) { | ||
const aptosNode = globalThis.__LOCAL_NODE__; | ||
// Local node runs multiple procceses, to avoid asynchronous operations | ||
// that weren't stopped in our tests, we kill all the descendent processes | ||
// of the node process, including the node process itself | ||
aptosNode.stop(); | ||
} | ||
}; |
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,7 @@ | ||
const { LocalNode } = require("../src/cli"); | ||
|
||
module.exports = async function setup() { | ||
const localNode = new LocalNode(); | ||
globalThis.__LOCAL_NODE__ = localNode; | ||
await localNode.run(); | ||
}; |