-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproofofwork.go
102 lines (82 loc) · 2.03 KB
/
proofofwork.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
package main
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"fmt"
"log"
"math"
"math/big"
)
var (
maxNonce = math.MaxInt64
)
// In Bitcoin targetBits is the block header that stores the
// difficulty at which the block was mined. In the real world
// this is couple with a target adjusting algorithm but we won't
// do that now so we define the difficulty as a global constant
const targetBits = 24
// ProofOfWork is a struct that holds a block and the target hash
type ProofOfWork struct {
block *Block
target *big.Int
}
// NewProofOfWork Calculates the target and returns a ProofOfWork struct
func NewProofOfWork(b *Block) *ProofOfWork {
target := big.NewInt(1)
target.Lsh(target, uint(256-targetBits))
pow := &ProofOfWork{b, target}
return pow
}
// PerpareData returns the data we want to hash
func (pow *ProofOfWork) PerpareData(nonce int) []byte {
data := bytes.Join(
[][]byte{
pow.block.PrevBlockHash,
pow.block.Data,
IntToHex(pow.block.Timestamp),
IntToHex(int64(targetBits)),
IntToHex(int64(nonce)),
},
[]byte{},
)
return data
}
// IntToHex converts an integer to a hex
// TODO: maybe change this to bigendian bytes array?
func IntToHex(num int64) []byte {
buff := new(bytes.Buffer)
err := binary.Write(buff, binary.BigEndian, num)
if err != nil {
log.Panic(err)
}
return buff.Bytes()
}
// func IntToHex(n int64) []byte {
// return []byte(strconv.FormatInt(n, 16))
// }
// Run in a loop:
// 1. PrepareData
// 2. Hash it with Sha-256
// 3. convert the hash to a big integer
// 4. Compare the integer with the target.
// (We do this till we get a hash that satisfies the target.)
func (pow *ProofOfWork) Run() (int, []byte) {
var hashInt big.Int
var hash [32]byte
nonce := 0
fmt.Printf("Mining the block containing \"%s\"\n", pow.block.Data)
for nonce < maxNonce {
data := pow.PerpareData(nonce)
hash = sha256.Sum256(data)
fmt.Printf("\r%x", hash)
hashInt.SetBytes(hash[:])
if hashInt.Cmp(pow.target) == -1 {
break
} else {
nonce++
}
}
fmt.Print("\n\n")
return nonce, hash[:]
}