What is Ethereum Ganache?
Ethereum smart contracts are programs executed within the context of transactions on the Ethereum blockchain. Ethereum Ganache forms part of the Truffle Suite, a set of developer tools that allows users to recreate blockchain environments locally and test smart contracts. Smart contract execution on the Ethereum blockchain is very different from other types of software.
First of all, the on-chain context makes it hard to communicate with the outside world. Simple input and output operations, such as writing to a console, are not possible, as transactions are the only means of communicating with the blockchain. Secondly, the transactional nature of the blockchain means that all state changing interactions with a smart contract are asynchronous in nature. This means that when transactions are sent, the effects are not visible until the transaction has been confirmed by being included in a block. Finally, the blockchain environment places some specific restrictions on the code that can be executed, mostly related to the cost associated with each operation.
Programmers have to consider factors such as the block gas limit, or how many operations can be executed safely within the gas allowance of certain functions. Other challenges include the difficulty of generating random numbers, transaction ordering dependencies, and the public nature of all blockchain operations.
In short, smart contracts are hard to program. In addition, once deployed, smart contracts cannot be modified and each deployment has an associated cost. Getting things right the first time, therefore, has a criticality usually only associated with software in high-risk applications such as control software in critical infrastructures or aviation.
In order to debug and test smart contracts before going into production, it is therefore essential to allow developers to recreate blockchain environments locally, without the added inconvenience of deployment costs and transaction delays.
Fortunately, the Truffle Suite, a set of developer tools for Ethereum, includes Ethereum Ganache, a tool designed for this purpose.
Ethereum Ganache: The Personal Blockchain
Ethereum Ganache is a local in-memory blockchain designed for development and testing. It simulates the features of a real Ethereum network, including the availability of a number of accounts funded with test Ether.
Etheruem Ganache is available in two versions, as a graphical application with a user interface, and as a command line version. The former can be downloaded for a number of platforms from the project’s website, whereas the latter can be installed using the NPM package manager with the following command:
- npm install -g ganache-cli
Once launched by clicking the item of the graphical version or executing ganache-cli, both versions present an RPC interface that can be accessed on a port of the local host in the same way one would connect to a real Ethereum node. This allows smart contracts to be deployed easily and to be tested in a number of ways, most easily through the other smart contract interaction and testing tools included in the Truffle Suite.
With such a setup, developers can easily test their smart contracts and related applications without the need to worry about creating and funding accounts, and with instant blockchain response.
Using Ethereum Ganache
Using Ethereum Ganache out of the box is a straightforward process. After launching the graphical user interface, the user may choose between a quickstart or customized workspace. The former sets up a ready to use in-memory blockchain with some basic default values for parameters, such as the Ethereum Virtual Machine (EVM) to be used, gas price, gas limit and the RPC interface to be presented to the local host. It also prepares ten funded and unlocked accounts.
During testing, transactions can be inspected on a transactions page, whereas the state of smart contracts can be monitored on the contracts page. There is also an events page, logging all events emitted on the test blockchain.
Ethereum Ganache: Advanced Features
The basic setup is enough for deploying most smart contracts, and developers will never venture beyond the quickstart workspace. However, sometimes it may be necessary to simulate certain blockchain conditions, such as a specific EVM version or gas price. It may also be necessary to launch the RPC interface on a different port, create more accounts, or generate specific keys and accounts from a mnemonic seed phrase. These functionalities are facilitated by the custom workspace.
By using the command line version, it is even possible to fork a running network, meaning a user can re-create the exact state at a certain block number. One use for this, for instance, is to copy the current Ethereum main network for testing.