Package com.privatechain.consensus.pow
Class ProofOfWorkEngine
java.lang.Object
com.privatechain.consensus.pow.ProofOfWorkEngine
- All Implemented Interfaces:
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault difficulty in leading zero bits. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a proof-of-work engine using the default difficulty.ProofOfWorkEngine(int difficulty) Creates a proof-of-work engine using the supplied difficulty. -
Method Summary
Modifier and TypeMethodDescriptionReturns the stable name used to identify this engine in logs and status output.intReturns the configured difficulty.mineBlock(List<Transaction> transactions, Block previousBlock) Mines a new block by searching for a nonce that satisfies the configured leading-zero-bit target.booleanvalidateBlock(Block block, Blockchain chain) Validates whether the candidate block satisfies the proof-of-work rules.
-
Field Details
-
DEFAULT_DIFFICULTY
public static final int DEFAULT_DIFFICULTYDefault 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- ifdifficultyis less than 1
-
-
Method Details
-
getDifficulty
public int getDifficulty()Returns the configured difficulty.- Returns:
- difficulty in leading zero bits
-
validateBlock
Validates whether the candidate block satisfies the proof-of-work rules.- Specified by:
validateBlockin interfaceConsensusEngine- Parameters:
block- the candidate block to validatechain- the current blockchain state- Returns:
trueif the block is valid for this engine- Throws:
NullPointerException- ifblockorchainis null
-
mineBlock
Mines a new block by searching for a nonce that satisfies the configured leading-zero-bit target.- Specified by:
mineBlockin interfaceConsensusEngine- Parameters:
transactions- ordered transactions to include in the blockpreviousBlock- the current chain tip- Returns:
- a newly mined block
- Throws:
NullPointerException- iftransactionsorpreviousBlockis nullConsensusException- if mining is interrupted or the nonce search is exhausted
-
engineName
Returns the stable name used to identify this engine in logs and status output.- Specified by:
engineNamein interfaceConsensusEngine- Returns:
- the engine name
-