Home

Awesome

<br /> <p align="center"> <a href="https://docs.onflow.org/emulator/"> <img src="docs/emulator-banner.svg" alt="Logo" width="410" height="auto"> </a> <p align="center"> <i>The Flow Emulator is a lightweight tool that emulates the behaviour of the real Flow network.</i> <br /> <a href="https://docs.onflow.org/emulator/"><strong>Read the docs»</strong></a> <br /> <br /> <a href="https://github.com/onflow/flow-emulator/issues">Report Bug</a> · <a href="https://github.com/onflow/flow-emulator/blob/master/CONTRIBUTING.md">Contribute</a> </p> </p> <br /> <br />

The Emulator

The emulator exposes a gRPC server that implements the Flow Access API, which is designed to have near feature parity with the real network API.

The Flowser Emulator Explorer

There is also a block explorer GUI for the emulator, that will help you speed up development when using the emulator.

Running

Configuration

The Flow Emulator can be run in different modes and settings, all of them are described in the table bellow.

Please note that if you will run the emulator using the Flow CLI you must use flags to pass configuration values and if you plan to run the emulator with Docker you must use the environment variables (Env) to pass configuration values.

FlagEnvDefaultDescription
--port, -pFLOW_PORT3569gRPC port to listen on
--rest-portFLOW_RESTPORT8888REST API port to listen on
--admin-portFLOW_ADMINPORT8080Admin API port to listen on
--verbose, -vFLOW_VERBOSEfalseEnable verbose logging (useful for debugging)
--log-formatFLOW_LOGFORMATtextOutput log format (valid values text, JSON)
--block-time, -bFLOW_BLOCKTIME0Time between sealed blocks. Valid units are ns, us (or µs), ms, s, m, h
--contractsFLOW_WITHCONTRACTSfalseStart with contracts like NFT and an NFT Marketplace, when the emulator starts
--service-priv-keyFLOW_SERVICEPRIVATEKEYrandomPrivate key used for the service account
--service-sig-algoFLOW_SERVICEKEYSIGALGOECDSA_P256Service account key signature algorithm
--service-hash-algoFLOW_SERVICEKEYHASHALGOSHA3_256Service account key hash algorithm
--initFLOW_INITfalseGenerate and set a new service account
--rest-debugFLOW_RESTDEBUGfalseEnable REST API debugging output
--grpc-debugFLOW_GRPCDEBUGfalseEnable gRPC server reflection for debugging with grpc_cli
--persistFLOW_PERSISTfalseEnable persistence of the state between restarts
--snapshotFLOW_SNAPSHOTfalseEnable snapshot support
--dbpathFLOW_DBPATH./flowdbSpecify path for the database file persisting the state
--simple-addressesFLOW_SIMPLEADDRESSESfalseUse sequential addresses starting with 0x1
--token-supplyFLOW_TOKENSUPPLY1000000000.0Initial FLOW token supply
--transaction-expiryFLOW_TRANSACTIONEXPIRY10Transaction expiry, measured in blocks
--storage-limitFLOW_STORAGELIMITENABLEDtrueEnable account storage limit
--storage-per-flowFLOW_STORAGEMBPERFLOWSpecify size of the storage in MB for each FLOW in account balance. Default value from the flow-go
--min-account-balanceFLOW_MINIMUMACCOUNTBALANCESpecify minimum balance the account must have. Default value from the flow-go
--transaction-feesFLOW_TRANSACTIONFEESENABLEDfalseEnable variable transaction fees and execution effort metering <br> as decribed in Variable Transaction Fees: Execution Effort FLIP
--transaction-max-gas-limitFLOW_TRANSACTIONMAXGASLIMIT9999Maximum gas limit for transactions
--script-gas-limitFLOW_SCRIPTGASLIMIT100000Specify gas limit for script execution
--coverage-reportingFLOW_COVERAGEREPORTINGfalseEnable Cadence code coverage reporting
--contract-removalFLOW_CONTRACTREMOVALtrueAllow removal of already deployed contracts, used for updating during development
--skip-tx-validationFLOW_SKIPTRANSACTIONVALIDATIONfalseSkip verification of transaction signatures and sequence numbers
--hostFLOW_HOST Host to listen on for emulator GRPC/REST/Admin servers (default: All Interfaces)
--chain-idFLOW_CHAINIDemulatorChain to simulate, if 'mainnet' or 'testnet' values are used, you will be able to run transactions against that network and a local fork will be created. Valid values are: 'emulator', 'testnet', 'mainnet'
--redis-urlFLOW_REDIS_URL''Redis-server URL for persisting redis storage backend ( redis://[[username:]password@]host[:port][/database] )
--start-block-heightFLOW_STARTBLOCKHEIGHT0Start block height to use when starting the network using 'testnet' or 'mainnet' as the chain-id
--rpc-hostFLOW_RPCHOST''RPC host (access node) to query for previous state when starting the network using 'testnet' or 'mainnet' as the chain-id
--legacy-upgradeFLOW_LEGACYUPGRADEfalseEnable upgrading of legacy contracts
--computation-reportingFLOW_COMPUTATIONREPORTINGfalseEnable computation reporting for Cadence scripts & transactions
--checkpoint-dirFLOW_CHECKPOINTDIR''Checkpoint directory to load the emulator state from, if starting the emulator from a checkpoint
--state-hashFLOW_STATEHASH''State hash of the checkpoint, if starting the emulator from a checkpoint

Running the emulator with the Flow CLI

The emulator is bundled with the Flow CLI, a command-line interface for working with Flow.

Installation

Follow these steps to install the Flow CLI.

Starting the server

Starting the emulator by using Flow CLI also leverages CLI configuration file flow.json. You can use the flow.json to specify the service account which will be reused between restarts. Read more about CLI configuration here.

You can start the emulator with the Flow CLI:

flow emulator

You need to make sure the configuration flow.json exists, or create it beforehand using the flow init command.

Using the emulator in a project

You can start the emulator in your project context by running the above command in the same directory as flow.json. This will configure the emulator with your project's service account, meaning you can use it to sign and submit transactions. Read more about the project and configuration here.

Using Emulator in Go

You can use the emulator as a module in your Go project. To install emulator, use go get:

go get github.com/onflow/flow-emulator

After installing the emulator module you can initialize it in the code:

var opts []emulator.Option
privKey, err := crypto.DecodePrivateKeyHex(crypto.ECDSA_P256, "")

opts = append(opts, emulator.WithServicePublicKey(
privKey.PublicKey(),
crypto.ECDSA_P256,
crypto.SHA3_256,
))

blockchain, err := emulator.NewBlockchain(opts...)

You can then access all methods of the blockchain like so:

account, err := blockchain.GetAccount(address) 

Rolling back state to blockheight

It is possible to roll back the emulator state to a specific block height. This feature is extremely useful for testing purposes. You can set up an account state, perform tests on that state, and then roll back the state to its initial state after each test.

To roll back to a specific block height, you can utilize below HTTP request: `` POST http://localhost:8080/emulator/rollback

Post Data: height={block height}


Note: it is only possible to roll back state to a height that was previously executed by the emulator.
To roll back to a past block height when using a forked Mainnet or Testnet network, use the
`--start-block-height` flag.

## Managing emulator state
It's possible to manage emulator state by using the admin API. You can at any point 
create a new named snapshot of the state and then at any later point revert emulator 
state to that reference. 

In order to use the state management functionality you need to run the emulator with persistent state:
```bash
flow emulator --persist

Create a new snapshot by doing an HTTP request:

POST http://localhost:8080/emulator/snapshots

Post Data: name={snapshot name}

Please note the example above uses the default admin API port

At any later point you can reload to that snapshot by executing:

PUT http://localhost:8080/emulator/snapshots?name={snapshot name}

You need to use the same value for name parameter.

The snapshot functionality is a great tool for testing where you can first initialize a base snapshot with seed values, execute the test and then revert to that initialized state.

You can list existing snapshots with:

GET http://localhost:8080/emulator/snapshots

Cadence Code Coverage

The admin API includes endpoints for viewing and managing Cadence code coverage.

In order to use this functionality you need to run the emulator with the respective flag which enables code coverage:

flow emulator --coverage-reporting

To view the code coverage report, visit this URL: http://localhost:8080/emulator/codeCoverage

To flush/reset the collected code coverage report, run the following command:

curl -XPUT 'http://localhost:8080/emulator/codeCoverage/reset'

Note: The above command will reset the code coverage for all the locations, except for A.f8d6e0586b0a20c7.FlowServiceAccount, which is a system contract that is essential to the operations of Flow.

To get better reports with source file references, you can utilize the sourceFile pragma in the headers of your transactions and scripts.

#sourceFile("scripts/myScript.cdc")

Cadence Computation Reporting

The admin API includes an endpoint for viewing the Cadence computation reports for scripts & transactions.

In order to use this functionality you need to run the emulator with the respective flag which enables computation reporting:

flow emulator --computation-reporting

To view the computation report, visit this URL: http://localhost:8080/emulator/computationReport

Running the emulator with Docker

Docker builds for the emulator are automatically built and pushed to gcr.io/flow-container-registry/emulator, tagged by commit and semantic version. You can also build the image locally.

docker run -p 3569:3569 -p 8080:8080 -e FLOW_HOST=0.0.0.0 gcr.io/flow-container-registry/emulator

The full list of environment variables can be found here. You can pass any environment variable by using -e docker flag and pass the valid value.

Custom Configuration Example:

docker run -p 3569:3569 -p 8080:8080 -e FLOW_HOST=0.0.0.0 -e FLOW_PORT=9001 -e FLOW_VERBOSE=true -e FLOW_SERVICEPRIVATEKEY=<hex-encoded key> gcr.io/flow-container-registry/emulator

To generate a service key, use the keys generate command in the Flow CLI.

flow keys generate

Emulating mainnet and testnet transactions

The emulator allows you to simulate the execution of transactions as if performed on the Mainnet or Testnet. To activate this feature, you must specify the network name for the chain ID flag and the RPC host to connect to.

flow emulator --chain-id mainnet --rpc-host access.mainnet.nodes.onflow.org:9000
flow emulator --chain-id mainnet --rpc-host access.devnet.nodes.onflow.org:9000

Please note, that the actual execution on the real network may differ depending on the exact state when the transaction is executed.

By default, the forked network will start from the latest sealed block when the emulator is started. You can specify a different starting block height by using the --start-block-height flag.

You can also store all of your changes and cached registers to a persistent db by using the --persist flag, along with the other SQLite settings.

To submit transactions as a different account, you can use the --skip-tx-validation flag to disable the transaction signature verification. Then submit transactions from any account using any valid private key.

Debugging

To debug any transactions sent via VSCode or Flow CLI, you can use the debugger pragma. This will cause execution to pause at the debugger for any transaction or script which includes that pragma.

#debugger()

Development

Read contributing document.