0xTrustTryEP

Just do it, deeply...

Follow me on GitHub

Geth搭建私链

write by donaldhan, 2020-05-19 10:01

引言

区块链当前存在很多公链,针对我们想使用区块链的特性,但对数据不想公开的话,我们需要私有的方式,管理的的链,今天我们来看一下如何私有化部署链。

目录

搭建测试链

我们选择从源码编译的方式,安装geth

下载GO安装包,并解压,配置环境变量

export GO_HOME=/home/donaldhan/Documents/go/go
export PATH=${JAVA_HOME}/bin:${GO_HOME}/bin:$PATH

从github克隆go-ethereum

git clone https://github.com/ethereum/go-ethereum

Building geth requires Go and C compilers to be installed:

sudo apt-get install -y build-essential

Finally, build the geth program using the following command.

cd go-ethereum
make geth

You can now run build/bin/geth to start your node.

配置环境变量

export GO_HOME=/home/donaldhan/Documents/go/go
export GETH_HOME=/home/donaldhan/Documents/ethereum/go-ethereum-master/build
export PATH=${JAVA_HOME}/bin:${GO_HOME}/bin:${GETH_HOME}/bin:$PATH

验证是否安装成功

geth -help

geth

安装参考: Installation Instructions for Ubuntu

你也可以选择在线安装,不过可能一遇到依赖包的问题。

创建初始账户

创建账户, 需要输入账户密码, 主要为了给这些用户初始化币

geth --datadir /home/donaldhan/Documents/data  account new
donaldhan@Donaldhan:~/Documents$ geth --datadir /home/donaldhan/Documents/data  account new
INFO [03-13|15:40:25.094] Maximum peer count                       ETH=50 LES=0 total=50
INFO [03-13|15:40:25.095] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
Your new account is locked with a password. Please give a password. Do not forget this password.
Password: 
Repeat password: 

Your new key was generated

Public address of the key:   0xA65Cf03166298503D8E81053167bB8d97213F4A2
Path of the secret key file: /home/donaldhan/Documents/data/keystore/UTC--2020-03-13T07-40-29.362296417Z--a65cf03166298503d8e81053167bb8d97213f4a2

- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!


会在指定目录下生成/home/donaldhan/Documents/data/keystore目录,并建立一个类似于 ’UTC–2018-10-25T05-33-32.625847732Z–{账户地址}’的文件。 花括号里面就是新生成的账户地址。

donaldhan@Donaldhan:~/Documents$ ls ./data/keystore/
UTC--2020-03-13T07-40-29.362296417Z--a65cf03166298503d8e81053167bb8d97213f4a2

相同的方式,再创建两个账户。

查看我们创建的账户

geth account list --keystore /home/donaldhan/Documents/data/keystore
donaldhan@Donaldhan:~/Documents/data/keystore$ geth account list --keystore /home/donaldhan/Documents/data/keystore
INFO [03-13|15:45:39.193] Maximum peer count                       ETH=50 LES=0 total=50
INFO [03-13|15:45:39.193] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
Account #0: {a65cf03166298503d8e81053167bb8d97213f4a2} keystore:///home/donaldhan/Documents/data/keystore/UTC--2020-03-13T07-40-29.362296417Z--a65cf03166298503d8e81053167bb8d97213f4a2
Account #1: {2916e02bed03db3e3bc7bc50d3dc58600b6345e7} keystore:///home/donaldhan/Documents/data/keystore/UTC--2020-03-13T07-42-14.963869120Z--2916e02bed03db3e3bc7bc50d3dc58600b6345e7
Account #2: {9caffa7e0b76ed259725105ada174041023cf4a3} keystore:///home/donaldhan/Documents/data/keystore/UTC--2020-03-13T07-42-36.078329675Z--9caffa7e0b76ed259725105ada174041023cf4a3

我们创建了3个账户

donaldhan@Donaldhan:~/Documents$ ls ./data/keystore/
UTC--2020-03-13T07-40-29.362296417Z--a65cf03166298503d8e81053167bb8d97213f4a2
UTC--2020-03-13T07-42-14.963869120Z--2916e02bed03db3e3bc7bc50d3dc58600b6345e7
UTC--2020-03-13T07-42-36.078329675Z--9caffa7e0b76ed259725105ada174041023cf4a3
donaldhan@Donaldhan:~/Documents$ 

自定义创世块(Genesis Block)

{
		"config": {
			"chainId": 3131,
			"homesteadBlock": 0,
      "eip150Block": 0,
			"eip155Block": 0,
			"eip158Block": 0
		},
		"difficulty": "10000",
		"gasLimit": "2100000",
		"alloc": {
			"a65cf03166298503d8e81053167bb8d97213f4a2": { "balance": "800" },
			"2916e02bed03db3e3bc7bc50d3dc58600b6345e7": { "balance": "600" },
            "9caffa7e0b76ed259725105ada174041023cf4a3": { "balance": "800" }
		}
}

创世块是区块链中的第一个初始区块,我们通过定义CustomGenesis.json文件来对其进行自定义。

在创世块的配置文件^1中,有四个参数是必须要指定的:

config:区块链的相关参数
    chainId:防止重放攻击(replay attack,简单来说就是未经授权的用户来假扮交易的发送者)
    homesteadBlock:Homestead^2是以太坊的第二个发行版本(Frontier^3是第一个发行版本),值为0表示的使用的是该版本的以太坊
difficult:初始挖矿难度
gasLimit:每一个区块所消耗的gas上限
alloc:可以往地址中预先分配以太币

difficult配置低的话,有利于我们挖矿。

注意,alloc中的账户,必须提前创建

生成创世纪块

使用下列命令来初始化创世块:

geth --datadir /home/donaldhan/Documents/data init /home/donaldhan/Documents/CustomGenesis.json

相关参数说明

  • –datadir:指定区块链的数据目录(默认为~/.ethereum)
  • init:创世块初始化JSON文件的目录

日志输出

donaldhan@Donaldhan:~/Documents$ geth --datadir /home/donaldhan/Documents/data/geth init /home/donaldhan/Documents/CustomGenesis.json
INFO [03-13|16:05:53.355] Maximum peer count                       ETH=50 LES=0 total=50
INFO [03-13|16:05:53.355] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [03-13|16:05:53.360] Allocated cache and file handles         database=/home/donaldhan/Documents/data/geth/geth/chaindata cache=16.00MiB handles=16
INFO [03-13|16:05:53.380] Writing custom genesis block 
INFO [03-13|16:05:53.381] Persisted trie from memory database      nodes=5 size=650.00B time=134.305µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-13|16:05:53.381] Successfully wrote genesis state         database=chaindata hash=67c663…85d298
INFO [03-13|16:05:53.381] Allocated cache and file handles         database=/home/donaldhan/Documents/data/geth/geth/lightchaindata cache=16.00MiB handles=16
INFO [03-13|16:05:53.396] Writing custom genesis block 
INFO [03-13|16:05:53.397] Persisted trie from memory database      nodes=5 size=650.00B time=195.231µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-13|16:05:53.398] Successfully wrote genesis state         database=lightchaindata hash=67c663…85d298
                 hash=54fd3f…dfac23

现在我们启动geth节点。

启动以太坊私有测试链

相关启动参数

启动geth有如下主要参数(可以使用geth –help进行查看):

--nodiscover:关闭节点的可发现性,可以防止使用了相同network id和创世块的节点连接到你的区块链网络中(只能通过手动来添加节点)
--maxpeers 0:指定网络中的最多节点数
--rpc:启用RPC服务
--rpcapi "db,eth,net,web3":指定启用的RPC API
--rpcport "8080":指定RPC的端口
--rpccorsdomain:指定哪些URL可以连接到你的节点
--datadir:以太坊区块链的数据目录
--port:连接到其它节点的网络监听端口
--identity "FirstNode":指定节点名称
console:启动geth控制台程序

启动geth

使用以下命令来启动geth:

geth --identity "ETH-MainNode" --rpc --rpcport "6060" --rpccorsdomain "*" --datadir "/home/donaldhan/Documents/data" --port "30303" --maxpeers 5 --rpcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3" --networkid 3131 console

查看账户余额

> eth.getBalance("a65cf03166298503d8e81053167bb8d97213f4a2")
800
> eth.getBalance("2916e02bed03db3e3bc7bc50d3dc58600b6345e7")
600
> eth.getBalance("9caffa7e0b76ed259725105ada174041023cf4a3")
800
> 

初始化账户余额成功;如果不成功,将data目录下的geth目录移除,重新创建创世纪块,及启动节点即可。

donaldhan@Donaldhan:~/Documents/data$ ls
geth  history  keystore
donaldhan@Donaldhan:~/Documents/data$ rm -r geth
donaldhan@Donaldhan:~/Documents/data$ ls
history  keystore
donaldhan@Donaldhan:~/Documents/data$ rm -r history 
donaldhan@Donaldhan:~/Documents/data$ ls
keystore

使用相同的配置对一个节点进行配置并启动。

注意配置一定要一直, 否则,添加peer节点的时候,可能会失败

启动另外一个节点(ETH-SlaveNode)时, 可以重新定义节点的identity

geth --identity "ETH-SlaveNode" --rpc --rpcport "6060" --rpccorsdomain "*" --datadir "/home/donaldhan/Documents/data" --port "30303" --maxpeers 5 --rpcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3" --networkid 3131 console

当前我们的有连个节点,我们现在两个节点添加到同一链上。

添加Peer节点

在其中ETH-MainNode节点的geth控制台中执行:

admin.nodeInfo

可以的节点信息

> admin.nodeInfo
{
  enode: "enode://f78e7061fbc93ebb03ca7cdb36f2bda17b726cd034ef7d46423af64f8e8040d6fe5379885aa2f40052194421c123627ab4cf311b82d6696856be6b68fe5cb337@127.0.0.1:30303?discport=0",
  enr: "enr:-JC4QE0BLBE3R6dJ1YYMdbWQTUdca46zJiQleYY8yfTSUafjRp2sH-DIvmrFn8MZr0w1rYAsoFtBq54RUEH1lCNyGisBg2V0aMfGhO9Ad0-AgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQP3jnBh-8k-uwPKfNs28r2he3Js0DTvfUZCOvZPjoBA1oN0Y3CCdl8",
  id: "6d0015752dd6c66aa4b89981f627dc4aa35b82d40df2735c85c55d8e46f09c32",
  ip: "127.0.0.1",
  listenAddr: "0.0.0.0:30303",
  name: "Geth/ETH-MainNode/v1.9.12-unstable/linux-amd64/go1.14",
  ports: {
    discovery: 0,
    listener: 30303
  },
  protocols: {
    eth: {
      config: {
        chainId: 3131,
        eip150Block: 0,
        eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
        eip155Block: 0,
        eip158Block: 0,
        homesteadBlock: 0
      },
      difficulty: 10000,
      genesis: "0x67c66380b3275fb6fd3e534ef51ca097fc9c5fbb66aed255ec29869bfe85d298",
      head: "0x67c66380b3275fb6fd3e534ef51ca097fc9c5fbb66aed255ec29869bfe85d298",
      network: 3131
    }
  }
}
> 

从上面,我们可以得到enode信息:

enode: "enode://f78e7061fbc93ebb03ca7cdb36f2bda17b726cd034ef7d46423af64f8e8040d6fe5379885aa2f40052194421c123627ab4cf311b82d6696856be6b68fe5cb337@192.168.230.128:30303?discport=0"

我们需要在另一台ETH-SlaveNode节点上添加ETH-MainNode节点,使用命令:

> admin.addPeer("enode://f78e7061fbc93ebb03ca7cdb36f2bda17b726cd034ef7d46423af64f8e8040d6fe5379885aa2f40052194421c123627ab4cf311b82d6696856be6b68fe5cb337@192.168.230.128:30303")
true

注意返回为true不一定添加成功

我们可以使用如下命令查看是否添加peer

> net.peerCount
1
> admin.peers
[{
    caps: ["eth/63", "eth/64", "eth/65"],
    enode: "enode://f78e7061fbc93ebb03ca7cdb36f2bda17b726cd034ef7d46423af64f8e8040d6fe5379885aa2f40052194421c123627ab4cf311b82d6696856be6b68fe5cb337@192.168.230.128:30303",
    id: "6d0015752dd6c66aa4b89981f627dc4aa35b82d40df2735c85c55d8e46f09c32",
    name: "Geth/ETH-MainNode/v1.9.12-unstable/linux-amd64/go1.14",
    network: {
      inbound: false,
      localAddress: "192.168.230.129:34018",
      remoteAddress: "192.168.230.128:30303",
      static: true,
      trusted: false
    },
    protocols: {
      eth: {
        difficulty: 10000,
        head: "0x67c66380b3275fb6fd3e534ef51ca097fc9c5fbb66aed255ec29869bfe85d298",
        version: 65
      }
    }
}]
> 


当net.peerCount大于0,这可以确认节点添加成功,同时我们可以使用admin.peers,查看对应的节点状态;

我们看到ETH-MainNode已经添加了ETH-SlaveNode节点。

同时在另外peer节点,可以看到控制输出以下日志

INFO [03-16|11:20:42.774] Looking for peers                        peercount=1 tried=2 static=1
INFO [03-16|11:20:59.281] Looking for peers                        peercount=1 tried=6 static=1
INFO [03-16|11:21:14.282] Looking for peers                        peercount=1 tried=2 static=1

peer节点数为1。

测试以太坊私有链

我们可以通过在两个节点分别创建账号构造一笔转账交易来验证多节点的私链是否搭建成功。

挖矿

我们先来体验一下挖矿

分别在两个节点中创建账号:

> personal.newAccount()

设置密码后,得到地址。具体如下:

ETH-MainNode节点:

>  personal.newAccount()
Passphrase: 
Repeat passphrase: 
INFO [03-16|11:24:47.633] Your new key was generated               address=0xB5F49168E69DF3030541199F5A5b3442bB40172C
WARN [03-16|11:24:47.633] Please backup your key file!             path=/home/donaldhan/Documents/data/keystore/UTC--2020-03-16T03-24-40.534092299Z--b5f49168e69df3030541199f5a5b3442bb40172c
WARN [03-16|11:24:47.633] Please remember your password! 
"0xb5f49168e69df3030541199f5a5b3442bb40172c"

ETH-SlaveNode节点:

> personal.newAccount()
Passphrase: 
Repeat passphrase: 
INFO [03-16|11:25:25.479] Your new key was generated               address=0x4B16A5C65FCcac7c7B535b53a7003fAc829543D4
WARN [03-16|11:25:25.480] Please backup your key file!             path=/home/donaldhan/Documents/data/keystore/UTC--2020-03-16T03-25-18.482618070Z--4b16a5c65fccac7c7b535b53a7003fac829543d4
WARN [03-16|11:25:25.480] Please remember your password! 
"0x4b16a5c65fccac7c7b535b53a7003fac829543d4"
> eth.accounts
["0x4b16a5c65fccac7c7b535b53a7003fac829543d4"]
> 

ETH-MainNode节点开始挖矿:

由于新创建的账号中是没有以太币的,因此我们需要启用矿工进行挖矿:

> miner.setEtherbase("0xb5f49168e69df3030541199f5a5b3442bb40172c")

true
> miner.start(1)

INFO [03-13|14:27:54] Updated mining threads                   threads=1
INFO [03-13|14:27:54] Transaction pool price threshold updated price=18000000000
INFO [03-13|14:27:54] Starting mining operation
null
> INFO [03-13|14:27:54] Commit new mining work                   number=1 txs=0 uncles=0 elapsed=0s
INFO [03-13|14:27:57] Generating DAG in progress               epoch=0 percentage=0 elapsed=2.001s
INFO [03-13|14:27:59] Generating DAG in progress               epoch=0 percentage=1 elapsed=3.953s

ETH-SlaveNode节点同样开启挖矿:

> miner.setEtherbase("0x4b16a5c65fccac7c7b535b53a7003fac829543d4")
> miner.start(1)

等一段时间,我们可以看到控制台输出

INFO [03-16|14:05:08.399] 🔨 mined potential block                  number=2378 hash=cb6462…80e194
INFO [03-16|14:05:08.396] Commit new mining work                   number=2379 sealhash=3c6f86…0be3d4 uncles=0 txs=0 gas=0 fees=0 elapsed=143.602µs
> miner.stop()
null

已挖的2379个区块。 查看挖矿账户的余额

> eth.blockNumber
2378
> eth.getBalance("0xb5f49168e69df3030541199f5a5b3442bb40172c")
5.8871875e+21

停止挖矿

> miner.stop()

true
>

这是账户已经有了ETH,我们来体验一下转账交易。

这里有个疑问,没有交易,怎么会凭空产生块呢???,自动挖矿

转账交易

我们新创建一个账号模拟转账:

> personal.newAccount()
Passphrase: 
Repeat passphrase: 
INFO [03-16|14:33:30.865] Looking for peers                        peercount=1 tried=2  static=1
INFO [03-16|14:33:30.943] Your new key was generated               address=0x26d6E7a5593BD71Bfc7E8D1042a975F6E29Ee794
WARN [03-16|14:33:30.945] Please backup your key file!             path=/home/donaldhan/Documents/data/keystore/UTC--2020-03-16T06-33-26.791966726Z--26d6e7a5593bd71bfc7e8d1042a975f6e29ee794
WARN [03-16|14:33:30.945] Please remember your password! 
"0x26d6e7a5593bd71bfc7e8d1042a975f6e29ee794"

从ETH-MainNode节点挖矿账号中转2个以太币到上面这个账号中;

先看一下两个账户的余额

> eth.getBalance("0xb5f49168e69df3030541199f5a5b3442bb40172c")
5.8871875e+21
> eth.getBalance("0x26d6e7a5593bd71bfc7e8d1042a975f6e29ee794")
0

在转账之前,先解锁账号

> personal.unlockAccount("0xb5f49168e69df3030541199f5a5b3442bb40172c")
Unlock account 0xb5f49168e69df3030541199f5a5b3442bb40172c
Passphrase: 
true
> 

从原始账户转账到新创建的账户

 eth.sendTransaction({from: "0xb5f49168e69df3030541199f5a5b3442bb40172c", to: "0x26d6e7a5593bd71bfc7e8d1042a975f6e29ee794", value: web3.toWei(2, "ether")})
>  eth.sendTransaction({from: "0xb5f49168e69df3030541199f5a5b3442bb40172c", to: "0x26d6e7a5593bd71bfc7e8d1042a975f6e29ee794", value: web3.toWei(2, "ether")})
INFO [03-16|16:22:24.014] Setting new local account                address=0xB5F49168E69DF3030541199F5A5b3442bB40172C
INFO [03-16|16:22:24.014] Submitted transaction                    fullhash=0xea177fff6c80eac5e57b537b6452e1617533e4d6853698d39e749574b30c19c6 recipient=0x26d6E7a5593BD71Bfc7E8D1042a975F6E29Ee794
"0xea177fff6c80eac5e57b537b6452e1617533e4d6853698d39e749574b30c19c6"
> 

查看对应账号的余额。

> eth.getBalance("0xb5f49168e69df3030541199f5a5b3442bb40172c")
5.8871875e+21
> eth.getBalance("0x26d6e7a5593bd71bfc7e8d1042a975f6e29ee794")
0

这是我们查看收款账号是没有余额的

> eth.getBalance("0x26d6e7a5593bd71bfc7e8d1042a975f6e29ee794")
0

这是因为这个交易没有上链,我们

我们执行挖矿操作

> miner.start(1)
INFO [03-16|16:26:28.157] Updated mining threads                   threads=1
INFO [03-16|16:26:28.158] Transaction pool price threshold updated price=1000000000
null
> INFO [03-16|16:26:28.158] Commit new mining work                   number=2379 sealhash=5a4f9c…74fa83 uncles=0 txs=0 gas=0 fees=0 elapsed=404.089µs
INFO [03-16|16:26:28.159] Commit new mining work                   number=2379 sealhash=25c415…25f46c uncles=0 txs=1 gas=21000 fees=2.1e-05 elapsed=1.243ms
INFO [03-16|16:26:35.035] Looking for peers                        peercount=1 tried=2 static=1
> miner.stop
function()
> miner.stopINFO [03-16|16:26:43.178] Successfully sealed new block            number=2379 sealhash=25c415…25f46c hash=20c93f…5f0c2d elapsed=15.019s
INFO [03-16|16:26:43.179] 🔨 mined potential block                  number=2379 hash=20c93f…5f0c2d
INFO [03-16|16:26:43.180] Commit new mining work                   number=2380 sealhash=e0ac34…d09cce uncles=0 txs=0 gas=0     fees=0       elapsed=234.21µs

然后停止挖矿

> miner.stop()
null

再次查看收款账户余额,如下

> eth.getBalance("0x26d6e7a5593bd71bfc7e8d1042a975f6e29ee794")
2000000000000000000

表明转账成功。

使用原始账户转账,会出现,账户余额不足的情况,具体原因见附篇问题集;

查看交易信息

eth.getTransaction('0xea177fff6c80eac5e57b537b6452e1617533e4d6853698d39e749574b30c19c6')
eth.getTransaction('0xea177fff6c80eac5e57b537b6452e1617533e4d6853698d39e749574b30c19c6')
{
  blockHash: "0x20c93f53d1eadc665f3c7e9cdf0eaca296daffec9f5c4f2aed7d568cbc5f0c2d",
  blockNumber: 2379,
  from: "0xb5f49168e69df3030541199f5a5b3442bb40172c",
  gas: 21000,
  gasPrice: 1000000000,
  hash: "0xea177fff6c80eac5e57b537b6452e1617533e4d6853698d39e749574b30c19c6",
  input: "0x",
  nonce: 0,
  r: "0xebdb97967ce641501c5505c4e4aae26a4c4548c9bbd32678315fe1caecf2313d",
  s: "0x50f000622ad1908f7a2e35f984d19fa9172d2e2852936240222043568b0d76a3",
  to: "0x26d6e7a5593bd71bfc7e8d1042a975f6e29ee794",
  transactionIndex: 0,
  v: "0x1899",
  value: 2000000000000000000
}

在ETH-SlaveNode节点上查看交易,及账户余额

> eth.getTransaction('0xea177fff6c80eac5e57b537b6452e1617533e4d6853698d39e749574b30c19c6')
{
  blockHash: "0x20c93f53d1eadc665f3c7e9cdf0eaca296daffec9f5c4f2aed7d568cbc5f0c2d",
  blockNumber: 2379,
  from: "0xb5f49168e69df3030541199f5a5b3442bb40172c",
  gas: 21000,
  gasPrice: 1000000000,
  hash: "0xea177fff6c80eac5e57b537b6452e1617533e4d6853698d39e749574b30c19c6",
  input: "0x",
  nonce: 0,
  r: "0xebdb97967ce641501c5505c4e4aae26a4c4548c9bbd32678315fe1caecf2313d",
  s: "0x50f000622ad1908f7a2e35f984d19fa9172d2e2852936240222043568b0d76a3",
  to: "0x26d6e7a5593bd71bfc7e8d1042a975f6e29ee794",
  transactionIndex: 0,
  v: "0x1899",
  value: 2000000000000000000
}
> eth.getBalance("0x26d6e7a5593bd71bfc7e8d1042a975f6e29ee794")
2000000000000000000
> 

信息与MainNode节点信息,一致,表明在同一个链,同时数据一致。

我们也可以通过以太坊浏览器查看先关交易和区块,具体参考:

区块链浏览器

至此,我们的私链搭建完成。

总结

我们从源码编译安装的以太坊客户端GETH,使用两个节点体验了一下挖矿和转账交易的整个过程。在初始化创世纪块的时候,如果要给初始账号分配余额,需要这些账户需要提前创建。在添加peer节点的时候,注意两个节点的配置要一致,特别是chainId,同时节点时可发现的(disable –nodiscover)。在转账之前需要解锁转账账户,一般我们不会控制直接解锁账号,这种情况容易导致账户资金被HTTP方式转移。解锁账户需要开启(–allow-insecure-unlock)配置。

参考文献

geth
go-ethereum
go-ethereum github
private blockchain generate
geth Command-Line-Options
Geth管理API文档 Web3.js 0.20 使用说明 以太坊测试私链的搭建
Geth 环境搭建及使用说明
geth使用笔记
以太坊创世区块与链配置载入分析
搭建环境,搭建私链
以太坊私链搭建
以太坊文档整理
创建有余额的账户

在线安装GETH

sudo apt-get install -q -y software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update -q -y
sudo apt-get install -q -y eth

创建账号的以另一种方式

$ geth account new
INFO [03-13|14:22:54] Maximum peer count                       ETH=25 LES=0 total=25
Your new account is locked with a password. Please give a password. Do not forget this password.
!! Unsupported terminal, password will be echoed.
Passphrase: 123456

Repeat passphrase: 123456

Address: {20edf297f7d0758b3cd8d36e3ae5d9fe02b62fad}

服务启动

最快速启动 geth 的方式就是创建一个全节点的以太坊主网络,使用命令如下:

geth console

其中 console 是指开启交互式命令控制台,我们可以在控制台上输入各种 geth 的命令,比如调用查看账号信息,查看账号金额,转账交易等。

服务启动后过一会就会开始同步主网络的信息,主网络的信息是非常庞大的,以前有人同步了十几天都没有同步完成,如果只是本地测试,可以用下面的方式搭建测试网络。

文件存储 geth 的默认存储路径是~/.ethereum,当然你也可以通过–datadir 文件路径 这个命令来改变存储的位置。

管理api补充

eth.accounts 或者 personal.listAccounts 列出 geth 阶段创建的所有账号。

> eth.accounts
["0xa65cf03166298503d8e81053167bb8d97213f4a2", "0x2916e02bed03db3e3bc7bc50d3dc58600b6345e7", "0x9caffa7e0b76ed259725105ada174041023cf4a3"]
> 

查询账号的金额,需要输入账号地址。

> eth.getBalance("0x9caffa7e0b76ed259725105ada174041023cf4a3")
0

eth.gasPrice 查询矿工”工作量”价格,想了解更多关于gas的内容请参考这篇文章。

eth.gasPrice
>
10000

eth.sendTransaction 或者 personal.sendTransaction 进行转账交易,需要输入转账的账号地址和转账金额。

获取最新区块号:eth.blockNumber

> eth.blockNumber
0

相关问题

Unable to locate package ethereum

E: Unable to locate package ethereum
donaldhan@Donaldhan:~$ sudo apt-get install -q -y eth
[sudo] password for donaldhan: 
Reading package lists...
Building dependency tree...
Reading state information...
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 eth : Depends: libboost-random1.58.0 but it is not installable
       Depends: libboost-thread1.58.0 but it is not installable
       Depends: libjsoncpp0v5 but it is not installable
       Depends: libjsonrpccpp-client0 but it is not installable
       Depends: libjsonrpccpp-common0 but it is not installable
       Depends: libjsonrpccpp-server0 but it is not installable
       Depends: libethereum (= 1.2.9-SNAPSHOT--20160629-6a05dd7~wily-0ubuntu2) but it is not going to be installed
       Depends: libleveldb1v5 but it is not installable
E: Unable to correct problems, you have held broken packages.

出现这个问题,我也没有办法,依赖包确实,试了好多方法,最后放弃,决定使用源码编译安装模式。

Fatal: Failed to write genesis block

Fatal: Failed to write genesis block: unsupported fork ordering: eip150Block not enabled, but eip155Block enabled at 0

问题原因配置缺失,具体如下:

donaldhan@Donaldhan:~/Documents$ cat CustomGenesis.json 
{
		"config": {
			"chainId": 3131,
			"homesteadBlock": 0,
			"eip150Block": 0,
			"eip155Block": 0,
			"eip158Block": 0
		},
		"difficulty": "10000",
		"gasLimit": "2100000",
		"alloc": {
			"a65cf03166298503d8e81053167bb8d97213f4a2": { "balance": "800" },
			"2916e02bed03db3e3bc7bc50d3dc58600b6345e7": { "balance": "600" },
            "9caffa7e0b76ed259725105ada174041023cf4a3": { "balance": "800" }
		}
}

我的是缺失如下配置

"eip150Block": 0,

Fatal: Failed to write genesis block

新增节点后不生效

首先确保enode信息正确包,包括端口,ip地址等。 同时节点的配置也要一致,在此基础上可以通过以下中方式解决。

  1. 控制台命令添加
> admin.addPeer("enode://f78e7061fbc93ebb03ca7cdb36f2bda17b726cd034ef7d46423af64f8e8040d6fe5379885aa2f40052194421c123627ab4cf311b82d6696856be6b68fe5cb337@192.168.230.128:30303")
true
  1. 启动时指定 ``` geth –bootnodes enode://f78e7061fbc93ebb03ca7cdb36f2bda17b726cd034ef7d46423af64f8e8040d6fe5379885aa2f40052194421c123627ab4cf311b82d6696856be6b68fe5cb337@192.168.230.128:30303 –identity “ETH-SlaveNode” –rpc –rpcport “6060” –rpccorsdomain “*” –datadir “/e/geth/data” –port “30303” –maxpeers 5 –rpcapi “admin,db,eth,debug,miner,net,shh,txpool,personal,web3” –networkid 3131 console

3. 在静态节点文件中添加

donaldhan@Donaldhan:~/Documents/data$ cd geth/ donaldhan@Donaldhan:~/Documents/data/geth$ ls chaindata lightchaindata nodekey static-nodes.json ethash LOCK nodes transactions.rlp donaldhan@Donaldhan:~/Documents/data/geth$ pwd /home/donaldhan/Documents/data/geth donaldhan@Donaldhan:~/Documents/data/geth$ cat static-nodes.json [ “enode://f78e7061fbc93ebb03ca7cdb36f2bda17b726cd034ef7d46423af64f8e8040d6fe5379885aa2f40052194421c123627ab4cf311b82d6696856be6b68fe5cb337@192.168.230.128:30303” ]


我的是由于配置不一致导致的,具体原因具体对待。 如果不行可以启动时,开启debug日志看一下。
[新增节点后不生效](https://www.netkiller.cn/blockchain/ethereum/faq.add.node.html)  



### Error: authentication needed: password or unlock

Error: authentication needed: password or unlock at web3.js:6347:37(47) at web3.js:5081:62(37) at :1:21(15)


问题原因,账户钱包被锁,需要使用如下命令先解锁

personal.unlockAccount(“0x7dccf9e1f506cefb3e3f2b86d80af52573614209”)




### account unlock with HTTP access is forbidden

GoError: Error: account unlock with HTTP access is forbidden at web3.js:6347:37(47) at native at :1:24(3)


问题原因:在控制命令下,禁止解锁账户,防止通过HTTP方式转移账户资金的情况发生。解决方式,我们只需要,在启动时,添加如下配置即可

–allow-insecure-unlock


我的启动方式

geth –identity “ETH-MainNode” –rpc –rpcport “6060” –rpccorsdomain “*” –datadir “/home/donaldhan/Documents/data” –port “30303” –maxpeers 5 –allow-insecure-unlock –rpcapi “admin,db,eth,debug,miner,net,shh,txpool,personal,web3” –networkid 3131 console


[GoError: Error: account unlock ](https://github.com/ethereum/go-ethereum/issues/19507)

### insufficient funds for gas * price + value

Error: insufficient funds for gas * price + value at web3.js:6347:37(47) at web3.js:5081:62(37) at :1:21(15)

问题原因:
据说是个bug,是由于数字进行16进制转换四舍五入导致的。有一种解决方式

或者直接引用账号

personal.unlockAccount(eth.accounts[0]) true eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(“0.1”, “ether”)}) “0x178d40ffeeaa30bbab397d038b1a88a91639ad0c57abdb5b19b63b1ae5dc83f4” ```

另外一种方式,直接使用Geth命令执行

geth --exec "eth.sendTransaction({from:'0xXXXX', to:'0xXXXX', value: 44999159999999999996, gas:21000});" attach

还有修改配置创世纪文件的,不过上面两个方式,没试过。

最后我将转账的账户换为挖矿账户,转账成功;我的原先转账失败,使用的是创世纪的初始账户。 具体失败原因待解决。

Insufficient funds error when sending entire balance
Error: insufficient funds for gas * price + value
Insufficient funds for gas * price + value

Error: intrinsic gas too low

Error: intrinsic gas too low
	at web3.js:6347:37(47)
	at web3.js:5081:62(37)
	at <eval>:1:21(19)

error Intrinsic gas too low