Understanding Smart Contracts

[ Huixin Cloud ] Regularly update articles for everyone, [ Huixin Cloud ] IT software and hardware project business platform


0x00 Preface

Understanding smart contracts is critical to understanding blockchain technology. Let's first look at what a smart contract is.

Smart contracts are an idea put forward by Nick Szabo in the 1990s, almost the same age as the Internet. Due to the lack of a credible execution environment, smart contracts have not been applied to the actual industry. Since the birth of Bitcoin, people have realized that the underlying technology blockchain of Bitcoin can inherently provide a credible execution environment for smart contracts. Ethereum First saw the fit of blockchain and smart contracts, released the white paper "Ethereum: The Next Generation of Smart Contracts and Decentralized Application Platforms", and has been committed to making Ethereum the best smart contract platform, so Bitcoin leads the way Blockchain, Ethereum resurrects smart contracts. [1]

That is to say, the concept of smart contracts was not born with blockchain technology. Blockchain technology naturally brings unique application prerequisites to smart contracts. If we talk about the application of blockchain, Bitcoin and Ethereum are the biggest applications.


题图来自: © Bitcoin Guide / Everything you need to know about the Ethereum Hard Fork that will take place tomorrow / guiadobitcoin.com.br

To understand what is the concept of smart contract, let's see how to understand it next.

0x01 Smart Contract

A smart contract program is not just a computer program that executes automatically: it is itself a system participant. It responds to the information it receives, it can receive and store value, and it can send information and value out.

This program is like a person who can be trusted to temporarily keep assets and always act according to the rules in advance.

The following diagram is a smart contract model: a piece of code (smart contract), deployed on a shared, replicated ledger, that maintains its own state, controls its own assets, and responds to incoming external information or assets.


Smart contract model: It is a computer program running on a replicable, shared ledger that can process information, receive, store and send value.

简单地说,智能合约就是传统合约的数字化版本。它们是在区块链数据库上运行的计算机程序,可以在满足其源代码中写入的条件时自行执行。智能合约一旦编写好就可以被用户信赖,合约条款不能被改变,因此合约是不可更改的。

现实世界是怎么样应用智能合约的呢?开发人员会为智能合约撰写代码。智能合约可用于交易和(或)两方/多方之间的任何交换行为。该代码包含一些会触发合约自动执行的条件。一旦编码完成,智能合约就会被上传到区块链网络上,即它们被发送到所有连接到网络的设备上。一旦将数据上传到所有设备上,用户就可以与执行程序代码的结果达成协议。然后更新数据库以记录合约的执行情况,并监督合约的条款以检查合规性。[2]

0x02 Solidity

Solidity 是一种语法类似 JavaScript 的智能合约高级语言,它被设计成以编译的方式生成 以太坊虚拟机(Ethereum Virtual Machine,EVM) 代码。

它的语法接近于 Javascript,是一种面向对象的语言。但作为一种真正意义上运行在网络上的去中心合约,它又有很多的不同,下面列举一些:

以太坊底层是基于帐户,而非 UTXO 的,所以有一个特殊的Address的类型。用于定位用户,定位合约,定位合约的代码(合约本身也是一个帐户)。

由于语言内嵌框架是支持支付的,所以提供了一些关键字,如payable,可以在语言层面直接支持支付。

存储是使用网络上的区块链,数据的每一个状态都可以永久存储,所以需要确定变量使用内存,还是区块链。

运行环境是在去中心化的网络上,会比较强调合约或函数执行的调用的方式。因为原来一个简单的函数调用变为了一个网络上的节点中的代码执行。

最后一个非常大的不同则是它的异常机制,一旦出现异常,所有的执行都将会被回撤,这主要是为了保证合约执行的原子性,以避免中间状态出现的数据不一致。[3]

0x03 DAPP

去中心化应用(DAPP)是可以使用户和供应商之间直接互动的服务。以太坊去中心化应用典型地通过 HTML/Javascript 网络应用与用户互动,使用 Javascript API 与区块链通信。去中心化应用典型地在区块链上有自己的相关合约套件,用来编码商业逻辑,允许持久存储对共识要求严格的状态。

DAPP 和 APP 是对应的。APP 我们已经很熟悉了,目前我们主要在 iOS 和 Android 平台使用各种类型的 APP。通常一个 APP 从设计、开发、测试、上线等过程都是由一个中心化的组织完成的,APP 的应用也是基于 C/S 架构,其中的 S 表示 Server,也是中心化结构。而 DAPP 不完全是指它的开发是去中心化的,而是它的运行是去中心化的。

目前最为成功的 DAPP 是 CryptoKitties,也就是那个撸猫游戏。这个游戏上线一度造成以太坊网络严重堵塞。

根据 State of the DAPPS 网站统计,目前基于以太坊的 DAPP 有 977 个。感兴趣的读者不妨去搜索看看。

0x04 实践

如果要学习智能合约编程,以下事项需要读者注意:

学习智能合约之前,需要搞清楚区块链、比特币和以太坊的相关知识。

细读 ERC20 协议

学习 JavaScript、Solidity 编程语言。

熟悉周边工具,如 Truffle、Remix、Metamask 等。

制定开发目标,实现一些小应用。

阅读知名基于 ERC20 区块链项目代码。

比如在第五步,你可以自行在以太坊测试网络或者正式网络发行 Token,还可以基于开源的 Pet Shop 实现第一个 DAPP。

0x05 相关资源

5.1 Terminal

Mist

MetaMask

MyEtherWallet

5.2 IDE

Remix-Solidity IDE

如果你使用 Sublime Text 进行 Solidity 编程,推荐使用以下插件:

Solium Gutter

Ethereum

EthereumSoliditySnippets

如果你使用 Atom 进行 Solidity 编程,推荐使用以下插件:

linter-solidity

autocomplete-solidity

linter-solium

5.3 Frameworks/Tools

JSON RPC API

Truffle development framework

Ethereum JavaScript API

Solidity GitHub

Open-zeppelin

truffle-artifactor

5.4 Docs

Ethereum 官网

Ethereum GitHub

Solidity 官方文档

embark’s documentation

《SOLIDITY 智能合约编程指南》

以太坊智能合约快速入门

The Ethereum Wiki

Dapp Developer Resources

EthFans 知识库

如何编写智能合约

汪晓明对区块链、以太坊的思考

5.5 Forum

web3-js 论坛

Ethereum for web developers

以太坊中文技术社区

台北以太坊社群專欄

区块链技术中文社区

developerWorks 社区

Story Backlog)

Gitter Chat

0x06 小结

本文介绍了智能合约,智能合约程序不只是一个可以自动执行的计算机程序:它自己就是一个系统参与者。它对接收到的信息进行回应,它可以接收和储存价值,也可以向外发送信息和价值。接着讲解了智能合约使用的编程语言 Solidity,接着介绍了 DAPP,然后给出了学习智能编程的步骤,最后给出了相关的资源,希望可以帮到读者。

未来会有更多的 DAPP 进入我们的生活,如果读者是个开发者,不妨找个场景实现个 DAPP。

0x07 参考

[1] GitHub (2016-07-07). 智能合约. Retrieved from https://github.com/EthFans/wiki/wiki/智能合约.

[2] 知乎 (2017-07-05). 为什么要用区块链技术实现智能合约?. Retrieved from https://www.zhihu.com/question/43404157/answer/193952393.

[3] 简书 (2016-12-18). Solidity 语言介绍. Retrieved from https://www.jianshu.com/p/b9f78f9b4c3e.


学好区块链,单笔项目百万+不是梦:

区块链产品经理(点击入驻),能力不够也来混个脸熟

Guess you like

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