Querying data in an Ethereum Blockchain (2024)

Inside Ethereum Blockchain

Querying data in an Ethereum Blockchain (3)

As you may have come across that Ethereum is a public blockchain network that transacts billions of dollars in value. But you may have not realized that the Ethereum codebase is also used by organizations worldwide to form business blockchain networks. These private Ethereum Blockchain networks are usually permissioned, although separating them from the public Ethereum main-net, they have an option to access the data on the main-net. This adaptation of Ethereum Blockchain Technology requires a good understanding of ways to read and write using Smart Contracts.

Try Bitquery GraphQL APIs to query Ethereum blockchain.

Solidity

Ethereum Smart contracts are mostly developed in a newly designed high-level Turing-complete programming language, Solidity. Solidity has gained popularity as it has been specifically designed to provide the requirements outlined by an Ethereum transaction. These programs are compiled to the Ethereum Virtual Machine(EVM) bytecode, a low-level stack-based language, and then deployed on the Ethereum Blockchain network where it runs on all Ethereum nodes. For instance, the following listing shows a Smart Contract written in the Solidity programming language. The first line of the program declares Solidity’s version used in this Smart Contract. The program contains a constructor to create an instance of the contract, and functions.

Querying data in an Ethereum Blockchain (4)

Data in Solidity

One can say that Solidity is like other programming languages, with an exception being in running the code and handling data. It is interesting to note that Solidity handles data only in the EVM or the blockchain network and is peculiar about its storage. There are mainly two types of variables in Solidity: state variables and local variables. State variables are responsible for storing the state of a Smart Contract by saving the values in a block on the blockchain. These variables are like a global variable in other programming languages as they are defined in the contract section and are available anywhere in the smart contract. And like local variables of other programming languages, local variables in Solidity are defined inside functions. These variables save their values only during the execution of the function they are defined in.

Data storage in Ethereum Blockchain

Ethereum implements a trie data structure to manage temporary and permanent data. The three types of trie data structures in the Ethereum Blockchain are State Trie, Storage Trie and Transaction Trie.

A mined and confirmed transaction is recorded in the transaction trie. Temporary data like the above mentioned local variables and account addresses are stored in the state trie where it changes and gets updated continuously, thereby updating the state of the complete Ethereum Blockchain.

State trie

There is one global state trie in Ethereum that is constantly updated. It contains a key-value pair, where a key is the address of an Ethereum account and value is the Recursive-Length Prefix (RLP) encoded value of nonce, balance, storageRoot, the hash of an account on Ethereum network.

Storage trie

A storage trie is where all of the contract data is stored. Each Ethereum account has its own storage trie. A 256-bit hash of the storage trie’s root node is stored as the storageRoot value in the global state trie.

Transaction trie

The path to a specific transaction in the transaction trie is via (the RLP encoding of) the index of where the transaction sits in the block. Mined blocks are never updated; the position of the transaction in a block is never changed. This means that once you locate a transaction in a block’s transaction trie, you can return to the same path over and over to retrieve the same result.

Geth and LevelDB to access Ethereum Blockchain Metadata

Go Ethereum is one of the three original implementations (along with C++ and Python) of the Ethereum protocol. It is written in Go, is fully open-source and is available as a standalone client called Geth. Geth downloads the complete Ethereum blockchain and indexes it with some database to enable efficient lookup and retrieval of data or to store the state. Geth uses LevelDB because LevelDB is implemented in GO, has key-value pairs and includes modern data storage (i.e. multiple layers on disk, organized in the background)

However, there are also some limitations to using LevelDB:

  • Ethereum uses hashes that are uniformly randomly distributed identifiers. LevelDB organizes the keys “alphabetically” on disk, therefore, accessing values associated with hashes is very expensive.
  • As long as most of LevelDB can fit into memory, it performs very well, but with increasing disk space, the performance gets degraded.
  • Querying LevelDB returns encoded results because Ethereum uses a “Modified Merkle Patricia Trie” implementation when interacting with LevelDB.

However, to access data stored on the Ethereum Blockchain, there are some existing tools as well. If the data is associated with a transaction, Ethereum (ETH) BlockChain Explorer can be used to view the data. If the data is stored in some smart contracts, the smart contract’s interface can be used to read the data. To write some data onto the Ethereum Blockchain, either sending some ethers to an address with embedded data or interacting with smart contracts on the Ethereum blockchain can enable writing data for you.

Web3.js to access events data on Ethereum Blockchain

Web3.js can be used to effectively query and listen for contract events on the Ethereum blockchain enabling specifications of actions that get triggered when certain criteria are met.

An event listener to upcoming events to specify a callback that will occur when the event is emitted can be used in the following way,

  • For any event, contract.events.allEvents() can be used to specify a callback for all events.
contract.events.allEvents()
.on('data', (event) => {
console.log(event);
})
.on('error', console.error);
  • For a specific event, contract.events.EventName() can be used to specify actions for specific event types. For example, Transfer:
contract.events.Transfer()
.on('data', (event) => {
console.log(event);
})
.on('error', console.error);

The output of the above-described event listeners returns valuable data which is described in detail in the table below.

Querying data in an Ethereum Blockchain (5)

Conclusion

Ethereum Blockchain functioning is fairly straightforward, the different storage space for permanent and temporary data in different tries ensures that data is safe and the data is easily manageable. Therefore, enabling access to the data stored on the Ethereum blockchain safe and easy.

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

Querying data in an Ethereum Blockchain (2024)
Top Articles
Will Multiple Credit Inquiries Hurt My Credit Score?
What is military-grade encryption? - PassCamp
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Virgilio Hermann JD

Last Updated:

Views: 6105

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Virgilio Hermann JD

Birthday: 1997-12-21

Address: 6946 Schoen Cove, Sipesshire, MO 55944

Phone: +3763365785260

Job: Accounting Engineer

Hobby: Web surfing, Rafting, Dowsing, Stand-up comedy, Ghost hunting, Swimming, Amateur radio

Introduction: My name is Virgilio Hermann JD, I am a fine, gifted, beautiful, encouraging, kind, talented, zealous person who loves writing and wants to share my knowledge and understanding with you.