Skip to content

Commit

Permalink
Support ganache 7 cli flags
Browse files Browse the repository at this point in the history
  • Loading branch information
danhper committed Jan 31, 2022
1 parent c3dd4a7 commit 5db80e4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This changelog format is based on [Keep a Changelog](https://keepachangelog.com/
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/eth-brownie/brownie)
### Added
- Add support for Ganache 7 CLI flags
### Changed
- Force files to be opened as UTF-8
- Added a new solidity compiler setting `use_latest_patch` in brownie-config.yaml to use the latest patch version of a compiler based on the pragma version of the contract.
Expand Down
68 changes: 52 additions & 16 deletions brownie/network/rpc/ganache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/python3

import datetime
import re
import sys
import warnings
from subprocess import DEVNULL, PIPE
Expand All @@ -16,19 +17,36 @@
from brownie.network.web3 import web3

CLI_FLAGS = {
"port": "--port",
"gas_limit": "--gasLimit",
"accounts": "--accounts",
"evm_version": "--hardfork",
"fork": "--fork",
"mnemonic": "--mnemonic",
"account_keys_path": "--acctKeys",
"block_time": "--blockTime",
"default_balance": "--defaultBalanceEther",
"time": "--time",
"unlock": "--unlock",
"network_id": "--networkId",
"chain_id": "--chainId",
"7": {
"port": "--server.port",
"gas_limit": "--miner.blockGasLimit",
"accounts": "--wallet.totalAccounts",
"evm_version": "--hardfork",
"fork": "--fork.url",
"mnemonic": "--wallet.mnemonic",
"account_keys_path": "--wallet.accountKeysPath",
"block_time": "--miner.blockTime",
"default_balance": "--wallet.defaultBalance",
"time": "--chain.time",
"unlock": "--wallet.unlockedAccounts",
"network_id": "--chain.networkId",
"chain_id": "--chain.chainId",
},
"<=6": {
"port": "--port",
"gas_limit": "--gasLimit",
"accounts": "--accounts",
"evm_version": "--hardfork",
"fork": "--fork",
"mnemonic": "--mnemonic",
"account_keys_path": "--acctKeys",
"block_time": "--blockTime",
"default_balance": "--defaultBalanceEther",
"time": "--time",
"unlock": "--unlock",
"network_id": "--networkId",
"chain_id": "--chainId",
},
}

EVM_VERSIONS = ["byzantium", "constantinople", "petersburg", "istanbul"]
Expand All @@ -46,6 +64,13 @@ def launch(cmd: str, **kwargs: Dict) -> None:
else:
cmd += ".cmd"
cmd_list = cmd.split(" ")
ganache_version = get_ganache_version(cmd_list[0])

This comment has been minimized.

Copy link
@sandybradley

sandybradley Feb 27, 2022

This fails with cmd="npx ganache-cli"


if ganache_version <= 6:
cli_flags = CLI_FLAGS["<=6"]
else:
cli_flags = CLI_FLAGS["7"]

kwargs.setdefault("evm_version", EVM_DEFAULT) # type: ignore
if kwargs["evm_version"] in EVM_EQUIVALENTS:
kwargs["evm_version"] = EVM_EQUIVALENTS[kwargs["evm_version"]] # type: ignore
Expand All @@ -57,10 +82,10 @@ def launch(cmd: str, **kwargs: Dict) -> None:
for address in value:
if isinstance(address, int):
address = HexBytes(address.to_bytes(20, "big")).hex()
cmd_list.extend([CLI_FLAGS[key], address])
cmd_list.extend([cli_flags[key], address])
else:
try:
cmd_list.extend([CLI_FLAGS[key], str(value)])
cmd_list.extend([cli_flags[key], str(value)])
except KeyError:
warnings.warn(
f"Ignoring invalid commandline setting for ganache-cli: "
Expand All @@ -73,6 +98,15 @@ def launch(cmd: str, **kwargs: Dict) -> None:
return psutil.Popen(cmd_list, stdin=DEVNULL, stdout=out, stderr=out)


def get_ganache_version(ganache_executable: str) -> int:
ganache_version_proc = psutil.Popen([ganache_executable, "--version"], stdout=PIPE)
ganache_version_stdout, _ = ganache_version_proc.communicate()
ganache_version_match = re.search(r"v([0-9]+)\.", ganache_version_stdout.decode())
if not ganache_version_match:
raise ValueError("could not read ganache version: {}".format(ganache_version_stdout))
return int(ganache_version_match.group(1))


def on_connection() -> None:
pass

Expand Down Expand Up @@ -109,6 +143,8 @@ def unlock_account(address: str) -> None:


def _validate_cmd_settings(cmd_settings: dict) -> dict:
ganache_keys = set(k for f in CLI_FLAGS.values() for k in f.keys())

CMD_TYPES = {
"port": int,
"gas_limit": int,
Expand All @@ -124,7 +160,7 @@ def _validate_cmd_settings(cmd_settings: dict) -> dict:
}
for cmd, value in cmd_settings.items():
if (
cmd in CLI_FLAGS.keys()
cmd in ganache_keys
and cmd in CMD_TYPES.keys()
and not isinstance(value, CMD_TYPES[cmd])
):
Expand Down

0 comments on commit 5db80e4

Please sign in to comment.