安装geth

直接在https://geth.ethereum.org/下载安装即可

配置初始化json

在geth.exe所在目录下,新建genesis.json文件,配置如下:

  1. {
  2. "config": {
  3. "chainId": 666,
  4. "homesteadBlock": 0,
  5. "eip150Block": 0,
  6. "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  7. "eip155Block": 0,
  8. "eip158Block": 0,
  9. "byzantiumBlock": 0,
  10. "constantinopleBlock": 0,
  11. "petersburgBlock": 0,
  12. "istanbulBlock": 0,
  13. "ethash": {}
  14. },
  15. "nonce": "0x0",
  16. "timestamp": "0x5ddf8f3e",
  17. "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
  18. "gasLimit": "0x47b760",
  19. "difficulty": "0x00002",
  20. "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  21. "coinbase": "0x0000000000000000000000000000000000000000",
  22. "alloc": {},
  23. "number": "0x0",
  24. "gasUsed": "0x0",
  25. "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
  26. }

其中:

  • difficulty:表示挖矿难度,参数越小越容易
  • gasLimit:转账交易手续费
  • nonce: 一个64位随机数,用于挖矿
  • extraData: 附加信息,可以填你的个性信息
  • coinbase: 矿工账号
  • alloc: 用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以我们不需要预置有币的账号,需要的时候自己创建即可以
  • timestamp: 设置创世块的时间戳
  • parentHash: 上一个区块的hash值,因为是创世块,所以这个值是0
  • chainId: 区块链识别ID,不可与主网及其他测试网ID冲突

初始化私链

  1. 选择一个目录作为数据存放目录,绝对路径与相对路径皆可,目录不存在则自动创建
  2. 执行以下指令: geth —datadir “testchain” init genesis.json
    1. E:\geth>geth --datadir "testchain" init genesis.json
    2. INFO [01-22|14:17:37.013] Maximum peer count ETH=50 LES=0 total=50
    3. INFO [01-22|14:17:37.165] Set global gas cap cap=25000000
    4. INFO [01-22|14:17:37.167] Allocated cache and file handles database=E:\geth\testchain\geth\chaindata cache=16.00MiB handles=16
    5. INFO [01-22|14:17:37.246] Writing custom genesis block
    6. INFO [01-22|14:17:37.248] Persisted trie from memory database nodes=0 size=0.00B time=0s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
    7. INFO [01-22|14:17:37.253] Successfully wrote genesis state database=chaindata hash="d3d6bb…c5304a"
    8. INFO [01-22|14:17:37.257] Allocated cache and file handles database=E:\geth\testchain\geth\lightchaindata cache=16.00MiB handles=16
    9. INFO [01-22|14:17:37.363] Writing custom genesis block
    10. INFO [01-22|14:17:37.364] Persisted trie from memory database nodes=0 size=0.00B time=0s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
    11. INFO [01-22|14:17:37.369] Successfully wrote genesis state database=lightchaindata hash="d3d6bb…c5304a"
    其中:
  • —datadir:指定数据文件夹
  • init:表示初始化
  • genesis.json: 表示创始区块配置文件路径


启动私链

执行以下指令: geth —identity chain —datadir testchain —networkid 666 —nodiscover console 2>log.txt

  1. E:\geth>geth --identity chain --datadir testchain --networkid 666 --nodiscover console 2>log.txt
  2. Welcome to the Geth JavaScript console!
  3. instance: Geth/chain/v1.9.25-stable-e7872729/windows-amd64/go1.15.6
  4. at block: 0 (Thu Nov 28 2019 17:11:26 GMT+0800 (CST))
  5. datadir: E:\geth\testchain
  6. modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
  7. To exit, press ctrl-d
  8. >

其中:

  • —identity: 区块链的标示,随便填写,用于标示目前网络的名字
  • —datadir:指定数据文件夹
  • —networkid 666:指定连接哪个网络,此处666为genesis.json中配置的网络ID
  • —nodiscover: 表示不搜索其他节点
  • console: 表示启动后打开控制台
  • 2>log.txt: 错误记录重定向到log.txt


测试私链

  1. 创建账户

    1. > eth.accounts
    2. []
    3. > personal.newAccount("test1")
    4. "0x193784cea02443b5aeedc3514d3b5f0f01e77eac"
    5. > personal.newAccount("test2")
    6. "0xacc1f4deff6900965c02e3a773113dcb8699fcce"
    7. > eth.accounts
    8. ["0x193784cea02443b5aeedc3514d3b5f0f01e77eac", "0xacc1f4deff6900965c02e3a773113dcb8699fcce"]
    9. > eth.getBalance(eth.accounts[0])
    10. 0
    11. > eth.getBalance(eth.accounts[1])
    12. 0
    13. >
  2. 挖矿测试

    1. > eth.coinbase
    2. "0x193784cea02443b5aeedc3514d3b5f0f01e77eac"
    3. > eth.getBalance(eth.accounts[0])
    4. 0
    5. > eth.getBalance(eth.accounts[1])
    6. 0
    7. > eth.accounts
    8. ["0x193784cea02443b5aeedc3514d3b5f0f01e77eac", "0xacc1f4deff6900965c02e3a773113dcb8699fcce"]
    9. > miner.start()
    10. null
    11. > eth.mining
    12. true
    13. > miner.stop()
    14. null
    15. > eth.getBalance(eth.accounts[0])
    16. 20000000000000000000
    17. >