-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility_mining.go
More file actions
37 lines (34 loc) · 899 Bytes
/
Copy pathutility_mining.go
File metadata and controls
37 lines (34 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// ABOUTME: Mining utilities for SHA-256 proof-of-work header hashing,
// ABOUTME: difficulty checking, and the core mining loop.
package quark
import (
"encoding/hex"
"math/big"
)
func meetsDifficulty(headerHash string, difficulty int32) (bool, error) {
h, err := hex.DecodeString(headerHash)
if err != nil {
return false, err
}
var x big.Int
x.SetBytes(h)
target := big.NewInt(1)
target.Lsh(target, uint(256-difficulty))
return x.Cmp(target) < 0, nil
}
func mineHeader(previousHash string, txs []*Transaction, difficulty int32, timestamp int64) *BlockHeader {
bh := &BlockHeader{
PreviousHash: previousHash,
MerkleRoot: merkleRoot(txs),
Timestamp: timestamp,
Difficulty: difficulty,
}
for nonce := int64(0); ; nonce++ {
bh.Nonce = nonce
bh.Hash = bh.computeHash()
ok, err := meetsDifficulty(bh.Hash, difficulty)
if err == nil && ok {
return bh
}
}
}