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

问题描述

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

发送事务听事件我的问题是:

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

解决方案

一个很好的问题,先生.

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

交易通过 JSON-RPC 推送到以太坊 API 节点.节点将交易广播到P2P 网络.矿工内存池.创建了一个新块.矿工将新创建的区块广播回点对点网络.您的以太坊节点接收新区块.Web3.js 后端应用程序从您的以太坊节点轮询或订阅与智能合约相关的事件.后端事件 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 transacrionlisten to eventsmy 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.

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

06-16 12:16