以太坊是一个开源的公有区块链平台,支持图灵完备的智能合约Solidity,能够开发去中心化的DApps。但是直接在公网上做开发会非常昂贵,因为每一笔交易都要支付gas,在测试链上做开发显然是更好的选择。本篇将讲述如何在局域网环境下,在多台机器搭建以太坊测试链。
genesis.json
{
"config": {
"chainId": 14,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x05000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
CHAIN_ID | Chain(s) |
---|---|
1 | Ethereum mainnet |
2 | Morden (disused), Expanse mainnet |
3 | Ropsten |
4 | Rinkeby |
30 | Rootstock mainnet |
31 | Rootstock testnet |
42 | Kovan |
61 | Ethereum Classic mainnet |
62 | Ethereum Classic testnet |
1337 | Geth private chains (default) |
vadmin@ubuntu:~/fileData$ geth --datadir data0 init genesis.json
WARN [07-09|19:50:27.769688] Sanitizing cache to Go's GC limits provided=1024 updated=982
INFO [07-09|19:50:27.782066] Maximum peer count ETH=25 LES=0 total=25
INFO [07-09|19:50:27.790579] Allocated cache and file handles database=/home/vadmin/fileData/data0/geth/chaindata cache=16 handles=16
INFO [07-09|19:50:27.802341] Writing custom genesis block
INFO [07-09|19:50:27.802486] Persisted trie from memory database nodes=0 size=0.00B time=5.677µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [07-09|19:50:27.803067] Successfully wrote genesis state database=chaindata hash=06701b…b7b009
INFO [07-09|19:50:27.803132] Allocated cache and file handles database=/home/vadmin/fileData/data0/geth/lightchaindata cache=16 handles=16
INFO [07-09|19:50:27.812823] Writing custom genesis block
INFO [07-09|19:50:27.812924] Persisted trie from memory database nodes=0 size=0.00B time=3.565µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [07-09|19:50:27.814915] Successfully wrote genesis state database=lightchaindata hash=06701b…b7b009
vadmin@ubuntu:~/fileData$ geth --networkid 14 --nodiscover --datadir data0 --rpc --rpcapi net,eth,web3,personal --rpcaddr 192.168.23.193 console
WARN [07-09|19:53:00.921526] Sanitizing cache to Go's GC limits provided=1024 updated=982
INFO [07-09|19:53:00.922131] Maximum peer count ETH=25 LES=0 total=25
INFO [07-09|19:53:00.922839] Starting peer-to-peer node instance=Geth/v1.8.12-unstable-c95e4a80/linux-amd64/go1.10.1
INFO [07-09|19:53:00.922951] Allocated cache and file handles database=/home/vadmin/fileData/data0/geth/chaindata cache=736 handles=512
INFO [07-09|19:53:00.940248] Initialised chain configuration config="{ChainID: 14 Homestead: 0 DAO: DAOSupport: false EIP150: EIP155: 0 EIP158: 0 Byzantium: Constantinople: Engine: unknown}"
INFO [07-09|19:53:00.940315] Disk storage enabled for ethash caches dir=/home/vadmin/fileData/data0/geth/ethash count=3
INFO [07-09|19:53:00.94033] Disk storage enabled for ethash DAGs dir=/home/vadmin/.ethash count=2
INFO [07-09|19:53:00.940361] Initialising Ethereum protocol versions="[63 62]" network=14
INFO [07-09|19:53:00.941793] Loaded most recent local header number=0 hash=06701b…b7b009 td=20480
INFO [07-09|19:53:00.941858] Loaded most recent local full block number=0 hash=06701b…b7b009 td=20480
INFO [07-09|19:53:00.941872] Loaded most recent local fast block number=0 hash=06701b…b7b009 td=20480
INFO [07-09|19:53:00.942016] Regenerated local transaction journal transactions=0 accounts=0
INFO [07-09|19:53:00.94256] Starting P2P networking
INFO [07-09|19:53:00.943098] RLPx listener up self="enode://8424ff6b41be1f6ee2a2e374ba88d12b30466b81f6d8684e9355e65b8a28b58ff0b9f7ec7684f5dc4b697a90f961e86db6dc75186eb283e3619c0fc4c4b7988d@[::]:30303?discport=0"
INFO [07-09|19:53:00.948868] IPC endpoint opened url=/home/vadmin/fileData/data0/geth.ipc
INFO [07-09|19:53:00.949161] HTTP endpoint opened url=http://192.168.23.193:8545 cors= vhosts=localhost
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.12-unstable-c95e4a80/linux-amd64/go1.10.1
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
personal.newAccount(“123”)
“0xd585211734b283f6f290d1e0662b4ecf1166d54d”
miner.start()
vadmin@ubuntu:~/fileData$ geth --datadir data1 init genesis.json
geth --networkid 14 --nodiscover --datadir data1 --port 61911 --rpcapi net,eth,web3,personal --rpc --rpcaddr 192.168.23.125 --rpcport 8101 console
personal.newAccount(“123”)
“0x22d968f93a63fe35a85daf4ab3c880543150193e”
在节点0的控制台,查看0节点的enode
admin.nodeInfo.enode
“enode://8424ff6b41be1f6ee2a2e374ba88d12b30466b81f6d8684e9355e65b8a28b58ff0b9f7ec7684f5dc4b697a90f961e86db6dc75186eb283e3619c0fc4c4b7988d@[::]:30303?discport=0”
在节点1的控制台,添加节点0
admin.addPeer(“enode://8424ff6b41be1f6ee2a2e374ba88d12b30466b81f6d8684e9355e65b8a28b58ff0b9f7ec7684f5dc4b697a90f961e86db6dc75186eb283e3619c0fc4c4b7988d@192.168.23.193:30303”)
可以在节点00和01控制台中,查看连接的节点数量和连接的节点列表
net.peerCount
admin.peers
baseAccount = eth.accounts[0]
personal.unlockAccount(baseAccount,“123”)eth.sendTransaction({from : baseAccount, to : “0xee1d67b7bfd646e080e18d39c6049a1362177db0” , value : web3.toWei(1,“ether”)})
“0x6a9c25fbe3c16e4a8107c91ab3b1b982e07b3fca7e19749de450db1744ceef47”
geth attach ipc:/home/vadmin/fileData/data0/geth.ipc