Geth常用操作

什么是 geth

全称为 Go-Etherenum

是一个用 go 实现的以太坊客户端

使用方法:

  1. 官网安装

  2. geth --datadir testNet --dev console 2>> test.log开启一个开发者网络节点

  3. geth --datadir testNet --dev console 2>> test.log --rpc --rpccorsdomain https://remix.ethereum.org

  4. 允许 HTTP 链接 unlock 账户:--allow-insecure-unlock

  5. tail -f test.log实时监控日志状态

  6. 配置文件修改

    • 新建创世块参数文件 genesis.json

    • {
        "config": {
          "chainId": 15,
          "homesteadBlock": 0,
          "eip155Block": 0,
          "eip158Block": 0
        },
        "difficulty": "10000",
        "gasLimit": "2100000",
        "alloc": {
          "7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
          "f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }
        }
      }
    • geth --datadir ./ethdev init genesis.json:初始化测试网络的以太坊。./ethdev是存放测试网络数据的文件夹,可以任意指定

  7. 常用方法:

  • eth:
    • eth.accounts:查看网络中所有账户
    • eth.getBalance(account):获得账户余额
    • eth.sendTransaction({from:’', to:’', value:web3.toWei(99, "ether")}):发起一笔交易(转账)
  • personal:
    • personal.listAccounts:查看网络中所有用户
    • personal.newAccount(”accountPasswd“):创建一个新的账户
    • personal.unlockAccount(eth.account[1],"passwd"):解锁一个账户
  • 挖矿
    • 开始挖矿:miner.start(),不停地挖矿,会返回一个null
    • 只挖一个区块miner.start(1)
      • 挖矿奖励默认发送给 eth.accounts[0]
      • 测试网络里,把 genesis.json 中的 “difficulty” 设低一点,会比较好挖。
    • 停止挖矿:miner.stop()
    • geth --mine --minerthreads=4--minerthreads 设置并行挖矿的线程数量,默认为所有的处理器数量;
    • 稍微复杂点的挖矿命令:geth --mine --minerthreads 4 --datadir /usr/local/share/ethereum/30303 --port 30303:使用不同数据目录(ethereum/30303)和不同的端口(30303)挖矿
    • 发放奖励:eth.etherbase(也叫 coinbase)是一个地址,挖矿奖励会发到这个地址里。改变 etherbase 的方式如下
    • geth --etherbase 1 --mine:改变 etherbase 为编号 1 的地址;
    • 控制台:> miner.setEtherbase(eth.accounts[2])
  • 交易
    • eth.getBlock(i).miner:查看块的挖出者
    • eth.getBlockTransactionCount("pending"):查看未确认交易的数量、
    • eth.getBlock("pending", true).transactions:查看所有未确认交易
查看评论