《GoEthereum Walkthrough》学习笔记(一)

不要试图使用Windows

作者在这里建议我们不要用Windows来构建或调试go-ethereum。【注】事实上,在Windows上构建或调试go-ethereum也是可以的。

安装geth

下载go-ethereum源码,获取所有的依赖,并构建geth。这一步比较常规。这里就不展开。

创建新的私有以太坊网络

这一步也比较常规。这里就不展开。

启动新的以太坊网络

您可以有多种方式运行新的以太坊网络:
1、在非调试模式下运行网络
a. 直接运行
b. 远程关联该进程
2. 在调试器模式下运行,这稍微有点复杂,但为了设置断点以及顺着跟踪,这是很有必要的。
第一种选项最简单:直接运行。让我们想来看看第一种。

在非调试模式下启动

1、这些命令不会启动一个测试网,相反,它们会启动一个私有的、实时的、完整的以太坊网络:

$ geth --syncmode full --cache 64 --maxpeers 12 \
 --rpcapi --wsapi \
 --datadir ~/.gowalkthrough

上面的–rpcapi选项会使geth创建一个文件,名称是~/.gowalkthrough/geth.ipc。当geth中止,该文件会自动被删除【译者亲测,确实是这样!】。

用Goland/IntelliJ调试go-ethereum

StackOverflow上有讨论这一方法的记录。

首先确保您拥有最新的版本,EAP 15, 173.2696.28,并且您正在使用最新版本的go,1.9。因为有更好的调试体验,鉴于Go在调试方面的最新改进。

接着,前往Run | Edit Configurations | Go Application |select the run configuration you want to edit | Run kind ,然后将Run kind中的File换成Package。接着输入包的名字,例如,github.com/ethereum/go-ethereum/cmd/geth并保存设置。接着前往Run | Debug…并选择你先前已经编辑的运行配置,将其作为调试运行。

我已经创建了一个小视频,它会指导你对于一次Run Configuration如何改变Run kind,具体您可以点击这里

一定要用和先前一样的命令行选项:–syncmode full –cache 64 –maxpeers 12 –rpcapi –wsapi –datadir ~/.gowalkthrough
【注意】参数不需要带可执行程序本身。

关联一个处于运行状态的geth进程

这对于想要亲睹geth在面对各种输入时做何反应时是非常有用的。首先,在Linux上使用一个refresher:

man pgrep描述了一种发现进程IDs的有用工具。

NAME
       pgrep, pkill - look up or signal processes based on name and other attributes

SYNOPSIS
       pgrep [options] pattern
       pkill [options] pattern

DESCRIPTION
       pgrep looks through the currently running processes and lists the process IDs which match the selection criteria to stdout.  All the criteria have to match.  For example,

          $ pgrep -u root sshd

   will only list the processes called sshd AND owned by root.  On the other hand,

          $ pgrep -u root,daemon

   will list the processes owned by root OR daemon.

   pkill will send the specified signal (by default SIGTERM) to each process instead of listing them on stdout.

strace -fp 连接所有的已有线程。

strce命令的-p选项接受用分号分割的进程号pid的列表。以下的命令使用pgrep和paste来创建那个列表,并且捕捉来自进程内所有线程的系统调用。

sudo strace -t -p $(ls /proc/$(pgrep geth)/task -1 | paste -sd "," -) \ -o geth.strace

文件geth.strace将包含类似下面的一些东西:

strace: Process 23019 attached
strace: Process 23020 attached
strace: Process 23021 attached
strace: Process 23022 attached
strace: Process 23023 attached
strace: Process 23024 attached
strace: Process 23025 attached
strace: Process 23026 attached
strace: Process 23027 attached
strace: Process 23028 attached
strace: Process 23029 attached
strace: Process 23030 attached
strace: Process 23031 attached
strace: Process 23032 attached
strace: Process 23033 attached

关联一个JavaScript控制台

1、在另一个终端控制台,启动一个JS控制台,它连上了上面处于运行状态的geth实例,具体的命令是:

$ geth attach ~/.gowalkthrough/geth.ipc
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.11-stable-dea1ce05/linux-amd64/go1.10
coinbase: 0xd1c037b9d67b8b0af6003cfaa6951adfa1f67d89
at block: 0 (Wed, 31 Dec 1969 16:00:00 STD)
datadir: /home/mslinn/.gowalkthrough
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

>

2、运行以下的JS命令创建默认账户。注意其余额是42以太币:

> personal.newAccount()
Passphrase:
Repeat passphrase:
"0xbc2dba1e18dd874707c4e495b139138d050a5846"

> web3.fromWei(eth.getBalance(eth.coinbase), "ether")
42

关闭geth

1、在第一个终端控制台中按Ctrl-C来结束geth。在关闭geth的时候会产生类似下面的输出:

INFO [06-27|13:39:03] IPC endpoint closed                      endpoint=/home/mslinn/.gowalkthrough/geth.ipc
INFO [06-27|13:39:03] Writing cached state to disk             block=46376 hash=7efacc…f41d6c root=d4e03e…041271
INFO [06-27|13:39:03] Persisted trie from memory database      nodes=283 size=49.29kB time=9.335ms gcnodes=1617 gcsize=407.84kB gctime=77.473ms livenodes=1 livesize=0.00B
INFO [06-27|13:39:03] Writing cached state to disk             block=46375 hash=83aa1c…be0d26 root=d4e03e…041271
INFO [06-27|13:39:03] Persisted trie from memory database      nodes=0   size=0.00B   time=24µs    gcnodes=0    gcsize=0.00B    gctime=0s       livenodes=1 livesize=0.00B
INFO [06-27|13:39:03] Writing cached state to disk             block=46249 hash=b4cdf5…f24d50 root=d4e03e…041271
INFO [06-27|13:39:03] Persisted trie from memory database      nodes=0   size=0.00B   time=9µs     gcnodes=0    gcsize=0.00B    gctime=0s       livenodes=1 livesize=0.00B
INFO [06-27|13:39:03] Blockchain manager stopped
INFO [06-27|13:39:03] Stopping Ethereum protocol
WARN [06-27|13:39:03] Synchronisation failed, retrying         err="header processing canceled (requested)"
INFO [06-27|13:39:03] Ethereum protocol stopped
INFO [06-27|13:39:03] Transaction pool stopped
INFO [06-27|13:39:03] Database closed                          database=/home/mslinn/.gowalkthrough/geth/chaindata

2、在JS控制台中按Ctrl-D来退出。
【注】关闭了geth,还得关闭刚刚连到geth的JS控制台。

学习小结

本部分内容最大的亮点是介绍了调试方法。调试对于整体把握一个项目的内部流程非常重要。最后非常感谢作者将自己的经验分享出来。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值