# 以太坊节点同步 **Published by:** [Nerbonic](https://paragraph.com/@nerbonic/) **Published on:** 2022-05-03 **URL:** https://paragraph.com/@nerbonic/Gwpga87MWULVA7Gzvr2b ## Content Go-ethereum同步轻节点 0.前置知识 节点:客户端的软件。全节点、轻节点 客户端:一种以太坊的实现,提供RPC API端点方便用户控制客户端,标准的JSON RPC每个客户端都可以使用。geth、OpenEthereum(停止维护) 轻量同步:下载所有区块头、区块数据并对其进行随机验证 通过客户端与以太坊网络进行交互: -使用合适的协议(eg.curl)手动调用 -附加一个控制台(geth attach) -应用程序中执行 1.安装所需基础工具 yum update -y && yum install git wget bzip2 vim gcc-c++ ntp epel-release nodejs cmake -y 2.安装golang并配置 yum install golang // 不需要翻墙,yum search xxx可以查找需要安装的文件路径 golang配置 echo 'export GOROOT=/usr/lib/go' >> /etc/profile //golang的安装路径 echo 'export PATH=$PATH:$GOROOT/bin' >> /etc/profile echo 'export GOPATH=/ssd/go' >> /etc/profile // go文件执行时存放的路径 echo 'export PATH=$PATH:$GOPATH/bin' >> /etc/profile source /etc/profile go env -w GOPROXY=https://goproxy.cn,direct // 修改代理,国内编译go文件 3.下载并编译go-ethereum 下载:下载到本地然后拖到ssd文件里 cd go-ethereum make all // 编译 echo 'export PATH=$PATH:/ssd/go-ethereum/build/bin' >> /etc/profile // 在path中加入geth路径 source /etc/profile geth version // 验证是否安装成功 4.同步以太坊区块 nohup /ssd/go-ethereum/build/bin/geth --syncmode "snap" --port "30303" --cache=4096 --datadir /ssd/data/geth --http --http.api db,eth,net,web3,personal,admin,miner,debug --http.port 8545 --http.addr 0.0.0.0 --http.corsdomain "*" --maxpendpeers 999 > /ssd/data/output.log 2>&1 & "snap":替代传统的"fast",当指定同步模式为 “fast” 时,以太坊节点会从网络同步所有的区块头,区块体以及状态数据,但不对区块中的交易进行重放,只会对区块中的数据进行校验。 当指定同步模式为"full"时,以太坊节点会从网络同步所有的区块头,区块体并重放区块中的交易以生成状态数据。 当指定同步模式为“light”时,以太坊节点仅从网络中同步所有区块头,不去同步区块体,也不去同步状态数据,仅在需要相应区块和状态数据时从网络上获取。 当不指定任何模式时,默认为 “fast” 模式。 最新的命令参考:https://geth.ethereum.org/docs/interface/command-line-options https://www.cnblogs.com/kaifayuan/p/14970409.html(中文版) 5.geth console命令 / attach 命令 /ssd/go-ethereum/build/bin/geth attach /ssd/data/geth/geth.ipc // 使用ipc方式连接geth客户端(http/ws/ipc) 打开JS控制台,可以使用web3.js与节点进行交互(初始化账户、向网络中写入和广播交易请求、查找账户余额和元数据等) 6.节点交互:JSON RPC / web3模块 https://www.quicknode.com/docs/ethereum/ (可以查询不同的命令的使用方法) JSON形式、RPC方法调用API对象 RPC方式: curl -X POST \ (\符号表示换行,真正使用的时候要去掉\) -H "Content-Type: application/json" \ --data '{"method":"debug_traceTransaction","params":["0x9e63085271890a141297039b3b711913699f1ee4db1acb667ad7ce304772036b"],"id":1,"jsonrpc":"2.0"}' 127.0.0.1:8545 //jsonrpc-指定JSON-RPC版本号,method字段指定要调用的api方法名 eth:包含一些跟操作区块链相关的方法 net:包含一些查看p2p网络状态的方法 admin:包含一些与管理节点相关的方法 miner:包含启动&停止挖矿的一些方法 personal:主要包含一些管理账户的方法 txpool:包含一些查看交易内存池的方法 net.peerCount : 返回连接到的对等点的数量 admin.peers : 列出节点连接到的所有对等点 eth.blockNumber查看区块号,eth.synacing查看更多信息exit 搜索关键词:json-rpc eth debug.traceTransaction("Transaction_hash") !1648982534926 例:通过debug_traceTransaction获得指定交易hash的trace debug.stacks / debug.storageRangeAt / debug. eth.syncing --- false:表示已经同步到最新的区块了 attach状态下 eth命令 eth.getTransactionCount("BlockNumber") -- 可以获得该区块中发生的交易数 eth.getBlock("BlockNumber") --- 可以获得发生在这个区块中的所有交易hash eth.getRawTransactionFromBlock("BlockNumber") eth.getCode("Contract_address") -- 可以获得合约的字节码 eth.getBlockByHash("Block_Hash") -- 能获得区块内容,获得的一部分信息是hash形式的 eth.getBlockByNumber("0x(BlockNumber)") -- "0xddbe98"区块信息中出现的number,是区块号的十六进制形式 eth.getRawTransaction("Transaction_hash") / eth.getRawTransactionFromBlock("BlockNumber") -- 根据交易hash获得了一串十六进制数,不知道是啥意思 eth.getTransaction("Transaction_hash") -- 获得交易的blockHash、blockNumber、input、from、to等等的信息 eth.getTransactionCount("Contract_addr") -- 获得合约发生过的交易数,但是在什么范围内(某一区块内还是所有区块内)未知 eth.getTransactionFromBlock("BlockNumber") -- 或者区块号发生的第一个交易及信息 eth.getStorageAt 、eth.getTransaction不会用 eth.blockNumber -- 获得当前区块号 debug.traceTransaction("Transaction_hash") -- 获得该交易中具体的操作码信息 // 将给定事务转出给定块 debug.standardTraceBlockToFile("0x0bbe9f1484668a2bf159c63f0cf556ed8c8282f99e3ffdb03ad2175a863bca63", {txHash:"0x4049f61ffbb0747bb88dc1c85dd6686ebf225a3c10c282c45a8e0c644739f7e9", disableMemory:true}) ["/tmp/block_0x0bbe9f14-14-0x4049f61f-099048234"] // 转出某个区块中的所有TX debug.standardTraceBlockToFile("0x0bbe9f1484668a2bf159c63f0cf556ed8c8282f99e3ffdb03ad2175a863bca63", {disableMemory:true}) ["文件路径"] 使用命名标准在临时位置创建文件block_---。每个操作码都会立即流到文件,除了os正常进行的缓冲外,没有缓冲缓冲 使用跨客户端标准化输出,即所谓的“标准json” 用途op为操作码的字符串表示,而不是op/ opName对数字/串,和其他小simlar差异。 有 refund 将内存表示为连续的数据块,而不是32像-byte段这样的列表debug_traceTransaction debug.traceTransaction( "0x0903c3f6b08ff8729598de40932355410c3cac5e2a48e9f383629eeb6b23c972") 节点服务 https://ethereum.org/zh/developers/docs/nodes-and-clients/nodes-as-a-service/ ## Publication Information - [Nerbonic](https://paragraph.com/@nerbonic/): Publication homepage - [All Posts](https://paragraph.com/@nerbonic/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@nerbonic): Subscribe to updates