Class Block
Every Block is self-contained: its hash is computed from
the block's own fields (including previousHash and the Merkle root
of its transactions) during construction and stored immutably. Tampering with
any field of a block — or any transaction inside it — invalidates the hash, which
in turn invalidates every subsequent block's previousHash link, making
fraud detectable by Blockchain.isChainValid().
minerAddress is an optional field populated by consensus engines that
need to identify which node produced a block (e.g. PoA, RoundRobin, PBFT).
PoW blocks leave this field null since the producer is identified by
hash effort alone.
Usage
// PoW block (no miner address needed)
Block block = Block.builder()
.index(1)
.previousHash(genesisBlock.getHash())
.transactions(txList)
.header(BlockHeader.builder().nonce(42L).build())
.build();
// PoA / RoundRobin / PBFT block (miner address required)
Block block = Block.builder()
.index(1)
.previousHash(genesisBlock.getHash())
.transactions(txList)
.header(BlockHeader.builder().build())
.minerAddress("0xABC...")
.build();
- Since:
- 1.0.0
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThepreviousHashvalue of the genesis block (index 0). -
Constructor Summary
ConstructorsConstructorDescriptionBlock(int index, BlockHeader header, String previousHash, String hash, List<Transaction> transactions, String minerAddress) Constructs a fully formed, immutable block. -
Method Summary
Modifier and TypeMethodDescriptionstatic Block.Builderbuilder()Returns a new, emptyBlock.Builder.static StringcomputeHash(int index, String previousHash, BlockHeader header) Backwards-compatible overload that omitsminerAddress.static StringcomputeHash(int index, String previousHash, BlockHeader header, String minerAddress) Computes the SHA-256 hash of a block from its constituent fields.booleanTwo blocks are equal if and only if their hashes are equal.getHash()Returns the pre-computed SHA-256 hash of this block.Returns the block header (version, nonce, Merkle root, timestamp).intgetIndex()Returns the zero-based index of this block in the chain.Convenience accessor for the Merkle root stored in the header.Returns the address of the node that produced this block, ornullfor PoW blocks where no identity claim is embedded.Returns the SHA-256 hash of the preceding block.Returns the ordered, immutable list of transactions in this block.inthashCode()Hash code based on the block's SHA-256 hash string.booleanVerifies that the storedhashmatches a fresh computation.toString()Human-readable summary safe for logging (hash truncated, no tx data).
-
Field Details
-
GENESIS_PREVIOUS_HASH
ThepreviousHashvalue of the genesis block (index 0). Defined as 64 hex-encoded zero bytes per FR-CORE-07.- See Also:
-
-
Constructor Details
-
Block
public Block(int index, BlockHeader header, String previousHash, String hash, List<Transaction> transactions, String minerAddress) Constructs a fully formed, immutable block.The
hashparameter is stored as-is (not recomputed). New blocks should be created viaBlock.Builder.build()which computes the hash automatically.- Parameters:
index- block index (≥ 0)header- block header (non-null)previousHash- SHA-256 hex of the preceding block (non-null)hash- SHA-256 hex of this block (non-null)transactions- transactions in this block (non-null; may be empty)minerAddress- address of the producing node, ornullfor PoW blocks- Throws:
NullPointerException- if indexed, header, previousHash, hash, or transactions is nullIllegalArgumentException- if index is negative
-
-
Method Details
-
computeHash
public static String computeHash(int index, String previousHash, BlockHeader header, String minerAddress) Computes the SHA-256 hash of a block from its constituent fields.The hash input is a deterministic canonical string formed by concatenating:
index,previousHash,merkleRoot,timestamp,version,bits,nonce, andminerAddress. IncludingminerAddressin the hash ensures a block cannot claim a different producer without invalidating its hash.- Parameters:
index- block indexpreviousHash- hex hash of the previous blockheader- block header (supplies nonce, Merkle root, etc.)minerAddress- producing node address; use""when absent- Returns:
- hex-encoded SHA-256 digest of the canonical input
- Throws:
IllegalStateException- if SHA-256 is not available (unreachable on JDK 17+)
-
computeHash
Backwards-compatible overload that omitsminerAddress.Delegates to
computeHash(int, String, BlockHeader, String)withminerAddress = null. Preserved so existing callers inblockchain-corethat do not supply a miner address compile without changes.- Parameters:
index- block indexpreviousHash- hex hash of the previous blockheader- block header- Returns:
- hex-encoded SHA-256 digest
-
builder
Returns a new, emptyBlock.Builder. -
getIndex
public int getIndex()Returns the zero-based index of this block in the chain. -
getHeader
Returns the block header (version, nonce, Merkle root, timestamp). -
getPreviousHash
Returns the SHA-256 hash of the preceding block. -
getHash
Returns the pre-computed SHA-256 hash of this block. -
getTransactions
Returns the ordered, immutable list of transactions in this block. -
getMerkleRoot
Convenience accessor for the Merkle root stored in the header. -
getMinerAddress
Returns the address of the node that produced this block, ornullfor PoW blocks where no identity claim is embedded.Used by identity-based consensus engines (ProofOfAuthorityEngine, RoundRobinEngine, PBFTEngine — all in
blockchain-consensus) to validate that the block was produced by an authorized node.- Returns:
- miner address string, or
null
-
isHashValid
public boolean isHashValid()Verifies that the storedhashmatches a fresh computation.Used by
Blockchain.isChainValid()and storage implementations after deserialization to detect corruption (NFR-SEC-03).- Returns:
trueif the stored hash equals a freshly computed hash
-
equals
Two blocks are equal if and only if their hashes are equal. -
hashCode
public int hashCode()Hash code based on the block's SHA-256 hash string. -
toString
Human-readable summary safe for logging (hash truncated, no tx data).
-