Proof-of-Work Miner
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. HashHeaderis double SHA-256 of the serialized header.CheckProofOfWorkinterprets the hash as a big-endian integer and compares it with the target derived fromBits(compact target format).Minescans 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