A Step by Step Guide for Setting-up Hyperledger Sawtooth

Pulkit Saraf
Akeo
Published in
6 min readApr 19, 2019

Hyperledger Sawtooth is a blockchain framework initially developed by Intel and later submitted to Hyperledger Consortium. Hyperledger Sawtooth is an enterprise blockchain platform for building distributed ledger applications and networks. Using sawtooth, the blockchain application development is simplified by separating the main core system and the application domain.

Components in Sawtooth Network:

There are few components which are responsible for running the entire sawtooth network.

  1. Validator: Validators are peer nodes which are interconnected in the form of a mesh network and can interact with each other. These nodes holds the data in the form of a merkle tree which is also called as the ledger data. Our application writes and retires data from this merkle tree.
  2. Transaction Processors: There are a set of business rules that are to be validated during the functioning of a system. These rules are called transaction processors as they process each and every transactions before committing it to each and every node.
  3. Sawtooth Rest Server: Rest Server is the service which helps the users to interact with the validators to submit any transactions with the help of HTTP requests.

Setting Up a Sawtooth Application Development Environment Using Docker:

The application development environment using docker is a single validator node that is running a validator, a REST API and three transaction processors.

Prerequisites:

Following are the prerequisites for the application development environment in Sawtooth

  • Operating Systems: Ubuntu Linux 16.04 , or Mac OS 10.12
  • Docker Engine (Latest Version)
  • Docker Compose (Latest Version)

Once you have all the prerequisites installed, you have to download the docker compose file which will enable the deployment of Sawtooth network in docker containers.

  • Step 1: Navigate to the folder where the docker compose file is download and run the command:
$ docker-compose -f sawtooth-default.yaml up
  • Step 2: Once the command is completed, all the necessary docker containers gets initialized. You can check the docker containers using the command:
$ docker ps
  • Step 3: As the docker containers gets initialized, login into the client container which is used to run the Sawtooth commands, which interact with the validator through REST API.

Open a new terminal and run the following to log into the client container.

$ docker exec -it sawtooth-shell-default bash
  • Step 4: Confirm that the REST API and the validator are running and reachable from the client computer, Run the curl command.
root@client# curl http://rest-api:8008/blocks

You should get the output as given

  • Step 5: Creating and Submitting Transactions with intkey or sawtooth

Using intkey

1. To prepare batches of transactions that set a few keys to random values and randomly increment and decrement those values, Use command:

  root@client# intkey create_batch — count 10 — key-count 5

2. TO submit the batches to validator, Use command:

root@client# intkey load -f batches.intkey — url http://rest-api:8008

Using sawtooth

The batches created using the command intkey create_batch, can also be submitted to validator using the following command.

root@client# sawtooth batch submit -f batches.intkey — url http://rest-api:8008
  • Step 6: Use sawtooth block command to view the blockchain and the block data
root@client# sawtooth block list — url http://rest-api:8008

To view the block data, copy the block id of the block you want to view and replace the {BLOCKID} with the copied block id.

root@client# sawtooth block show — url http://rest-api:8008 {BLOCKID}

NOTE: You can also connect to any other container using the command

$docker exec -it {ContainerName} bashe.g. — $ docker exec -it sawtooth-validator-default bash
  • Step 7: To display the sawtooth logs use command:
$docker logs {ContainerName}
  • Step 8: To stop the sawtooth environment, open the terminal where docker-compose and press ctrl+C. All the sawtooth containers will get stopped and the output will resemble the given figure below.

Once all the containers gets stopped, run the command given below.

$ docker-compose -f sawtooth-default.yaml down

TIC TAC TOE (XO Transaction) using Sawtooth Transaction Processor

Tic tac toe is a game in which two players takes the turn one by one making spaces on a 3x3 grid.

  • The first player marks spaces with an X and the second player marks spaces with O. The first player always takes the first move.
  • The player who marks three adjoining spaces either horizontally or vertically or diagonally wins the game.
  • The game is tie if all the spaces in the grid gets marked but no player has satisfied the above condition.

About XO Transaction Family:

The XO transaction family states the data model used and the business logic used to play the tic-tac-toe. It includes:

  • Transaction Processors:- which executes the business logic to play the XO game.
  • XO Client:- provides a command-line interface to play XO game.

For more about XO Transaction, visit link

Steps to Create and Start Game:

Step 1: Follow the above given steps to setup the sawtooth application development platform.

Step 2: Verify the connectivity to the REST API.

Step 3: Open the sawtooth-shell default container using command. Create two players to play the game.

root@client: sawtooth keygen xroot@client: sawtooth keygen y

Step 4: Create game named “new-game” using the command.

root@client# xo create new-game — username x — url http://rest-api:8008

To verify the game is created, use command

root@client# xo list — url http://rest-api:8008

Step 5: Take space as first player (x) in the grid.

root@client# xo take new-game 5 — username x — url http://rest-api:8008

Step 6: Take space as second player (y)

root@client# xo take new-game 8 — username y — url http://rest-api:8008

To check the game status, run command

root@client# xo show new-game — url http://rest-api:8008

Continue the game till you get the final result of the game.

To delete the game, Use command:

xo delete new-game — url http://rest-api:8008

This tutorial shows a simple setup of Hyperledger Sawtooth using docker. To learn more about Hyperledger Sawtooth, Click Here. There are different examples of hyperledger sawtooth available on github like sawtooth-supply-chain, sawtooth-market-place, sawtooth-private-utxo, etc.

--

--