How sending transactions and receiving events work in backends in Ethereum blockchain
Asked Answered
M

1

7

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 enter image description here listen to events enter image description here 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?
Microgamete answered 21/1, 2021 at 15:51 Comment(3)
the component that is sending the transaction? That depends. Are you sending it in raw format from the backend or are you sending it using geth by unlocking the account? If it is the first, then its the backend, if it is the second, then its geth that sends the TxCat
Nobody captures the events. The events are stored in the Receipts. You can get all the receipts of the block and scan them to pick the ones you are interested in. You can read the events over and over again, so no need in any capturing. The only thing you have to validate when reading this way is that parent hash of the block matches the hash of previous block, so you are protected against the chainsplit (i.e. chain reorg)Cat
@Cat Thank you for your answer. However, I need to write the steps on the individual arrows, so I'm in trouble because I can't find a way to do it correctlyMicrogamete
A
10

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.

Albaalbacete answered 22/1, 2021 at 9:33 Comment(6)
So is it correct to say that the backend requests new blocks from the geth, extracts events from them and selects the necessary ones?Microgamete
Yes, though backend does not request the new blocks. Your Ethereum node (geth is not the only one) requests the new blocks and manages the authoritative state. Your backend then just reads the variables in this state it chooses to.Albaalbacete
thanks, you gave me the clearest answer. Ok so in the end it is my backend that is listening for events. Is it correct?Microgamete
Can I ask you another question? I wrote this question in stackExchange. Geth can be both a full node and a light node but I was asked this question linkMicrogamete
Backend listening for the event is correct.Albaalbacete
Also do not forget to tick the "Correct answer" box on StackOverflow.Albaalbacete

© 2022 - 2024 — McMap. All rights reserved.