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
Is 64GB iPad Enough for Students?
Never Worry About Losing Your Wallet Again
Hometown Pizza Sheridan Menu
Printable Whoville Houses Clipart
Ixl Elmoreco.com
Kent And Pelczar Obituaries
10000 Divided By 5
Noaa Weather Philadelphia
Edgar And Herschel Trivia Questions
13 The Musical Common Sense Media
2021 Lexus IS for sale - Richardson, TX - craigslist
Craigslist Boats For Sale Seattle
Gwdonate Org
Vcuapi
Morgan And Nay Funeral Home Obituaries
Cvb Location Code Lookup
ARK: Survival Evolved Valguero Map Guide: Resource Locations, Bosses, & Dinos
2020 Military Pay Charts – Officer & Enlisted Pay Scales (3.1% Raise)
Alfie Liebel
Bjerrum difference plots - Big Chemical Encyclopedia
Baja Boats For Sale On Craigslist
Drug Test 35765N
8005607994
Boston Dynamics’ new humanoid moves like no robot you’ve ever seen
Jayah And Kimora Phone Number
fft - Fast Fourier transform
Foodsmart Jonesboro Ar Weekly Ad
City Of Durham Recycling Schedule
Best Middle Schools In Queens Ny
Craigslist Rome Ny
Geico Car Insurance Review 2024
Marilyn Seipt Obituary
Gunsmoke Tv Series Wiki
The Goonies Showtimes Near Marcus Rosemount Cinema
Rubmaps H
Grays Anatomy Wiki
Hypixel Skyblock Dyes
Exploring TrippleThePotatoes: A Popular Game - Unblocked Hub
Marine Forecast Sandy Hook To Manasquan Inlet
Polk County Released Inmates
Mcgiftcardmall.con
Puretalkusa.com/Amac
1Exquisitetaste
Frigidaire Fdsh450Laf Installation Manual
2017 Ford F550 Rear Axle Nut Torque Spec
Nope 123Movies Full
Clock Batteries Perhaps Crossword Clue
Is Chanel West Coast Pregnant Due Date
Goosetown Communications Guilford Ct
Bob Wright Yukon Accident
All Obituaries | Roberts Funeral Home | Logan OH funeral home and cremation
Guidance | GreenStar™ 3 2630 Display
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.