使用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: 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")})
   

 

转载于:https://www.cnblogs.com/shuaixiha/p/9929081.html

你可能感兴趣的:(使用Docker Images ethereum/client-go 搭建以太坊节点)