1.介绍
以太坊网络由无数个节点组成,每个节点的地位都是一样的,大多数节点上的数据也是一样的。
每个节点都是实现了以太坊协议服务端。
以太坊协议服务端有多种语言实现,常见的是三种c++、go、python。
2.geth
go语言实现的称之为geth。
geth使用最广,功能非常多。比如可以作为以太坊网络的一个full node全节点,可以创建一个私有以太坊网络作为开发环境和学习环境。
geth的官网:https://geth.ethereum.org/
geth安装的参考文档https://geth.ethereum.org/docs/install-and-build/installing-geth
在ubuntu16.04安装geth:
sudo add-apt-repository -y ppa:ethereum/ethereum sudo apt-get update sudo apt-get install ethereum安装完毕后,执行geth --help,现实帮助信息,表明安装成功。
3.使用
从getting start开始熟悉ethrereum https://geth.ethereum.org/docs/getting-started
3.1 “clef init”,初始化帐号管理,设置密码,配置masterseed.json,并备份。
3.2 "clef newaccount",创建新帐号。按照提示进行即可。注意,要备份地址、密码、keyfile。
3.3 启动geth。启动geth需要连接各种网络,比如:以太坊主网,自建私有网络,三种测试网络。启动geth节点需要从网络同步数据,有三种同步方式,不同的方式下载的数据量和验证是不一样的。
3.3.1 先启动clef “clef --keystore ~/.ethereum/keystore --chainid 5”,这里的chainid=5,指的是一个特定的测试网络goerli。启动后,打印一个提示信息“extapi_ipc : /home/bri/.clef/clef.ipc”,后头会用到。
3.3.2 启动geth “geth --goerli --syncmode "light" --rpc --signer=/home/bri/.clef/clef.ipc”。这里,--goeli指示是连接到测试网络goeli,--syncmode "light"是同步模式,数据量和验证工作量最小的模式,--rpc是指rpc通信方式,--signger是前面启动的clef。同步时间大概十几分钟左右。
3.4 连接geth。有两种方式ipc和rpc。前者需要能直接连接节点,能实现全部功能。后者是远程使用,为了安全起见,功能受限。
3.4.1 测试rpc连接。比如,查询一个钱包地址/公钥上的以太币余额,命令如下:
curl -X POST http://127.0.0.1:8545 \ -H "Content-Type: application/json" \ --data '{"jsonrpc":"2.0", "method":"eth_getBalance", "params":["0xf4413Bd7Ea069625525a55Dc3795C034FFA14465","latest"], "id":1}'返回结果:
{"jsonrpc":"2.0","id":1,"result":"0x0"}刚创建的新地址没有钱。
其他命令以此类推。
3.4.2 测试ipc连接。注意,在3.3.2的启动提示信息,有“ IPC endpoint opened url=/home/bri/.ethereum/goerli/geth.ipc”,要连到这里。命令是:
geth attach ~/.ethereum/goerli/geth.ipc查询一个帐号有多少余额
web3.fromWei(eth.getBalance("0xf4413Bd7Ea069625525a55Dc3795C034FFA14464"),"ether")
