Go-ethereum

blockchain

Posted by lanbery on July 8, 2019

Go-ethereum compiler in windows 坑


go get -v -u -x github.com/ethereum/go-ethereum

Issue 坑

升级Golang 时覆盖文件问题导致编译报错

/usr/local/go/src/crypto/rc4/rc4_asm.go:15:18: (*Cipher).XORKeyStream redeclared in this block
previous declaration at /usr/local/go/src/crypto/rc4/rc4.go:61:6    # bytes
/usr/local/go/src/bytes/bytes_decl.go:10:6: IndexByte redeclared in this block
previous declaration at /usr/local/go/src/bytes/bytes.go:101:34
/usr/local/go/src/bytes/bytes_decl.go:17:6: Equal redeclared in this block
previous declaration at /usr/local/go/src/bytes/bytes.go:18:25
/usr/local/go/src/bytes/bytes_decl.go:24:6: Compare redeclared in this block
previous declaration at /usr/local/go/src/bytes/bytes.go:37:27
# crypto/cipher
/usr/local/go/src/crypto/cipher/xor_amd64.go:9:6: xorBytes redeclared in this block
previous declaration at /usr/local/go/src/crypto/cipher/xor.go:58:33
/usr/local/go/src/crypto/cipher/xor_amd64.go:22:6: xorWords redeclared in this block
previous declaration at /usr/local/go/src/crypto/cipher/xor.go:83:27
# strings
/usr/local/go/src/strings/strings_decl.go:8:6: IndexByte redeclared in this block
previous declaration at /usr/local/go/src/strings/strings.go:150:34

解决办法

whereis go rm -rf [all golang] 重新安装

参见安装够浪


go-eth Commands

  • geth init ./genesis.json –datadir “./chain”
  • 启用私链
  • 创建账户
  • 挖矿

初始化一个创世区块

初始化创世区块时,要先创建一个genesis.json文件,内容如下:

{
  "config": {
        "chainId": 10,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x20000",
  "extraData"  : "",
  "gasLimit"   : "0xffffffff",
  "nonce"      : "0x0000000000000042",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00",
  "alloc"      : {}
}
参数名称 参数描述
mixhash 与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意他和nonce的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件
nonce nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件
difficulty 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度
alloc 用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以我们不需要预置有币的账号,需要的时候自己创建即可以
coinbase 矿工的账号,随便填
timestamp 设置创世块的时间戳
parentHash 上一个区块的hash值,因为是创世块,所以这个值是0
extraData 附加信息,随便填,可以填你的个性信息
gasLimit 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大

创世区块的初始化

geth init ./genesis.json –datadir “./chain”

当前区块链网络数据存放的位置会保存在chain目录中

yuyangdeMacBook-Pro:~ yuyang$ mkdir TestGeth
yuyangdeMacBook-Pro:~ yuyang$ cd TestGeth
yuyangdeMacBook-Pro:TestGeth yuyang$ geth init ./genesis.json --datadir "./chain"
INFO [02-24|11:08:41] Maximum peer count                       ETH=25 LES=0 total=25
INFO [02-24|11:08:41] Allocated cache and file handles         database=/Users/yuyang/TestGeth/chain/geth/chaindata cache=16 handles=16
INFO [02-24|11:08:41] Writing custom genesis block 
INFO [02-24|11:08:41] Persisted trie from memory database      nodes=0 size=0.00B time=10.106µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [02-24|11:08:41] Successfully wrote genesis state         database=chaindata                                   hash=5e1fc7…d790e0
INFO [02-24|11:08:41] Allocated cache and file handles         database=/Users/yuyang/TestGeth/chain/geth/lightchaindata cache=16 handles=16
INFO [02-24|11:08:41] Writing custom genesis block 
INFO [02-24|11:08:41] Persisted trie from memory database      nodes=0 size=0.00B time=1.743µs  gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [02-24|11:08:41] Successfully wrote genesis state         database=lightchaindata                                   hash=5e1fc7…d790e0

使用以下命令,启用私有链,并创建log日志文件

geth
–datadir “./chain”
–nodiscover
console 2»eth_output.log

参数名称 参数描述
datadir 设置当前区块链网络数据存放的位置
console 启动命令行模式,可以在Geth中执行命令
nodiscover 私有链地址,不会被网上看到

其他命令

web3.personal.newAccount(“password”) 创建帐户的方式有两种,第一种创建帐户时直接初始化密码 web3.eth.accounts 查看帐户 miner.start() 挖矿执行以下命令:挖矿会默认保存到创建的第一个帐户 miner.stop() 停止挖矿后,以太币则不会产生,同样智能合约、转帐等操作也不会起作用

余额查询

web3.eth.getBalance(“account id”)

每次记一长串的地址很麻烦,我们可以通过设置变量来acc0表示帐户1

acc0 = web3.eth.accounts[0]