Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?
like image 966
EMANUEL Avatar asked Jan 21 '21 15:01

EMANUEL


People also ask

How does a transaction work in Ethereum?

An Ethereum transaction refers to an action initiated by an externally-owned account, in other words an account managed by a human, not a contract. For example, if Bob sends Alice 1 ETH, Bob's account must be debited and Alice's must be credited. This state-changing action takes place within a transaction.

How does ganache work Ethereum?

Ganache is a personal blockchain for rapid Ethereum and Corda distributed application development. You can use Ganache across the entire development cycle; enabling you to develop, deploy, and test your dApps in a safe and deterministic environment. Ganache comes in two flavors: a UI and CLI.

Where are Ethereum events stored?

Events. Smart contracts have the ability to "emit" events during execution. Events are also known as "logs" in Ethereum. The output of the events are stored in transaction receipts under a logs section.


1 Answers

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.

like image 196
Mikko Ohtamaa Avatar answered Oct 21 '22 20:10

Mikko Ohtamaa