Smart contract on Algorand: On-chain and Off-chain

In this article I will be addressing the ‘Two-Tier Architecture’ that has been recently introduced by Algorand. In addition to the Layer-1 on-chain smart contracts, off-chain Layer-2 smart contracts to include customization. Last year, with Algorand 2.0, Layer-1 Algorand Smart contracts feature was introduced providing a fast and secure path for on-chain transaction execution. Still there were scenarios, listed below, in which additional customization were required for which off-chain Layer-2 smart contracts have been introduced.


  • Bigger contracts 

  • Computationally demanding 

  • Complex smart contracts due to multiple dependencies


On-Chain and Off-chain smart contracts on Algorand


Algorand Layer-1 smart contracts have been addressed in my earlier article.  In the Algorand blockchain, new blocks are selected by a consensus committee chosen securely and at random by Algorand’s cryptographic self-selection algorithm - typical for an on-chain smart contract. The two prominent examples where for Layer-1  (on-chain) smart contract are used are as follows:

  • Atomic Swaps - in which execute all transactions or none governed by smart contracts

  • Customized token issuance - via ASA with built in standards and security at same levels as the native currency ALGO which has been provided built-in protection against inadvertently creating or discarding tokens, along with direct support for optionally freezing, clawing back, minting, and burning tokens;

Off-chain contract processing

In the case of a call to an off-chain contract, there is a parallel committee ‘contract execution committee’ to the consensus committee that executes and validates the call. Each validator on that committee executes the contract call and generates a sequence of blockchain transactions generated by the contract call. This is followed by the contract execution committee producing a signed certificate endorsing the call's effects. The simple list of effects, together with the signed certificate and other validation conditions, are then submitted to the consensus committee. Batch processing is used for efficiency by executing multiple contract calls in a batch, so they can all be endorsed with a single certificate. 

Once the consensus committee receives effects, validating conditions and signed certificates, all they do is check the certificates and validation conditions before applying the transaction effects. Unlike for on-chain contracts validators do not have to execute user-defined contract code.


Off-Chain Contract Execution

In a normal Algorand block execution, where a transaction block is selected every 5 seconds  include Algorand’s Layer-1 smart contracts without slowing down block production. To add contract calls to this there can be 2 options, adding 10-second contract calls to each block: clearly it is impossible to maintain a 5-second block time if each contract call takes an additional 10 seconds OR executing contract calls off-chain, in parallel with regular transactions, without jeopardizing the blockchain’s throughput.

The contract execution committee is chosen by Algorand’s self-selection algorithm, just like the main consensus committee. The contract execution being deterministic does not require the contract execution committee to be as large as the block consensus committee to achieve the same level of security. 

                       

 off-chain Contract call Execution in Algorand


Off-chain contracts: Opening to customization 

Layer-1 smart contracts are written in TEAL, an assembly-like stack machine language. Algorand off-chain contracts can be big, computationally demanding, idiosyncratic, and/or complex without clogging up the blockchain. Unlike most blockchains, where the smart contract architecture is intimately intertwined with the blockchain architecture, Algorand’s smart contract architecture isolates off-chain contract execution from the blockchain consensus committee. By adding flexibility to the smart contract architecture, Algorand has opened doors to multiple contract execution committees (to be demarcated by functionality of transactions they are validating), each with a different service level guarantee, and each with its own contract language (high-level language) and virtual machine. 


An off-chain contract has its own long-lived state, called contract storage which stores the privacy and security related information of the transactions apart from related dependencies for the execution of the contract, the effects of which are validated and certified by the contract execution committee validators rather than being executed directly. The call’s effects transactions are packaged into a Layer-1 “all-or-nothing” batch of transactions guaranteed to succeed or fail together. Each atomic transaction, together with its certificate and dependencies, is submitted, much like any other sequence of transactions, to the consensus committee, which checks the atomic transaction’s certificate and dependencies, and includes that atomic transaction in a future block.


Potential points to lookout for

  1. Well, off-chain contract calls are “speculative” in the sense that on-chain state, say, an account balance, might change in the interval between when a contract call is validated and when that call's effects reach the blockchain. Even so, correctness is guaranteed. The off-chain contract implementation keeps track of contract call dependencies, ensuring that the effects of a call whose dependencies have been violated will never be included on the blockchain.

  2. What if a contract call repeatedly passes contract execution committee validation but never makes it onto the chain because it's on-chain dependencies are repeatedly violated? Off-chain contracts may sometimes fail, but we expect them to succeed most of the time, because their dependencies are mostly under the user's control, and users will refrain from violating their own dependencies.


Conclusion, Insights and Interest

With the addition of the Layer-2 off-chain contract execution to be executed in parallel with Layer-1 on-chain smart contracts, Algorand has opened the platform for wider applications without compromising on the security, privacy or the blockchain transaction execution throughput.  

Whatever your specialized smart contract needs, the Algorand smart contract architecture can support a matching language and virtual machine. Sure sounds interesting and promising at the same time!