How to choose a smart contract development language?

Introduction to blockchain technology, which programming languages ​​are involved? In this article, I will introduce the development languages ​​used for smart contract programming in the three blockchains of Bitcoin, Hyperledger Fabric and Ethereum , and provide links to resources for your further study.

bitcoin

Bitcoin was the first real blockchain, but, strictly speaking, it was not friendly to smart contract developers.

Smart contracts can be written on the Bitcoin system using Bitcoin Script , a low-entry programming language . Each Bitcoin address corresponds to a Bitcoin Script program. It looks like this:

IF 
    2 <Alices' pubkey> <Bob's pubkey> <Escrow's pubkey> 3 CHECKMULTISIG
ELSE
    "30d" CHECKSEQUENCEVERIFY DROP
    <Alices' pubkey> CHECKSIG
ENDIF

Another higher-level language is Ivy, which compiles to Bitcoin Script. Ivy can help you write custom Bitcoin addresses that are SegWit compatible and can execute arbitrary conditions with the support of the Bitcoin protocol (including signature checking, hash eigenvalues ​​(commitment), and time locks) combination. E.g:

contract EscrowWithDeplay{
    sender: PublicKey
    recipient: PublicKey,
    escrow: PublicKey,
    delay: Duration,
    val: Value
}{
    clause transfer(sig1: Signature, sig2: Signature){
        verify checkMultiSig([sender, recipient, escrow],[sig1, sig2])
        unlock val
    }
    clause timeout(sig: Signature){
        verify checkSig(sender, sig)
        verify older(delay)
        unlock val
    }
}

Ivy's github address: https://github.com/ivy-lang/ivy-bitcoin

The Bitcoin "virtual machine" - the part of the protocol responsible for executing the Bitcoin Script program - is more limited than the virtual machines of other smart contract platforms such as Ethereum or Chain Protocol, and its instruction system is not even Turing-complete . But Bitcoin Script does provide a useful set of basic primitives—signature verification, hashing, and relative and absolute timelocks—plus a free combination of these primitives.

Hyperledger fabric

Fabric is the most mature blockchain project in the Hyperledger family. It is mainly used in industry chains, alliances or private chains. It does not require mining to form consensus, so it can achieve high transaction speed.

In fabric, smart contracts are called chaincodes, which are essentially business logic that controls how different entities or related parties in the blockchain network interact or transact with each other. In short, chaincode encapsulates business network transactions in code. Chaincode can be called to set and get the ledger or world state.

Hyperledger can use go, java or nodejs to develop smart contracts, but the best supported language is go. Here is a simple fabric smart contract developed using go:

package main
 
import "fmt"
import "github.com/hyperledger/fabric/core/chaincode/shim"
 
type SampleChaincode struct {
}
 
func (t *SampleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
    return nil, nil
}
 
func (t *SampleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
    return nil, nil
}
 
func (t *SampleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
    return nil, nil
}
 
func main() {
    err := shim.Start(new(SampleChaincode))
    if err != nil {
        fmt.Println("Could not start SampleChaincode")
    } else {
        fmt.Println("SampleChaincode successfully started")
    }
 
}

Frabric's smart contract can be implemented using a class in go, which must implement the agreed interface Init and Query.

The Init method is called when the chaincode is first deployed to the blockchain network and will be executed by each peer node that deploys its own instance of chaincode. And whenever any read/get/query operation is performed on the blockchain state, the Query method is called.

Visit here to learn more about smart contract development with fabric: Fabric Chaincode

Ethereum

Ethereum is the first blockchain to provide a complete framework for smart contract development, so it is also known as the representative of blockchain 2.0. In fact, the vast majority of current blockchain applications, including ICO token issuance, are smart contract applications based on Ethereum.

Ethereum has four specialized languages ​​that can be used to develop smart contracts:

  • Solidity, inspired by JavaScript
  • Serpent, inspired by Python
  • Mutan, Uke Go Heuristic
  • LLL Inspired by Lisp

These four languages ​​are all languages ​​designed from the bottom for contract-oriented programming, but from the current development point of view, Solidity has become the well-deserved preferred language for Ethereum smart contract development.

Solidity's syntax is similar to JavaScript, which lowers the learning curve and is easy to master and use, as JavaScript is a common language for web developers. For example, here is a simple but complete smart contract developed using Solidity:

pragma solidity ^0.4.21;

contract HelloWorld {
    string hello = "Hello World!!!";
    event say(string _value);
    
    function sayHello() public {
        emit say(hello);
    }   
}

The first line of the contract code specifies that the Solidity version used by the contract is 0.4.21, which does not support Solidity features higher than 0.4.21.

In Solidity, the code segment contained in the contract keyword represents a smart contract, which has some member variables and functions, and looks very similar to a class in traditional object-oriented development.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325929120&siteId=291194637