-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR are connected to [Ganache-like testing feature #49](inkdevhub/swanky-node#49). The main purpose of this issue is to make testing easier by implementing next features: > * block time stamp manipulation (can mine a block with arbitrary block time stamp) > * block number manipulation > * taking a snapshot and retrieve later > * Balance manipulation of accounts > * Impersonate any account All of them can already implemented in [Chopsticks](https://github.com/AcalaNetwork/chopsticks). Below I will explain how to use it. Firstly, you should set up your swanky project with `swanky init` and choose that you want to install swanky-node. Then you should use `swanky node chopsticks init` to initiate chopsticks config, which will be stored in the `./node/config/` folder. Then you start the node(`swanky node start`) and fork it with chopsticks(`swanky node chopsticks start`). Now we have a testing node that has all the features that we need. Chopsticks allow to use of different RPC calls. Full list of them you can check [here](https://acalanetwork.github.io/chopsticks/docs/chopsticks/README.html). For example, you can do timestamp manipulation by `dev_timeTravel' RPC call: ```TypeScript import { WsProvider } from '@polkadot/rpc-provider' const ws = new WsProvider(`ws://localhost:8000`) await ws.send('dev_timeTravel', ['Jan 1, 2023']) ``` Or go back to any block by 'dev_setHead': ```TypeScript import { WsProvider } from '@polkadot/rpc-provider' const ws = new WsProvider(`ws://localhost:8000`) await ws.send('dev_setHead', [1000000]) ``` --------- Co-authored-by: Igor Papandinas <[email protected]>
- Loading branch information
1 parent
c196997
commit f926f6a
Showing
11 changed files
with
136 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import path from "node:path"; | ||
import { SwankyCommand } from "../../../lib/swankyCommand.js"; | ||
import { | ||
copyChopsticksTemplateFile, | ||
getSwankyConfig, | ||
getTemplates, | ||
} from "../../../lib/index.js"; | ||
import { ConfigBuilder } from "../../../lib/config-builder.js"; | ||
import { SwankyConfig } from "../../../types/index.js"; | ||
|
||
export const chopsticksConfig = "dev.yml"; | ||
|
||
export class InitChopsticks extends SwankyCommand<typeof InitChopsticks> { | ||
static description = "Initialize chopsticks config"; | ||
|
||
async run(): Promise<void> { | ||
const localConfig = getSwankyConfig("local") as SwankyConfig; | ||
const projectPath = path.resolve(); | ||
|
||
const chopsticksTemplatePath = getTemplates().chopsticksTemplatesPath; | ||
const configPath = path.resolve(projectPath, "node", "config"); | ||
|
||
await this.spinner.runCommand( | ||
() => | ||
copyChopsticksTemplateFile(chopsticksTemplatePath, configPath), | ||
"Copying Chopsticks template files...", | ||
); | ||
|
||
await this.spinner.runCommand(async () => { | ||
const newLocalConfig = new ConfigBuilder(localConfig) | ||
.addChopsticks(path.resolve(projectPath, "node", "config", chopsticksConfig)) | ||
.build(); | ||
await this.storeConfig(newLocalConfig, "local"); | ||
}, "Updating Swanky configuration with Chopsticks settings..."); | ||
|
||
this.log("Chopsticks config initialized successfully"); | ||
} | ||
} | ||
|
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,40 @@ | ||
import { Flags } from "@oclif/core"; | ||
import { execaCommand } from "execa"; | ||
import { SwankyCommand } from "../../../lib/swankyCommand.js"; | ||
import { ensureSwankyNodeInstalled } from "../../../lib/index.js"; | ||
import { pathExists } from "fs-extra/esm"; | ||
import { ConfigError, FileError } from "../../../lib/errors.js"; | ||
export class StartChopsticks extends SwankyCommand<typeof StartChopsticks> { | ||
static description = "Start chopsticks"; | ||
|
||
static flags = { | ||
"config": Flags.string({ | ||
description: "Path to the chopsticks config file", | ||
}) | ||
} | ||
|
||
async run(): Promise<void> { | ||
const { flags } = await this.parse(StartChopsticks); | ||
|
||
ensureSwankyNodeInstalled(this.swankyConfig); | ||
|
||
const chopsticksConfigPath = flags.config ?? this.swankyConfig.node.chopsticks?.configPath; | ||
|
||
if(!chopsticksConfigPath) { | ||
throw new ConfigError("Chopsticks config not set in swanky config. Please set it in swanky config or provide the path to the chopsticks config file using --config flag."); | ||
} | ||
|
||
if (!(await pathExists(chopsticksConfigPath))) { | ||
throw new FileError(`Chopsticks config file not found at ${flags.config}`); | ||
} | ||
|
||
await execaCommand( | ||
`npx @acala-network/chopsticks@latest --config=${chopsticksConfigPath}`, | ||
{ | ||
stdio: "inherit", | ||
} | ||
); | ||
|
||
this.log("Chopsticks started successfully."); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { SwankyConfig } from "../index.js"; | ||
import { FileError } from "./errors.js"; | ||
|
||
export function ensureSwankyNodeInstalled(config: SwankyConfig) { | ||
if (config.node.localPath === "") { | ||
throw new FileError('Swanky node is not installed. Please run `swanky node:install` first.'); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
endpoint: ws://127.0.0.1:9944 | ||
mock-signature-host: true | ||
block: 1 | ||
db: ./db.sqlite |
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