美文网首页宛十八区块链研发区块链
以太坊私链教程:如何搭建基于PoA的以太坊私链(联盟链)

以太坊私链教程:如何搭建基于PoA的以太坊私链(联盟链)

作者: _黑冰_ | 来源:发表于2018-12-30 00:08 被阅读0次
    LOGO镇楼

    前言

    前面我们介绍了使用geth搭建私链的教程,但经过测试,这个私链由于使用的是PoW共识机制,对内存要求比较高(无法在树莓派上跑起来),并且布署需要以太币做为启动资金。

    好在以太坊提供了一种PoA算法,这种算法相比PoW在私链上更具优势,辟如交易更便宜,延迟更低,可控性更强。

    下面就几种共识机制一一介绍,最后使用五块树莓派,搭建基于以太坊Clique的PoA私链网络,为下一步探索区块链应用创造条件。而之前的PoW私链则不再继续研究。

    共识机制比较

    PoW(Proof-of-Work)

    众所周知的比特币、以太坊以及其它基于PoW的加密货币,最大的缺陷之一就是会遭遇51%攻击(虽然难度较大),虽然区块链是不可变的,并且记录无法更改,但是攻击者可能会实施其它攻击,如拒绝服务攻击,从而导致应用瘫痪。

    另外,PoW的计算消耗了大量的电力资源,以太坊一直致力研究PoW的替代方案,例如PoS,这解决了电力问题,但它更多地针对的是公链。

    对于私链,工作证明根本没有意义。

    PoS(Proof-of-Stake)

    以太坊很快过渡到了PoS算法,此算法解决了公链工作证是且的问题,想要成为矿工的人会将一些以太币存入智能合约中,如果矿工被认为是恶意的,那么这部分以太币将会被锁定。

    因为没有竞争来解决难以解决的数学问题,所以不必总是发行新币,矿工将从交易中获得回报。

    这种算法确实解决了过度用电的问题,并确实在一定程度上可以防止51%攻击。

    对于私链,PoS可能比Pow更 好,但它仍然没有为我们提供所需的控制和安全级别,因为如果他们提交了足够的以太币做为押金,那么公链上的任何人都可以成为矿工。

    PoA(Proof-of-Authority)

    PoA是区块链世界中的一个新概念,在这个概念中,您拥有许多预先批准的授权节点(称为审查者,即通常意义上的矿工)。

    您要添加任何新节点,必须由当前已有的审查者投票,这使您可以完全控制哪一些节点可以做为审查者。

    以太坊的PoA协议称为Clique,它适用于私链,但不适用于公链。

    使用PoA协议搭建私链

    现在我们知道了PoA是什么,我们将一步步的搭建一个基于PoA的私链网络,网络中包含4个审查者节点。

    安装树莓派系统

    安装过程略过,请各位自行搜索网上教程,此处安装的是2018-04-18-raspbian-stretch-lite.img,下面讲一点重要的地方:

    1、安装完成后开启root并允许SSH登录:

    pi@raspberry:/# sudo passwd root
    

    编辑/etc/ssh/sshd_config文件,添加PermitRootLogin yes行,并使用raspi-config开启ssh服务,重启。

    2、各节点通过编辑/etc/hostname修改节点的机器名称

    3、各节点通过编辑/etc/dhcpcd.conf设置静态IP地址如下:

    • 192.168.0.10
    • 192.168.0.11
    • 192.168.0.12
    • 192.168.0.13
    • 192.168.0.14

    安装环境

    apt install golang -y
    apt-get install -y build-essential
    

    编译

    踩坑1:我们安装的go是1.7版,所以这里要下载1.7的源码

    cd /home
    mkdir geth
    cd geth
    git clone https://github.com/ethereum/go-ethereum -b release/1.7 --depth=1
    

    编译:

    make all
    cp build/bin/ /usr/local/bin/
    chmod 777 /usr/local/bin/*
    

    编译过程在其中一台设备上完成(如:192.168.0.10),其它设备通过WinScp上传到/usr/local/bin并执行一遍chmod 777 /usr/local/bin/*

    创建签名帐户

    在各节点上运行geth --datadir node account new创建帐户

    注:要将密码保存到/home/passwd.eth

    节点1:

    root@eth01:/home# geth --datadir node account new
    Your new account is locked with a password. Please give a password. Do not forget this password.
    Passphrase: 
    Repeat passphrase: 
    Address: {06de0c4d8c40fb1cbb86e0b00401d395828ae4f1}
    
    

    节点2:

    root@eth02:~# geth --datadir node account new
    Your new account is locked with a password. Please give a password. Do not forget this password.
    Passphrase: 
    Repeat passphrase: 
    Address: {f55b442c40632602ce5b8270102294c57f2b2531}
    
    

    节点3:

    root@eth03:~# geth --datadir node account new
    Your new account is locked with a password. Please give a password. Do not forget this password.
    Passphrase: 
    Repeat passphrase: 
    Address: {761bbd27d783894696b0aaae9b4616dbebb6ed3f}
    

    节点4:

    root@eth04:~# geth --datadir node account new
    Your new account is locked with a password. Please give a password. Do not forget this password.
    Passphrase: 
    Repeat passphrase: 
    Address: {a17492dcdb51c90c75334cdf0e47be93d82a0ee9}
    

    节点5:

    root@eth05:~# geth --datadir node account new
    Your new account is locked with a password. Please give a password. Do not forget this password.
    Passphrase: 
    Repeat passphrase: 
    Address: {0afe6957dd356288a52c7539d28df964d0de5197}
    
    

    记住各节点的帐户,后面启动节点时需要用到

    06de0c4d8c40fb1cbb86e0b00401d395828ae4f1
    f55b442c40632602ce5b8270102294c57f2b2531
    761bbd27d783894696b0aaae9b4616dbebb6ed3f
    0afe6957dd356288a52c7539d28df964d0de5197
    a17492dcdb51c90c75334cdf0e47be93d82a0ee9
    

    使用puppeth创建创世文件

    此处要用到上面的帐户代码,注意要记住网络ID

    root@eth01:/home# puppeth
    +-----------------------------------------------------------+
    | Welcome to puppeth, your Ethereum private network manager |
    |                                                           |
    | This tool lets you create a new Ethereum network down to  |
    | the genesis block, bootnodes, miners and ethstats servers |
    | without the hassle that it would normally entail.         |
    |                                                           |
    | Puppeth uses SSH to dial in to remote servers, and builds |
    | its network components out of Docker containers using the |
    | docker-compose toolset.                                   |
    +-----------------------------------------------------------+
    
    Please specify a network name to administer (no spaces, please)
    > deling
    Sweet, you can set this via --network=deling next time!
    
    INFO [12-29|14:23:03] Administering Ethereum network           name=deling
    WARN [12-29|14:23:03] No previous configurations found         path=/root/.puppeth/deling
    
    What would you like to do? (default = stats)
     1. Show network stats
     2. Configure new genesis
     3. Track new remote server
     4. Deploy network components
    > 2
    
    Which consensus engine to use? (default = clique)
     1. Ethash - proof-of-work
     2. Clique - proof-of-authority
    > 2
    
    How many seconds should blocks take? (default = 15)
    > 
    
    Which accounts are allowed to seal? (mandatory at least one)
    > 0x06de0c4d8c40fb1cbb86e0b00401d395828ae4f1
    > 0xf55b442c40632602ce5b8270102294c57f2b2531
    > 0x761bbd27d783894696b0aaae9b4616dbebb6ed3f
    > 0x0afe6957dd356288a52c7539d28df964d0de5197
    > 0xa17492dcdb51c90c75334cdf0e47be93d82a0ee9
    > 0x
    
    Which accounts should be pre-funded? (advisable at least one)
    > 0x06de0c4d8c40fb1cbb86e0b00401d395828ae4f1
    > 0xf55b442c40632602ce5b8270102294c57f2b2531
    > 0x761bbd27d783894696b0aaae9b4616dbebb6ed3f
    > 0x0afe6957dd356288a52c7539d28df964d0de5197
    > 0xa17492dcdb51c90c75334cdf0e47be93d82a0ee9
    > 0x
    
    
    Specify your chain/network ID if you want an explicit one (default = random)
    > 2010
    
    Anything fun to embed into the genesis block? (max 32 bytes)
    > 
    
    What would you like to do? (default = stats)
     1. Show network stats
     2. Manage existing genesis
     3. Track new remote server
     4. Deploy network components
    > 2
    
     1. Modify existing fork rules
     2. Export genesis configuration
    > 2
    
    Which file to save the genesis into? (default = deling.json)
    > 
    INFO [12-29|14:27:12] Exported existing genesis block 
    
    What would you like to do? (default = stats)
     1. Show network stats
     2. Manage existing genesis
     3. Track new remote server
     4. Deploy network components
    > ^C
    root@eth01:/home# ls
    deling.json  gobase  node  pi  src
    
    

    节点初始化

    将上面生成的deling.json通过WinScp上传到各节点/home/deling.json,并执行geth --datadir node init deling.json进行初始化

    root@eth01:/home# geth --datadir node init deling.json
    INFO [12-29|14:30:52] Allocated cache and file handles         database=/home/node/geth/chaindata cache=16 handles=16
    INFO [12-29|14:30:52] Writing custom genesis block 
    INFO [12-29|14:30:52] Successfully wrote genesis state         database=chaindata                 hash=230c1a…f59c8a
    INFO [12-29|14:30:52] Allocated cache and file handles         database=/home/node/geth/lightchaindata cache=16 handles=16
    INFO [12-29|14:30:52] Writing custom genesis block 
    INFO [12-29|14:30:52] Successfully wrote genesis state         database=lightchaindata                 hash=230c1a…f59c8a
    root@eth01:/home# 
    

    第一次启动节点

    本次启动节点是为了获得节点的enodeid,因为节点之间还没有建立连接,所以会显示Signed recently, must wait for others并卡在这里

    坑2:直接启动节点时会报错Failed to unlock account xxx (no key for given address or file)

    root@eth02:/home# geth --datadir node --maxpeers 3 --networkid '2010' --nodiscover --port '30300' --unlock 'f55b442c40632602ce5b8270102294c57f2b2531' --password '/home/passwd.eth' --mine --minerthreads=1 --etherbase=f55b442c40632602ce5b8270102294c57f2b2531 --rpc --rpcaddr '0.0.0.0' --rpcport '8500' --rpccorsdomain '*' --rpcapi 'personal,eth,net,web3'
    INFO [12-29|14:45:48] Starting peer-to-peer node               instance=Geth/v1.7.3-stable-4bb3c89d/linux-arm/go1.7.4
    INFO [12-29|14:45:48] Allocated cache and file handles         database=/home/node/geth/chaindata cache=128 handles=1024
    INFO [12-29|14:45:48] Initialised chain configuration          config="{ChainID: 2010 Homestead: 1 DAO: <nil> DAOSupport: false EIP150: 2 EIP155: 3 EIP158: 3 Byzantium: 4 Engine: clique}"
    INFO [12-29|14:45:48] Initialising Ethereum protocol           versions="[63 62]" network=2010
    INFO [12-29|14:45:48] Loaded most recent local header          number=0 hash=230c1a…f59c8a td=1
    INFO [12-29|14:45:48] Loaded most recent local full block      number=0 hash=230c1a…f59c8a td=1
    INFO [12-29|14:45:48] Loaded most recent local fast block      number=0 hash=230c1a…f59c8a td=1
    INFO [12-29|14:45:48] Loaded local transaction journal         transactions=0 dropped=0
    INFO [12-29|14:45:48] Regenerated local transaction journal    transactions=0 accounts=0
    INFO [12-29|14:45:48] Starting P2P networking 
    INFO [12-29|14:45:48] RLPx listener up                         self="enode://24b2d4fd3672360121587e4677339ddc7af9dea9ee0d68c41f3758648bed3dc20372d2233d97f9b1c3ea52ffc6e9d26ba0aa1849b210235272961f4909de4968@[::]:30300?discport=0"
    INFO [12-29|14:45:48] Mapped network port                      proto=tcp extport=30300 intport=30300 interface=NAT-PMP(192.168.0.1)
    INFO [12-29|14:45:48] IPC endpoint opened: /home/node/geth.ipc 
    INFO [12-29|14:45:48] HTTP endpoint opened: http://0.0.0.0:8500 
    Fatal: Failed to unlock account f55b442c40632602ce5b8270102294c57f2b2531 (no key for given address or file)
    
    

    此时需要将生成的key复制到datadir中的keystore,例如:

    cp /root/node/keystore/UTC--2018-12-29T14-17-09.954994382Z--f55b442c40632602ce5b8270102294c57f2b2531 /home/node/keystore/
    

    分别在每台机运行

    geth --datadir /home/node --maxpeers 5 --networkid '2010' --nodiscover --port '30300' --unlock '06de0c4d8c40fb1cbb86e0b00401d395828ae4f1' --password '/home/passwd.eth' --mine --minerthreads=1 --etherbase=06de0c4d8c40fb1cbb86e0b00401d395828ae4f1
    
    geth --datadir /home/node --maxpeers 5 --networkid '2010' --nodiscover --port '30301' --unlock 'f55b442c40632602ce5b8270102294c57f2b2531' --password '/home/passwd.eth' --mine --minerthreads=1 --etherbase=f55b442c40632602ce5b8270102294c57f2b2531 --rpc --rpcaddr '0.0.0.0' --rpcport '8501' --rpccorsdomain '*' --rpcapi 'personal,eth,net,web3'
    
    geth --datadir /home/node --maxpeers 5 --networkid '2010' --nodiscover --port '30302' --unlock '761bbd27d783894696b0aaae9b4616dbebb6ed3f' --password '/home/passwd.eth' --mine --minerthreads=1 --etherbase=761bbd27d783894696b0aaae9b4616dbebb6ed3f --rpc --rpcaddr '0.0.0.0' --rpcport '8502' --rpccorsdomain '*' --rpcapi 'personal,eth,net,web3'
    
    geth --datadir /home/node --maxpeers 5 --networkid '2010' --nodiscover --port '30303' --unlock 'a17492dcdb51c90c75334cdf0e47be93d82a0ee9' --password '/home/passwd.eth' --mine --minerthreads=1 --etherbase=a17492dcdb51c90c75334cdf0e47be93d82a0ee9 --rpc --rpcaddr '0.0.0.0' --rpcport '8503' --rpccorsdomain '*' --rpcapi 'personal,eth,net,web3'
    
    geth --datadir /home/node --maxpeers 5 --networkid '2010' --nodiscover --port '30304' --unlock '0afe6957dd356288a52c7539d28df964d0de5197' --password '/home/passwd.eth' --mine --minerthreads=1 --etherbase=0afe6957dd356288a52c7539d28df964d0de5197 --rpc --rpcaddr '0.0.0.0' --rpcport '8504' --rpccorsdomain '*' --rpcapi 'personal,eth,net,web3'
    

    分别记录下各节点的地址,编辑为static-nodes.json文件,上传到各节点node目录下

    [
    "enode://e0b52df7c8f6cbd214fba602225c5fb0f9a0f523f83fcd0882e3fd08eb4116283abd85e6c91771e24e0918ec92392c88a3b24e48b57cb1d9f9aa2f26b1e47f93@192.168.0.10:30300?discport=0",
    "enode://24b2d4fd3672360121587e4677339ddc7af9dea9ee0d68c41f3758648bed3dc20372d2233d97f9b1c3ea52ffc6e9d26ba0aa1849b210235272961f4909de4968@192.168.0.11:30300?discport=0",
    "enode://f125d29453f2a2736eea614ff548c14c1e3e28b115dac8b212ae31c0f2d5ed6e9e03554c0482141ad81bf8e26c7ba171bf9f5de9f015dc4d4d055c19a6b8fd19@192.168.0.12:30300?discport=0",
    "enode://b8f12b7a4f6a3be313df9f9871c9c42cf3d53b17c0d37726db065938db4c2f2c946485f3683dbd5cf450035b8f791540d770efa1d327e8e3f124f359530d42d4@192.168.0.13:30300?discport=0",
    "enode://916ec46aa2e9dd308351e70eba7228cc0b534b88f05dceed7679a70124d5035a5f66024a5f74c00778dc7fe4fb563f838b290f1a70a31bf187fa0d2ac3b1a9d1@192.168.0.14:30300?discport=0"
    ]
    

    重新启动节点,会发现节点间开始通信,出块。

    INFO [12-29|15:28:59] Starting peer-to-peer node               instance=Geth/v1.7.3-stable-4bb3c89d/linux-arm/go1.7.4
    INFO [12-29|15:28:59] Allocated cache and file handles         database=/home/node/geth/chaindata cache=128 handles=1024
    INFO [12-29|15:28:59] Initialised chain configuration          config="{ChainID: 2010 Homestead: 1 DAO: <nil> DAOSupport: false EIP150: 2 EIP155: 3 EIP158: 3 Byzantium: 4 Engine: clique}"
    INFO [12-29|15:28:59] Initialising Ethereum protocol           versions="[63 62]" network=2010
    INFO [12-29|15:28:59] Loaded most recent local header          number=1 hash=ef6d21…25d952 td=2
    INFO [12-29|15:28:59] Loaded most recent local full block      number=1 hash=ef6d21…25d952 td=2
    INFO [12-29|15:28:59] Loaded most recent local fast block      number=1 hash=ef6d21…25d952 td=2
    INFO [12-29|15:28:59] Loaded local transaction journal         transactions=0 dropped=0
    INFO [12-29|15:28:59] Regenerated local transaction journal    transactions=0 accounts=0
    WARN [12-29|15:28:59] Blockchain not empty, fast sync disabled 
    INFO [12-29|15:28:59] Starting P2P networking 
    INFO [12-29|15:28:59] RLPx listener up                         self="enode://e0b52df7c8f6cbd214fba602225c5fb0f9a0f523f83fcd0882e3fd08eb4116283abd85e6c91771e24e0918ec92392c88a3b24e48b57cb1d9f9aa2f26b1e47f93@[::]:30300?discport=0"
    INFO [12-29|15:28:59] Mapped network port                      proto=tcp extport=30300 intport=30300 interface=NAT-PMP(192.168.0.1)
    INFO [12-29|15:28:59] IPC endpoint opened: /home/node/geth.ipc 
    INFO [12-29|15:29:19] Block synchronisation started 
    INFO [12-29|15:29:19] Imported new chain segment               blocks=1 txs=0 mgas=0.000 elapsed=6.237ms mgasps=0.000 number=1 hash=4e2d79…f4f054
    INFO [12-29|15:29:19] Imported new chain segment               blocks=1 txs=0 mgas=0.000 elapsed=2.327ms mgasps=0.000 number=2 hash=98f195…a3e219
    INFO [12-29|15:29:20] Unlocked account                         address=0x06de0c4d8C40Fb1cBb86e0b00401d395828ae4f1
    INFO [12-29|15:29:20] Transaction pool price threshold updated price=18000000000
    INFO [12-29|15:29:20] Starting mining operation 
    INFO [12-29|15:29:20] Commit new mining work                   number=3 txs=0 uncles=1 elapsed=1.695ms
    INFO [12-29|15:29:34] Imported new chain segment               blocks=1 txs=0 mgas=0.000 elapsed=8.598ms mgasps=0.000 number=3 hash=d10035…f300c1
    INFO [12-29|15:29:34] Commit new mining work                   number=4 txs=0 uncles=1 elapsed=3.577ms
    INFO [12-29|15:29:34] Imported new chain segment               blocks=1 txs=0 mgas=0.000 elapsed=3.253ms mgasps=0.000 number=3 hash=05a957…b65e7d
    INFO [12-29|15:29:49] Successfully sealed new block            number=4 hash=4f535d…ade019
    INFO [12-29|15:29:49] 🔨 mined potential block                  number=4 hash=4f535d…ade019
    INFO [12-29|15:29:49] Commit new mining work                   number=5 txs=0 uncles=2 elapsed=8.495ms
    

    我们另外开一个ssh client,连上一个节点geth attach ipc://home/node/geth.ipc,可以查询到连接的节点,并发现区块在不断的增加

    root@eth01:/home# geth attach ipc://home/node/geth.ipc
    Welcome to the Geth JavaScript console!
    
    instance: Geth/v1.7.3-stable-4bb3c89d/linux-arm/go1.7.4
    coinbase: 0x06de0c4d8c40fb1cbb86e0b00401d395828ae4f1
    at block: 74 (Sat, 29 Dec 2018 15:47:19 UTC)
     datadir: /home/node
     modules: admin:1.0 clique: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
    
    > admin.peers
    [{
        caps: ["eth/62", "eth/63"],
        id: "24b2d4fd3672360121587e4677339ddc7af9dea9ee0d68c41f3758648bed3dc20372d2233d97f9b1c3ea52ffc6e9d26ba0aa1849b210235272961f4909de4968",
        name: "Geth/v1.7.3-stable-4bb3c89d/linux-arm/go1.7.4",
        network: {
          localAddress: "192.168.0.10:30300",
          remoteAddress: "192.168.0.11:46206"
        },
        protocols: {
          eth: {
            difficulty: 105,
            head: "0x444b8cac7e3c8d6dc5788ff59f9be1d8e3b245ce8cb104a06329080e341c74af",
            version: 63
          }
        }
    }, {
        caps: ["eth/62", "eth/63"],
        id: "916ec46aa2e9dd308351e70eba7228cc0b534b88f05dceed7679a70124d5035a5f66024a5f74c00778dc7fe4fb563f838b290f1a70a31bf187fa0d2ac3b1a9d1",
        name: "Geth/v1.7.3-stable-4bb3c89d/linux-arm/go1.7.4",
        network: {
          localAddress: "192.168.0.10:30300",
          remoteAddress: "192.168.0.14:57558"
        },
        protocols: {
          eth: {
            difficulty: 103,
            head: "0xf0826a47fbb00d6323559b4bdcfee245653715c76b1b9c18f042c55d2eb9a054",
            version: 63
          }
        }
    }, {
        caps: ["eth/62", "eth/63"],
        id: "b8f12b7a4f6a3be313df9f9871c9c42cf3d53b17c0d37726db065938db4c2f2c946485f3683dbd5cf450035b8f791540d770efa1d327e8e3f124f359530d42d4",
        name: "Geth/v1.7.3-stable-4bb3c89d/linux-arm/go1.7.4",
        network: {
          localAddress: "192.168.0.10:30300",
          remoteAddress: "192.168.0.13:39058"
        },
        protocols: {
          eth: {
            difficulty: 102,
            head: "0x22c2f963248e5591ad41c389e34ecb4807ab68758becf3cd9d1611665cddcb9e",
            version: 63
          }
        }
    }, {
        caps: ["eth/62", "eth/63"],
        id: "f125d29453f2a2736eea614ff548c14c1e3e28b115dac8b212ae31c0f2d5ed6e9e03554c0482141ad81bf8e26c7ba171bf9f5de9f015dc4d4d055c19a6b8fd19",
        name: "Geth/v1.7.3-stable-4bb3c89d/linux-arm/go1.7.4",
        network: {
          localAddress: "192.168.0.10:30300",
          remoteAddress: "192.168.0.12:60764"
        },
        protocols: {
          eth: {
            difficulty: 104,
            head: "0x58077849fc0ff8b96f0c519a3671f11f6abd62cfbbf34e2994f5771a912603c2",
            version: 63
          }
        }
    }]
    > personal.listWallets
    [{
        accounts: [{
            address: "0x06de0c4d8c40fb1cbb86e0b00401d395828ae4f1",
            url: "keystore:///home/node/keystore/UTC--2018-12-29T14-07-22.077051229Z--06de0c4d8c40fb1cbb86e0b00401d395828ae4f1"
        }],
        status: "Unlocked",
        url: "keystore:///home/node/keystore/UTC--2018-12-29T14-07-22.077051229Z--06de0c4d8c40fb1cbb86e0b00401d395828ae4f1"
    }]
    > eth.blockNumber
    74
    > net.peerCount
    4
    > net.version
    "2010"
    > eth.coinbase
    "0x06de0c4d8c40fb1cbb86e0b00401d395828ae4f1"
    > personal.listAccounts
    ["0x06de0c4d8c40fb1cbb86e0b00401d395828ae4f1"]
    > 
    
    

    以上,我们从技术上搭建了一个联盟链,后续我们将去探索如何解决应用层的问题:

    • 如何实现便捷的管理
    • 节点加入、退出要如何处理
    • 如何在App场景中产生交易

    大合照收尾

    控制台合照 五层架构

    引用

    Rolling your own Proof-of-Authority Ethereum consortium

    Setup your own private Proof-of-Authority Ethereum network with Geth

    Proof-of-authority

    geth基于PoA共识机制构建联盟链

    Clique PoA consensus 建立Private chain

    搭建以太坊联盟链教程

    续 CentOS 7上安装过程

    hostnamectl set-hostname ethhost01
    cd /home
    wget https://studygolang.com/dl/golang/go1.13.4.linux-amd64.tar.gz
    tar -C /usr/local -xzf go1.13.4.linux-amd64.tar.gz
    mkdir -p /home/gocode
    

    编辑环境变量

    vim /etc/profile
    

    在最后加入

    export GOROOT=/usr/local/go
    export GOPATH=/home/gocode
    export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
    
    source /etc/profile
    
    yum install gcc -y
    yum install git -y
    
    mkdir /home/geth
    cd /home/geth
    git clone https://github.com/ethereum/go-ethereum -b release/1.9 --depth=1
    
    cd go-ethereum/
    make all
    cp build/bin/* /usr/local/bin/
    chmod 777 /usr/local/bin/*
    

    节点1:

    geth --datadir node account new
    
    [root@ethhost01 home]# geth --datadir node account new
    INFO [11-07|10:34:37.747] Maximum peer count                       ETH=50 LES=0 total=50
    INFO [11-07|10:34:37.748] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
    Your new account is locked with a password. Please give a password. Do not forget this password.
    Password: 
    Repeat password: 
    
    Your new key was generated
    
    Public address of the key:   0x313826d6d8867fb31A6c1331F430A8D060eAA0aa
    Path of the secret key file: node/keystore/UTC--2019-11-07T15-34-44.517449810Z--313826d6d8867fb31a6c1331f430a8d060eaa0aa
    
    - You can share your public address with anyone. Others need it to interact with you.
    - You must NEVER share the secret key with anyone! The key controls access to your funds!
    - You must BACKUP your key file! Without the key, it's impossible to access account funds!
    - You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
    
    
    

    节点2:

    [root@ethhost02 home]# geth --datadir node account new
    INFO [11-07|10:35:33.061] Maximum peer count                       ETH=50 LES=0 total=50
    INFO [11-07|10:35:33.061] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
    Your new account is locked with a password. Please give a password. Do not forget this password.
    Password: 
    Repeat password: 
    
    Your new key was generated
    
    Public address of the key:   0x8Cf96Ef1000dbC9F002502968fC3ed5a1C745bbA
    Path of the secret key file: node/keystore/UTC--2019-11-07T15-35-43.435663437Z--8cf96ef1000dbc9f002502968fc3ed5a1c745bba
    
    - You can share your public address with anyone. Others need it to interact with you.
    - You must NEVER share the secret key with anyone! The key controls access to your funds!
    - You must BACKUP your key file! Without the key, it's impossible to access account funds!
    - You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
    
    
    

    节点3:

    [root@ethhost03 home]# geth --datadir node account new
    INFO [11-07|10:35:37.508] Maximum peer count                       ETH=50 LES=0 total=50
    INFO [11-07|10:35:37.508] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
    Your new account is locked with a password. Please give a password. Do not forget this password.
    Password: 
    Repeat password: 
    
    Your new key was generated
    
    Public address of the key:   0x91a68cb7093A9c28EDe3EbBE562ddFb82c930962
    Path of the secret key file: node/keystore/UTC--2019-11-07T15-35-50.382471417Z--91a68cb7093a9c28ede3ebbe562ddfb82c930962
    
    - You can share your public address with anyone. Others need it to interact with you.
    - You must NEVER share the secret key with anyone! The key controls access to your funds!
    - You must BACKUP your key file! Without the key, it's impossible to access account funds!
    - You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
    
    

    节点4:

    [root@ethhost04 home]# geth --datadir node account new
    INFO [11-07|10:35:32.377] Maximum peer count                       ETH=50 LES=0 total=50
    INFO [11-07|10:35:32.377] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
    Your new account is locked with a password. Please give a password. Do not forget this password.
    Password: 
    Repeat password: 
    
    Your new key was generated
    
    Public address of the key:   0x69B6c2d05ff0a0AD93c02c567A9C2E38F3c06946
    Path of the secret key file: node/keystore/UTC--2019-11-07T15-36-03.765940295Z--69b6c2d05ff0a0ad93c02c567a9c2e38f3c06946
    
    - You can share your public address with anyone. Others need it to interact with you.
    - You must NEVER share the secret key with anyone! The key controls access to your funds!
    - You must BACKUP your key file! Without the key, it's impossible to access account funds!
    - You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
    
    
    

    节点5:

    [root@ethhost05 home]# geth --datadir node account new
    INFO [11-07|10:35:35.154] Maximum peer count                       ETH=50 LES=0 total=50
    INFO [11-07|10:35:35.154] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
    Your new account is locked with a password. Please give a password. Do not forget this password.
    Password: 
    Repeat password: 
    
    Your new key was generated
    
    Public address of the key:   0xea5888309a5ED43315ae3F9f83cC87408fc0FC4d
    Path of the secret key file: node/keystore/UTC--2019-11-07T15-35-57.139160031Z--ea5888309a5ed43315ae3f9f83cc87408fc0fc4d
    
    - You can share your public address with anyone. Others need it to interact with you.
    - You must NEVER share the secret key with anyone! The key controls access to your funds!
    - You must BACKUP your key file! Without the key, it's impossible to access account funds!
    - You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
    
    
    

    创世

    [root@ethhost01 home]# puppeth
    +-----------------------------------------------------------+
    | Welcome to puppeth, your Ethereum private network manager |
    |                                                           |
    | This tool lets you create a new Ethereum network down to  |
    | the genesis block, bootnodes, miners and ethstats servers |
    | without the hassle that it would normally entail.         |
    |                                                           |
    | Puppeth uses SSH to dial in to remote servers, and builds |
    | its network components out of Docker containers using the |
    | docker-compose toolset.                                   |
    +-----------------------------------------------------------+
    
    Please specify a network name to administer (no spaces, hyphens or capital letters please)
    > deling
    
    Sweet, you can set this via --network=deling next time!
    
    INFO [11-07|10:39:19.501] Administering Ethereum network           name=deling
    INFO [11-07|10:39:19.501] No remote machines to gather stats from 
    
    What would you like to do? (default = stats)
     1. Show network stats
     2. Configure new genesis
     3. Track new remote server
     4. Deploy network components
    > 2
    
    What would you like to do? (default = create)
     1. Create new genesis from scratch
     2. Import already existing genesis
    > 1
    
    Which consensus engine to use? (default = clique)
     1. Ethash - proof-of-work
     2. Clique - proof-of-authority
    > 2
    
    How many seconds should blocks take? (default = 15)
    > 
    
    Which accounts are allowed to seal? (mandatory at least one)
    > 0x313826d6d8867fb31A6c1331F430A8D060eAA0aa
    > 0x8Cf96Ef1000dbC9F002502968fC3ed5a1C745bbA
    > 0x91a68cb7093A9c28EDe3EbBE562ddFb82c930962
    > 0x69B6c2d05ff0a0AD93c02c567A9C2E38F3c06946
    > 0xea5888309a5ED43315ae3F9f83cC87408fc0FC4d
    > 0x
    
    Which accounts should be pre-funded? (advisable at least one)
    > 0x313826d6d8867fb31A6c1331F430A8D060eAA0aa
    > 0x8Cf96Ef1000dbC9F002502968fC3ed5a1C745bbA
    > 0x91a68cb7093A9c28EDe3EbBE562ddFb82c930962
    > 0x69B6c2d05ff0a0AD93c02c567A9C2E38F3c06946
    > 0xea5888309a5ED43315ae3F9f83cC87408fc0FC4d
    > 0x
    
    Should the precompile-addresses (0x1 .. 0xff) be pre-funded with 1 wei? (advisable yes)
    > yes
    
    Specify your chain/network ID if you want an explicit one (default = random)
    > 2010
    INFO [11-07|10:40:31.231] Configured new genesis block 
    
    What would you like to do? (default = stats)
     1. Show network stats
     2. Manage existing genesis
     3. Track new remote server
     4. Deploy network components
    > 2
    
     1. Modify existing configurations
     2. Export genesis configurations
     3. Remove genesis configuration
    > 2
    
    Which folder to save the genesis specs into? (default = current)
      Will create deling.json, deling-aleth.json, deling-harmony.json, deling-parity.json
    > 
    INFO [11-07|10:40:41.201] Saved native genesis chain spec          path=deling.json
    ERROR[11-07|10:40:41.201] Failed to create Aleth chain spec        err="unsupported consensus engine"
    ERROR[11-07|10:40:41.201] Failed to create Parity chain spec       err="unsupported consensus engine"
    INFO [11-07|10:40:41.203] Saved genesis chain spec                 client=harmony path=deling-harmony.json
    
    What would you like to do? (default = stats)
     1. Show network stats
     2. Manage existing genesis
     3. Track new remote server
     4. Deploy network components
    > ^C
    
    
    

    关闭各节点防火墙

    systemctl stop firewalld
    
    

    分别运行各节点

    geth --datadir node init deling.json
    

    然后运行各节点

    geth --datadir /home/node --maxpeers 5 --nodiscover --port '30300' --unlock 'AbDe0e36ace0e1293B3c709Cb16F22D22237Bb46' --password '/home/passwd.eth' --mine --minerthreads=1 --etherbase=AbDe0e36ace0e1293B3c709Cb16F22D22237Bb46
    
    geth --datadir /home/node --maxpeers 5 --nodiscover --port '30301' --unlock 'B32E18A6d0aa7CeAaFBC2528cEDcA69EA833E107' --password '/home/passwd.eth' --mine --minerthreads=1 --etherbase=B32E18A6d0aa7CeAaFBC2528cEDcA69EA833E107 --rpc --rpcaddr '0.0.0.0' --rpcport '8501' --rpccorsdomain '*' --rpcapi 'personal,eth,net,web3' --allow-insecure-unlock
    
    geth --datadir /home/node --maxpeers 5 --nodiscover --port '30302' --unlock 'FB6c8c48ca08fab6B275E7BC7397505Cb2E8981e' --password '/home/passwd.eth' --mine --minerthreads=1 --etherbase=FB6c8c48ca08fab6B275E7BC7397505Cb2E8981e --rpc --rpcaddr '0.0.0.0' --rpcport '8502' --rpccorsdomain '*' --rpcapi 'personal,eth,net,web3' --allow-insecure-unlock
    
    geth --datadir /home/node --maxpeers 5 --nodiscover --port '30303' --unlock 'F4E1A6F97Ee80D69c5975711882E3BE0644d9F46' --password '/home/passwd.eth' --mine --minerthreads=1 --etherbase=F4E1A6F97Ee80D69c5975711882E3BE0644d9F46 --rpc --rpcaddr '0.0.0.0' --rpcport '8503' --rpccorsdomain '*' --rpcapi 'personal,eth,net,web3' --allow-insecure-unlock
    
    geth --datadir /home/node --maxpeers 5 --nodiscover --port '30304' --unlock '406ADA2FAb991A46313E4aBfef72E42214fc4E8E' --password '/home/passwd.eth' --mine --minerthreads=1 --etherbase=406ADA2FAb991A46313E4aBfef72E42214fc4E8E --rpc --rpcaddr '0.0.0.0' --rpcport '8504' --rpccorsdomain '*' --rpcapi 'personal,eth,net,web3' --allow-insecure-unlock
    
    

    采集各节点的enode信息,编制下面的文件,并上传到各节点/home/node文件夹下,重启节点,开始运作出块。

    static-nodes.json

    [
    "enode://2fa6d255b0f09d24c2e37b015aa38f3c3f2a38e7c63f3a4ec1ac175484c33b768c5a9ff7c03701949d83ef5b7f21635be6395bfb2afb3bb8a9741b4f8c0238cc@113.119.62.243:30300?discport=0",
    "enode://64f757aecce9510faa188481286bebd9345dd93714c34062da620f2c484b2ea6bdcef3a9df2caf1ce03a29fc06b268ed7f0b3c33cdbf2097e351eb9226133483@127.0.0.1:30301?discport=0",
    "enode://2580f18af999d1a970ed18fb3f2458ad1d13067b5d95cc18e0cea3f31e1a255f4959ec834ab83687601d1450088f28092554eae47a7194af6bc393798ca9ed8d@127.0.0.1:30302?discport=0",
    "enode://4960729e58d498c87ceb770a0ac4edb59d10b236dc109d08b3878b59884d57e177b85f9210af56fde97b95546051f074bd7f269d85719064acc017cd6a02d46c@127.0.0.1:30303?discport=0",
    "enode://7557b2cb5f48d1a7d4cd442506acc59a37f0bcdbc9f608ad232c4c6a58947ba91d0c3dc785e6c76e918a7da39ff29e0ed38c9e4c433ac4dc05d3f01bd8159cbb@127.0.0.1:30304?discport=0"
    ]
    

    钱包连接,创建快捷方式,连接节点

    "C:\Program Files\Ethereum-Wallet\Ethereum Wallet.exe" --rpc http://192.168.10.42:8501
    

    相关文章

      网友评论

        本文标题:以太坊私链教程:如何搭建基于PoA的以太坊私链(联盟链)

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