forked from Wasakachain/Miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (71 loc) · 1.74 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"runtime"
"sync"
)
var (
account map[string]string
)
func main() {
host := flag.String("host", "http://localhost:5555", "--host <url>") // host flag
cpu := flag.Int("cpu", runtime.NumCPU(), "--cpu <max cpu count>") // cpu flag
help := flag.Bool("help", false, "--help Display help")
// parse flags
flag.Parse()
if *help {
flag.PrintDefaults()
return
}
// Create account file if does not exists
checkAccountFile()
// Read account file and save the data in account map
loadAccount()
//Set the max cpu count
if *cpu > runtime.NumCPU() {
*cpu = runtime.NumCPU()
}
runtime.GOMAXPROCS(*cpu)
fmt.Printf("Using %d CPU\n", *cpu)
fmt.Printf("Mining on node: %v\n", *host)
for {
var wg sync.WaitGroup
wg.Add(10)
block, err := requestBlock(*host) // request block candidate
if err != nil {
println("Error on mining job request")
panic(err.Error())
}
fmt.Printf("\033[44mMining block #%d\033[0m\n", block.index)
mutex := new(sync.Mutex)
once := new(sync.Once)
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
block.mine(*host, mutex, once)
}()
}
wg.Wait()
}
}
func checkAccountFile() {
// execPath, _ := exec.LookPath(os.Args[0])
dir, _ := /* filepath.Abs(execPath) */ os.Getwd()
if _, err := os.Stat(path.Join(dir, "account.json")); os.IsNotExist(err) {
exec.Command("node", path.Join(dir, "generateAccount.js")).Run()
}
}
func loadAccount() {
// execPath, _ := exec.LookPath(os.Args[0])
dir, _ := /* filepath.Abs(execPath) */ os.Getwd()
jsonFile, _ := os.Open(path.Join(dir, "account.json"))
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteValue, &account)
}