本地环境的智能合约编写
小白星云链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;">
export GOPATH=<gopath>
export GOBIN=$GOPATH/bin
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;">
git clone -b v1.0.0 https://github.com/nebulasio/go-nebulas.git --depth=1
</pre>
这个过程会很慢,但是一定要通过这种方法下载下来,否则会影响后面编译过程。下载的源码放在$PATH/src下,需要新建目录src
3安装rocksdb依赖库
- 首先要安装支持 C++11 的 C++ 编译器,需要安装gcc4.8以上版本,执行:
brew install gcc@4.9
vim ~/.zshrc
设置环境变量:
<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;">
alias gcc="gcc-4.9"
alias g++="g++-4.9"
alias cc="gcc-4.9"
alias c++="c++-4.9"
</pre>
然后执行source ~/.zshrc保存文件
- 安装 rocksdb:
brew install rocksdb
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;">
$ brew install dep
$ 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;">
cd <path>/go-nebulas
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键查看。
介绍下常用函数:
- sendTransation(from, to, value, nonce, gasPrice, gasLimit, contract),发送交易和,部署合约,执行合约,返回结果为哈希和合约地址
from: 用户钱包地址
to: 目标账户地址
value: 调用智能合约用于转账的金额
nonce: from用户transaction标识,严格递增1
value:部署合约时为"0",转账时为转账金额;
gasPrice:部署智能合约用到的gasPrice,可以通过GetGasPrice
获取,或者使用默认值:"1000000";
gasLimit: 部署合约的gasLimit,也可以设置一个较大值"2000000",执行时以实际使用计算。
contract: 合约信息,部署合约时传入的参数
注1:from和to相同时,用于部署合约,不同时用于发送交易;
注2:发送转账交易时,value为转账金额,在部署和调用智能合约时,v#####alue最好一直保持为0,否则自动转账到to账户;
注3:nonce严格自增1,在使用这个函数之前,先使用api.getAccountState()查看账户的信息,把返回结果的nonce加1后填入。
注4:contract在发送交易时为空(不写),
在部署合约时,contract包括以下几个字段:
-
source
: 合约代码 -
sourceType
: 合约代码类型,支持js
和ts
(对应javaScript和typeScript代码) -
args
: 合约初始化方法参数,无参数为空字符串,有参数时为字符串数组,形如["abc","eee"]
在调用合约时,contract包括: -
function
: 调用合约方法 -
args
: 合约方法参数,无参数为空字符串,有参数时为字符串数组
-
getUnlockAccount(),参数为账户地址
如果遇到报错信息是 account is locked ,使用这个函数解锁账户。 -
getTransactionReceipt(),参数为交易哈希,返回结果是一个json字符串,
<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;">
"result": {
"chainId": 100,
"contract_address": "",
"data": "eyJGdW5jdGlvbiI6ImdhbWUiLCJBcmdzIjoiW1wiOFwiLFwiMTB cIl0ifQ==",
"from": "n1cYKNHTeVW9v1NQRWuhZZn9ETbqAYozckh",
"gas_limit": "2000000",
"gas_price": "1000000",
"gas_used": "23729",
"hash": "a807f627e5d0ef520daf9855346f82479a073dae6a0932bd6d3 646da8c6d913c",
"nonce": "56",
"status": 1,
"timestamp": "1524734919",
"to": "n1iFiFDnJdMDHAswsM5XkpymNJhv3hTBaC7",
"type": "call",
"value": "50"
}
</pre>
status字段:0,1,2三种状态,对应错误,成功,等待。
- getAccountState(),参数是账户地址,
<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;">
"result": {
"balance": "4999999999998755939999508",
"nonce": "55",
"type": 87
}
</pre>
注:nonce严格自增1
编写部署智能合约
1部署合约前的了解工作
- 存储属性,分为map属性和非map属性
<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;">
LocalContractStorage.defineMapProperty(this, "bankVault", {
parse: function (text) {
return new DepositeContent(text);
},
stringify: function (o) {
return o.toString();
}
});
LocalContractStorage.defineProperty(this, "banker");
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;">
this.banker = from;//非map属性写
this.bankVault.put(from, depositTemp);//map写
var cur_deposit = this.bankVault.get(from);//map读
//map遍历
forEach: function(limit, offset){
limit = parseInt(limit);
offset = parseInt(offset);
if(offset>this.size){
throw new Error("offset is not valid");
}
var number = offset+limit;
if(number > this.size){
number = this.size;
}
var result = "";
for(var i=offset;i<number;i++){
var key = this.arrayMap.get(i);
var object = this.dataMap.get(key);
result += "index:"+i+" key:"+ key + " value:" +object+"_";
}
return result;
}
</pre>
- 方法,分为公有和私有方法,私有方法以“_"开头,用户不能直接调用执行私有方法。方法的参数读取来自于args(见前文args[]介绍),此处参数类型为字符串,必要时进行类型转换。
<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;">
if(parseInt(subscript)<14&&parseInt(subscript)>0){
porkPoint = arr[parseInt(subscript)-1];
}else{
throw new Error("No sufficient subscript.");
}
</pre>
- BigNumber,这是一个智能合约里面定义的数据类型,它的四则运算都是有定义的,用的时候不能把它当做普通整型浮点型进行运算,否则会产生错误。
详见BigNumberAPI。
<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;">
bonus = bonus.plus(depositT.bets);//加法
</pre>
2编写部署智能合约
2.1 prototyppe
- 除了null以外,javascript的每个对象都是继承于prototype对象,原型对象上的所有属性和方法,都能被派生对象共享。
- 原型对象的属性不是实例对象自身的属性。只要修改原型对象,变动就立刻会体现在所有实例对象上。
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;">
-
'use strict';
-
var DepositeContent = function (text) {
-
if (text) {
-
var o = JSON.parse(text);
-
this.balance = new BigNumber(o.balance);
-
this.expiryHeight = new BigNumber(o.expiryHeight);
-
} else {
-
this.balance = new BigNumber(0);
-
this.expiryHeight = new BigNumber(0);
-
}
-
};
-
DepositeContent.prototype = {
-
toString: function () {
-
return JSON.stringify(this);
-
}
-
};
-
var BankVaultContract = function () {
-
LocalContractStorage.defineMapProperty(this, "bankVault", {
-
parse: function (text) {
-
return new DepositeContent(text);
-
},
-
stringify: function (o) {
-
return o.toString();
-
}
-
});
-
};
-
}
-
module.exports = BankVaultContract;
</pre>
注1:DepositContent在存储时,会把object转成字符串类型,在读取时,会取出相应字符串转成object。
注2:智能合约开始结尾的使用能节约时间,提高效率。
2.3tips
- 建议多启动几个节点,这样上链速度会快一些,否则等很久。
- 建议多开几个终端窗口,命令复用。
- 合约代码在部署时需要进行格式转化,http://javascriptcompressor.com,https://www.freeformatter.com/javascript-escape.html#ad-output,这两个网址对你有用。
- web版:https://github.com/nebulasio/web-wallet
将这个工程下载下来,打开index.html,就能在web界面测试部署你的智能合约。
智能合约开发的特点
- 上链数据不可更改
- 数据透明,节点可以知道链上的所有数据
- 不能得到真随机数,js的random()方法只能的都伪随机数