在中继开发中,事件的监听是开发难度最高的。
事件监听的技术原理主要是通过获取一个区块内部的交易信息并解析交易信息内的“EventLog”(日志事件)来达到目的。
目前ETH提供web.js库,该库中有区块事件监听函数,但并不完美,有以下不足:
xff0c; event listening is the most difficult to develop.
event listening technical principles are primarily achieved by obtaining transactional information inside a block and dissecting “EventLog” (log event) in the transaction information.
Currently ETH provides web.js library, there is block event listening function xff0c; but not perfect xff0c; xff1a below;
- 如果客户端的进程被杀死,监听动作就会被丢失
- 如果对上一次最后遍历成功的区块号进行存储,重新启动的时候将会造成时间段内新生成的区块的数据丢失
- 完整的监听流程比较消耗客户端的设备资源,影响用户体验
为什么要监听?
中心化Exchange就要做这样的事情,比如充值或者提现,都需要对链上的数据进行监控,要链上转账成功后,再修改中心化服务端的数据库,如果不监控,就提现而言,如果链上交易失败,但是用户在中心化的数值减少了,就会有问题。
Why do you want to listen to xff1f;
centralised Exchange do something like this xff0c; e.g. charge value or withdrawal xff0c; all need to monitor data on the chain xff0c; xff0c after successful transfer on the chain; remodify the central service's database xff0c; if not monitor xff0c; xff0c; if transactions fail on the chain xff0c; but the user's centralized values are reduced xff0c; there are problems.
在ETH机制中,链上的授权,交易,合约发布等事件都是一条条的交易信息,可以把每一笔交易信息提取出来,进行划分处理,就可以实现不同的功能和业务。
In the ETH mechanism xff0c; authorization on the chain xff0c; transaction xff0c; events such as contract publication are a single transaction information xff0c; each transaction information can be extracted xff0c; division processing xff0c; different functions and operations can be achieved.
RPC接口在基于通讯协议方面有多种实现方法,但是主要是如下两种:
RPC interfaces have a variety of methods of realization based on communication agreements xff0c; however, mainly two xff1a, as follows;
- 同“RESTful API”一样,基于应用层HTTP/HTTPS协议实现
- 基于传输层的TCP协议实现,也被称为Socket(嵌套字)的实现
两种方式的区别在于请求和响应的位置,一个是在应用层,一个是在传输层。
RPC接口和“RESTful API”接口,在HTTP/HTTPS协议实现,在速度方面几乎没有差别,
但是基于传输层的TCP协议实现的RPC接口,除了数据传输流的层级上比"RESTful"接口少而整体比它快外,传输时的整体数据报层面还少了HTTP/HTTPS的头部数据量及组装的时间损耗。也即是说,当RPC接口基于传输层的TCP协议实现时,RPC接口不仅请求和响应的速度比“RESTful”接口快,而且数据量也相对要少。
The difference between the location of the request and the response xff0c; one in the application layer xff0c; one in the transmission layer.
RPC interface and `RESTful API' interface xff0c; xff0c under the HTTP/HTTPS protocol; little difference in speed xff0c; br/>but RPC interface xff0c under the TCP agreement based on the transmission layer; 34 over the transmission stream; Tuxul34; less interfaces in whole than it does xff0c; less HTTP/HTPS head data volume and time loss for assembly at the transmission level; and less data than #xff0.
常见的RPC框架有以下几种:
The following are common RPC frames: xff1a;
- JSON-RPC
- XML-RPC
- Protobuf-RPC
- SOAP-RPC
简言之:RPC协议,就是规范了一种客户端和实现了RPC接口的服务端交互的数据格式。
In short, xff1a; RPC Protocol xff0c; is a data format that regulates an interface between the client and the service that achieves the RPC interface.
RPC接口实现的大致流程:
The approximate process achieved by the RPC interface & #xff1a;
- 服务调用方案按照规范好了的编码方式把某个RPC接口的函数和参数进行序列化编码
- 发送到服务的提供方,即服务器端
- 服务器端再通过反序列化后把对应的参数提取出来
- 然后通过调用相关函数
- 最后把结果返回给服务端的调用方
在了解ETH RPC接口之前,先了解3个参数,因为这3个参数会出现在每一个接口中。
Before understanding the ETHRPC interface & #xff0c; understanding three parameters & #xff0c; these three parameters appear in each interface.
- genesis,代表的是最早的,早期的源码版本中等同于 earliest
- pending,代表的是等待或者挂起状态中的
- latest,代表的是最新完成的
1.eth_blockNumber
该接口可以根据传参(genesis,pending,latest)获取 3 种类型的区块高度
This interface can capture the height of three types of blocks by reference (genesis,pending,latest)
- genesis, 代表的是创世区块,因为可以指定最早的区块号,所以创世区块不一定是0
- pending,代表的是当前正在被minter开采的区块,代表正在打包交易的区块,一个区块在打包完一定的交易后才会完整上链
- latest,当前链上最新生成的区块的高度
2.eth_getBlockByNumber
该接口根据区块高度获取区块的部分信息,是在遍历区块时主要用来获取区块数据信息的一个接口。该接口可以提供如下数据:
The interface captures parts of block information according to block height & #xff0c; it is an interface that is used mainly to obtain block data information when crossing blocks. The interface provides the following data & #xff1a;
- 区块头部的部分字段
- 所有打包在这个区块中的交易hash数组
也就是说,返回的数据的结构体是Block,即这个结构体所含有的信息,都可以拿得到。
That is xff0c; the structure of the returned data is Blockxff0c; that is, the information contained in this structure xff0c; is available.
3.eth_getTransactionByHash
该接口可以根据一笔交易记录的哈希值,可以获取这笔交易的详细信息。
该接口提供了交易查询功能,在获取交易区块信息后,从区块信息中获取被打包的交易的哈希值,进行全部交易信息的提取。处于pending(等待或者挂起)状态的交易,返回的将会是空,也可以根据接口的这一特点来判断一笔交易是否成功。
获取数据的结构体是Transaction。
The interface can be derived from the Hashi value of a transaction xff0c; details of the transaction can be obtained.
The interface provides a transaction query function xff0c; xff0c after obtaining block information; xff0c when obtaining packaged transactions xff0c from block information; and extracts all transaction information. Transactions in pending (waiting or hanging up) xff0c; returns will be empty xff0c; a transaction can also be judged to be successful on the basis of this feature of the interface.
The structure for obtaining the data is Transaction.
4.eth_getTransactionReceipt
该接口可以根据一个交易的哈希值来获取这笔交易收据信息。
该接口返回的数据在一定程度上和eth_TransactionByHash是相同的,但该接口返回的数据更加详细,例如交易的日志Logs,这也是进行事件监听最主要的数据源。和eth_getTransactionByHash一样,处于pending状态的交易,返回的将会是空。返回的结构体是Receipt
The interface can capture the transaction receipt information based on the Hashi value of a transaction.
The interface returns the same data to a certain extent as Eth_TransactionByHash & #xff0c; but the interface returns more detailed data & #xff0c; for example, the transaction log Log Log Log Logs & #xff0c; this is also the most important source of data for event monitoring.
5.eth_getBalance
该接口获取的是某个ETH地址的ETH数值,即ETH余额。它不是获取ERC20 Token或者其他Token的余额。
The interface captures the ETH value of an ETH address xff0c; i.e. the ETH balance. It does not capture the balance of ERC20 Token or other Token.
6.eth_sendRawTransaction 和 eth_sendTransaction
具体请跳转,是所有交易的触发接口。其中eth_sendRawTransaction 可以完成的交易类型包含eth_sendTransaction.
, are trigger interfaces for all transactions.
7.eth_getTransactionCount
该接口可以根据一个ETH钱包地址获取当前钱包地址的交易序列号Nonce,该接口的第二参数是genesis、pending、latest,各个参数的分别对应的是下面的作用:
The interface can obtain the transaction serial number Nonce, of the current wallet address from an ETH wallet address; the second parameter of the interface is genesis,pending, latest, the respective parameters correspond to the role xff1a below;
- 取genesis时: 获取当前ETH地址第一次发起交易时的Nonce序列号
- 取pending时: 获取当前ETH地址提交了正处于pending状态等待被区块打包的交易订单所对应的Nonce序列号。如果当前查询的地址没有处于pending状态的交易,那么返回的是与latest一样的Nonce序列号。
- 取latest时: 获取当前ETH地址提交了且被区块打包成功的交易订单所对应的Nonce序列号加1的值。比如地址A最后一笔成功交易对应的Nonce为8,那么当调用接口传入参数为latest的时,获取的结果时9,即8+1.
- 在eth_getTransaction中,Nonce查询满足:pending>=latest>= genesis
8.eth_getCode
该接口根据ETH地址判断当前地址时合约地址还是非合约地址。如果不是,返回值是"0x",否则,返回的是智能合约当初创建时的十六进制码。
The interface determines whether the current address is a contractual or non-contractual address based on the ETH address. If not xff0c; returns value 34; 0x#34; otherwise xff0c; returns the hexadecimal number from the time the smart contract was created.
9.eth_estimateGas
该接口用来估算一笔交易要消耗多少GasLimit,该所估算出来的值没有涉及GasPrice。
入参中的data是被估算的数据量,估算的结果数值和数据量成正比。
这个接口在钱包发起交易的时让用户选择燃料费为多少的功能上会用到,提现在燃料费进度条中的最大值上。最大值一般就是这个接口返回的数值。
The interface is used to estimate how much GasLimit, is consumed by a transaction; the estimated value does not relate to GasPrice.
The included data data is the estimated amount of xff0c; the estimated results are proportional to the amount of data.
The interface allows the user to select the functionality of the fuel fee for the transaction at the time the wallet is initiated to xff0c; and refers to the maximum value in the current fuel cost progress. The maximum value is generally the value returned from this interface.
10.eth_call
这是ETH用来访问智能合约的万能RPC接口,也就是说,任何智能合约上的公有函数都能使用这个接口访问。
This is the `strong' almighty RPC interface used by ETH to access smart contracts & #xff0c; that is, xff0c; any public function on smart contracts can be accessed using this interface.
eth_getBalance是用来查询ETH余额的,但是ERC20 Token 余额,就需要通过eth_call来进行查询。这个接口的入参和 eth_sendRawTransaction/eth_sendTransaction入参是一样的。
eth_call接口是一个只读接口,不会改变区块链上的数据,可以访问ETH智能合约中的所有函数。
Eth_getBalance is the xff0c used to search for ETH balances; but the ERC20 Token balancexff0c; the search needs to be done through Eth_call. The entry into this interface is the same as the entry into Eth_sendRawTransation/eth_sendTransation.
eth_Call interface is a read-only interface xff0c; data on block chains are not changed xff0c; access to all functions in the ETH smart contract is available.
eth_call只读特性从源码上层面的理解
EVM在执行合约代码时,
先,使用区块号blockNumber找出对应的区块信息,
再,系统地根据这个区块的数据实例化一个名为status的变量,由status来实例化对应的EVM实例,
然后,根据区块提供的信息来实例化合约代码的变量值,即智能合约的函数在被执行前它的变量值是由当前区块高度决定的。
也就是说,eth_call在调用transfer函数时会直接在代码的内存层面进行值的修改,并没有广播出去和修改到数据层面。
EVM xff0c; br/> xff0c; use block number Block number xff0c;
to find matching block information xff0c; b/> xff0c; systematize a variable named xff0c; xff0c; EVM example xus; r/> xff0c; exemplify the variable value of the contract code based on information provided by blocks xff0c; i.e., the variable value of the intelligent contract was determined at the highest level of the current block before it was implemented.
That is xff0c; Eth_call directly revises the value of the code xfer function; no xffffff0c to the data.
目前获取节点链接的的方案有两种
There are currently two options for obtaining node links.
- 自行购买服务器,然后启动ETH节点服务程序,例如Geth,再获取节点程序提供的接口链接。
- 使用第三方服务平台提供的节点链接
对于第一种方法,操作流程是先下载对应的ETH节点版本源码,然后根据文档的编译方法进行自编译,生成可执行程序,再部署到服务器。需要注意的是,ETH节点的源码不仅仅有版本号不同的版本,还有不同计算机语言的版本,官方的Geth是Golang语言开发的版本。
For the first method & #xff0c; the operating process is to download the corresponding ETH node source & #xff0c; then compose & #xff0c according to the compilation method of the document; generate the executable & #xff0c; then deploy to the server. Note that , the ETH node source code is not only a different version xff0c; there is also a different computer language version xff0c; the official Geth is a version developed in the Golang language.
第一种方整体实施起来的优缺点:
(1) 可自定义性高
The advantages and disadvantages of implemented as a whole by
(1) are self-defined
- 可以自行配置节点启动的配置文件
- 可自行修改源码进行二次开发后再编译
- 可自行设置服务网关、节点集群等相关的运行方式
- 可参与公链minter,赚取ETH的收入
(2) 技术要求难度相对来说比较高
(2) Relatively difficult technical requirements
- 要求使用者必须掌握和了解配置文件中各项属性的含义及其影响
- 要求使用者必须懂一些服务端运维的知识
- 要求使用者懂得对应节点编写语言的编译命令,甚至使用过该门语言
(3) 自运维成本高
(3) High self-sustainment costs
- 要求监控节点服务的运行情况
- 要求实现节点程序,因为某些问题被杀死后能够进行自动重启
- 在集群情况下,要求保证各服务的稳定性
(4) 需要自付费服务器等产品的花销
(4) Cost of products such as self-paid servers required
第二种方案
Second Option
- 适用于学习阶段 和 线上业务不需要高度自定义化的情况使用
第二种方案和第一种方案对比,优缺点相反。
第二种方案也是最方便和便宜的,因为只需要一个链接即可。
The second option compares with the first option xff0c; the opposite is true.
The second option is also the most convenient and cheap xff0c; because only one link is needed.
链接获取
可以根据 https://infura.io/ 来免费获取节点链接。
Infura是国外一个托管ETH节点的集群,还允许开发者免费申请属于自己的ETH节点链接(包括主网和测试网),功能比较齐全和方便。
Free node links can be obtained from .
Infura is a host cluster of ETH nodes xff0c from abroad; developers are also allowed to apply for their ETH node links (including main and test networks) xff0c; it is more fully functional and convenient.
Detail go to 153 and 159。
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论