Class Block.Builder

java.lang.Object
com.privatechain.core.model.Block.Builder
Enclosing class:
Block

public static final class Block.Builder extends Object
Fluent builder for Block.

Calling build() computes the block hash automatically. Supply minerAddress(String) for identity-based consensus engines (PoA, RoundRobin, PBFT); omit it for PoW blocks.


 // PoA block
 Block block = Block.builder()
     .index(1)
     .previousHash(prev.getHash())
     .transactions(txList)
     .header(BlockHeader.builder().build())
     .minerAddress(localAddress)
     .build();
 
  • Method Details

    • index

      public Block.Builder index(int index)
      Sets the block index.
      Parameters:
      index - block index (≥ 0)
      Returns:
      this builder
    • header

      public Block.Builder header(BlockHeader header)
      Sets the block header.
      Parameters:
      header - non-null block header
      Returns:
      this builder
    • previousHash

      public Block.Builder previousHash(String previousHash)
      Sets the hash of the preceding block.
      Parameters:
      previousHash - hex-encoded SHA-256 hash (non-null)
      Returns:
      this builder
    • transactions

      public Block.Builder transactions(List<Transaction> transactions)
      Sets the transaction list for this block.
      Parameters:
      transactions - list of transactions (non-null; may be empty)
      Returns:
      this builder
    • minerAddress

      public Block.Builder minerAddress(String minerAddress)
      Sets the address of the node producing this block.

      Required for identity-based consensus (PoA, RoundRobin, PBFT). Leave unset (or pass null) for PoW blocks.

      Parameters:
      minerAddress - producing node address; null is allowed
      Returns:
      this builder
    • build

      public Block build()
      Builds and returns an immutable Block.

      The block hash is computed automatically from all supplied fields including minerAddress.

      Returns:
      a new immutable Block with a computed hash