使用Docker Images ethereum/client-go 搭建以太坊節點


我的環境: VirtualBox Ubuntu

1. 此過程是以Docker為基礎操作的, 所以首先需要安裝Docker。
    可參考:https://www.cnblogs.com/shuaixiha/p/9923041.html
    
2. 下載以太坊的鏡像
  docker pull ethereum/client-go

3. 創建文件夾
  mkdir ethereum_node
  cd ethereum_node
  vi start-node.sh

  文件start-node.sh的內容如下:
  #!/bin/bash -e
  docker rm -f eth_node || true
  NETWORKID=12345
  DATADIR=/home/vagrant/ethereum_node/chain
  mkdir -p $DATADIR
  docker run -it --name eth_node -v $DATADIR:/root/.ethereum  -p 8545:8545  -p 30303:30303 ethereum/client-go --networkid $NETWORKID  --ws --rpc --rpcaddr 0.0.0.0 --rpccorsdomain '*' --rpcapi "db,eth,net,web3,personal" --wsapi "personal,web3" console
    
4. 執行shell文件start-node.sh ( 如執行文件沒有權限需 chmod 777 start-node.sh )

  [shaiwang@vagrant]$./start-node.sh
  eth_node
  INFO [11-08|06:29:37] Maximum peer count                                ETH=25 LES=0 total=25
  INFO [11-08|06:29:37] Starting peer-to-peer node                     instance=Geth/v1.8.11-unstable-c8dcb958/linux-amd64/go1.10.2
  INFO [11-08|06:29:37] Allocated cache and file handles              database=/root/.ethereum/geth/chaindata cache=768 handles=1024
  INFO [11-08|06:29:37] Writing default main-net genesis block
  INFO [11-08|06:29:38] Persisted trie from memory database      nodes=12356 size=2.34mB time=42.060416ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
  INFO [11-08|06:29:38] Initialised chain configuration                  config="{ChainID: 1 Homestead: 1150000 DAO: 1920000 DAOSupport: true EIP150: 2463000 EIP155: 2675000 EIP158: 2675000 Byzantium: 4370000 Constantinople: <nil> Engine: ethash}"
  INFO [11-08|06:29:38] Disk storage enabled for ethash caches   dir=/root/.ethereum/geth/ethash count=3
  INFO [11-08|06:29:38] Disk storage enabled for ethash DAGs     dir=/root/.ethash               count=2
  INFO [11-08|06:29:38] Initialising Ethereum protocol                  versions="[63 62]" network=12345
  INFO [11-08|06:29:38] Loaded most recent local header              number=0 hash=d4e567…cb8fa3 td=17179869184
  INFO [11-08|06:29:38] Loaded most recent local full block          number=0 hash=d4e567…cb8fa3 td=17179869184
  INFO [11-08|06:29:38] Loaded most recent local fast block         number=0 hash=d4e567…cb8fa3 td=17179869184
  INFO [11-08|06:29:38] Regenerated local transaction journal      transactions=0 accounts=0
  INFO [11-08|06:29:38] Starting P2P networking
  INFO [11-08|06:29:40] UDP listener up                          self=enode://90cdb102e7afe173345a4dea883a1cf02ec55990fe44f66a8848030d670b290ddb995561a94cd61d18a4745e68847d3e8ca4518546921aec7287e786662ef2b6@[::]:30303
  INFO [11-08|06:29:40] RLPx listener up                         self=enode://90cdb102e7afe173345a4dea883a1cf02ec55990fe44f66a8848030d670b290ddb995561a94cd61d18a4745e68847d3e8ca4518546921aec7287e786662ef2b6@[::]:30303
  INFO [11-08|06:29:40] IPC endpoint opened                                url=/root/.ethereum/geth.ipc
  INFO [11-08|06:29:40] HTTP endpoint opened                            url=http://0.0.0.0:8545      cors=* vhosts=localhost
  INFO [11-08|06:29:40] WebSocket endpoint opened                   url=ws://127.0.0.1:8546
  Welcome to the Geth JavaScript console!

  instance: Geth/v1.8.11-unstable-c8dcb958/linux-amd64/go1.10.2
  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
  []
        
5. 此時節點中沒有賬戶, 需創建賬戶如下:
    // 表示創建的賬戶密碼為空
  > personal.newAccount("")
  "0xa60c18c6c8219d5ce26274d32cd03f4ddcd6df4a"


6. 查看所有的賬戶
  > eth.accounts
  ["0xa60c18c6c8219d5ce26274d32cd03f4ddcd6df4a"]
  或者
  > personal.listAccounts
  ["0xa60c18c6c8219d5ce26274d32cd03f4ddcd6df4a"]

7. 查詢賬戶余額
  eth.getBalance(eth.accounts[0])
    或者
  web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")

8. 開始挖礦
    miner.start()
    
9. 停止挖礦
    miner.stop()

10. 當賬戶有余額的時候可進行交易
    personal.unlockAccount("0x21cb7cb49d4fe1c7afde2a60690219c3dad0b357", "")        // 交易之前需先解鎖賬戶, 否則交易會報錯
    
    > eth.sendTransaction( {from: 發起交易賬戶地址, to: 接收賬戶地址, value: 貨幣數量} )
    例:
    eth.sendTransaction({from: '0x21cb7cb49d4fe1c7afde2a60690219c3dad0b357', to: '0x90380f68612fee4288699b41daf6a81fbb9b5972', value: web3.toWei(1, "ether")})
   

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM