区块链开发:以太坊网络

区块链开发:以太坊网络

一、geth

Geth 又名Go Ethereum. 是以太坊协议的三种实现之一,由Go语言开发,完全开源的项目。Geth 可以被安装在很多操作系统上,包括Windows、Linux、Mac的OSX、Android或者IOS系统

Geth官网:https://geth.ethereum.org/
Geth的Github地址:https://github.com/ethereum/go-ethereum

Ubuntu安装geth客户端:
官方教程:https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-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,bootnode, evm, disasm, rlpdump,ethtest

此时如果输入Geth命令,会出现启动以太坊启动的画面

安装方法二:

git clone https://github.com/ethereum/go-ethereum
sudo apt-get install -y build-essential golang
cd go-ethereum
make geth

greg@greg:~$ geth version
Geth
Version: 1.7.3-stable
Git Commit: 4bb3c89d44e372e6a9ab85a8be0c9345265c763a
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9
Operating System: linux
GOPATH=/home/greg/go
GOROOT=/home/greg/local/go

二、以太坊查看网络状态

attach:Start an interactive JavaScript environment (connect to node) 启动交互式JavaScript环境(连接到node)

打开geth --testnet
greg@greg:~/.ethereum$ geth attach ipc://${HOME}/.ethereum/testnet/geth.ipc
Welcome to the Geth JavaScript console!

instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0


查看链接状态
net.listening
net.peerCount
查看自己的伙伴的网络信息
admin.peers
查看自己的网络信息
admin.nodeInfo

打开.bashrc写入
alias gat='geth attach ipc://${HOME}/.ethereum/testnet/geth.ipc'

三、以太坊构建本地私有网络

1.编辑创世区块

greg@greg:~$ mkdir ethprivate
greg@greg:~$ cd ethprivate/
greg@greg:~/ethprivate$ vim genesis.json
greg@greg:~/ethprivate$ vim genesis.json
greg@greg:~/ethprivate$ cat genesis.json 
{
    "config": {
        "chainId": 525137,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "coinbase" : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x40000",
    "extraData" : "",
    "gasLimit" : "0xffffffff",
    "nonce" : "0x0000000000000042",
    "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp" : "0x00",
    "alloc": { }
}

genesis(创世)区块是区块链的起点,是它的第一块区块,0号区块,唯一一个没有前任的区块。这个协议确保了没有其他节点会和你的节点的区块链版本一致,除非它们的创世区块和你的一模一样。通过这种方法,你就可以创建任意多的私有区块链。

Mixhash
一个256位的哈希值,和nonce配合,一起用来证明在区块链上已经做了足够的计算量(工作证明)。这个nonce 和 mixhash 的组成,必须满足一个在黄皮书中所描述的数学上的条件,黄皮书 4.3.4。
Nonce
一个64位的哈希值,和mixhash配合,一起用来证明在区块链上已经做了足够的计算量(工作证明)
Difficulty
定义挖矿的目标,可以用上一个区块的难度值和时间戳计算出来,值越高,矿工越难挖到区块
Alloc 预先填入一些钱包和余额
Coinbase
160位的钱包地址。在创世区块中可以被定义成任何的地址,因为当每挖到一个区块的时候,这个值会变成矿工的etherbase地址
Timestamp  一个unix的time()函数的输出值,时间戳
extraData  32字节长度,可以为私有链留下一些信息,如你的姓名等,用以证明这个私有链是你创建的
gasLimit   当前链,一个区块所能消耗的gas上限

私有链中会使用到的命令行参数
--nodiscover
添加这个参数,确保没有人能发现你的节点。不然的话,可能有人无意中会链接到你的私有区块链。
--maxpeers 0
使用maxpeers 0,如果你不希望其他人连接到您的测试链。当然,您也可以调整这个数,如果你知道有多少同伴会连接你的节点。
--rpc
在你的节点上激活RPC接口。这参数在geth中默认启用。
--rpcapi "db,eth,net,web3"
这个命令描述哪些接口可以通过RPC来访问,默认情况下,geth开启的是web3接口。
--rpcport "8080"
将端口号设置成8000以上的任何一个你网络中打开的端口。默认是8080。
--rpccorsdomain http://chriseth.github.io/browser-solidity/
设置可以连接到你的节点的url地址,以执行RPC客户端的任务。最好不要使用通配符 * ,这样将允许任何url都可以链接到你的RPC实例。
--datadir "/home/TestChain1"
私有链的数据目录,确保与公共以太坊链的数据目录区分开来。
--port "30303"
这是“网络监听的端口”,您可以用它手动的和你的同伴相连。
--identity "TestnetMainNode"
为你的节点设置一个ID。用于和你们的一系列同伴进行区分。

2.初始化创世区块

greg@greg:~/ethprivate$ geth --datadir "./" init genesis.json
WARN [02-21|23:48:06] No etherbase set and no accounts found as default 
INFO [02-21|23:48:06] Allocated cache and file handles         database=/home/greg/ethprivate/geth/chaindata cache=16 handles=16
Fatal: Failed to write genesis block: genesis has no chain configuration
greg@greg:~/ethprivate$ ls
genesis.json  geth  keystore

此时当前目录下面会新增出两个文件夹geth和keystore

geth中保存的是区块链的相关数据
keystore中保存的是该链条中的用户信息

每次当你想要使用你自定义的私有链时,你都需要先使用geth来运行以上的命令来启动它。

3.创建自己的私有链条

创建自己的私有链条,同时记录日志输出到geth.log
greg@greg:~/ethprivate$ geth --datadir "./" --nodiscover console 2>>geth.log
Welcome to the Geth JavaScript console!

instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
在自己的私有链条上创建用户
> eth.accounts
[]
> personal.newAccount("123456")
"0xe158bf2c29ca283c7bdc99880e36985d9f7d23f8"
> eth.accounts
["0xe158bf2c29ca283c7bdc99880e36985d9f7d23f8"]
> personal.newAccount("abcdef")
"0x8cd642b5f69a24fd3d05786dfe0eb3f44dc8bb33"
> eth.accounts
["0xe158bf2c29ca283c7bdc99880e36985d9f7d23f8", "0x8cd642b5f69a24fd3d05786dfe0eb3f44dc8bb33"]

输入命令personal.newAccount("123456"), 该命令将创造一个新的用户,该用户的密码是123456

再次输入命令 eth.accounts, 我们会发现一个新的用户被创建了出来,这就代表我们已经创建了一个账户,重复personal.newAccount()eth.accounts 我们可以创建若干个账户出来

posted @ 2018-02-22 00:09  ninxin18  阅读(3322)  评论(1编辑  收藏  举报