2. 入门

在这个例子里面,我们将创建一个简单基础的 Iroha网络,将其运行起来,创建一些事务 并且确认这些数据有写入的账本。为了简便,我们将使用Docker.

注解

Ledger is the synonym for a blockchain, and Hyperledger Iroha is known also as Distributed Ledger Technology framework — which in essence is the same as “blockchain framework”. You can check the rest of terminology used in the 核心概念 section.

2.1. 先决条件

For this guide, you need a machine with Docker installed. You can read how to install it on a Docker’s website.

注解

Of course you can build Iroha from scratch, modify its code and launch a customized node! If you are curious how to do that — you can check Building Iroha section. In this guide we will use a standard distribution of Iroha available as a docker image.

2.2. 运行Iroha节点

2.2.1. 创建一个Docker网络

为了操作,Iroha需要一个``PostgreSQL``数据库。 让我们从创建一个Docker网络开始,这样Postgres和Iroha的容器就可以在同一个虚拟网络上运行并且能够互相通信。 在本指南中,我们将其称为“iroha-network”,你也可以使用其他任何名字。 在终端中输入以下命令:

docker network create iroha-network

2.2.2. 启动PostgreSQL容器

现在我们需要在容器里面运行``PostgreSQL``, 把network attach到之前创建的那个network上面,为了与外部通信,还有需要expose 端口:

docker run --name some-postgres \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-p 5432:5432 \
--network=iroha-network \
-d postgres:9.5 \
-c 'max_prepared_transactions=100'

注解

If you already have Postgres running on a host system on default port (5432), then you should pick another free port that will be occupied. For example, 5433: -p 5433:5432

2.2.3. 创建 Blockstore

Before we run Iroha container, we may create a persistent volume to store files, storing blocks for the chain. It is done via the following command:

docker volume create blockstore

2.2.4. Preparing the configuration files

注解

To keep things simple, in this guide we will create a network containing only a single node. To understand how to run several peers, follow 部署 Iroha

Now we need to configure our Iroha network. This includes creating a configuration file, generating keypairs for a users, writing a list of peers and creating a genesis block.

Don’t be scared away — we have prepared an example configuration for this guide, so you can start testing Iroha node now. In order to get those files, you need to clone the Iroha repository from Github or copy them manually (cloning is faster, though).

git clone -b master https://github.com/hyperledger/iroha --depth=1

提示

--depth=1 option allows us to download only the latest commit and save some time and bandwidth. If you want to get a full commit history, you can omit this option.

There is a guide on how to set up the parameters and tune them with respect to your environment and load expectations: 配置. We don’t need to do this at the moment.

2.2.5. 启动 Iroha 容器

We are almost ready to launch our Iroha container. You just need to know the path to configuration files (from the step above).

Let’s start Iroha node in Docker container with the following command:

docker run --name iroha \
-d \
-p 50051:50051 \
-v $(pwd)/iroha/example:/opt/iroha_data \
-v blockstore:/tmp/block_store \
--network=iroha-network \
-e KEY='node0' \
hyperledger/iroha:latest

If you started the node successfully you would see the container id in the same console where you started the container.

Let’s look in details what this command does:

  • docker run --name iroha \ creates a container iroha
  • -d \ runs container in the background
  • -p 50051:50051 \ exposes a port for communication with a client (we will use this later)
  • -v YOUR_PATH_TO_CONF_FILES:/opt/iroha_data \ is how we pass our configuration files to docker container. The example directory is indicated in the code block above.
  • -v blockstore:/tmp/block_store \ adds persistent block storage (Docker volume) to a container, so that the blocks aren’t lost after we stop the container
  • --network=iroha-network \ adds our container to previously created iroha-network for communication with PostgreSQL server
  • -e KEY='node0' \ - here please indicate a key name that will identify the node allowing it to confirm operations. The keys should be placed in the directory with configuration files mentioned above.
  • hyperledger/iroha:latest is a reference to the image pointing to the latest release

You can check the logs by running docker logs iroha.

You can try using one of sample guides in order to send some transactions to Iroha and query its state.