Although the documentation of cita is already in details, there are some tricks that I would like to share.


Getting CITA (OPTIONAL, skip if you are using an existing chain such as test chain)

So, first we need to download cita. There are two options:

  1. download the latest source file and compile
git clone && \
cd cita && \
git submodule init && \
git submodule update && \
./ make release 

or you can compile the debug version by ./ make debug

  1. download the release version

You can find release version there

Create/Run nodes && Setup an app chain (OPTIONAL, skip if you are using an existing chain such as test chain)

Take a look at tests/integration_test/, it contains the all the methods that cita provides. We can use to generate and run 4 local nodes. You will find the generated files at target/install/test-chain.

For multiple chains, you can run

./ ./scripts/ create --chain_name test2-chain --jsonrpc_port 2337 --ws_port 5337 --grpc_port 6000 --nodes ",,,"

to generate a test2-chain but notice multiple chains are runned in one docker container because of the limitation of rabbitmq.

Create side chain with a main chain

./ ./scripts/ create \
--chain_name test2-chain \
--jsonrpc_port 2337 \
--ws_port 5337 \
--grpc_port 6000 \
--nodes ",,," \
--contract_arguments "SysConfig.chainId=3" "SysConfig.economicalModel=1" \
"ChainManager.parentChainId=2" "ChainManager.parentChainAuthorities=0x17f3487df9f9331969602bf203165abf886a0ed1"

You can check the parentChainAuthorities at test-chain/template/authorities.list. The example only uses the auth of node0 created by default.

Check the status (OPTIONAL, skip if you are using an existing chain such as test chain)

An elegant way to do this is install cita-cli

git clone
cd cita-cli/cita-cli
cargo install

Then you can run it (interactive mode) at ~/.cargo/bin/cita-cli or cargo run under cita-cli/cita-cli

# default is
# the following is a test chain provided by nervos
cita> switch --host
cita> rpc blockNumber
cita> exit

The full list of json-rpc list is here

Deploy contracts

The default config super_admin account for local test-chain

  • private key: 5f0258a4778057a8a7d97809bd209055b2fbafa654ce7d31ec7191066b9225e6
  • address: 0x4b5ae4567ad5d9fb92bc9afd6a657e6fa13a2523

The test chain provided by nervos

  • private key: 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

The deployment of this tutorial follows the example of dapp-demos/first-forever.

  1. Compile the contract by solc
solcjs --optimized --abi --bin <your contract>
  1. Copy the abi & bin to compiled.js (actually we can automate this process with node, take a look at this example, but I was not able to do this due to some unknow reason)
  2. npm run deploy and you will get a contract address (You probably want to note it down for later use)

Integrate contracts to dapp

You can use the nervos.js which is a sdk for cita.

const abi = JSON.parse(
const contract = new nervos.appchain.Contract(abi, contractAddress)

// call method
// get method is specified by contract through abi
// contract.methods.myMethod(paramters).call(transaction)

// send method
// set method is specified by contract through abi
// contract.methods.myMethod(parameters).send(transaction)

Final thoughts

I just learned about CITA (nervos) two days ago at a hacakthon. It really is a developer-friendly blockchain system. I got prompt response from the maintainers on Wechat group and on site at hackathon. There are some improvements needed, but overall it is a pleasure experience.

For the hackathon, our team did an decentralized random number generator - Thanos based on Nervos (link). Although we did not win a prize, we think this could be an important infrastructure for Nervos system because random number generator could power up many applications in games, cryptographs, …