简体   繁体   English

发送交易和接收事件如何在以太坊区块链的后端工作

[英]How sending transactions and receiving events work in backends in Ethereum blockchain

I am working on an ethereum project but I have some doubts.我正在从事一个以太坊项目,但我有一些疑问。 I have a backend that connects to the blockchain via web3.js.我有一个通过 web3.js 连接到区块链的后端。 To connect to the blockchain I use a geth node.为了连接到区块链,我使用了一个 geth 节点。 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?它是后端组件还是 geth 节点?
  • 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?它是后端组件还是 geth 节点?

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.后端有一个带有 ETH 余额的热钱包,可以创建和广播交易。

The transaction is pushed to Ethereum API node over JSON-RPC.交易通过 JSON-RPC 推送到以太坊 API 节点 The node broadcasters the transaction to P2P network .节点将交易广播到P2P 网络 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. Web3.js 后端应用程序从您的以太坊节点轮询或订阅与智能合约相关的事件。 Backend event web3.js handlers are fired for the state changes in the new block.为新块中的 state 更改触发后端事件 web3.js 处理程序

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.当前的 state 是概率性的,因此您始终需要等待几个块才能确定。

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.对于区块链中其他参与者的事件和交易,您只需订阅事件并在新块从矿工到达您的节点时处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM