The application layer is comprised of smart contracts, chaincode, and dApps. Application layer can be further divided into two sub-layers –application layer and execution layer. Application layer has the applications that are used by end users to interact with the blockchain network. It comprises of scripts, APIs, user interfaces, frameworks. For these applications, blockchain network is the back-end system and they often connect with blockchain network via APIs. Execution layer is the sublayer which constitutes of smart contracts, underlying rules and chaincode. This sublayer has the actual code that gets executed and rules that are executed. A transaction propagates from application layer to execution layer, however the transaction is validated and executed at the semantic layer (smart contracts and rules). Applications sends instructions to execution layer (chaincode; in case of Hyperledger fabric), which performs the execution of transactions and ensure the deterministic nature of the blockchain (such as permissioned blockchain like hyperledger fabric).
Smart contracts to be executed on the Ethereum runtime engine are written in Solidity. It needs a compiler to syntactically prove the code. Since it is compiled, the bytecode is smaller and runs faster on EVM. Code executed on EVM is fully isolated and does not have any interaction with the network or filesystem.
Smart contract: A code with business logic is identified by a unique address, and it resides on the EVM. A smart contract contains functions that are executed when a transaction is performed against those functions. Depending on the logic of the smart contract, a transaction can result in a change of state in the contract. Developers can use any language such as Solidity or Python, to write a smart contract, and can use a specific complier to compile the code into bytecode and then deploy those bytecodes to blockchain. Once deployed, a unique address is assigned to the smart contract. Any user on the blockchain can execute a transaction against that smart contract. Refer to the following transaction flow for the steps of a transaction on an Ethereum blockchain. Smart contracts are written in a high language such as Solidity and deployed to EVM for execution. However, there are codes that link the smart contract to the outside world; for example, inter-blockchain, logic execution, and so on. These are called oracles and dApps.
Oracles: Smart contracts operate on values and trigger contract state change, but only when the defined logic is met. An oracle is an agent whose task is to securely provide these values to a smart contract. Oracles are like data feeds from third-party services, which supply values to smart contracts.
Chaincode (Hyperledger Fabric): Smart contracts are the transaction logic that controls the life cycle of business objects, which are contained in the world state. Smart contracts are then packaged together into chaincode, which is then deployed to the blockchain business network. In Hyperledger, smart contracts govern the transactions, while chaincode governs the packaging and deployment of smart contracts. A chaincode can contain many smart contracts. For example, in an insurance chaincode, there can be smart contracts for claims, liability, processing, and so on. Chaincode defines the ledger’s data schema, initiates it, performs updates to ledgers (consensus-based), and responds to queries for ledger data. Chaincode also emits events, which allows other applications to subscribe to chaincode events and perform subsequent downstream functions or processes.
In Hyperledger Fabric, there is no VM like EVM (Ethereum). Chaincode is deployed on network nodes, and smart contracts run on a peer node owned by an organization, mostly written in standard languages such as Java, Node.js, and Go. Chaincode runs on a secure Docker container that's available to each blockchain instance. These containers are independent of other nodes in the network; however, these chaincodes are orchestrated by the peer nodes and act as proxies, allowing access to client applications via REST APIs or SDK.
Chaincodes are initiated for channels. An administrator can define an endorsement policy for a chaincode for a given channel. This ensures that all the smart contracts, which are packaged in the chaincode, are available for that channel. Because of this, a chaincode might follow different endorsement policies on different channels based on the endorsement policy that's been configured for that channel. Smart contracts can communicate with other smart contracts on the same channel or other channels.
dApps: dApps is a distributed application that runs on top of a distributed technology like Blockchain, such as Ethereum, Bitcoin, or Hyperledger Fabric. It's a decentralized application that leverages smart contracts or chaincode. dApps can be considered a web application that interacts with the smart contract or chaincode; however, the dApps are not controlled by a single entity or an organization. Once deployed, they belong to the blockchain network. dApps are user-friendly applications, which business users can use to transact onto a blockchain network. Smart contracts allow you to connect to blockchains, whereas dApps allows you to connect to a smart contract or chaincode. For example, if you go to LinkedIn, the web page calls APIs, which gather data from a database. However, in dApps and the smart contract world, dApps are API-based web applications that connect with smart contracts, which in turn execute transactions on the ledger. A few examples of dApps are financial applications such as invoice factoring, KYC, and so on.