A network of nodes that synchronize over the latest state of a ledger and validate it through cryptography
Let’s refresh our blockchain knowledge and see what we covered in the previous parts.
The blockchain is a network of computer-accountants called nodes. The nodes keep track of identical copies of a ledger. The ledger consists of transactions bundled in blocks. The blocks are linked together forming a chain – blockchain. The transactions are cryptographically signed by the person spending some of their balance. “People” in the ledger are identified by their public key (known as address), while transactions are signed via their private key. The public-private keys pair is known as a wallet.
There is no central node in the network and all the nodes are equal. They all relеy to each other information about the transactions happening and synchronize their copies of the ledger with each other. This communication process is called gossiping.
The goal of the network is to reach an agreement about the latest valid version of the ledger and synchronize their own copies in accordance with it. The nodes are given economic incentives to act correctly and guard the network, while also being given economical punishments for trying to fraud/cheat the system. This balance between incentives and punishments is known as crypto-economics.
The process of reaching consensus is known as “consensus algorithm”. The different consensus algorithms have different crypto-economic incentives and punishments. In the previous article, we outlined one of the cryptocurrency types – coins – given for correct behavior. In this article, we will showcase one of the most powerful tools of blockchain – smart contracts – and give the foundation to talk about tokens in the next articles.
What do you imagine when you hear the words “smart contracts”? Normally people imagine two people signing a contract of some kind that is in a way “smarter”.
Quite possibly, the term “Smart contracts” is the most misleading term in the blockchain sphere. They have nothing to do with signing contracts at all. The more appropriate name for these blockchain objects is “automatically executable rules”.
These blockchain contracts are first-class citizens of the blockchain networks, much like you with your wallet. The smart contracts do not have a keypair but they still have an address. This address can be used to send to them coins or to trigger some of the rules inside.
How does one trigger a rule of a smart contract?
The smart contract rules are triggered by transactions sent to them. Remember how Alice can send Bob coins via a transaction to Bob’s address. Similarly, Alice can send coins to a certain smart contract address, and/or Alice can send pieces of data to trigger a certain rule in the smart contract. The smart contract automatically executes the logic of the triggered rule and produces the result. If anything fails or is not correct according to the rules logic, the whole transaction is rolled back.
What are these rules and how did they come to be?
The rules in the contracts follow the “if this then that” pattern and are written by specialized software developers. Once a rule is written into a smart contract, it can never be changed. This gives the assurances to the people interacting with the smart contracts, that whatever the rules were in the beginning, will be the same forever. Interestingly enough, smart contracts can trigger the rules of other smart contracts and thus form a complex system of smart contracts together.
Puzzle piece 22: Smart contracts – set of automatically executable rules living on the blockchain, written by developers and used by the people or other smart contracts.
This is a bit too abstract for me, can you give an example
Let’s start with something simple – mother giving her two children allowance by sending them money on the blockchain. A smart contract can be created that has a rule that says when it receives any coins, it splits them equally between the two children. The mother, by sending the coins to the smart contract will trigger this rule. The rule will split the sent coins and send each child its half.
This way the children will know that they have always been given their fair share.
More about the smart contract rules
There are two types of rules that can be written into smart contracts – the default rule and named rules. The default rule is a rule that governs what happens when the smart contract receives money. This was the rule that was encoded in the smart contract described above.
Named rules are specific rules that need to be referenced by name in order to be triggered. The same example as above could be achieved by the developer creating a rule called “split” that splits the received money. Named rules are triggered by sending their identifier (think about it as their name) as data, alongside the coins of the transaction. Interestingly enough, one can trigger rules without actually sending any coins alongside the transaction.
Unlike wallets and people, smart wallets have data storage capacity. You can use rules to store, modify or delete information from the blockchain. Let’s look at a bit more complex example to see the power of the smart contracts in blockchain.
Multiple people owning one “bank” account
Imagine a scenario where you and your two best friends, decide to put together some money into a trust and only be able to spend this money if the majority of you (2 out of 3) agree with the spending. In order for you to be sure that everyone is going to follow the rules, you decide to put this on the blockchain – where the rules are set and cannot be changed or cheated. You ask your developer friend to create you a smart contract that will govern this scenario. Here is what the developer will encode as rules.
Rule 1 – Spending proposal rule
The developer will create one named rule and will call it “propose”. This rule will also expect a couple of pieces of data to be supplied when being triggered – how much coins you want to spend, and where do you want to send (read spend) them. She will also write a logic that says that it is only one of the three of you that can trigger this rule (and not someone else outside of this group). Once the rule is triggered, the two pieces of data are stored in the smart contract memory.
The money is not yet sent away, as only 1 (you) of the 3 account owners have authorized this spending.
Rule 2 – Authorise spending
The developer will create one more named rule and will call it “execute”. This rule will be used by one of the other owners to approve a certain proposal made by the previous triggering of the “propose” rules. Once the second owner triggers the execute rule, the smart contract will trigger the logic, and send the proposed coins to the address that the proposer specified.
We have the smart contracts blockchain rules, now what?
The developer will take these rules, bundle them in code, and put them on the blockchain where they will live under certain address – the address of the smart contract. You can now safely send your coins to this address and only ever spend them by following the “propose-execute” scheme.
In the example above we described it working with 2 out of 3 owners. Different configurations can be encoded by the developer – we can decide that spending goes through only when 3 out of 3 owners agree that spending is made. Or even just 1 out of 3 (but this does not make too much sense).
The smart contracts in blockchain and their rules are the playground for the business logic that you would want to create and the developers are the ones putting this logic in code and putting it on the blockchain. It is up to your imagination what kinds of rules you would want to encode and put there.
Note on naming
When wondering around the blockchain world you will frequently hear the words “smart contract method” or “smart contract function” these are the more sophisticated names for the smart contracts rules.
Next – Token Smart Contracts
Now that we’ve learned what are smart contracts and smart wallets, we have all the knowledge that is needed in order to talk about the second type of cryptocurrencies – the tokens. The tokens are based on blockchain smart contracts and have specific rules, and in the next article, we will see what these rules are and how they emulate the movement of money.