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?
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
EMANUEL
  • 103
  • 1
  • 5
  • 1
    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 Tx – Nulik Jan 22 '21 at 13:29
  • 1
    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) – Nulik Jan 22 '21 at 13:35
  • @Nulik 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 correctly – EMANUEL Jan 23 '21 at 13:18

1 Answers1

9

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.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • So is it correct to say that the backend requests new blocks from the geth, extracts events from them and selects the necessary ones? – EMANUEL Jan 22 '21 at 10:14
  • 1
    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. – Mikko Ohtamaa Jan 22 '21 at 10:32
  • thanks, you gave me the clearest answer. Ok so in the end it is my backend that is listening for events. Is it correct? – EMANUEL Jan 22 '21 at 10:39
  • 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 [link](https://ethereum.stackexchange.com/questions/92654/full-node-light-node-and-future-changes) – EMANUEL Jan 22 '21 at 10:42
  • 1
    Backend listening for the event is correct. – Mikko Ohtamaa Jan 22 '21 at 12:02
  • Also do not forget to tick the "Correct answer" box on StackOverflow. – Mikko Ohtamaa Jan 22 '21 at 12:02