-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpow.go
111 lines (91 loc) · 2.11 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"math"
"math/big"
"math/rand"
)
// Controls difficulty of mining
const targetBits = 24
// Maximum value of counter
var maxNonce = math.MaxInt32
type ProofOfWork struct {
block *Block
target *big.Int
}
func NewProofOfWork(b *Block) *ProofOfWork {
target := big.NewInt(1)
target.Lsh(target, uint(256-targetBits))
return &ProofOfWork{b, target}
}
func (pow *ProofOfWork) Try(iterations int) (bool, int, []byte) {
blockBytes := GetBytes(pow.block.Data)
blockData := pow.prepareData(blockBytes)
for i := 0; i < iterations; i++ {
nonce := rand.Intn(maxNonce)
success, hash := pow.Calculate(blockData, nonce)
if success {
Log("MINING SUCCESS")
return success, nonce, hash
}
}
return false, 0, nil
}
// Returns nonce and hash
func (pow *ProofOfWork) Calculate(dataHash []byte, nonce int) (bool, []byte) {
var hashInt big.Int
hash := nonceHash(dataHash, nonce)
hashInt.SetBytes(hash[:])
if hashInt.Cmp(pow.target) == -1 {
return true, hash[:]
}
return false, hash[:]
}
// Validate proof of work
func (pow *ProofOfWork) Validate() bool {
var hashInt big.Int
hash := pow.GetHash()
hashInt.SetBytes(hash[:])
isValid := hashInt.Cmp(pow.target) == -1
return isValid
}
func (pow *ProofOfWork) GetHash() []byte {
blockBytes := GetBytes(pow.block.Data)
blockData := pow.prepareData(blockBytes)
return nonceHash(blockData, pow.block.Nonce)
}
// Merge block fields with target
func (pow *ProofOfWork) prepareData(dataBytes []byte) []byte {
data := bytes.Join(
[][]byte{
pow.block.PrevBlockHash,
dataBytes,
IntToBytes(pow.block.Timestamp),
IntToBytes(int64(targetBits)),
},
[]byte{},
)
dataHash := sha256.Sum256(data)
return dataHash[:]
}
func nonceHash(blockData []byte, nonce int) []byte {
withNonce := bytes.Join(
[][]byte{
blockData,
IntToBytes(int64(nonce)),
},
[]byte{},
)
hash := sha256.Sum256(withNonce)
return hash[:]
}
func IntToBytes(num int64) []byte {
buff := new(bytes.Buffer)
err := binary.Write(buff, binary.BigEndian, num)
if err != nil {
LogPanic(err.Error())
}
return buff.Bytes()
}