美文网首页我爱编程
本地环境的智能合约编写

本地环境的智能合约编写

作者: liuzhangjie | 来源:发表于2018-05-06 12:40 被阅读0次

    小白星云链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依赖库

    • 首先要安装支持 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;">

    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保存文件

    • 安装 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;">

    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键查看。
    介绍下常用函数:

    • 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: 合约代码类型,支持jsts(对应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;">

    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三种状态,对应错误,成功,等待。

    • 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;">

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

    </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;">

    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>

    • 方法,分为公有和私有方法,私有方法以“_"开头,用户不能直接调用执行私有方法。方法的参数读取来自于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;">

    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>

    • 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;">

    1. 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;">

    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

    智能合约开发的特点

    • 上链数据不可更改
    • 数据透明,节点可以知道链上的所有数据
    • 不能得到真随机数,js的random()方法只能的都伪随机数

    相关文章

      网友评论

        本文标题:本地环境的智能合约编写

        本文链接:https://www.haomeiwen.com/subject/iyfurftx.html