区块链不同机器的节点相互连接

1节点所在的机器,相互之间可以互联互通
2它们有相同的创世纪文件
3 有相同的networkid

一 第一个节点创建

root@i-67u87x1c:/home/ubuntu# mkdir private-geth
root@i-67u87x1c:/home/ubuntu# cd private-geth/
root@i-67u87x1c:/home/ubuntu/private-geth# vim genesis.json

{
   "config": {
        "chainId": 15,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "coinbase" : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x40000",
    "extraData" : "",
    "gasLimit" : "0xffffffff",
    "nonce" : "0x0000000000000042",
    "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp" : "0x00",
    "alloc": { }
}

初始化创始块
root@i-67u87x1c:/home/ubuntu/private-geth# geth --datadir ./data/00 init genesis.json
WARN [07-06|14:38:34] No etherbase set and no accounts found as default 
INFO [07-06|14:38:34] Allocated cache and file handles         database=/home/ubuntu/private-geth/data/00/geth/chaindata cache=16 handles=16
INFO [07-06|14:38:34] Writing custom genesis block 
INFO [07-06|14:38:34] Successfully wrote genesis state         database=chaindata                                        hash=a0e580鈥5e82e
INFO [07-06|14:38:34] Allocated cache and file handles         database=/home/ubuntu/private-geth/data/00/geth/lightchaindata cache=16 handles=16
INFO [07-06|14:38:34] Writing custom genesis block 
INFO [07-06|14:38:34] Successfully wrote genesis state         database=lightchaindata    

进入geth后面连接节点


root@i-67u87x1c:/home/ubuntu/private-geth# geth --datadir ./data/00 --networkid 314590 --ipcdisable --port 632 --rpcport 8156 --bootnodes "enode://96f7a95a8c5e7a1f911142d87ce53ae882568be0435016e4803a6d410a9ecc7a69322cee8efbe8499ec7c737c377793182cab8869a076389c6b47626df00450b@192.168.0.4:61910" console
WARN [07-06|14:43:54] No etherbase set and no accounts found as default 
INFO [07-06|14:43:54] Starting peer-to-peer node               instance=Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
INFO [07-06|14:43:54] Allocated cache and file handles         database=/home/ubuntu/private-geth/data/00/geth/chaindata cache=128 handles=1024
INFO [07-06|14:43:54] Initialised chain configuration          config="{ChainID: 15 Homestead: 0 DAO:  DAOSupport: false EIP150:  EIP155: 0 EIP158: 0 Metropolis:  Engine: unknown}"
INFO [07-06|14:43:54] Disk storage enabled for ethash caches   dir=/home/ubuntu/private-geth/data/00/geth/ethash count=3
INFO [07-06|14:43:54] Disk storage enabled for ethash DAGs     dir=/root/.ethash                                 count=2
INFO [07-06|14:43:54] Initialising Ethereum protocol           versions="[63 62]" network=314590
INFO [07-06|14:43:54] Loaded most recent local header          number=0 hash=a0e580鈥5e82e td=262144
INFO [07-06|14:43:54] Loaded most recent local full block      number=0 hash=a0e580鈥5e82e td=262144
INFO [07-06|14:43:54] Loaded most recent local fast block      number=0 hash=a0e580鈥5e82e td=262144
INFO [07-06|14:43:54] Starting P2P networking 
INFO [07-06|14:43:57] UDP listener up                          self=enode://ca2dd7e49417097f68292c19e4af9a8311223ce9153559f68d0cb618fb961fcf615ded9c90b635c26d0cdff8aa5ee58e2bbf83949df31e841bab03f451f636ec@[::]:632
INFO [07-06|14:43:57] RLPx listener up                         self=enode://ca2dd7e49417097f68292c19e4af9a8311223ce9153559f68d0cb618fb961fcf615ded9c90b635c26d0cdff8aa5ee58e2bbf83949df31e841bab03f451f636ec@[::]:632
Welcome to the Geth JavaScript console!

instance: Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.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

> eth.blockNumber
0


>  admin.nodeInfo
{
  enode: "enode://ca2dd7e49417097f68292c19e4af9a8311223ce9153559f68d0cb618fb961fcf615ded9c90b635c26d0cdff8aa5ee58e2bbf83949df31e841bab03f451f636ec@[::]:632",
  id: "ca2dd7e49417097f68292c19e4af9a8311223ce9153559f68d0cb618fb961fcf615ded9c90b635c26d0cdff8aa5ee58e2bbf83949df31e841bab03f451f636ec",
  ip: "::",
  listenAddr: "[::]:632",
  name: "Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1",
  ports: {
    discovery: 632,
    listener: 632
  },
  protocols: {
    eth: {
      difficulty: 262144,
      genesis: "0xa0e580c6769ac3dd80894b2a256164a76b796839d2eb7f799ef6b9850ea5e82e",
      head: "0xa0e580c6769ac3dd80894b2a256164a76b796839d2eb7f799ef6b9850ea5e82e",
      network: 314590
    }
  }
}

root@i-67u87x1c:/home/ubuntu# ip addr
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:37:78:15:bb brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.3/24 brd 192.168.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:37ff:fe78:15bb/64 scope link 
       valid_lft forever preferred_lft forever

二 另外一台电脑

创建相同的创始块
root@i-kectp1zd:/home/ubuntu/private-geth# 
root@i-kectp1zd:/home/ubuntu/private-geth# geth --datadir ./data/00 init genesis.json
WARN [07-06|15:26:39] No etherbase set and no accounts found as default 
INFO [07-06|15:26:39] Allocated cache and file handles         database=/home/ubuntu/private-geth/data/00/geth/chaindata cache=16 handles=16
INFO [07-06|15:26:39] Writing custom genesis block 
INFO [07-06|15:26:39] Successfully wrote genesis state         database=chaindata                                        hash=a0e580鈥5e82e
INFO [07-06|15:26:39] Allocated cache and file handles         database=/home/ubuntu/private-geth/data/00/geth/lightchaindata cache=16 handles=16
INFO [07-06|15:26:39] Writing custom genesis block 
INFO [07-06|15:26:39] Successfully wrote genesis state         database=lightchaindata                                        hash=a0e580鈥5e82e



进入geth


root@i-kectp1zd:/home/ubuntu/private-geth# geth --datadir ./data --networkid 314590 --ipcdisable --port 3456 --rpcport 78 --bootnodes "enode://ca2dd7e49417097f68292c19e4af9a8311223ce9153559f68d0cb618fb961fcf615ded9c90b635c26d0cdff8aa5ee58e2bbf83949df31e841bab03f451f636ec@192.168.0.3:632" console
WARN [07-06|15:30:31] No etherbase set and no accounts found as default 
INFO [07-06|15:30:31] Starting peer-to-peer node               instance=Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
INFO [07-06|15:30:31] Allocated cache and file handles         database=/home/ubuntu/private-geth/data/geth/chaindata cache=128 handles=1024
INFO [07-06|15:30:31] Writing default main-net genesis block 
INFO [07-06|15:30:31] Initialised chain configuration          config="{ChainID: 1 Homestead: 1150000 DAO: 1920000 DAOSupport: true EIP150: 2463000 EIP155: 2675000 EIP158: 2675000 Metropolis: 9223372036854775807 Engine: ethash}"
INFO [07-06|15:30:31] Disk storage enabled for ethash caches   dir=/home/ubuntu/private-geth/data/geth/ethash count=3
INFO [07-06|15:30:31] Disk storage enabled for ethash DAGs     dir=/root/.ethash                              count=2
WARN [07-06|15:30:31] Upgrading db log bloom bins 
INFO [07-06|15:30:31] Bloom-bin upgrade completed              elapsed=123.418碌s
INFO [07-06|15:30:31] Initialising Ethereum protocol           versions="[63 62]" network=314590
INFO [07-06|15:30:31] Loaded most recent local header          number=0 hash=d4e567鈥b8fa3 td=17179869184
INFO [07-06|15:30:31] Loaded most recent local full block      number=0 hash=d4e567鈥b8fa3 td=17179869184
INFO [07-06|15:30:31] Loaded most recent local fast block      number=0 hash=d4e567鈥b8fa3 td=17179869184
INFO [07-06|15:30:31] Starting P2P networking 
INFO [07-06|15:30:33] UDP listener up                          self=enode://a22e7a8f502521ac685df1f001c59a06549b5292a42728e08ac3ca3f15603c75a0680f47ced4a736eab047335a0d2331e91ccb483591a83ecae57181878a7e07@[::]:3456
INFO [07-06|15:30:33] RLPx listener up                         self=enode://a22e7a8f502521ac685df1f001c59a06549b5292a42728e08ac3ca3f15603c75a0680f47ced4a736eab047335a0d2331e91ccb483591a83ecae57181878a7e07@[::]:3456
Welcome to the Geth JavaScript console!

instance: Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.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
function github.com/ethereum/go-ethereum/console.(*bridge).NewAccount-fm()
> personal.newAccount()

镜像制作新的电脑

root@i-0mu8dg3p:/home/ubuntu# mkdir private-gethyi
root@i-0mu8dg3p:/home/ubuntu# cd private-gethyi
root@i-0mu8dg3p:/home/ubuntu/private-gethyi# vim genesis.json 

{
    "config": {
        "chainId": 15,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "coinbase" : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x40000",
    "extraData" : "",
    "gasLimit" : "0xffffffff",
    "nonce" : "0x0000000000000042",
    "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp" : "0x00",
    "alloc": { }
}

root@i-0mu8dg3p:/home/ubuntu/private-gethyi#  geth --datadir ./data/00 init genesis.json
WARN [07-06|16:47:03] No etherbase set and no accounts found as default 
INFO [07-06|16:47:03] Allocated cache and file handles         database=/home/ubuntu/private-gethyi/data/00/geth/chaindata cache=16 handles=16
INFO [07-06|16:47:03] Writing custom genesis block 
INFO [07-06|16:47:03] Successfully wrote genesis state         database=chaindata                                          hash=a0e580鈥5e82e
INFO [07-06|16:47:03] Allocated cache and file handles         database=/home/ubuntu/private-gethyi/data/00/geth/lightchaindata cache=16 handles=16
INFO [07-06|16:47:03] Writing custom genesis block 
INFO [07-06|16:47:03] Successfully wrote genesis state         database=lightchaindata   
root@i-0mu8dg3p:/home/ubuntu/private-gethyi# geth --datadir ./data/00 --networkid 314590 --ipcdisable --port 123 --rpcport 456 --bootnodes "enode://96f7a95a8c5e7a1f911142d87ce53ae882568be0435016e4803a6d410a9ecc7a69322cee8efbe8499ec7c737c377793182cab8869a076389c6b47626df00450b@192.168.0.4:61910" console


> miner.start()
INFO [07-06|17:05:47] Updated mining threads                   threads=0
INFO [07-06|17:05:47] Transaction pool price threshold updated price=18000000000
null
> INFO [07-06|17:05:47] Starting mining operation 
INFO [07-06|17:05:47] Commit new mining work                   number=1 txs=0 uncles=0 elapsed=180.94碌s
INFO [07-06|17:21:25] Successfully sealed new block            number=1 hash=0b386b鈥dfce1
INFO [07-06|17:21:25] 馃敤 mined potential block                  number=1 hash=0b386b鈥dfce1
INFO [07-06|17:21:25] Commit new mining work                   number=2 txs=0 uncles=0 elapsed=3.832ms
INFO [07-06|17:24:43] Successfully sealed new block            number=2 hash=6ff261鈥2e306
INFO [07-06|17:24:43] 馃敤 mined potential block                  number=2 hash=6ff261鈥2e306
INFO [07-06|17:24:43] Commit new mining work                   number=3 txs=0 uncles=0 elapsed=683.976碌s
INFO [07-06|17:27:11] Successfully sealed new block            number=3 hash=92983c鈥222ab
INFO [07-06|17:27:11] 馃敤 mined potential block                  number=3 hash=92983c鈥222ab
INFO [07-06|17:27:11] Commit new mining work                   number=4 txs=0 uncles=0 elapsed=704.935碌s
INFO [07-06|17:34:06] Successfully sealed new block            number=4 hash=a0272f鈥88970
INFO [07-06|17:34:06] 馃敤 mined potential block                  number=4 hash=a0272f鈥88970
INFO [07-06|17:34:06] Commit new mining work                   number=5 txs=0 uncles=0 elapsed=618.806碌s
INFO [07-06|17:45:42] Successfully sealed new block            number=5 hash=0880e3鈥9a7d3
INFO [07-06|17:45:42] 馃敤 mined potential block                  number=5 hash=0880e3鈥9a7d3
INFO [07-06|17:45:42] Commit new mining work                   number=6 txs=0 uncles=0 elapsed=504.843碌s

> 
> 
> 
> miner.stop()
true
> 
> 
> eth.blockNumber
5
admin.addPeer("enode://96f7a95a8c5e7a1f911142d87ce53ae882568be0435016e4803a6d410a9ecc7a69322cee8efbe8499ec7c737c377793182cab8869a076389c6b47626df00450b@192.168.0.4:61910")

> admin.nodeInfo
{
  enode: "enode://c49ec7069c76c6414b2debc6e0f27d3774cc54133681a28c7283b9b822851f21aee710d91ef7ccf10861d9c8cb6fce8b8189ac71441d051353ce9c10bdaf7892@[::]:123",
  id: "c49ec7069c76c6414b2debc6e0f27d3774cc54133681a28c7283b9b822851f21aee710d91ef7ccf10861d9c8cb6fce8b8189ac71441d051353ce9c10bdaf7892",
  ip: "::",
  listenAddr: "[::]:123",
  name: "Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1",
  ports: {
    discovery: 123,
    listener: 123
  },
  protocols: {
    eth: {
      difficulty: 1451162,
      genesis: "0xa0e580c6769ac3dd80894b2a256164a76b796839d2eb7f799ef6b9850ea5e82e",
      head: "0x0880e3e3f21e5601661aeb01fc8589a46d6efe5ef31adebb5f7f9679d7e9a7d3",
      network: 314590
    }
  }
}

你可能感兴趣的:(以太坊)