Proof-of-Work Miner

medium · bitcoin, pow, mining

Proof-of-Work Miner

Implement this pack's Bitcoin-inspired header hashing and bounded nonce search.

Types

type BlockHeader struct {
    Version    uint32
    PrevHash   [32]byte
    MerkleRoot [32]byte
    Timestamp  uint32
    Bits       uint32
    Nonce      uint32
}

func SerializeHeader(h BlockHeader) []byte
func HashHeader(h BlockHeader) [32]byte
func CheckProofOfWork(h BlockHeader) bool
func Mine(h BlockHeader, maxNonce uint32) (BlockHeader, bool)

Rules

  • Serialize header fields in this order, little-endian for integers: Version || PrevHash || MerkleRoot || Timestamp || Bits || Nonce.
  • Copy the two supplied 32-byte hash arrays unchanged; serialization is 80 bytes.
  • HashHeader is double SHA-256 of the serialized header.
  • Hash raw bytes at both stages, not hexadecimal text.
  • CheckProofOfWork interprets the hash as a big-endian integer and compares it with the target derived from Bits (compact target format).
  • Mine scans nonces from 0..maxNonce inclusive, returns the first header that satisfies PoW.
  • The initial header nonce does not set the start. maxNonce=0 tests one candidate. Stop after the endpoint before uint32 increment can wrap.

Notes

  • Decode compact targets using this pack's unsigned 24-bit mantissa rule. Accept hash <= target, including equality. The raw digest's big-endian interpretation and unsigned compact targets are course conventions; this function is not Bitcoin consensus validation.
  • If no nonce is found within the range, return (BlockHeader{}, false).
Run tests to see results
No issues detected
    Join Discord