-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpow.go
65 lines (54 loc) · 1.18 KB
/
pow.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"bytes"
"crypto/sha256"
"fmt"
"math"
"math/big"
"strconv"
)
const targetBits = 16
type POW struct {
block *Block
target *big.Int
}
func MakePOW(b *Block) *POW {
target := big.NewInt(1)
target.Lsh(target, uint(256-targetBits))
return &POW{block: b, target: target}
}
func (pow *POW) MakeData(nonce int) []byte {
return bytes.Join([][]byte{
pow.block.PrevHash,
pow.block.Data,
[]byte(strconv.FormatInt(pow.block.Timestamp, 16)),
[]byte(strconv.FormatInt(int64(targetBits), 16)),
[]byte(strconv.FormatInt(int64(nonce), 16)),
}, []byte{})
}
func (pow *POW) Run() (int, []byte) {
nonce := 0
var hashInt big.Int
var hash [32]byte
fmt.Printf("Mining block with data \"%s\" ...\n", string(pow.block.Data))
for nonce < math.MaxInt64 {
data := pow.MakeData(nonce)
hash = sha256.Sum256(data)
fmt.Printf("\r%x", hash)
hashInt.SetBytes(hash[:])
if hashInt.Cmp(pow.target) == -1 {
break
} else {
nonce++
}
}
fmt.Printf("\n\n")
return nonce, hash[:]
}
func (pow *POW) Validate() bool {
var hashInt big.Int
data := pow.MakeData(pow.block.Nonce)
hash := sha256.Sum256(data)
hashInt.SetBytes(hash[:])
return hashInt.Cmp(pow.target) == -1
}