# 开发DAPP(1) - geth篇

By [Untitled](https://paragraph.com/@0x36d3a7c6abea02f762192e9bb714aef65ae2834b) · 2022-01-05

---

什么是geth
-------

Geth是由以太坊基金会提供的官方客户端软件，用Go编程语言编写的。Geth提供了一个交互式命令控制台，通过命令控制台中包含了以太坊的各种功能（API）。全名go-ethereum，github地址[go-ethereum](https://github.com/ethereum/go-ethereum)。wiki里为[使用文档](https://github.com/ethereum/go-ethereum/wiki)。

安装GETH（MAC）
-----------

    brew tap ethereum/ethereum
    brew install ethereum
    

tip: 如遇安装报错--No such file or directory  _@_  _dir\_chdir_ -  _/usr/local/Cellar_

手动创建/usr/local/Cellar文件，并添加权限即可解决

使用GETH搭建私链
----------

1、初始化配置文件,工程根目录添加文件genesis.json

    {
      "config": {
        "chainId": 123,
        "homesteadBlock": 0,
        "eip150Block": 0,
        "eip155Block": 0,
        "eip158Block": 0,
        "byzantiumBlock": 0,
        "constantinopleBlock": 0,
        "petersburgBlock": 0,
        "istanbulBlock": 0
      },
      "alloc": {},
      "coinbase": "0x0000000000000000000000000000000000000000",
      "difficulty": "0x2000",
      "extraData": "",
      "gasLimit": "0x2fefd8",
      "nonce": "0x0000000000000042",
      "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
      "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
      "timestamp": "0x00"
    }
    
    复制代码
    

2、工程目录下初始化私链

    geth --datadir "chain" init genesis.json
    

3、启动私链

     geth --datadir "chain" --http --http.api personal,eth,net,web3 console --allow-insecure-unlock
    

geth 参数通过geth help查看

console配置是打开js交互控制台，进入控制台后，命令参考[web3js.readthedocs.io/en/v1.5.2/](https://link.juejin.cn?target=https%3A%2F%2Fweb3js.readthedocs.io%2Fen%2Fv1.5.2%2F) 此时会启动web服务，默认端口为8545

常用命令

*   命令含义eth.blockNumber
    
*   区块数量eth.accounts
    
*   区块账户信息miner.start(1)开始挖矿,参数为挖矿线程数
    
*   eth.mining挖矿状态
    
*   personal.newAccount()新建账户
    

测试新建账户, 123456为密码

    personal.newAccount('123456')
    

查看账户

    eth.accounts
    

此时命令行会输出一个账号

完成上面的基本操作，开始尝试通过geth完成两个账户之间的转账。

基于geth链，完成一笔交易
--------------

再添加一个账户

    personal.newAccount('123456')
    

默认挖矿的收益都归属于第一个账户，也可以手动配置

    miner.setEtherbase(eth.accounts[0])
    

设置完成后，查看

    eth.coinbase
    

会输出我们刚刚设置的账户地址。

接下来开始挖矿，挖一个区块就停止。

    miner.start(1);admin.sleepBlocks(1);miner.stop();
    

等待完成后，通过`eth.blockNumber`查询区块数量；通过`web3.fromWei(eth.getBalance(eth.accounts[0]),"ether")`查看挖矿收益。eth.getBalance(addr)会返回账户的余额，余额以wei为单位，传入的参数是账户的公钥，web3.fromWei单位转换，在这个例子中是将wei转换成ether。

### 解锁账户

在发送转账交易之前我们需要先解锁账户，否则会报错。执行以下命令解锁账户：

    personal.unlockAccount(eth.accounts[0], "123456")
    

personal.unlockAccount(addr, passwd, duration)命令用来解锁账户，第一个参数传入需要解锁的账户地址，第二参数传入账户密码，第三个参数传入账户解锁状态持续时间，单位是秒。

如果之前的命令行中没有--allow-insecure-unlock，会报错`account unlock with HTTP access is forbidden`。

使用最新版本geth客户端，当执行personal.unlockAccount()或在程序中调用personal\_unlockAccount接口时，会出现：account unlock with HTTP access is forbidden异常。 异常原因: 新版本geth，出于安全考虑，默认禁止了HTTP通道解锁账户。

### 开始交易

    eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(1,'ether')})
    

eth.sendTransaction(transactionObject)用来向区块链网络中发送一笔转账交易。当交易发送到区块链后，会返回一个交易hash，此时的交易正在矿工的交易池中等待被打包。

### 查看交易池等待被打包的交易

`txpool.status`返回正在等待打包的交易。其中有一条pending的交易，pending表示已提交但未被处理的交易。

![查看状态](https://storage.googleapis.com/papyrus_images/02a763b042154880bf72d4f4b92c8b67627124b7bff03fbd9753a207635a4002.png)

查看状态

### 查看pending交易的详情

`txpool.inspect.pending`返回pending交易的详细信息。

要想交易被处理，必须要挖矿。这里启动挖一个区块；

    miner.start(1);admin.sleepBlocks(1);miner.stop();
    

挖到矿后，现在交易已经被成功打包了，可以通过账户查看账户余额时候变动

---

*Originally published on [Untitled](https://paragraph.com/@0x36d3a7c6abea02f762192e9bb714aef65ae2834b/dapp-1-geth)*
