Ethereum: Chainstate vs. Blocks folder
In the Ethereum ecosystem, both “chainstate” and “block folder” are decisive components for packaging and calling up blockchain data. However, they serve different purposes and make it important to understand their differences before they immerse themselves in reading and display the data on a web frontend.
chain state
The “Chainstate” folder is the central repository of Ethereum’s state, which contains various information, such as: B.:
- Block height
- Block number
- Block the block time stamp
- Transaction number
- Gas price
- Unconfirmed transaction number
The “Chainstate” folder stores this data in a hierarchical structure, whereby each file represents a block or section of the blockchain. The files are organized in directories such as state_root
,state_tree
and tx_count
.
Blocks folder
The “block folder” is responsible for the packaging of individual blocks of the blockchain. Each block contains different data such as:
- Block hash
- Previous hash block (superordinate hash)
- Transaction number
- Unconfirmed transaction number
- Gas price
- Time stamp
The “block folder” stores each block in a separate file, whereby each file represents a single block.
Key differences
While both folders save the same type of data, there are important differences:
Chainstate
is the central repository of Ethereum’s state, while” block folder “stores individual blocks.
- The files in “Chainstate” have a hierarchical structure, whereby each directory contains subdirectories and files, while “block folder” has separate files for each block.
Reading and displaying data
In order to read and display the data on a web front end like Blockchain Explorer. Here is an example of how you can do it with solidity (the Ethereum programming language) and Web3.js:
`JavaScript
Import * as Web3 from "Web3";
Const web3 = new web3 (new web3.providers.httprovider ('
// get the chain state
Const chainstate = web3.eth.chainstate;
// get all blocks
Asynchrical function fetblocks () {
const txlist = waiting web3.eth.Gettransactionlist ();
For (let i = 0; i
Const block = txlist [i]. Block;
// Read block data from chainstate
console.log (block.timestamp);
}
}
// you get a certain block from his hash
Asynchrical function fetblockbyhash () {
Const Blockhash = '0x1234567890abcdef';
Const Blockdata = WaTte Web3.eth.GetBlock (blockhash, true);
// Read block data from Blocksfolder
console.log (blockdata.hash);
}
Show data on a web frontend
In order to display the data on a web -front end like Blockchain Explorer -website, you must use the “Ether” library from Web3.js to interact with the Ethereum network and access certain data. You can use HTML5 screen or other technologies to visualize the data.
Here is an example of how you can display the block time stamp using HTML5 screen:
`Html
Const CTX = Document.GETELEMTYID ('Block-Timestamp').
Set interval (() => {
Web3.eth.getbalance (web3.eth.account.getdaddress (), (error, balance) => {
If (error) {
console.error (error);
} Differently {
Const Timestamp = new date ().
ctx.Clearrect (0, 0, canvas.width, canvas.height);
ctx.beginpath ();
ctx.arc (canvas.width / 2, canvas.height / 2, math.min (time temple, balance), 0, 2 * math.pi);
ctx.fillstyle = 'black';
ctx.fill ();
}
});
}, 1000); // update every second
`
This code calls the current block time and draws a circle on an HTML5 -Canvas element. You can adjust the appearance of the circle by changing the CTX.