以太坊etherenum入门教程


入门安装go-etherenum

1 搭建私有链

1.1 安装

官方文档提供两种方式,本文使用源码编译

下载源码

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

安装golang

yum install golang

编译

cd go-ethereum
make geth


1.2 config配置文件

{
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x400",
  "extraData"  : "0x0",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0xdeadbeefdeadbeef",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00",
  "alloc"      : {}
}

异常

go-ethereum/build/bin/geth init go-ethereum/eth/piccgenesis.json Fatal: invalid genesis file: json: cannot unmarshal hex string without 0x prefix into Go struct field Genesis.extraData of type hexutil.Bytes

执行成功的config.json

[root@iz2ze20vl8jnph0si0n7jsz watt]# go-ethereum/build/bin/geth init go-ethereum/eth/piccgenesis.json 
INFO [01-29|21:20:19] Allocated cache and file handles         database=/root/.ethereum/geth/chaindata cache=16 handles=16
Fatal: Failed to write genesis block: database already contains an incompatible genesis block (have d4e56740f876aef8, new a0e580c6769ac3dd)

原因由于上次执行命令初始化

启动私有链节点

启动Geth即可以启动以太坊的区块链,为了构建私有链 ,需要在Geth启动时加入一些参数,Geth参数含义如下:
build/bin/geth --datadir /home/watt/ethereumdata init eth/piccgenesis.json

config.json文件

{
  "config": {
        "chainId": 15,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "coinbase" : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x40000",
    "extraData" : "",
    "gasLimit" : "0xffffffff",
    "nonce" : "0x0000000000000042",
    "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp" : "0x00",
    "alloc": { }
}

有个疑问那初始化的数据存储路径在哪呢? 通过指定的路径生成数据文件名称,使用find命令查找如下

查看初始化数据文件大小如下

重新配置参数

获取当前的目录

/home/watt/go-ethereum/build/bin/geth  --datadir "/home/watt/ethereumchain" init /home/watt/go-ethereum/eth/piccgenesis.json 

创建数据存放地址并初始化创世块

/home/watt/go-ethereum/build/bin/geth --identity "PICCetherum"  --rpc  --rpccorsdomain "*" --datadir "/home/watt/ethereumchain" --port "30303"  --rpcapi "db,eth,net,web3"--networkid 95518 console

启动日志

INFO [01-29|22:08:58] Allocated cache and file handles         database=/home/watt/ethereumchain/geth/chaindata cache=16 handles=16
INFO [01-29|22:08:58] Writing custom genesis block 
INFO [01-29|22:08:58] Successfully wrote genesis state         database=chaindata                               hash=a0e580…a5e82e
INFO [01-29|22:08:58] Allocated cache and file handles         database=/home/watt/ethereumchain/geth/lightchaindata cache=16 handles=16
INFO [01-29|22:08:58] Writing custom genesis block 
INFO [01-29|22:08:58] Successfully wrote genesis state         database=lightchaindata                               hash=a0e580…a5e82e
[root@iz2ze20vl8jnph0si0n7jsz go-ethereum]# /home/watt/go-ethereum/build/bin/geth --identity "PICCetherum"  --rpc  --rpccorsdomain "*" --datadir "/home/watt/ethereumchain" --port "30303"  --rpcapi "db,eth,net,web3"--networkid 95518 console
INFO [01-29|22:09:20] Starting peer-to-peer node               instance=Geth/PICCetherum/v1.8.0-unstable-722bac84/linux-amd64/go1.8.3
INFO [01-29|22:09:20] Allocated cache and file handles         database=/home/watt/ethereumchain/geth/chaindata cache=128 handles=1024
WARN [01-29|22:09:20] Upgrading database to use lookup entries 
INFO [01-29|22:09:20] Initialised chain configuration          config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Engine: unknown}"
INFO [01-29|22:09:20] Disk storage enabled for ethash caches   dir=/home/watt/ethereumchain/geth/ethash count=3
INFO [01-29|22:09:20] Disk storage enabled for ethash DAGs     dir=/root/.ethash                        count=2
INFO [01-29|22:09:20] Initialising Ethereum protocol           versions="[63 62]" network=1
INFO [01-29|22:09:20] Database deduplication successful        deduped=0
INFO [01-29|22:09:20] Loaded most recent local header          number=0 hash=a0e580…a5e82e td=262144
INFO [01-29|22:09:20] Loaded most recent local full block      number=0 hash=a0e580…a5e82e td=262144
INFO [01-29|22:09:20] Loaded most recent local fast block      number=0 hash=a0e580…a5e82e td=262144
INFO [01-29|22:09:20] Regenerated local transaction journal    transactions=0 accounts=0
INFO [01-29|22:09:20] Starting P2P networking 
INFO [01-29|22:09:22] UDP listener up                          self=enode://4fadb2bd1b9e7efca20c99c067930c7bb5cd9c27ea38aef11fd514bed4abb1b2697651194fd1b765a51c3648e001542fbf57ef2c4912469e384139bb61c57f60@[::]:30303
INFO [01-29|22:09:22] HTTP endpoint opened: http://127.0.0.1:8545 
INFO [01-29|22:09:22] IPC endpoint opened: /home/watt/ethereumchain/geth.ipc 
INFO [01-29|22:09:22] RLPx listener up                         self=enode://4fadb2bd1b9e7efca20c99c067930c7bb5cd9c27ea38aef11fd514bed4abb1b2697651194fd1b765a51c3648e001542fbf57ef2c4912469e384139bb61c57f60@[::]:30303

卡住了,需要把root下的

rm -rf ethereumchain/ 
rm -rf .ethereum/

清除后重现执行执行输出如下:

INFO [01-29|22:47:57] HTTP endpoint opened: http://127.0.0.1:8545 
INFO [01-29|22:47:57] RLPx listener up                         self=enode://fc07f632a636c713a2621902d5eac3b1969afd54d255812ac767484d926fd1d5113d243467e59e8c0864e63b60240dbd7d55d3b34058fa388f1002f6b20bcfec@[::]:30303
INFO [01-29|22:47:57] IPC endpoint opened: /home/watt/ethereumchain/geth.ipc 
INFO [01-29|22:48:17] Block synchronisation started 
INFO [01-29|22:48:24] Imported new block headers               count=192 elapsed=1.250s number=192 hash=723899…123390 ignored=0
INFO [01-29|22:48:24] Imported new block headers               count=384 elapsed=53.824ms number=576 hash=41a746…6a8b38 ignored=0
INFO [01-29|22:48:24] Imported new block receipts              count=2   elapsed=17.637ms bytes=8 number=2   hash=b495a1…4698c9 ignored=0
INFO [01-29|22:48:24] Imported new block headers               count=384 elapsed=55.720ms number=960 hash=916910…1d0e2a ignored=0
INFO [01-29|22:48:24] Imported new block receipts      

但是未能调用javasript console页面 通过查询发现需要增加 --dev 参数 另开窗口

输入命令

/home/watt/go-ethereum/build/bin/geth  --dev console 2>> file_to_log_output

2


2、 使用私有链

查看账户

eth.accounts

创建用户:

personal.newAccount("111111")

其中参数为此账户的密码。

也可以先创建账户,然后输入密码:

personal.newAccount()

查看区块数据

eth.blockNumber

启动挖矿

miner.start()

返回结果为true则启动成功,具体执行情况可查看日志。

停止挖矿

miner.stop()

当在执行挖矿时日志会不停刷屏,不用管,只要命令输入全,执行即可停止挖矿。

查看账户余额

其中参数为区块链地址

eth.getBalance("0x7d1f7be4112ce63b9de04a0bf95c1e87e430bd1b")

转账

从账户0x7d1f7be4112ce63b9de04a0bf95c1e87e430bd1b转账3个以太币到0x587e57a516730381958f86703b1f8e970ff445d9。

eth.sendTransaction({from:"0x7d1f7be4112ce63b9de04a0bf95c1e87e430bd1b",to:"0x587e57a516730381958f86703b1f8e970ff445d9",value:web3.toWei(3,"ether")})

当直接执行此方法时会抛出异常:

account is locked
    at web3.js:3119:20
    at web3.js:6023:15
    at web3.js:4995:36
    at <anonymous>:1:1

很明显,账户被锁。

解锁转出账户

其中第一个参数为转出账户,第二个参数为密码。也可以直填写第一个参数,然后通过命令行提示再输入密码。

personal.unlockAccount("0x7d1f7be4112ce63b9de04a0bf95c1e87e430bd1b","111111")

解锁完成之后,即可执行转账操作。但此时查看时会发现接收账户依旧为原来数值。此时需要执行挖矿命令,才会把转账真正完成。

看似美好,执行命令后

> eth.accounts
["0x709447961e479f696a496243cbeff83b851908b2"]
> personal.newAccount("111111")
"0x489835dff4d2270ae2c6a148ca313858bf1e4994"
> eth.blockNumber
0
> miner.start()
null
> 

发现miner.start() 为null

Pre-allocating ether to your account
A difficulty of “0x400” allows you to mine Ether very quickly on your private testnet chain. If you create your chain and start mining, you should have hundreds of ether in a matter of minutes which is way more than enough to test transactions on your network. If you would still like to pre-allocate Ether to your account, you will need to:

Create a new Ethereum account after you create your private chain
Copy your new account address
Add the following command to your Custom_Genesis.json file:
"alloc":
{
        "0x709447961e479f696a496243cbeff83b851908b2":
        { "balance": "20000000000000000000" }
}
Note

Replace 0x1fb891f92eb557f4d688463d0d7c560552263b5a with your account address.

Save your genesis file and rerun your private chain command. Once geth is fully loaded, close it by .

We want to assign an address to the variable primary and check its balance.

Run the command geth account list in your terminal to see what account # your new address was assigned.

需要执行命令

miner.setEtherbase(personal.listAccounts[0])

查看日志

INFO [01-29|23:14:53] Starting mining operation 
INFO [01-29|23:14:53] Commit new mining work                   number=1 txs=0 uncles=0 elapsed=65.571µs
WARN [01-29|23:14:53] Block sealing failed                     err="waiting for transactions

继续努力

#返回钱包管理的账户地址列表  
#返回示例:["0x3138e3722fb4280cb67f6e858108136bfa1c9160"]
eth.accounts

#创建账户地址,参数为账户锁定密码,在转账前需要先解锁账户
#我们把这个命令运行两次,创建两个地址,加上默认的,一共有了三个账户地址
personal.newAccount('111111')
personal.newAccount('111111')

#为账户设置别名,方便命令输入
user1=eth.accounts[0]
user2=eth.accounts[1]
user3=eth.accounts[2]

#查看地址user1余额,这个地址是测试链默认开通的一个地址,里面初始化有很多币
#我们创建的另外两个地址余额未0
eth.getBalance(user1)

#查看区块高度,现在为0
eth.blockNumber

#转账测试,首先解锁账号user1
#命令运行后要求输入解锁密码,直接回车,默认账号锁定密码为空,返回true成功
personal.unlockAccount(user1)

#从user1向user2转账3个以太币
#命令运行后,提交交易立马回出发挖矿
eth.sendTransaction({from:user1,to:user2,value:web3.toWei(3,"ether")})

#查看区块高度,这时高度为1
eth.blockNumber

还是null

挖矿测试
geth启动后,自动启动挖矿,这时运行miner.start(),返回为null 无交易的时候不挖矿,当有交易时自动会触发挖矿流程

#我们可以先停止挖矿
miner.stop()

#提交交易,这时候只提交,查看账户余额,但是未确认
eth.sendTransaction({from:user1,to:user2,value:web3.toWei(3,"ether")})

#启动挖矿,确认交易,再次查看账户余额
miner.start()

#那么挖矿奖励去哪儿了?查看矿工地址
eth.coinbase

#设置矿工地址
miner.setEtherbase(eth.coinbase)


3、参考资料

区块链开发(一)搭建基于以太坊的私有链环境

私有链搭建文档

以太坊学习笔记:私有链搭建操作指南

区块链开发(二)以太坊客户端基本操作命令

以太坊执行miner.start返回null

10分钟完成阿里云环境搭建以太坊私有链

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,881评论 4 368
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,052评论 1 301
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,598评论 0 250
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,407评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,823评论 3 294
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,872评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,037评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,778评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,505评论 1 247
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,745评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,233评论 1 264
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,568评论 3 260
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,231评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,141评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,939评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,954评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,784评论 2 275

推荐阅读更多精彩内容