我爱编程

本地环境的智能合约编写

2018-05-06  本文已影响0人  liuzhangjie

小白星云链dapp开发之旅

基于区块链的应用与传统应用的区别

基于区块链的应用,又称为dapp,与传统app区别很大,主要在于dapp中没有中心化的服务器。dapp之于区块链,就像app之于ios/android,运行于分布式网络,网络节点调用智能合约就像app向服务器发送请求一样,不过dapp的数据被安全地存储在区块链上,一旦上链无法被更改,而app的数据被存储在数据库中,由提供服务方保管。

搭建本地环境

1.安装go环境

brew install go
vim ~/.bash_profile
设置环境变量:

<pre class="prettyprint linenums prettyprinted" data-anchor-id="0j61" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 14px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgba(102, 128, 153, 0.0470588); border: 0px solid rgba(0, 0, 0, 0.14902); background-image: none; background-attachment: scroll; box-shadow: rgba(255, 255, 255, 0.0980392) 0px 1px 2px inset, rgba(102, 128, 153, 0.0470588) 45px 0px 0px inset, rgba(102, 128, 153, 0.0470588) 0px 1px 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 0px 0px; background-repeat: repeat repeat;">

  1. export GOPATH=<gopath>
  2. export GOBIN=$GOPATH/bin
  3. export PATH=$PATH:$GOBIN

</pre>

注意1:$GOPATH最好设置在用户目录下,并且不能和GOROOT一样。
注意2:如果没有装homebrew,或者没有更新homebrew,可以下载安装homebrew,也可以去go官网下载安装包,直接安装。
注意3:设置完环境变量以后需要执行source ~/.bash_profile保存

2下载源码

执行git clone命令:

<pre class="prettyprint linenums prettyprinted" data-anchor-id="u7he" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 14px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgba(102, 128, 153, 0.0470588); border: 0px solid rgba(0, 0, 0, 0.14902); background-image: none; background-attachment: scroll; box-shadow: rgba(255, 255, 255, 0.0980392) 0px 1px 2px inset, rgba(102, 128, 153, 0.0470588) 45px 0px 0px inset, rgba(102, 128, 153, 0.0470588) 0px 1px 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 0px 0px; background-repeat: repeat repeat;">

  1. git clone -b v1.0.0 https://github.com/nebulasio/go-nebulas.git --depth=1

</pre>

这个过程会很慢,但是一定要通过这种方法下载下来,否则会影响后面编译过程。下载的源码放在$PATH/src下,需要新建目录src

3安装rocksdb依赖库

<pre class="prettyprint linenums prettyprinted" data-anchor-id="7rxa" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 14px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgba(102, 128, 153, 0.0470588); border: 0px solid rgba(0, 0, 0, 0.14902); background-image: none; background-attachment: scroll; box-shadow: rgba(255, 255, 255, 0.0980392) 0px 1px 2px inset, rgba(102, 128, 153, 0.0470588) 45px 0px 0px inset, rgba(102, 128, 153, 0.0470588) 0px 1px 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 0px 0px; background-repeat: repeat repeat;">

  1. alias gcc="gcc-4.9"
  2. alias g++="g++-4.9"
  3. alias cc="gcc-4.9"
  4. alias c++="c++-4.9"

</pre>

然后执行source ~/.zshrc保存文件

4安装go依赖库

使用brew安装dep:

<pre class="prettyprint linenums prettyprinted" data-anchor-id="lq6p" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 14px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgba(102, 128, 153, 0.0470588); border: 0px solid rgba(0, 0, 0, 0.14902); background-image: none; background-attachment: scroll; box-shadow: rgba(255, 255, 255, 0.0980392) 0px 1px 2px inset, rgba(102, 128, 153, 0.0470588) 45px 0px 0px inset, rgba(102, 128, 153, 0.0470588) 0px 1px 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 0px 0px; background-repeat: repeat repeat;">

  1. $ brew install dep
  2. $ brew upgrade dep

</pre>

切换至项目根目录安装go的依赖库:

<pre class="prettyprint linenums prettyprinted" data-anchor-id="hab2" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 14px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgba(102, 128, 153, 0.0470588); border: 0px solid rgba(0, 0, 0, 0.14902); background-image: none; background-attachment: scroll; box-shadow: rgba(255, 255, 255, 0.0980392) 0px 1px 2px inset, rgba(102, 128, 153, 0.0470588) 45px 0px 0px inset, rgba(102, 128, 153, 0.0470588) 0px 1px 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 0px 0px; background-repeat: repeat repeat;">

  1. cd <path>/go-nebulas
  2. make dep

</pre>

这里等待时间比较长,耐心等就好。

5安装v8

在项目根目录$PASH/src/github.com/nebulasio/go-nebulas直接执行:
make deploy-v8

6编译可执行文件

在项目根目录下执行:
make build
编译完成后就可以启动种子节点和普通节点,以及启动控制台。

7启动种子节点,普通节点,控制台

星云链节点可以通过执行编译后的neb可执行文件启动。节点启动需在终端执行,Neb节点包括种子节点和节点:

启动种子节点:./neb -c conf/default/config.conf
启动普通节点:./neb -c conf/example/miner.conf
进入控制台:./neb console

8控制台

控制台封装了很多有用的函数,常用的包括:sendTransaction,getTransactionReceipt,getAccountState等等,可以通过api.+tab键,admin.+tab键查看。
介绍下常用函数:

注1:from和to相同时,用于部署合约,不同时用于发送交易;
注2:发送转账交易时,value为转账金额,在部署和调用智能合约时,v#####alue最好一直保持为0,否则自动转账到to账户;
注3:nonce严格自增1,在使用这个函数之前,先使用api.getAccountState()查看账户的信息,把返回结果的nonce加1后填入。
注4:contract在发送交易时为空(不写),

在部署合约时,contract包括以下几个字段:

<pre class="prettyprint linenums prettyprinted" data-anchor-id="2c4e" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 14px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgba(102, 128, 153, 0.0470588); border: 0px solid rgba(0, 0, 0, 0.14902); background-image: none; background-attachment: scroll; box-shadow: rgba(255, 255, 255, 0.0980392) 0px 1px 2px inset, rgba(102, 128, 153, 0.0470588) 45px 0px 0px inset, rgba(102, 128, 153, 0.0470588) 0px 1px 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 0px 0px; background-repeat: repeat repeat;">

  1. "result": {
  2. "chainId": 100,
  3. "contract_address": "",
  4. "data": "eyJGdW5jdGlvbiI6ImdhbWUiLCJBcmdzIjoiW1wiOFwiLFwiMTB cIl0ifQ==",
  5. "from": "n1cYKNHTeVW9v1NQRWuhZZn9ETbqAYozckh",
  6. "gas_limit": "2000000",
  7. "gas_price": "1000000",
  8. "gas_used": "23729",
  9. "hash": "a807f627e5d0ef520daf9855346f82479a073dae6a0932bd6d3 646da8c6d913c",
  10. "nonce": "56",
  11. "status": 1,
  12. "timestamp": "1524734919",
  13. "to": "n1iFiFDnJdMDHAswsM5XkpymNJhv3hTBaC7",
  14. "type": "call",
  15. "value": "50"
  16. }

</pre>

status字段:0,1,2三种状态,对应错误,成功,等待。

<pre class="prettyprint linenums prettyprinted" data-anchor-id="i5mk" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 14px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgba(102, 128, 153, 0.0470588); border: 0px solid rgba(0, 0, 0, 0.14902); background-image: none; background-attachment: scroll; box-shadow: rgba(255, 255, 255, 0.0980392) 0px 1px 2px inset, rgba(102, 128, 153, 0.0470588) 45px 0px 0px inset, rgba(102, 128, 153, 0.0470588) 0px 1px 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 0px 0px; background-repeat: repeat repeat;">

  1. "result": {
  2. "balance": "4999999999998755939999508",
  3. "nonce": "55",
  4. "type": 87
  5. }

</pre>

注:nonce严格自增1

编写部署智能合约

1部署合约前的了解工作

<pre class="prettyprint linenums prettyprinted" data-anchor-id="i7s5" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 14px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgba(102, 128, 153, 0.0470588); border: 0px solid rgba(0, 0, 0, 0.14902); background-image: none; background-attachment: scroll; box-shadow: rgba(255, 255, 255, 0.0980392) 0px 1px 2px inset, rgba(102, 128, 153, 0.0470588) 45px 0px 0px inset, rgba(102, 128, 153, 0.0470588) 0px 1px 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 0px 0px; background-repeat: repeat repeat;">

  1. LocalContractStorage.defineMapProperty(this, "bankVault", {
  2. parse: function (text) {
  3. return new DepositeContent(text);
  4. },
  5. stringify: function (o) {
  6. return o.toString();
  7. }
  8. });
  9. LocalContractStorage.defineProperty(this, "banker");
  10. LocalContractStorage.defineProperty(this, "gameVault");

</pre>

bankVault为map属性,banker,gameVoult为非map属性,map属性可以进行读写和遍历操作,方法详见wiki。非map属性直接读写。

<pre class="prettyprint linenums prettyprinted" data-anchor-id="urmb" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 14px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgba(102, 128, 153, 0.0470588); border: 0px solid rgba(0, 0, 0, 0.14902); background-image: none; background-attachment: scroll; box-shadow: rgba(255, 255, 255, 0.0980392) 0px 1px 2px inset, rgba(102, 128, 153, 0.0470588) 45px 0px 0px inset, rgba(102, 128, 153, 0.0470588) 0px 1px 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 0px 0px; background-repeat: repeat repeat;">

  1. this.banker = from;//非map属性写
  2. this.bankVault.put(from, depositTemp);//map写
  3. var cur_deposit = this.bankVault.get(from);//map读
  4. //map遍历
  5. forEach: function(limit, offset){
  6. limit = parseInt(limit);
  7. offset = parseInt(offset);
  8. if(offset>this.size){
  9. throw new Error("offset is not valid");
  10. }
  11. var number = offset+limit;
  12. if(number > this.size){
  13. number = this.size;
  14. }
  15. var result = "";
  16. for(var i=offset;i<number;i++){
  17. var key = this.arrayMap.get(i);
  18. var object = this.dataMap.get(key);
  19. result += "index:"+i+" key:"+ key + " value:" +object+"_";
  20. }
  21. return result;
  22. }

</pre>

<pre class="prettyprint linenums prettyprinted" data-anchor-id="nae0" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 14px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgba(102, 128, 153, 0.0470588); border: 0px solid rgba(0, 0, 0, 0.14902); background-image: none; background-attachment: scroll; box-shadow: rgba(255, 255, 255, 0.0980392) 0px 1px 2px inset, rgba(102, 128, 153, 0.0470588) 45px 0px 0px inset, rgba(102, 128, 153, 0.0470588) 0px 1px 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 0px 0px; background-repeat: repeat repeat;">

  1. if(parseInt(subscript)<14&&parseInt(subscript)>0){
  2. porkPoint = arr[parseInt(subscript)-1];
  3. }else{
  4. throw new Error("No sufficient subscript.");
  5. }

</pre>

<pre class="prettyprint linenums prettyprinted" data-anchor-id="s81s" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 14px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgba(102, 128, 153, 0.0470588); border: 0px solid rgba(0, 0, 0, 0.14902); background-image: none; background-attachment: scroll; box-shadow: rgba(255, 255, 255, 0.0980392) 0px 1px 2px inset, rgba(102, 128, 153, 0.0470588) 45px 0px 0px inset, rgba(102, 128, 153, 0.0470588) 0px 1px 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 0px 0px; background-repeat: repeat repeat;">

  1. bonus = bonus.plus(depositT.bets);//加法

</pre>

2编写部署智能合约

2.1 prototyppe

2.2code style

智能合约一般采用js编写,具有js代码的特点,需要注意类型关键字的使用,除此以外,智能合约代码编写应该保持功能单一原则,注意数据结构的变换。

<pre class="prettyprint linenums prettyprinted" data-anchor-id="v845" style="padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 14px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgba(102, 128, 153, 0.0470588); border: 0px solid rgba(0, 0, 0, 0.14902); background-image: none; background-attachment: scroll; box-shadow: rgba(255, 255, 255, 0.0980392) 0px 1px 2px inset, rgba(102, 128, 153, 0.0470588) 45px 0px 0px inset, rgba(102, 128, 153, 0.0470588) 0px 1px 0px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 0px 0px; background-repeat: repeat repeat;">

  1. 'use strict';

  2. var DepositeContent = function (text) {

  3. if (text) {

  4. var o = JSON.parse(text);

  5. this.balance = new BigNumber(o.balance);

  6. this.expiryHeight = new BigNumber(o.expiryHeight);

  7. } else {

  8. this.balance = new BigNumber(0);

  9. this.expiryHeight = new BigNumber(0);

  10. }

  11. };

  12. DepositeContent.prototype = {

  13. toString: function () {

  14. return JSON.stringify(this);

  15. }

  16. };

  17. var BankVaultContract = function () {

  18. LocalContractStorage.defineMapProperty(this, "bankVault", {

  19. parse: function (text) {

  20. return new DepositeContent(text);

  21. },

  22. stringify: function (o) {

  23. return o.toString();

  24. }

  25. });

  26. };

  27. }

  28. module.exports = BankVaultContract;

</pre>

注1:DepositContent在存储时,会把object转成字符串类型,在读取时,会取出相应字符串转成object。
注2:智能合约开始结尾的使用能节约时间,提高效率。

2.3tips

智能合约开发的特点

上一篇下一篇

猜你喜欢

热点阅读