Proof-of-Work Miner

medium · bitcoin, pow, mining

Proof-of-Work Miner

Implement Bitcoin-style header hashing and a simple 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.
  • HashHeader is double SHA-256 of the serialized header.
  • 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.

Notes

  • Use the same compact target math as Bitcoin.
  • If no nonce is found within the range, return (BlockHeader{}, false).
Run tests to see results
No issues detected