How are Smart Contracts Executed in Hyperledger?
Smart Contracts are at the core of any blockchain implementation. The business logic is what drives many things in the blockchain, and Hyperledger makes it a little bit easier for us to implement Smart Contracts.
During many of our Blockchain Business Workshops, one area we heavily focus on is about Smart Contracts, and how they are executed in Hyperledger or any blockchain framework.
Before we jump in into its execution strategy, here is some essential information about Smart Contracts:
What are Smart Contracts?
Smart Contracts is the business logic that runs on a blockchain. The complexity of the Smart Contract depends on the strategy that you would use to solve your business problem with blockchain.
Smart Contracts is the business logic in a blockchain.
Smart Contracts are grouped into two, based on the use-case in which they are being implemented:
- On-chain Smart Contracts
- Installed Smart Contracts
On-chain Smart Contracts
These are executed when a transaction happens on the blockchain. And they are stored right inside the blockchain as well.
Installed Smart Contracts
These Smart contracts are executed before the commit happens to the ledger or before the network is launched.
To understand a little bit more about Smart Contracts, take a look at the Hyperledger Architecture our CIO wrote about a few weeks back.
How are Smart Contracts Executed?
To explain how Smart Contracts are executed in Hyperledger, here’s a diagram that would give you a very quick overview of the flow:
Everything starts with the input block or the contract interpreter, where the ID of the contract is sent for a transaction request. Whenever the Smart Contract finds that the request is not valid, it rejects the contract, and then the transaction is committed to the blockchain’s ledger.
Once this validation goes through, the transaction packet is sent to the consensus service, which sits in the middle of the processing unit. This is where the Smart Contract validates whether the transaction can be committed to the blockchain or not.
This validation is not just a straightforward implementation of yes or no rules in the Smart Contract code. When validating a transaction, the Smart Contract validates two major things:
- Syntax Errors
- Logic Errors
Validating the syntax errors are very straightforward. But validating the logic errors are very complex.
When validating logic errors, Smart Contract validation turns to a policy-driven decision making. The best example would be to store the details of a double-transaction in the chain, for audit purposes for later use. This can be done if the policy requires you to do that.
The best part about Hyperledger framework is the ability to handle Smart Contract in a unique way in each and every framework that it has:
- Hyperledger Fabric
- Hyperledger Sawtooth
- Hyperledger Burrow
- Hyperledger Iroha
Pro Tip: When using Hyperledger for a large-scale transaction, using Hyperledger Sawtooth is preferred considering the way it handles the Smart Contracts cryptographically, rather than a straightforward validation.