发送交易和接收事件在以太坊区块链的后端如何工作 [英] How sending transactions and receiving events work in backends in Ethereum blockchain

查看:67
本文介绍了发送交易和接收事件在以太坊区块链的后端如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个以太坊项目,但是我有一些疑问.我有一个通过web3.js连接到区块链的后端.为了连接到区块链,我使用了一个geth节点.我知道顺序是这样的:

发送跨种族听事件我的问题是:

  • 发送交易的组件是什么?是后端组件还是geth节点?
  • 然后假设网络中的另一个智能合约发出我要捕获的事件.捕获事件的组件是什么?是后端组件还是geth节点?

解决方案

一个很好的问题,先生.

通常,在这样的后端设置中标志带有其钱包密钥的交易.后端具有一个带有ETH余额的热钱包,可以创建和广播交易.

该交易通过JSON-RPC被推送到以太坊API节点.节点将交易广播到 P2P网络.矿工触发了后端事件web3.js处理程序状态更改在新块中.

请注意,在较小的区块链重组.无论是重组还是重组,事件处理程序都会为每个竞争块再次触发(两次,三次等).小型区块链重组可能在一小时内发生多次.当前状态是概率性的,因此您始终需要等待几步才能确定.

对于区块链中其他参与者的事件和交易,您只需订阅事件并在矿工将新区块到达您的节点时对其进行处理.

I am working on an ethereum project but I have some doubts. I have a backend that connects to the blockchain via web3.js. To connect to the blockchain I use a geth node. I understand that the sequence is this:

send transacrion listen to events my questions are:

  • What is the component sending the transaction? Is it the backend component or the geth node?
  • Then suppose that another smart contract in the network emits an event that I want to capture. What is the component that captures the event? Is it the backend component or the geth node?

解决方案

A very good question, sir.

Usually, in setups like this backend signs the transaction with its wallet key. The backend has a hot wallet with ETH balance to be able to create and broadcast transactions.

The transaction is pushed to Ethereum API node over JSON-RPC. The node broadcasters the transaction to P2P network. A miner picks up the transaction from the mempool. A new block is created. The miner broadcasts the newly crated block back to the peer-to-peer network. Your Ethereum node picks up the new block. Web3.js backend application polls or subscribes events related to the smart contracts from your Ethereum node. Backend event web3.js handlers are fired for the state changes in the new block.

Note that the blocks can be also rolled back in the case of a minor blockchain reorganisation. In the case or reorganisation, the event handlers fire again (twice, thrice, etc.) for each competing block. Minor blockchain reorganisation may occur many times in an hour. The current state is probabilistic, so you always need to wait for a few blocks to be sure.

For events and transactions by other actors in the blockchain, you just subscribe to the events and process them as new blocks arrive from miners to your node.

这篇关于发送交易和接收事件在以太坊区块链的后端如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆