VMWare虚拟机 + CentOs 7 + Golang
为了避免后续安装过程出错,这里提前安装一些需要用到的组件和依赖。如果不确定之前有没有安装过这些组件也没关系,检测到安装就不会重复安装。-y表示安装过程中弹出的问题一律回答yes。
yum update -y && yum install git wget bzip2 vim gcc-c++ ntp epel-release nodejs cmake -y
组件 | 作用 |
---|---|
git | 版本管理工具,用于后面从github克隆geth |
wget | Linux命令行的下载工具,用于后面下载cmake |
bzip2 | 无损压缩软件 |
vim | 编辑工具,用于后面编辑一些环境配置文件 |
gcc-c++ | c/c++编译工具,用于后面geth的编译 |
ntp | 网络时钟同步组件 |
nodejs | 用于前端js开发的包管理软件 |
epel | 网络第三方的linux安装包源 |
git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum && make all
# 打开环境配置文件
vim ~/.profile
# 新增path
export PATH=$PATH:$GOPATH/bin:$HOME/go-ethereum/build/bin
# 让更改生效
source ~/.profile
智能合约编译需要cmake,之前yum install cmake的版本只有2.8不够,这里需要去官网下载其独立安装包。这里我用的是最新版本3.12.3。
cd && wget https://cmake.org/files/v3.12/cmake-3.12.3.tar.gz
tar -xzvf cmake-3.12.3.tar.gz
cd cmake-3.12.3
./bootstrap && make && make install
# 打开环境配置文件
vim ~/.profile
# 新增path
export PATH=$PATH:$GOPATH/bin:$HOME/go-ethereum/build/bin:$HOME/cmake/bin
# 让更改生效
source ~/.profile
因为之前在配置golang私有云桌面的时候把防火墙关闭了,我在这里就不需要考虑。
systemctl stop firewalld
systemctl disable firewalld
firewall-cmd --zone=public --add-port=8087/tcp --permanent
firewall-cmd --zone=public --add-port=30303/tcp --permanent
systemctl enable firewalld
systemctl start firewalld
因为区块链需要同步网络时间,所以需要启用网络时间同步
systemctl enable ntpd
systemctl start ntpd
{
"nonce": "0x0000000000000042",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x00",
"gasLimit": "0x80000000",
"difficulty": "0x400",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x2D356ee3F5b8718d8690AFCD31Fe2CB5E602677e",
"alloc": {},
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
}
}
geth --datadir node0 init genesis.json
geth --datadir node0 console 2>> geth.log
tail -f geth.log
geth --datadir node1 init genesis.json
geth --datadir node0 -networkid 2018 -rpc console 2>> geth.log
geth --datadir node1 -networkid 2018 -rpc -rpcport 8546 -port 30304 console 2>> geth.log
admin.nodeInfo.enode
admin.addPeer(上面拷贝的enode)
有个小小的疑惑。当时忘记设置网络id,默认是1,网上说networkid是1的时候是接入公网?然后确实是会莫名其妙偶尔连到一些外国的节点,第一个193开头的是乌克兰,第一个52开头的是爱尔兰的。但是为何私链可以接到公网上面?后面发现加上-nodiscover就可以不被别的节点发现。
字段 | 意义 |
---|---|
difficulty | 挖到当前区块的难度 |
extraData | 额外数据 |
gasLimit | 该区块允许使用的手续费上限 |
gasUsed | 该区块实际包含的所有手续费 |
hash | 该区块的唯一哈希值标识,32字节 |
logsBloom | 区块日志的过滤器,256字节 |
miner | 打包该区块的矿工地址,20字节 |
mixHash | 混合哈希 |
nonce | 一个符合PoW难题的随机值,8字节 |
number | 当前区块的序号 |
parentHash | 父区块的哈希值,32字节 |
receiptRoot | 收据的唯一哈希值标识 |
sha3Uncles | 叔区块的SHA3哈希值 |
size | 区块的大小,单位是字节 |
stateRoot | 该区块最终前缀树的根哈希 |
timeStamp | 该区块被发现时的Unix时间戳 |
totalDifficulty | 挖到当前区块前所有区块的难度总和 |
transactions | 所有交易的哈希值集合 |
transactionsRoot | 该区块交易前缀树的根哈希 |
uncles | 叔哈希 |
可以观察到每次挖到一个新的区块都需要经历四个阶段。分别是commit new mining work => successfully sealed new block => block reached canonical chain => mined potential block,即先提交要打包某个区块的任务,然后成功打包该区块,该区块接到主链上(主链最后一个区块号一般会比当前的目标区块小一些,因为有分叉),成功完成挖矿。每条记录后面的number指明的是当前挖矿的目标区块序号。
通过remix,写了一个最简单的合约,有读操作也有写操作。
pragma solidity ^0.4.18;
contract Test{
string public name;
function Test() public {
name = "xietao";
}
function getName() public view returns (string) {
return name;
}
function setName(string _name) public {
name = _name;
}
}
var testContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getName","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"}],"name":"setName","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
var test = testContract.new(
{
from: web3.eth.accounts[0],
data: '0x6060604052341561000f57600080fd5b60408051908101604052600681527f78696574616f00000000000000000000000000000000000000000000000000006020820152600090805161005692916020019061005c565b506100f7565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009d57805160ff19168380011785556100ca565b828001600101855582156100ca579182015b828111156100ca5782518255916020019190600101906100af565b506100d69291506100da565b5090565b6100f491905b808211156100d657600081556001016100e0565b90565b61037f806101066000396000f3006060604052600436106100565763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166306fdde03811461005b57806317d7de7c146100e5578063c47f0027146100f8575b600080fd5b341561006657600080fd5b61006e61014b565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100aa578082015183820152602001610092565b50505050905090810190601f1680156100d75780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156100f057600080fd5b61006e6101e9565b341561010357600080fd5b61014960046024813581810190830135806020601f8201819004810201604051908101604052818152929190602084018383808284375094965061029295505050505050565b005b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101e15780601f106101b6576101008083540402835291602001916101e1565b820191906000526020600020905b8154815290600101906020018083116101c457829003601f168201915b505050505081565b6101f16102a9565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102875780601f1061025c57610100808354040283529160200191610287565b820191906000526020600020905b81548152906001019060200180831161026a57829003601f168201915b505050505090505b90565b60008180516102a59291602001906102bb565b5050565b60206040519081016040526000815290565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102fc57805160ff1916838001178555610329565b82800160010185558215610329579182015b8281111561032957825182559160200191906001019061030e565b50610335929150610339565b5090565b61028f91905b80821115610335576000815560010161033f5600a165627a7a72305820ca52a32829a7341ced9c589fa39647854f065bcec7bf1d7b4a2aed5185b579900029',
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
personal.unlockAccount(eth.accounts[0])
miner.start();admin.sleepBlocks(1);miner.stop()
Contract mined! address: 0x7e8109387a54814fcc61a48729b7a891f24cd291 transactionHash: 0x656fc28b69415dab65edbb5e62f2034daa6f2402f5db052aaba72da3269a1707
eth.defaultAccount = eth.accouts[0]
test.setName("xietao1", {from:eth.accounts[0]}");
字段 | 意义 |
---|---|
blockHash | 该交易所在区块的哈希,32字节 |
blockNumber | 该交易所在的区块序号 |
from | 该交易转账方的地址 |
gas | 转账方愿意支付的手续费 |
gasPrice | gas的单价,单位是Wei |
hash | 该交易的哈希 |
input | 交易过程中用到的数据 |
nonce | 转账方在该交易前发起的交易数 |
r,s | 交易的ECDSA签名 |
to | 该交易收款方的地址 |
v | 公钥恢复id |
transactionIndex | 该交易在所在区块交易集合的下标 |
value | 交易的金额,单位是Wei |