主页 > imtoken市场打不开 > 从头开始以太坊区块链开发指南 1
从头开始以太坊区块链开发指南 1
一切从头开始,不管它是开发出来的。 首先是搭建开发环境。 今天尝试写下自己搭建以太坊区块链开发环境的心得体会。 希望对您有所帮助。
阐明
以太坊是一个开源、去中心化的虚拟机,能够部署和运行智能合约。 你可以把它理解为运行各种去中心化应用程序的操作系统。 最著名的应用是比特币。 以太坊网络由单个节点组成。 然后以太坊区块链app,我们将编写的智能合约部署到以太坊网络上。 以太坊的应用是一个用于部署、运行和智能合约的程序。 由于以太坊的公链对于我们的开发程序来说太慢了以太坊区块链app,我们首先需要搭建一条以太坊的私有链。 这个过程主要需要geth。
1.geth
首先需要安装geth,我们使用geth创建私有链。
Mac平台
1.1 使用 Homebrew 安装
brew tap ethereum/ethereum
brew install ethereum
1.2 从源码安装
git clone https://github.com/ethereum/go-ethereum
brew install go
cd go-ethereum
make geth
1.3 从网站下载命令行工具
https://ethereum.github.io/go-ethereum/downloads/
Windows平台
从以下网站下载windows平台
https://ethereum.github.io/go-ethereum/downloads/
Ubuntu平台
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
安装geth后,使用命令行
geth version
如果输出像
Geth
Version: 1.7.3-stable
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9
Operating System: darwin
GOPATH=
GOROOT=/usr/local/Cellar/go/1.9/libexec
表示安装成功
2.使用geth创建私有链
运行命令
mkdir enode1
mkdir enode2
分别创建两个文件夹作为两个私链节点的datadir
对于能够链接的两个节点,它们必须是由同一个创世文件创建的链。
geth --datadir "enode1" init genesis.json
enode1是上面创建的文件之一,genesis.json是genesis配置文件
genesis.json如下
{
"coinbase" : "0x0000000000000000000000000000000000000001",
"difficulty" : "0x400",
"extraData" : "",
"gasLimit" : "4712388",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc": {},
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
}
}
创建后
使用命令
geth --identity "newEth" --rpc --rpccorsdomain "*" --datadir "enode1" --port 30303 --rpcapi "personal,db,net,eth,web3" --networkid 999 --rpcport 8080 console
我们可以看到这些信息
节点端口
启动另一个节点
使用命令启动另一个节点
geth --datadir "enode2" init genesis.json
geth --identity "newEth" --rpc --rpccorsdomain "*" --datadir "enode2" --port 30304 --rpcapi "personal,db,net,eth,web3" --networkid 999 --rpcport 8081 console
另一个节点
注意两个节点的不同位置分别是datadir、port、rpcport。 由于我们是在同一台电脑上运行两个节点,所以datadir、port、rpcport一定是不同的,不在同一台电脑上也没关系。
运行 admin.peers
节点信息
可以看到该节点没有链接到任何节点
添加另一个节点
运行admin.addPeer('')添加另外一个节点,节点的地址enode://6b944fdc0a3460977e67682428b29cec7b28a400a2a5c3ef6b56673eb9f4abc8fe7316018c2073e43569ec63e6542eb7dc8b23cc93f397bc41a1f077b8cec6e4@[::]:30304就是上图我们enode2的节点地址。
让我们再次运行 admin.peers 看看
图片.png
如您所见,两个节点已经在同一条链上运行。
好了,最重要的运行环境已经搭建好了。 下一章,我们将尝试部署一个智能合约。下章地址