Class ProofOfWorkEngine

java.lang.Object
com.privatechain.consensus.pow.ProofOfWorkEngine
All Implemented Interfaces:
ConsensusEngine

public final class ProofOfWorkEngine extends Object implements ConsensusEngine
SHA-256 proof-of-work consensus engine.

This engine follows the classic mining model: it repeatedly increments the nonce until the resulting block hash satisfies the configured leading-zero-bit target. Validation is intentionally strict and checks the following invariants:

  • the block hash still matches the block contents,
  • the Merkle root matches the ordered transaction list, and
  • the hash satisfies the configured proof-of-work prefix target.

The default difficulty matches the repository’s milestone requirements and is suitable for local tests and demos.

Since:
1.0.0
  • Field Details

    • DEFAULT_DIFFICULTY

      public static final int DEFAULT_DIFFICULTY
      Default difficulty in leading zero bits.
      See Also:
  • Constructor Details

    • ProofOfWorkEngine

      public ProofOfWorkEngine()
      Creates a proof-of-work engine using the default difficulty.

      This is the recommended constructor for tests and examples that only need a working proof-of-work chain with the repository default target.

    • ProofOfWorkEngine

      public ProofOfWorkEngine(int difficulty)
      Creates a proof-of-work engine using the supplied difficulty.
      Parameters:
      difficulty - required number of leading zero bits in a valid hash
      Throws:
      IllegalArgumentException - if difficulty is less than 1
  • Method Details

    • getDifficulty

      public int getDifficulty()
      Returns the configured difficulty.
      Returns:
      difficulty in leading zero bits
    • validateBlock

      public boolean validateBlock(Block block, Blockchain chain)
      Validates whether the candidate block satisfies the proof-of-work rules.
      Specified by:
      validateBlock in interface ConsensusEngine
      Parameters:
      block - the candidate block to validate
      chain - the current blockchain state
      Returns:
      true if the block is valid for this engine
      Throws:
      NullPointerException - if block or chain is null
    • mineBlock

      public Block mineBlock(List<Transaction> transactions, Block previousBlock)
      Mines a new block by searching for a nonce that satisfies the configured leading-zero-bit target.
      Specified by:
      mineBlock in interface ConsensusEngine
      Parameters:
      transactions - ordered transactions to include in the block
      previousBlock - the current chain tip
      Returns:
      a newly mined block
      Throws:
      NullPointerException - if transactions or previousBlock is null
      ConsensusException - if mining is interrupted or the nonce search is exhausted
    • engineName

      public String engineName()
      Returns the stable name used to identify this engine in logs and status output.
      Specified by:
      engineName in interface ConsensusEngine
      Returns:
      the engine name