区块链-私链创建与智能合约部署实践

准备:

ECS+ubuntu 16.0+go+go-ethereum

1.创世链节点:

{

    "config": {

        "chainId": 12345,

         "homesteadBlock":0,

         "eip155Block":0,

        "eip158Block":0

    },

 "coinbase":"0x0000000000000000000000000000000000000000",

 "difficulty":"0x400",

"extraData":"0x123456",

"gasLimit":"0xffffffff",

"nonce":"0x0000000000000042",

"mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",

"timestamp":"0x00",

"alloc": { }

}

字段讲解:

mixhash:与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意他和nonce的设置需要满足以太坊

nonce: nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊的Yellow paper,

difficulty: 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度

alloc: 用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以我们不需要预置有币的账号,需要的时候自己创建即可以。

coinbase: 矿工的账号,随便填

timestamp: 设置创世块的时间戳

parentHash: 上一个区块的hash值,因为是创世块,所以这个值是0

extraData: 附加信息,随便填,可以填你的个性信息,必须为十六进制的偶位字符串

gasLimit: 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大。

2.geth --datadir "./" init genesis.json 建立创世节点

3.geth --datadir "./" --nodiscover console 2>>geth.log 新建私有链,且日志输出到geth.log

4.启动节点 进入控制台:geth --datadir ./ --networkid 314590 --ipcdisable --port 61910 --rpcport 8200 console

5. tail -f geth.log 动态展示geth.log日志更新信息

6.eth.accounts  展示用户数组

7.acc0=eth.accounts[0]

8.eth.getBalance(acc0)

9.执行miner.start()时,geth console返回null。

解决方案1:先执行personal.listAccounts查看当前节点是否存在账号地址,再用eth.coinbase查看是否设置了coinbase账户地址,如果没有则使用miner.setEtherbase(eth.accounts[0])设置coinbase地址。再启动miner.start()

10.转账:eth.sendTransaction({from:"address1",to:"address2",gas:31000,gasPrice:web3.toWei(300,'gwei'),value:1})

console打印出fullhash,根据hashid去查看交易状态

11.txpool.status 查看矿池中未打包交易

{

    pending:1,

    queued:0

}

12. eth.getBlock(number) 查看交易区块信息

13.eth.getTransaction(fullhash)

    如果执行了交易但查看txpool.status时,还是存在未交易数据,先查查是否在挖矿中,挖矿中才能进行区块更新

14.再查看txpool.status

{

    pending:0,

    queued:0

}

15.部署智能合约:

https://ethereum.github.io/browser-solidity/

线上编写编译solc代码

Hello World代码:

pragma solidity ^0.4.18; contract hello { string greeting; function hello(string _greeting) public { greeting = _greeting; } function say() constant public returns (string) { return greeting; } }

点击Details获取部署代码,替换变量字符串 var _greeting = "Hello World";

修改后,直接拷贝到geth console控制台,点击回车,返回Contract mined! address:XXXXX transactionHash:XXXXX

(注意需要保持挖矿状态才能部署上去,同时记得unlock操作的账号,personal.unlockAccount(address,'XXX'))

运行合约:

>hello.say()

"Hello World"

等待解决问题:

1.ssh服务器重连时进入geth控制台报错,需要如何解决,Fatal: Error starting protocol stack: datadir already used by another process

学习资料:

1.安装geth客户端并转账  http://blog.csdn.net/ddffr/article/details/74327876

2.以太坊学习笔记:私有链搭建操作指南 http://blog.csdn.net/u013096666/article/details/72639906

3.智能合约开发环境搭建及Hello World合约 http://www.cnblogs.com/tinyxiong/p/7898599.html

4.区块链技术 http://wiki.jikexueyuan.com/project/blockchain/

5.Solidity官网 https://solidity.readthedocs.io/en/latest/installing-solidity.html

猜你喜欢

转载自my.oschina.net/gdxz111/blog/1622782
今日推荐