Understanding Blockchains Through Java Code

3 months ago 2

This project is just a java implementation for an educative block chain. The full code for this article is committed here

A blockchain is essentially a digital ledger that maintains a continuously growing list of records, called blocks, which are linked and secured using cryptography. Think of it as a chain of digital containers, where each container holds some data and is cryptographically connected to the previous one, making it nearly impossible to alter past records without detection.

Core Components of a Block

Every block in a blockchain contains several essential pieces of information:

  • Hash: A unique digital fingerprint that identifies the block

  • Block Height: The position of the block in the chain (starting from 0)

  • Previous Hash: The hash of the previous block, creating the "chain" connection

  • Timestamp: When the block was created

  • Data: The actual information stored in the block

  • Nonce: A number used in the mining process to find a valid hash

The Genesis Block

Every blockchain starts with a special first block called the "genesis block." This block has no previous block to reference, so its previous hash is empty. In our Java implementation, the genesis block is created with genesisBlock() method

Creating New Blocks

When adding a new block to the chain, it must reference the previous block's hash. This creates the fundamental "chain" structure that gives blockchain its name. Each new block contains:

  • An incremented block height

  • The hash of the previous block

  • A current timestamp

  • New data to be stored

Proof of Work Mining

One of blockchain's most important features is its security mechanism called "Proof of Work" This process, known as mining, requires computational effort to add new blocks to the chain.

How Mining Works

The mining process involves finding a special number called a "nonce" that, when combined with the block's data and hashed using SHA-256, produces a hash that meets certain criteria. In our example, we're looking for hashes that start with "00":

public static void minerProofOfWork(Block block) throws NoSuchAlgorithmException { int nonce = 0; MessageDigest digest = MessageDigest.getInstance("SHA-256"); while (true) { block.nonce = Long.toString(nonce); String encodedData = block.encodedData(); byte[] encodedHash = digest.digest(encodedData.getBytes(StandardCharsets.UTF_8)); String hash = bytesToHex(encodedHash); if (hash.startsWith("00")) { block.hash = hash; return; } nonce++; } }

Why Mining Matters

Mining serves several critical purposes:

  • Security: It makes tampering with the blockchain computationally expensive

  • Consensus: It provides a way for the network to agree on which blocks are valid

  • Immutability: Changing a past block would require redoing all the computational work for that block and every subsequent block

SHA-256

The SHA-256 hashing algorithm is central to blockchain security. This function takes any input and produces a fixed-length (256-bit) output that appears random. However, it's deterministic, meaning the same input always produces the same hash. This makes it easier for the network to verify that the hash meets the required conditions.

The proof of work process adjusts the nonce until the hash meets these conditions. Multiple miners race each other to find the correct nonce, so if someone wants to change something in a block, they would have to redo all the miners' work.

Each block contains the hash of the previous block, creating an unbreakable chain. If someone tries to alter data in an old block, its hash would change, invalidating all subsequent blocks since they reference the original hash. This is what makes blockchains tamper-evident.

Here is a sample output of the code:

Block 2025-07-02T15:09:20.192Z (Height: 0) ├─ Hash: 007e64bcb32305c4185a289d9a2963e8c004938f3246188f753e54c4131b423c... ├─ Previous Hash: "" └─ Data: "Genesis" │ ▼ Block 2025-07-02T15:09:20.347Z (Height: 1) ├─ Hash: 00cbd9262419c115d59df13e28f2fd8e3f348154511840d453794c6cefb4287d... ├─ Previous Hash: "007e64bcb32305c4185a289d9a2963e8c004938f3246188f753e54c4131b423c" └─ Data: "QuestPortal" │ ▼ Block 2025-07-02T15:09:22.470Z (Height: 2) ├─ Hash: 004da73e26b626cb4223af9173ecaf60f848642087092c83c4647e11c0271851... ├─ Previous Hash: "00cbd9262419c115d59df13e28f2fd8e3f348154511840d453794c6cefb4287d" └─ Data: "ActionSystem" │ ▼ Block 2025-07-02T15:09:24.493Z (Height: 3) ├─ Hash: 00db9f75009ba36f3c5f040fc06766304092786f39632427a6feb32cb9517ffa... ├─ Previous Hash: "004da73e26b626cb4223af9173ecaf60f848642087092c83c4647e11c0271851" └─ Data: "PowerfulPlatform"

While our example uses random text as data, real blockchains store transaction records like transfers and contract calls.

Hope this helps you understand how things work, and I might continue this project with other small code examples that will show how blockchain works.

Read Entire Article