BlockSurvey
Published in

BlockSurvey

Understanding “Clarity”- The smart contract programming language.

With increased Blockchain adoption, there are so many emerging blockchains and they have their own smart contract programming language. The most well known popular one is Solidity which runs on Ethereum blockchain. Facebook came up with its blockchain Libra, which has its programming language Move. Likewise, Clarity is the new smart contract programming language introduced by Blockstack for their very own Stacks blockchain.

Okay, so what is Blockstack?

Blockstack is a powerful platform intending to build a decentralized internet. They put users in control of their identity and data. It is a shift in how people use software, create it, and benefit from the internet. It puts people back in control of the computing systems that manage today’s world.

In this blog, lets journey together to understand the new smart contract programming language Clarity.

So, What are Smart Contracts?

A smart contract is a computer protocol intended to digitally facilitate, verify, or enforce the negotiation or performance of a contract. (as per wiki ). Smart contracts run on the blockchain technology — they are immutable and are enforced by the system itself. And because the contract is publicly verifiable — the chances of conflict are minimized.

When implemented, a smart contract can very well replace the need for an attorney or a notary to sign a document which then can be upheld in the court. These contracts execute themselves when all the clauses in the contract are met.

So, for example, if you draw up a smart contract to lease your apartment with the two conditions being rent paid and the time the key (assuming a digital key) is handed over. As soon as the renting party pays the due amount — the contract ensures the key is transferred to the tenant. The landlord cannot prevent the transfer of the key once the rent is paid — nor is the key transferred before the rent is paid. Because the contract is publicly recorded with the identities kept anonymous — neither the landlord nor the tenants can accuse of any wrongdoings. The smart contract acts as a liaison ensuring the contract conditions are always met. (Source:- https://codebrahma.com/brief-intro-smart-contracts-endless-possibilities/)

Source:- https://codebrahma.com/brief-intro-smart-contracts-endless-possibilities/

Okay, now I got what are smart contracts? So what is Clarity?

Clarity is a decidable programming language, which is designed as “non-Turing complete “ and “Not intended to compile“.

Wait, What is Non-Turing complete programming language?

To understand why Clarity is designed as non-Turing complete programming language, we should first know, what is Turing complete and why not Clarity be Turing complete? Let’s dig deeper.

First, What is Turing complete programming language?

A programming language is considered as Turing complete if it can do what a Turing machine can do.

What is a Turing machine?

Alan Turing was a mathematician who created a theoretical machine called, the Turing Machine. This mythical machine has access to an infinite amount of RAM and runs using a finite program that determines when it should write, read, and move within its memory. The machine’s programming also dictates under what conditions it should terminate and what it should do next. Programming languages that fit these conditions are known as TC (Turing complete) languages.

A language will be deemed Turing complete if it satisfies the below conditions.

  • Has the ability to implement any computable function.
  • Always includes a function that won’t terminate by itself.
  • Includes a function that, theoretically, could use an infinite amount of memory.

(Source:- https://hackernoon.com/should-smart-contracts-be-non-turing-complete-fe304203a49e)

I would recommend, watching over the videos by computerphile to understand the Turing machine functioning.

As we all know, most of the language which we are using to date are Turing complete.

What harm does a Turing complete smart contract programming language cause?

Smart contracts are powerful, but they cannot be fully entrusted due to the history of bugs reported. The most popular blockchain for smart contracts is Ethereum and its widely used smart contract language is Solidity. Solidity is a statically typed, contract-oriented, high-level language. It is a very effective language but had several issues reported, few of them listed below and one of the reasons is the fact that this is Turing complete programming language.

  1. DAO hack. (recursive call exploit)
  2. Reentrancy attacks. (caused by the poorly implemented contracts)
  3. Halting problem.
  4. Predicting the transaction fee beforehand is very difficult since static analysis cannot be performed.

What is Clarity and How is it different from existing smart contract programming language?

Clarity is Blockstack’s smart contracting language for use with the Stacks blockchain. Clarity supports programmatic control over digital assets within the Stacks blockchain (for example, BNS names, Stacks tokens, and so forth).

Unlike the Turing complete programming languages known so far, Clarity is a non-Turing complete language that intends to avoid all the above-said issues of a Turing complete programming language and also it aims to provide the users an option to do static analysis well before the code is being implemented.

Lets, see what will be the advantages of Clarity, being non-Turing complete and interpreted programming language.

Turing incompleteness enables static analysis to determine the cost of executing a given transaction. This will be helpful for the network to calculate the transaction fee well in advance and this will be conveyed to the user.

Turing incompleteness allows static analysis to find out which other contracts a particular transaction ever invoke. By analyzing this, the possible side-effects can be predicted and avoided. This improves user experience and helps the clients to gain credibility from users.

Improved, accurate, static analysis will help the developers analyze and find out possible bugs, before even implementing /executing the code, which avoids a whole lot of issues in the future and the quality of the code can be improved.

In Clarity, static analyses run before ever broadcasting the smart contract can provide information such as:

1. The cost to broadcast a given transaction as a function of input size.

2. The sets of transactions which will be able to modify any particular table. Future work could support even more advanced analysis features, such as the ability to automatically check proofs on smart contracting code.

(source:- https://blockstack.org/whitepaper.pdf)

Clarity is interpreted language, which means it’s not intended to be compiled. In Solidity, the contract has to be compiled. Before deploying smart contracts, we need to compile solidity code to Bytecode for EVM(Ethereum Virtual Machine). As per blockchain “code is law”, and rules committed to a blockchain are the source of ultimate truth. But is it exactly showing what was there in the developer’s mind? The answer to this question is not 100 %. Ultimately, bugs can be introduced during compilation as well. Also, if there is a bug in the source code, it is easy to fix it in the interpreter directly and restarting the nodes, rather than fixing it in the compiler. That’s why, in Clarity, the intermediate step of compilation is avoided and this language intends to publish the developer’s exact intention, written as a contract(source code), to be directly published/ deployed in the blockchain.

Okay, so now we got a fair idea of, why Clarity may have been designed as non-Turing complete and interpreted language. Next, let’s dig into the language deeper.

Now, What are the essential features of Clarity?

As per the Blockstack docs, the basic parts of Clarity are data-spaces and functions. Clarity is a LISP(List Processing language) and it has its own native types. Also, in Clarity, by default, all the functions are private, unless declared as public.

The basic building blocks of Clarity are “atoms” and “lists.”

The definition says,

An atom is a number or string of contiguous characters. Atoms can be native functions, user-defined functions, variables, and values that appear in a program.

A list is a sequences of atoms enclosed with () parentheses. Lists can contain other lists.

The data handling in Clarity is different. The features are listed below:

1)Each smart contract has its own data-space. Data within this data-space are stored in maps.

2)These stores relate a typed-tuple to another typed-tuple (almost like a typed key-value store).

3)As opposed to a table data structure, a map will only associate a given key with exactly one value.

4)Any smart contract may fetch data from any other smart contract maps. However, only a smart contract may directly update data within its own maps.

The simplicity of data maps allows for both a simple implementation within the VM, and easier reasoning about functions. By inspecting a given function definition, it is clear which maps will be modified and even within those maps, which keys are affected by a given invocation.

The interface of data maps ensures that the return types of map operations are fixed length, which is a requirement for static analysis of smart contracts’ runtime, costs, and other properties.

(Source:- https://blockstack.org/whitepaper.pdf)

The Clarity smart contract has the following design aspects /limitations/features :

  • The only atomic types are booleans, integers, fixed-length buffers, and principals.
  • Recursion is illegal and there is no lambda function.
  • Looping may only be performed via map, filter, or fold.
  • There is support for lists of the atomic types, however, the only variable-length lists in the language appear as function inputs; There is no support for list operations like append or join.
  • Variables are created via let binding and there is no support for mutating functions like set.
  • Defining of constants and functions are allowed for simplifying code using define statements. However, these are purely syntactic. If a definition cannot be inlined, the contract will be rejected as illegal. These definitions are also private, in that functions defined this way may only be called by other functions defined in the given smart contract.
  • Functions specified via define-public statements are public functions. Arguments to these functions must specify their types.
  • Smart contracts have the power to: 1. Call public functions from other smart contracts. These smart contracts are identified by their hash, and must already exist at the time the calling smart contract is published. This, paired with the illegality of recursion, prevents function reentrancy, which is a common attack vector in existing smart contracting platforms. 2. Own and control digital assets. Smart contracts are first-order principals just like public keys or multi-signature addresses.

(Source:- https://blockstack.org/whitepaper.pdf)

An example of Clarity smart contract:-

Here’s a Clarity smart contract side-by-side with a contract written in Solidity for Ethereum:

(source:- https://blog.blockstack.org/introducing-clarity-the-language-for-predictable-smart-contracts/)

Currently, Clarity is in the development phase and its offered as stand-alone-cli or as the software developer kit (SDK), which can be used along with DAPPs. Check out https://docs.blockstack.org/core/smart/overview.html for more details.

Conclusion:-

Smart contracts allow two parties to exchange anything of value (money, property, shares), in an automated, auditable, and secure way without the services of a middleman. Not every application requires smart contracts.

Clarity is a new smart contracting language, which runs on Stacks blockchain, created by Blockstack community which is declared as a decidable language. This language is non-Turing complete and it is not intended to be compiled.

This language is in pre-release and currently has been offered as “standalone-CLI” as well “Clarity-SDK”.

Clarity is the LISP(List processing language ) and has its own limitations and features. The building blocks of Clarity are atoms( number or string of contiguous characters) and lists(sequence of atoms enclosed with () parentheses).

Clarity is composed of 2 parts.

  1. data-spaces( Each contract is associated with data-space. Data within the data space is not stored as standard table structure instead, stored as a map and each store relates a typed -tuple to another typed-tuple. Only the associated smart contract may modify its corresponding data-space on the blockchain.
  2. functions- By default, all functions will be private, unless marked as public. Public functions can call other public functions from smart contracts.

Clarity has its set of limitations being a non-Turing complete language, such as the only atomic types are booleans, integers, fixed-length buffers, and principals. Recursion is illegal, and there is no lambda function. Looping maybe only performed via map, fold, etc. Variables can be created only via let and no support for set. Clarity has its own native types such as “principals”.

References:-

BlockstackWhitepaper.

Blockstack docs on Clarity.

Hackernoon article on “Should Smart contracts be non-Turing complete?”

Blockgeeks article on “Smart contracts.”

Smart Contracts- A brief intro

Computerphile Video on Turing completeness.

Computerphile Video on Turing machine.

Computerphile Video on Halting problem.

Decentralization reference.

About Blocksurvey

BlockSurvey is a privacy-focused platform to create surveys, polls, & forms with complete confidentiality. Through BlockSurvey, all your data is encrypted end to end and only you can see it. You own your data. It’s your digital right. There are zero trackers and we keep you as anonymous to the data collectors. Our platform utilizes Blockstack and helps in maintaining privacy and anonymity, resulting in effective surveys and polls. Try out by creating your surveys and polls with us.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store