-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathguts.go
79 lines (57 loc) · 1.49 KB
/
guts.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
// jeffCoin 2. MINER guts.go
package miner
import (
"crypto/sha256"
"fmt"
"math/big"
blockchain "github.com/JeffDeCola/jeffCoin/blockchain"
log "github.com/sirupsen/logrus"
)
func checkErr(err error) {
if err != nil {
fmt.Printf("Error is %+v\n", err)
log.Fatal("ERROR:", err)
}
}
// MINING ****************************************************************************************************************
// mine - tbd
func mine() {
s := "START mine() - tbd"
log.Debug("MINER: GUTS " + s)
// GET THE LOCKED BLOCK
theLockedblock := blockchain.GetLockedBlock()
// LOAD STRUCT
targetHash := big.NewInt(1)
targetHash.Lsh(target, uint(256-targetBits))
pow := &ProofOfWork{thelockedBlock, targetHash}
// MINE - FIND THE NONCE AND HASH
nonce, hash := pow.Run()
fmt.Println("The nonce is", nonce)
fmt.Println("The hash is", hash)
s = "END mine() - tbd"
log.Debug("MINER: GUTS " + s)
}
// Run - tbd
func (pow *ProofOfWork) Run() (int, []byte) {
s := "START Run() - tbd"
log.Debug("MINER: GUTS " + s)
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.prepareData(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")
s = "END Run() - tbd"
log.Debug("MINER: GUTS " + s)
return nonce, hash[:]
}