基于ethereum的五种链环境搭建方式 第8章 方式一、单结点部署

单结点部署,一般情况适用于本地开发阶段或线上测试阶段,方便智能合约的开发与调试。

  1. 创建工程目录
     > mkdir -p ~/workmeta/EduEthereumServerDeploy/deploy_1/data
     > cd ~/workmeta/EduEthereumServerDeploy/deploy_1
    

    此目录主要用于存储所有链生产过程中产生的区块、索引数据以及帐户keystore数据。

  2. 预先创建挖矿帐户及预置资金帐户
     # 1. 创建密码文件,后续生成帐户统一使用此密码
     > echo "111111" > .passwd
    
     # 2. 创建两个帐户,一个挖矿帐户,一个预置资金帐户
     > for ((n=0;n<2;n++)); do geth account new --password .passwd --datadir ./data; done
     INFO [10-30|10:37:29] Maximum peer count                       ETH=25 LES=0 total=25
    Address: {127eb97163f790dd25c174429d85c3fa7c3cf116} //预设此地址为: 挖矿帐户
    INFO [10-30|10:37:30] Maximum peer count                       ETH=25 LES=0 total=25
    Address: {438a22e068b7d62d50a25706476545ab896e8510} //预设此地址为: 预置资金帐户
    
  3. 生成创世区块配置
    挖矿帐户: 127eb97163f790dd25c174429d85c3fa7c3cf116
    预置资金帐户: 438a22e068b7d62d50a25706476545ab896e8510

    具体操作步骤可参考[五、配置创世区块]
    注: 因大家在本地生成的帐户与教程中的帐户不同,所以需要根据自己生成帐户进行创世区块配置。

  4. 初始化链数据
    • 4.1 根据创始配置(pubchain.json)初始化数据目录
        > geth init --datadir ./data pubchain.json
      

      执行结果如下:

        INFO [10-26|13:42:00] Maximum peer count                       ETH=25 LES=0 total=25
      INFO [10-26|13:42:00] Allocated cache and file handles         database=/Users/nolan/workmeta/pubchain/data/geth/chaindata cache=16 handles=16
      INFO [10-26|13:42:00] Writing custom genesis block
      INFO [10-26|13:42:00] Persisted trie from memory database      nodes=356 size=65.35kB time=3.010068ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
      INFO [10-26|13:42:00] Successfully wrote genesis state         database=chaindata                                          hash=f6fcaa…44829b
      INFO [10-26|13:42:00] Allocated cache and file handles         database=/Users/nolan/workmeta/pubchain/data/geth/lightchaindata cache=16 handles=16
      INFO [10-26|13:42:00] Writing custom genesis block
      INFO [10-26|13:42:00] Persisted trie from memory database      nodes=356 size=65.35kB time=2.1111ms   gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
      INFO [10-26|13:42:00] Successfully wrote genesis state         database=lightchaindata                                          hash=f6fcaa…44829b
      
    • 4.2 查询工程目录结构
        > tree
      

      工程目录如下:

        .
        ├── data
        │   ├── geth              //根据创世配置生成的文件目录
        │   │   ├── chaindata
        │   │   │   ├── 000001.log
        │   │   │   ├── CURRENT
        │   │   │   ├── LOCK
        │   │   │   ├── LOG
        │   │   │   └── MANIFEST-000000
        │   │   └── lightchaindata
        │   │       ├── 000001.log
        │   │       ├── CURRENT
        │   │       ├── LOCK
        │   │       ├── LOG
        │   │       └── MANIFEST-000000
        │   └── keystore        //[geth account new]命令生成的keystore帐户信息
        │       ├── UTC--2018-10-30T02-37-29.289558112Z--127eb97163f790dd25c174429d85c3fa7c3cf116
        │       └── UTC--2018-10-30T02-37-30.431416849Z--438a22e068b7d62d50a25706476545ab896e8510
        └── pubchain.json       //创世区块配置
      
        5 directories, 13 files
        # 工程可参考: https://github.com/ChainDesk/EduEthereumServerDeploy/tree/master/deploy_1
      
  5. 启动节点
    执行启动命令

     > geth --datadir ./data --rpc --rpcaddr "0.0.0.0" --rpcport 8545 --rpcapi "eth,web3,personal,net,miner,admin,debug,db" --unlock 127eb97163f790dd25c174429d85c3fa7c3cf116 --password .passwd --mine
    

    整个启动过程如下:

     INFO [10-30|10:49:32] Maximum peer count                       ETH=25 LES=0 total=25
     INFO [10-30|10:49:32] Starting peer-to-peer node               instance=Geth/v1.8.2-stable/darwin-amd64/go1.10
     INFO [10-30|10:49:32] Allocated cache and file handles         database=/Users/nolan/workmeta/EduEthereumServerDeploy/deploy_1/data/geth/chaindata cache=768 handles=1024
     INFO [10-30|10:49:32] Initialised chain configuration          config="{ChainID: 100 Homestead: 1 DAO: <nil> DAOSupport: false EIP150: 2 EIP155: 3 EIP158: 3 Byzantium: 4 Constantinople: <nil> Engine: clique}"
     INFO [10-30|10:49:32] Initialising Ethereum protocol           versions="[63 62]" 
     INFO [10-30|10:49:32] Starting P2P networking
     ....省去一些日志,要不太长了
     INFO [10-30|10:49:35] Starting mining operation
     INFO [10-30|10:49:35] Commit new mining work                   number=1 txs=0 uncles=0 elapsed=130.025µs
     INFO [10-30|10:49:35] Successfully sealed new block            number=1 hash=9c8e09…919dbe
     INFO [10-30|10:49:35] ? mined potential block                  number=1 hash=9c8e09…919dbe
     INFO [10-30|10:49:35] Commit new mining work                   number=2 txs=0 uncles=0 elapsed=585.992µs
     ^CINFO [10-30|10:49:36] Got interrupt, shutting down...
     INFO [10-30|10:49:36] HTTP endpoint closed                     url=http://0.0.0.0:8545
     INFO [10-30|10:49:36] IPC endpoint closed                      endpoint=/Users/nolan/workmeta/EduEthereumServerDeploy/deploy_1/data/geth.ipc
     INFO [10-30|10:49:36] Writing cached state to disk             block=1 hash=9c8e09…919dbe root=94b9df…184701
     INFO [10-30|10:49:36] Persisted trie from memory database      nodes=0 size=0.00B time=3.947µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
     ......
    

    到此,整个节点就算启动成功了。接下来我们将验证其内部转帐服务。

  6. 节点服务验证
    • 6.1 进入geth命令窗口
        > geth attach http://localhost:8545 //进入geth客户端
      

      进入的命令窗口如下所示:

        Welcome to the Geth JavaScript console!
      
        instance: Geth/v1.8.17-stable-8bbe7207/linux-amd64/go1.10.4
        coinbase: 0xd08e33b7f310aadfe52b5f94c8a04045d2594195
        at block: 841 (Tue, 30 Oct 2018 09:04:09 CST)
         datadir: /opt/node1
         modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 web3:1.0
      
    • 6.2 查看转帐前金额
        # 查看所有帐户
        > personal.listAccounts
        ["0x127eb97163f790dd25c174429d85c3fa7c3cf116", "0x438a22e068b7d62d50a25706476545ab896e8510"]
        # 查看帐户余额(0x127eb97163f790dd25c174429d85c3fa7c3cf116)
        > web3.fromWei(eth.getBalance(web3.personal.listAccounts[0]), "ether")
        0
        # 查看帐户余额(0x438a22e068b7d62d50a25706476545ab896e8510)
        > web3.fromWei(eth.getBalance(web3.personal.listAccounts[1]), "ether")
      9.04625697166532776746648320380374280103671755200316906558262375061821325312e+56
      
    • 6.3 执行转帐操作
      转帐10eth, 0x438a22e068b7d62d50a25706476545ab896e8510 -> 0x127eb97163f790dd25c174429d85c3fa7c3cf116   
      
        > web3.personal.unlockAccount(web3.personal.listAccounts[1])
        Unlock account 0x438a22e068b7d62d50a25706476545ab896e8510
        Passphrase:
        true
        > web3.eth.sendTransaction({from: web3.personal.listAccounts[1], to: web3.personal.listAccounts[0], value: web3.toWei(10, "ether")})
        "0x9a1057c619659a08ee15882f0954b9953435b7d065a64ec37fd5d5c8b0ef6247
    • 6.4 查看转帐后金额
        # 查看帐户余额(0x127eb97163f790dd25c174429d85c3fa7c3cf116)
        > web3.fromWei(eth.getBalance(web3.personal.listAccounts[0]), "ether")
        10.000378
        # 查看帐户余额(0x438a22e068b7d62d50a25706476545ab896e8510)
        > web3.fromWei(eth.getBalance(web3.personal.listAccounts[1]), "ether")
      9.04625697166532776746648320380374280103671755200316906548261997061821325312e+56
© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享