Blockchain Development (3) Write and debug the first Ethereum smart contract

Li He September 10, 2016

1. Introduction to Smart Contract IDE

    Currently, Ethereum supports three languages ​​to write smart contracts.

    Solidity : Similar to JavaScript, this is the official recommended language of Ethereum and the most popular smart contract language. For specific usage, please refer to Solidity documentation, address: https://solidity.readthedocs.io/en/latest/

    Serpent : Similar to Python style, document address: https://github.com/ethereum/wiki/wiki/Serpent

    LLL : Lisp-like, but now discontinued.

    You can choose different high-level languages ​​according to different habits, the most popular one is Solidity. All smart contracts in this article are written in Solidity language.

    There are several common IDEs that can write smart contracts:

    Mix : It is the main development IDE of Ethereum in the early days, which can support the writing, debugging, deployment of smart contracts and DAPPs, and a full graphical interface. However, with the departure of the original host Gavin Wood, it was gradually marginalized and finally stopped development. The entire team turned to the Remix project, and learning Mix is ​​not recommended for future consideration.

    Remix : It is a new work of the original Mix team. At present, only the simple Debug function is online. You can focus on it in the future.

    browser-solidity : This project is a development environment for the browser version of smart contracts, which can support direct development, debugging and compilation in the browser. For beginners, you can get started quickly without installation, which is very convenient and can be used directly by accessing the address. : https://ethereum.github.io/browser-solidity/ , this article uses this IDE for development.

    Ethereum Studio : An online IDE for enterprise smart contracts developed by third-party companies. It is powerful and free to use. It can be used as a tool for enterprise-level development. Access address: https://live.ether.camp/

    Visual Studio 2015 : Yes, it is Microsoft's VS 2015. Microsoft has integrated the smart contract writing function of Ethereum. It can be seen that Microsoft attaches great importance to Ethereum.

2. Write the first smart contract

1. Smart contract grammar learning method

    The syntax and examples of smart contracts can be viewed on Solidity's documentation website http://solidity.readthedocs.io/en/latest/ . Basically, after reading these online documents, you are already proficient, and the rest is just practice writing code.

2. Sample contract code

    First, we give an example code, which will be used as an example to explain the writing and debugging of smart contracts.

-------------------------------------------------------------------------------------------

contract Votelihe { 

   struct Candidate {

       uint votecount;

       string name;

   }

   struct Voter {

       bool voted;

   }

   mapping(address => Voter) public voters;

   Candidate[] public candidates;   

   function Votelihe() {

       candidates.push(Candidate({

                name: "lihe",

                votecount: 0

           }));

       candidates.push(Candidate({

                name: "dandan",

                votecount: 0

           }));

      }   

   function Vote_candidate(uint8 numCandidate)

   {

       if(voters[msg.sender].voted ||numCandidate>candidates.length)return;

       candidates[numCandidate].votecount+=1;

       voters[msg.sender].voted=true;

   }   

   function Getcount() returns(string,uint,string,uint){

       return(candidates[0].name,candidates[0].votecount,candidates[1].name,candidates[1].votecount);

   }

}

--------------------------------------------------------------------------------------------------

    该代码创建了一个投票程序,对两个候选人lihe和dandan进行投票,每个人只有一次投票的机会,最后反馈lihe和dandan的得票结果。各个函数说明如下:

function Votelihe():构造函数,智能合约只运行一次

function Vote_candidate():对候选人进行投票,每个投票者只能投一票

function Getcount():返回当前候选的得票数

3、           使用IDE编写智能合约

    首先我们打开browser-solidity,IDE的主要功能如下:

    将示例代码拷贝到左侧的代码编辑框,IDE将自动检测语法错误,并显示在右侧的窗口上,如下图所示:


    可以看到,提示有未声明的对象,是在14行的错误,很明显是我一个结构对象candidates误写为candidates2了,修改一下即可校验通过。

    注意,在浏览器里编写代码,他是自动保存在本地浏览器缓存里面的,只要清除浏览器缓存,代码不会丢失。 

三、调试第一个智能合约

    目前browser-solidity有两种常用的调试方式,一个是采用本地虚拟机调试模式,一个是连接到本地的私有链进行调试。

1、           本地虚拟机调试模式

    本地虚拟机调试,就是不连接任何一个节点,在内存虚拟出一个以太坊节点进行调试,优点是速度快,配置简单,缺点是因为只是虚拟调试,可能最后放到真正的区块链节点上运行智能合约会和预想的结果不同。

    首先在DEBUG环境设置中,选择JavaScript VM以设置本地虚拟调试模式,如下图:

    设置成功后,可以在账号状态栏看到可以用的账户列表,如下图

    智能合约代码编写好后,点击“Create”按钮部署智能合约到内存中,并进行调试,如果部署成功,会出现智能合约的函数运行按钮和参数输入框,然后就可以调试你的智能合约了,如下图:

    运行函数后,会出现相应的交易数据,可以完成整个智能合约调试。

    如果想逐步调试智能合约,那么选择小虫子图标,切换到逐步调试界面,即可实现单步运行智能合约,注意这里的单步运行不是指代码而是指智能合约编译后的OPCODE,如下图。

2、           连接到本地私有链调试

    连接到本地私有链调试,就是通过RPC接口,连接本地的以太坊节点,实际部署并调试智能合约,缺点是速度较慢,配置复杂,优点是能够真实运行智能合约,最大程度的防止出错,关于私有链的配置,请参考我原先发表的文章《区块链开发(一)搭建基于以太坊的私有链环境》。

    首先在DEBUG环境设置中,选择Web3 Provider以设置本地虚拟调试模式,同时默认会给出一个连接地址为http://localhost:8545,如果你配置的私有链RPC端口修改了,记得要改成对应的端口,如下图:

    然后,切换到账号状态栏,此时显示的可用账号,应该都是你部署的私有链里面的账号,如果不是,说明没有成功连接私有链。可能的原因有两个,一是私有链提供的端口是用http访问,而browser-solidity的网页访问地址是https,解决的方法就是将browser-solidity访问地址改为http协议的地址即可http://ethereum.github.io/browser-solidity/;二是系统的时间没有和网络同步,使用windows系统自带的时间同步功能同步一下即可。三是如果连接到本地私有链调试时出现Error: account is locked的错误,是因为geth需要解锁本地的以太坊帐号,在geth的控制台输入personal.unlockAccount('账号地址')即可。

四、       其他常见智能合约资源

    下面一些例子网站去参考一些成熟的代码,方便快速迭代学习,常见的例子网站如下:

https://github.com/ethereum/wiki/wiki/Solidity-Collections

http://ether.fund/contracts/

https://github.com/chriseth/solidity-examples

https://github.com/ethereum/dapp-bin

https://github.com/fivedogit/solidity-baby-steps

http://dapps.ethercasts.com

http://ether.fund/contracts

    开发框架常用的有3个:

Truffle:说明书地址http://truffle.readthedocs.io/en/latest/

以太坊目前很流行的开发框架Truffle的说明书,这个框架比较流行。

Dapple:说明书地址http://dapple.readthedocs.io/en/master/

这个开发框架是在gitter chart上看到的,感觉用的人不多,先观察

Meteor:说明书地址https://github.com/ethereum/wiki/wiki/Dapp-using-Meteor

这个开发框架是以太坊官方推荐的,写进了以太坊的官方wiki,值得学习,当然,以太坊官方经常转换方向,以后换别的也没准

原文章链接。 https://blog.csdn.net/sportshark/article/details/52497176

Guess you like

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