美文网首页以太坊centos应用服务配置以太坊开发笔记
以太坊大火:如何在CentOS7上搭建以太坊私有链?

以太坊大火:如何在CentOS7上搭建以太坊私有链?

作者: 解放号社区 | 来源:发表于2018-06-22 10:54 被阅读0次

    本文目的是帮助对以太坊开发感兴趣的同学,快速上手搭建搭建以太坊私有链。

    首先介绍一下环境:

    本次搭建的平台是基于Windows下Vmware中安装的CentOS7虚拟机,在虚拟机中完成所有操作。本文重点在以太坊私有链搭建,至于虚拟机的搭建,本文不再赘述。

    操作系统:CentOS Linux release 7.5.1804

    一、 什么是Ethereum(以太坊)

    以太坊(英语:Ethereum)是一个开源的有智能合约功能的公共区块链平台。通过其专用加密货币以太币(Ether)提供去中心化的虚拟机来处理点对点合约。截至2018年2月,以太币是市值第二高的加密货币,仅次于比特币。

    以太坊的概念首次在2013至2014年间由程序员Vitalik Buterin,受比特币启发后提出,大意为“下一代加密货币与去中心化应用平台”。所以很明显,以太坊是比比特币更先进的一代虚拟币。如果说比特币是区块链的1.0时代,那么以太坊是区块链的2.0时代。所以,建议大家从以太坊开始学习区块链技术。

    下面我们一起学习一下如何搭建以太坊私有链环境。

    二、 开始安装Go语言

    1.这里安装的是go1.10版本,因为,低版本可能导致无法编译后边的以太坊安装包。yum安装的是1.9版本,所以要手动下载安装

    [root@hadoop111 ~]# wget https://dl.google.com/go/go1.10.linux-amd64.tar.gz   #下载go安装包1.10版本

    复制代码

    --2018-06-14 11:16:26-- https://dl.google.com/go/go1.10.linux-amd64.tar.gz

    正在解析主机 dl.google.com (dl.google.com)...203.208.40.132, 203.208.40.128, 203.208.40.130, ...

    正在连接 dl.google.com(dl.google.com)|203.208.40.132|:443... 已连接。

    已发出 HTTP 请求,正在等待回应...200 OK

    长度:119905205 (114M)[application/octet-stream]

    正在保存至: “go1.10.linux-amd64.tar.gz”

    100%[===================================================================================================================================================>]119,905,205 1.75MB/s 用时 68s   

    2018-06-14 11:17:35 (1.67 MB/s) - 已保存 “go1.10.linux-amd64.tar.gz”[119905205/119905205])

    2.解压go安装包

    [root@hadoop111~]# tar -C /usr/local -xzf go1.10.linux-amd64.tar.gz

    复制代码

    3.添加go环境变量

    [root@hadoop111~]# echo 'export GOROOT=/usr/local/go' >> /etc/profile

    [root@hadoop111 ~]# echo 'export PATH=$PATH:$GOROOT/bin'>> /etc/profile

    [root@hadoop111 ~]# echo 'export GOPATH=/home/go' >>/etc/profile

    [root@hadoop111 ~]# echo 'export PATH=$PATH:$GOPATH/bin'>> /etc/profile

    复制代码

    4.刷新环境变量

    [root@hadoop111 ~]# source /etc/profile

    复制代码

    5. 验证go语言是否安装成功

    [root@hadoop111 ~]# go version

    复制代码

    goversion go1.10 linux/amd64

    三、 安装其它安装以太坊需要的环境组件

    安装curl  git nodejs  gcc-c++

    [root@hadoop111 ~]# yum install-y curl git  nodejs gcc-c++

    复制代码

    四、 编译安装以太坊(这里采用的是源码安装方式)

    1.   下载以太坊源码

    [root@hadoop111 ~]# cd/usr/local #创建目录

    [root@hadoop111 ~]# git clone https://github.com/ethereum/go-ethereum.git  #通过git下载以太坊源码

    复制代码

    正克隆到 'go-ethereum'...

    remote: Counting objects: 70885,done.

    remote: Total 70885 (delta 0),reused 0 (delta 0), pack-reused 70885

    接收对象中: 100% (70885/70885),96.27 MiB | 174.00 KiB/s, done.

    处理 delta 中: 100% (47083/47083), done.

    2.   编译

    [root@hadoop111 ~]# cdgo-ethereum

    [root@hadoop111 ~]# make all

    [root@hadoop111 ~]# ln -sv/usr/local/go-ethereum/build/bin/geth /usr/bin #添加软连接

    复制代码

    3.   安装cmake,智能合约需要使用

    [root@hadoop111 ~]# cd ~

    [root@hadoop111 ~]# wget https://cmake.org/files/v3.9/cmake-3.9.2.tar.gz #下载cmake安装包

    [root@hadoop111 ~]# tar xvfcmake-3.9.2.tar.gz     #解压

    [root@hadoop111 ~]# cdcmake-3.9.2

    [root@hadoop111 ~]# ./configure&& make && make install   #编译安装

    复制代码

    4.   检查安装结果

    [root@hadoop111 ~]# systemctlis-enabled chronyd   #时间同步服务

    [root@hadoop111 ~]# cmake --version

    复制代码

    cmake version 3.9.2

    CMake suite maintained andsupported by Kitware (kitware.com/cmake).

    [root@hadoop111 ~]# geth version

    复制代码

    WARN [06-14|14:18:56] Sanitizingcache to Go's GC limits      provided=1024 updated=607

    Geth

    Version: 1.8.12-unstable

    Git Commit:ea89f40f0dab7957aa1825b56be80e84d3b04b5c

    Architecture: amd64

    Protocol Versions: [63 62]

    Network Id: 1

    Go Version: go1.10

    Operating System: linux

    GOPATH=/home/go

    GOROOT=/usr/local/go

    五、 使用以太坊私有链

    1.  初始化创世区块

    创建一个genesis.json文件

    [root@hadoop111 ~]# cd /root

    [root@hadoop111 ~]# mkdireth-test

    [root@hadoop111 ~]# cd eth-test

    [root@hadoop111 ~]# vigenesis.json

    复制代码

    添加如下内容

    {

    "config":{

    "chainId":20,

    "homesteadBlock":0,

    "eip155Block":0,

    "eip158Block":0

    },

    "alloc":{},

    "coinbase":"0x0000000000000000000000000000000000000000",

    "difficulty":"0x200",

    "extraData":"",

    "gasLimit":"0x2fefd8",

    "nonce":"0x0000000000000042",

    "mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000",

    "parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",

    "timestamp":"0x00"

    }

    复制代码

    2. 初始化节点

    [root@hadoop111 ~]# geth--datadir "/root/eth-test/" init genesis.json  #初始化创世区块

    复制代码

    执行完成后,将会出现类似以下信息,表示创世区块初始化完成

    file:///C:/Users/pc/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg

    启动以太坊

    [root@hadoop111 ~]# cd /usr/bin

    [root@hadoop111 ~]# ./geth --rpc--rpccorsdomain "*" --datadir "/root/eth-test/" --rpcapi"db,net,eth,web3,personal,miner,debug,admin" --networkid 100000console 2>>eth_output.log

    复制代码

    注意:上面命令中–datadir 后的”/root/eth-test/”要跟我们上一步的–datadir 参数一致。命令执行完,自动进入geth的命令行则说明以太坊私有链安装成功了。

    file:///C:/Users/pc/AppData/Local/Temp/msohtmlclip1/01/clip_image004.jpg

    3. 创建账户,挖矿

    查看帐户,可以看到当前帐户是空的

    [root@hadoop111 ~]#  eth.accounts

    复制代码

    创建两个帐户,并初始化密码。注意密码是用户自定义的。

    >  personal.newAccount('123456')

    复制代码

    "0x3e5b345a8b88fe181f75d6956c40c104de7b61ee"

    >  personal.newAccount('123456')

    复制代码

    "0xc6b5ec8fff2696375c16c930a9fb7aec4a106b11"

    再次查询账户,发现有两个了

    > eth.accounts

    复制代码

    ["0x3e5b345a8b88fe181f75d6956c40c104de7b61ee","0xc6b5ec8fff2696375c16c930a9fb7aec4a106b11"]

    为了操作方便,给账户起一个名字

    > test1 = eth.accounts[0]

    复制代码

    "0x3e5b345a8b88fe181f75d6956c40c104de7b61ee"

    > test2 = eth.accounts[1]

    复制代码

    "0xc6b5ec8fff2696375c16c930a9fb7aec4a106b11"

    设置操作账号

    > miner.setEtherbase(test1) #如果不设置挖矿会默认保存到创建的第一个帐户

    复制代码

    true

    开始挖矿

    > miner.start() #执行挖矿一段时间,然账户中有余额,第一次最好时间长一点,约3分钟,避免挖不到

    复制代码

    停止挖矿

    > miner.stop()

    复制代码

    true

    查看账户

    > eth.getBalance(test1)

    复制代码

    65000000000000000000

    4. 账户转账

    转账前需要先解锁账户,否则,会报错Error: authentication needed:password or unlock

    >personal.unlockAccount(test1,'123456')

    复制代码

    true

    转账时要注意,超过余额会报错

    > eth.sendTransaction({from:test1, to: test2, value: web3.toWei(1, 'ether')})

    复制代码

    "0xe85d7e65919ce73c64958d07f29decece7cf7f6c1289d77c12e7c7eb5f631553"

    查看账户,发现转出账户余额并未减少,转入账户也没有增加,因为我们的交易还未成功写进区块,写进区块的方式是挖矿,我们再次开启挖矿,挖矿成功后,再次查看余额,发现到账:

    >  eth.getBalance(test2)

    复制代码

    0

    > miner.start(1)  #执行挖矿让交易执行,1表示使用一个cpuminer.stop()

    复制代码

    null

    过一会发现,已到账

    > eth.getBalance(test2)

    复制代码

    1000000000000000000

    此时,可以看出转入账户,增加了1个以太币。而转出账户,由于再次挖矿而获得奖励,从而余额增加,所以,从转出账户,不好判断交易的真实数额。

    到此为止,本节的内容就完成了。可见以太坊的环境和操作还是比较简单的。

    首发网址:解放号社区

    解放号社区官方公众号

    相关文章

      网友评论

        本文标题:以太坊大火:如何在CentOS7上搭建以太坊私有链?

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