/
miner.go
104 lines (91 loc) · 2.23 KB
/
miner.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
package main
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"runtime"
"time"
)
const baseUrl string = "http://git-coin.herokuapp.com"
//const baseUrl string = "http://localhost:9292"
func gcUrl(path string) string {
return fmt.Sprintf("%s%s", baseUrl, path)
}
func generateMessage() string {
return fmt.Sprintf("%d%v", rand.Int63(), time.Now())
}
func fetchTarget() []byte {
resp, err := http.Get(gcUrl("/target"))
if err != nil {
panic("Couldn't fetch target")
}
target, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic("error reading response body")
}
defer resp.Body.Close()
return target
}
func submitMessage(message string) {
resp, err := http.PostForm(gcUrl("/hash"), url.Values{"owner": {"worace"}, "message": {message}})
if err != nil {
panic("failed to submit guess")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic("error reading response body")
}
fmt.Println(string(body))
}
func digest(input string) []byte {
h := sha1.New()
h.Write([]byte(input))
return h.Sum(nil)
}
func mine(finished chan bool) {
iterations := 1
currentTarget := fetchTarget()
targetBytes, _ := hex.DecodeString(string(currentTarget))
reloadTarget := func() {
currentTarget = fetchTarget()
targetBytes, _ = hex.DecodeString(string(currentTarget))
fmt.Println("target now: ", string(currentTarget))
iterations = 1
}
message := generateMessage()
start := time.Now()
for {
iterations++
hashAttempt := digest(message)
if iterations > 4000000 {
fmt.Println("completed 4mil attempts; re-checking target; current message: ", message)
fmt.Println("elapsed time: ", time.Now().Sub(start))
start = time.Now()
fmt.Printf("digest: ", hex.EncodeToString(hashAttempt))
reloadTarget()
iterations = 1
}
if bytes.Compare(hashAttempt, targetBytes) < 0 {
fmt.Println("congrats got a hash!")
submitMessage(message)
reloadTarget()
//finished <- true
}
message = hex.EncodeToString(hashAttempt)
}
}
func main() {
fmt.Println("NUM CPUS", runtime.NumCPU())
finished := make(chan bool)
for i := 0; i < runtime.NumCPU(); i++ {
go mine(finished)
}
foundHash := <-finished
fmt.Println("Found hash", foundHash)
}