forked from dreams-money/merged-mining-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (97 loc) · 3.29 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"flag"
"fmt"
"log"
"runtime"
"time"
"designs.capital/dogepool/api"
"designs.capital/dogepool/config"
"designs.capital/dogepool/payouts"
"designs.capital/dogepool/persistence"
"designs.capital/dogepool/pool"
"designs.capital/dogepool/rpc"
)
func main() {
configFileName := parseCommandLineOptions()
if configFileName == "" {
configFileName = "config.json"
}
configuration := config.LoadConfig(configFileName)
err := persistence.MakePersister(configuration)
if err != nil {
log.Fatal(err)
}
rpcManagers := makeRPCManagers(configuration)
startPoolServer(configuration, rpcManagers)
startStatManager(configuration)
startAPIServer(configuration)
startPayoutService(configuration, rpcManagers)
startAppStatsService(configuration)
}
func parseCommandLineOptions() string {
flag.Parse()
return flag.Arg(0)
}
func startPoolServer(configuration *config.Config, managers map[string]*rpc.Manager) *pool.PoolServer {
poolServer := pool.NewServer(configuration, managers)
go poolServer.Start()
log.Println("Started Pool on port: " + configuration.Port)
return poolServer
}
func startAPIServer(configuration *config.Config) {
go api.ListenAndServe(configuration)
log.Println("Started API on port: " + configuration.API.Port)
}
func startStatManager(configuration *config.Config) {
hashrateWindow := mustParseDuration(configuration.HashrateWindow)
statsRecordInterval := mustParseDuration(configuration.PoolStatsInterval)
go persistence.UpdateStatsOnInterval(configuration.PoolName, hashrateWindow, statsRecordInterval)
log.Printf("Stat Manager running every %v with a hashrate window of %v\n", statsRecordInterval, hashrateWindow)
}
func startPayoutService(configuration *config.Config, manager map[string]*rpc.Manager) {
interval := mustParseDuration(configuration.Payouts.Interval)
go payouts.RunManager(configuration, manager, interval)
log.Printf("Payouts manager running every %v\n", interval)
}
func startAppStatsService(configuration *config.Config) {
interval := mustParseDuration(configuration.AppStatsInterval)
for {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
fmt.Println("STATS START")
log.Printf("Total CPU cores avaliable: %v", runtime.NumCPU())
log.Printf("Total Goroutines: %v", runtime.NumGoroutine())
log.Printf("Total System Memory: %v", memStats.Sys)
log.Printf("Total Memory Allocated: %v", memStats.TotalAlloc)
fmt.Println("STATS END")
time.Sleep(interval)
}
}
func makeRPCManagers(configuration *config.Config) map[string]*rpc.Manager {
managers := make(map[string]*rpc.Manager)
for _, chain := range configuration.BlockChainOrder {
nodeConfigs := configuration.BlockchainNodes[chain]
rpcConfig := make([]rpc.Config, len(nodeConfigs))
for i, nodeConfig := range nodeConfigs {
rpcConfig[i] = rpc.Config{
Name: nodeConfig.Name,
URL: nodeConfig.RPC_URL,
Username: nodeConfig.RPC_Username,
Password: nodeConfig.RPC_Password,
Timeout: nodeConfig.Timeout,
}
}
// TODO move interval to config if accepted
manager := rpc.MakeRPCManager(chain, rpcConfig, "1h")
managers[chain] = &manager
}
return managers
}
func mustParseDuration(s string) time.Duration {
value, err := time.ParseDuration(s)
if err != nil {
panic("util: Can't parse duration `" + s + "`: " + err.Error())
}
return value
}