Filter by category:

Knowledge hub home

What are Smart Contracts?

what are smart contracts

Smart contracts are tiny computer programmes stored inside a blockchain, in this case, CENNZnet. In this article we will explore:

  • what smart contracts are,
  • how smart contracts are used in DApps, and
  • the characteristics of smart contracts on CENNZnet.


What are smart contracts?

Smart contracts are small pieces of code that allow you to add additional logic to the core blockchain logic. They can be created and called by anyone on the network for a small fee. The code contains:

  • Data: stored by the contract. It can also read data from the wider blockchain.
  • A defined set of rules: when data is inputted, the smart contract (program) will automatically follow these rules and transform the data accordingly.

Smart contracts have some key characteristics:

  • They are immutable: Once created they can never be changed in any way. The only way to update a smart contract is to release a separate, new version. 
  • They are distributed: The output of the contract is validated by every node on the blockchain network.  
  • They are deterministic: The outcome of the execution of a smart contract is the same for every node that runs it because they must all follow the clearly defined rules.
  • They are atomic: Any changes a smart contract makes to the data on the blockchain (known as the state) are only recorded if the contract runs to completion – this is called successful termination, where the program executed without an error and reaches the end of execution. If execution fails, all of its effects (changes in state) are rolled back as if the transaction never ran.

Smart contracts don’t have to work in isolation, they can call upon other contracts to enact more functions or logic. There can even be a more complex chain of execution where a contract calls another contract that can call another contract, and so on.

Importantly, smart contracts can only run if they are called by a transaction, they never run on their own or in the background. In effect, they are dormant until a transaction triggers their execution, either directly or indirectly as part of a chain of contract calls. It is also important to note that smart contracts are not executed in parallel, they can connect in a chain but are run individually.

A critical security function of smart contracts is that they are charged every time they are triggered. This helps to prevent denial of service attacks and also prohibits pointless heavy code on the blockchain. This can however be a confusing concept for new users. To help ease them into the decentralised process, CENNZnet allows DApps to delegate their user’s fees. 

Smart contracts are typically written in a high-level programming language (Rust, JavaScript, C++ etc.). But in order to run on the blockchain, they must be compiled to a low-level bytecode, in our case, WebAssembly.


How do DApps use smart contracts?

Smart contracts allow DApp creators to publish additional logic and functionality on top of the existing core blockchain logic. They are the enablers of a DApps core functionality, connecting the UI to the blockchain and determining the business logic that can be enacted. Where normal apps connect to a backend system that defines some business rules and handles storage etc., for DApps this backend is replaced with a blockchain and a smart contract.

Decentralised app logic
Logic of a DApp

Each DApp on a blockchain can use one or more smart contracts to enable it to function on the chain. This smart contract will need to hold all of the DApps on-chain data and the defined set of rules that the DApp uses to transform data and complete transactions. It is VERY important that smart contracts are tested and secure before they are released onto the chain, as they essentially reveal all the key components of the DApp, it’s logic and data. This is fantastic for transparency and done correctly reinforces the trustless nature of blockchain, but if the contract exposes vulnerabilities, it can expose DApps and the blockchain to malicious attacks. 

Users will then interact with the smart contract through the DApp’s UI. It is possible for users to bypass the UI and interact directly with the smart contract, however, to do this they would need to be very familiar with the backend code and blockchain development. 


Building smart contracts on CENNZnet

CENNZnet’s smart contracts have a few specific characteristics.


  • Compile to WebAssembly: Smart contracts on CENNZnet are written in Rust (eventually they can be written in any language that compiles to WebAssembly). They are then compiled into WebAssembly which can be read by the blockchain. We chose WebAssembly as our lower level language as it is robust and accessible for most developers. We wanted to avoid creating our own specific lower-level blockchain language to avoid the occurrence of quirks that make hacks more likely. 
  • Link to protocol level runtime modules: The CENNZnet protocol includes a number of ready-built runtime modules within the state transition function. These core CENNZnet modules provide the building blocks to common functions required for DApp users. Using smart contracts on CENNZnet, developers can harness these functions rather than code their own from scratch. This not only makes the creation of smart contracts on CENNZnet faster but also makes the code leaner and more efficient.