Let’s Build a Hyperledger Sawtooth Application

Let’s Build a Hyperledger Sawtooth Application

You might have definitely come across the word Hyperledger if you were looking for an enterprise-grade blockchain platform. Sawtooth is one of the popular projects under the Hyperledger consortium. Proposed by Intel in the year 2014, Hyperledger Sawtooth was initially designed for imparting an application that is safer for enterprise use. Later it joined with the Linux Foundation to form its present shape, making it a perfect solution for developing networks and distributed ledger technologies. Today many enterprises are using this incredible platform as it offers exactly what businesses want. The enterprises are given the adaptability to pick their own private rules. 

Hyperledger Fabric is another project in the Hyperledger consortium. Here is a quick comparison of Fabric with Sawtooth.

No alt text provided for this image

Being highly modular, Sawtooth remains highly flexible in action and therein perfectly fits into the shoes of any enterprise model. It helps the business partners to make the right business decisions via building their own transaction rules, consensus algorithms, and permission policies. More so, Sawtooth also provides SDKs in various programming languages for the developers to develop applications in their own comfortable languages be it JavaScript, Python, Java, C++, Rust, Go, or any other. Hyperledger Sawtooth thus definitely remains a flexible environment for any business use case.

Components of a Sawtooth Application

Sawtooth introduces the concept of transaction families. A transaction family can be considered as a decentralized application in Sawtooth World. A transaction family includes the following components:

  • Client: module that handles the client logic of the application. The client is responsible for creating and signing transactions, combining those transactions into batches, and submitting them to the validator. This client can be a web application, mobile app, or even a command-line interface.
  • Transaction processor: which defines the business logic of the application. The transaction processor is responsible for registering with the validator, handling transaction requests, and getting/setting state as needed. This is a smart contract equivalent in Sawtooth. A single transaction family may have more than one transaction processor.
  • Data model: to store and retrieve data, usually referred to as a state.


HelloWorld Application

Now that you got a brief on Hyperledger Sawtooth, let’s get to the next step of running an application on Sawtooth.  For this, we will be using a simple Hello World application in Sawtooth. Using this application, you will be able to, run a Sawtooth test network and run an Express application where you could write some data to the Sawtooth blockchain and retrieve it. This demo app is developed with the help of JavaScript SDK in Sawtooth. By running this application you will get a general idea about the working of a Sawtooth application.

We already saw that a Sawtooth application or a Transaction Family consists of a client, transaction processor and state. In our example, the client-side application is used to input and view text data. Then a transaction processor to handle writing and reading from the blockchain. The input text will be stored in a state. We will be using Docker to run this application.

Let’s see how it is structured in our sample application.

HelloWorld application is built with two parts,

  • Client - The client is a Node.js application (in express) which provides a web user interface for the user to ‘write’ a message and then ‘read’ it. In the HelloWorld application, helloworldClient directory contains the client logic. The app.js is the main javascript file from where the main function call occurs. Handlebars are used for templating, client-related CSS and JavaScript code is written in the public folder, and server related files are written in routes/ folder. The application dependencies and the commands to be executed are included in DockerFile.
  • Transaction Processor - The helloworldprocessor directory contains the business logic. HelloWorldProcessor.js is a generic class for communicating with a validator and routing transaction processing requests to a registered handler. HelloWorldHandler.js is the handler class that contains the business logic for the particular family of transactions.
  • The data is stored at a 70 hex digit address in the state.

Now let’s see how to run the HelloWorld application.

For that, you need certain prerequisites.

Prerequisites

This example uses Docker Compose and Docker containers. For installing Docker Engine and Docker Compose, please follow instructions in the provided links: 

Docker: Install Docker

Docker Compose: Install Docker Compose

NOTE: The recommended OS environment is Ubuntu Linux 18.04 LTS x64. Although other Linux distributions which support Docker should work.

Running the HelloWorld Application

  1. Download or clone the HelloWorld application from GitHub link: https://github.com/Kerala-Blockchain-Academy/HelloWorld-Sawtooth 

git clone https://github.com/Kerala-Blockchain-Academy/HelloWorld-Sawtooth.git

2. Open a command terminal and navigate to the directory where the HelloWorld-Sawtooth code is present. 

cd HelloWorld-Sawtooth

3. Run the following command to start the HelloWorld application in the Sawtooth Docker environment.

sudo docker-compose up

No alt text provided for this image

4. Open a browser and go to https://localhost:3000 

No alt text provided for this image

Now the user can enter the message in the textbox and click the Write button. It will store the data in state and you can retrieve the message by clicking Read button.

No alt text provided for this image

When the ‘Write’ button is clicked, the corresponding transactions are reflected in the validator window.

No alt text provided for this image

5. For viewing the block, transaction and state details, open a new window in the browser and give the following URLs. 

a. Enter https://localhost:8008/blocks to view the block details.

No alt text provided for this image

b. Enter https://localhost:8008/transactions to view the transaction details.

No alt text provided for this image

c. Enter https://localhost:8008/state to view the state details.

No alt text provided for this image

6. Stopping the Sawtooth Environment

a. To stop the validator and destroy the containers, press CTRL+C from the terminal where we originally ran docker-compose.yaml file.

b. After all the containers have shut down, run the following docker-compose command:

sudo docker-compose down

c. To clean up all the docker containers, give the command.

sudo docker container prune

To conclude, Hyperledger Sawtooth is a modular enterprise blockchain platform that makes seamless the process of building, deploying, and running distributed ledgers. The design philosophy makes it enterprise safe and highly versatile. The network follows a novel consensus algorithm, Proof of Elapsed Time (PoET), that demands minimal resource consumption, making it feasible for any business size. But in our example we followed a simple developer mode(devmode) consensus, which is only for development and testing purposes.

Kerala Blockchain Academy offers a Certified Blockchain Associate Program as the first step towards blockchain technology. The course is offered both online and offline in a mission to impart the strong foundation in distributed ledger technologies. The course can be an ideal starting point to learn the fundamentals and use cases of blockchain technology. Also, it can be a foundation stone to pursue advanced developer courses offered by KBA, such as Corda, Hyperledger (Fabric & Sawtooth) and Ethereum. For more information visit our website.

For detailed discussions on Hyperledger Sawtooth and other blockchain topics, join our Linkedin Group : https://www.dhirubhai.net/groups/13861449/

Video on Step-by-step development of Hyperledger Sawtooth application is available in our YouTube Channel : https://youtu.be/-YV1Ms4AgxY



Abdelhak Bouguila

ingénieur en cybersécurité - Pentester | CPTS |

3 年

Hello, i am having error at docker-compose up that i can't find on internet. any clue please ? i tried it on two machines ubuntu 18 and 20 and i get the same problem

回复
Rajesh Sinha

Senior SAP Project Engineer at T-Systems Malaysia

4 年

Good tutorial on Sawtooth for beginners, thanks.

Srinivas Mahankali

Web3 Cybersecurity, Digital Transformation professional

4 年
Narendranath Reddy Thota

Senior Engineer Advanced Tech | AI, Blockchain and DevOps @ du Telecom | Kubernetes Ninja | CKA | CKAD | CKS | Ex Consensys, Dubai | Author of Mastering Hyperledger Fabric and Ethereum, HLF Wiley video courses

4 年

Keep it up the good work KBA

要查看或添加评论,请登录

Kerala Blockchain Academy的更多文章

社区洞察

其他会员也浏览了