-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (122 loc) · 3.36 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// +build go1.9
package main
import (
"encoding/json"
"fmt"
"github.com/cellcrypto/open-dangnn-pool/hook"
"github.com/cellcrypto/open-dangnn-pool/util"
"log"
"math/rand"
"os"
"path/filepath"
"runtime"
"time"
"github.com/yvasiyarov/gorelic"
"github.com/cellcrypto/open-dangnn-pool/api"
"github.com/cellcrypto/open-dangnn-pool/payouts"
"github.com/cellcrypto/open-dangnn-pool/proxy"
"github.com/cellcrypto/open-dangnn-pool/storage/mysql"
"github.com/cellcrypto/open-dangnn-pool/storage/redis"
"github.com/cellcrypto/open-dangnn-pool/util/plogger"
)
var cfg proxy.Config
var backend *redis.RedisClient
var db *mysql.Database
var logger *plogger.Logger
func startProxy() {
s := proxy.NewProxy(&cfg, backend, db)
s.Start()
}
func startApi() {
s := api.NewApiServer(&cfg.Api, cfg.Coin, cfg.Name, backend, db)
s.Start()
}
func startBlockUnlocker() {
if util.StringInSlice(cfg.Net,[]string{"mainnet", "testnet"}) == false {
fmt.Println("config file error MainNet or testnet cannot be set")
return
}
u := payouts.NewBlockUnlocker(&cfg.BlockUnlocker, backend, db, cfg.Net, cfg.NetId)
u.Start()
}
func startPayoutsProcessor() {
u := payouts.NewPayoutsProcessor(&cfg.Payouts, backend, db, cfg.NetId)
u.Start()
}
func startNewrelic() {
if cfg.NewrelicEnabled {
nr := gorelic.NewAgent()
nr.Verbose = cfg.NewrelicVerbose
nr.NewrelicLicense = cfg.NewrelicKey
nr.NewrelicName = cfg.NewrelicName
nr.Run()
}
}
func readConfig(cfg *proxy.Config) {
configFileName := "config.json"
if len(os.Args) > 1 {
configFileName = os.Args[1]
}
configFileName, _ = filepath.Abs(configFileName)
log.Printf("Loading config: %v", configFileName)
configFile, err := os.Open(configFileName)
if err != nil {
log.Fatal("File error: ", err.Error())
}
defer configFile.Close()
jsonParser := json.NewDecoder(configFile)
if err := jsonParser.Decode(&cfg); err != nil {
log.Fatal("Config error: ", err.Error())
}
if cfg.Mysql.Coin == "" {
cfg.Mysql.Coin = cfg.Coin
cfg.Mysql.Threshold = cfg.Payouts.Threshold
}
cfg.Api.Threshold = cfg.Payouts.Threshold
cfg.Api.Coin = cfg.Coin
cfg.Api.Name = cfg.Name
cfg.Api.Depth = cfg.BlockUnlocker.Depth
}
func main() {
readConfig(&cfg)
rand.Seed(time.Now().UnixNano())
if cfg.Threads > 0 {
runtime.GOMAXPROCS(cfg.Threads)
log.Printf("Running with %v threads", cfg.Threads)
if cfg.Threads < cfg.Payouts.ConcurrentTx {
log.Printf("The goroutine is smaller than the payout thread GOMAXPROCS:%v > ConcurrentTx:%v ", cfg.Threads, cfg.Payouts.ConcurrentTx)
}
}
startNewrelic()
backend = redis.NewRedisClient(&cfg.Redis, cfg.Coin, cfg.Proxy.Difficulty, cfg.Pplns)
pong, err := backend.Check()
if err != nil {
log.Printf("Can't establish connection to backend: %v", err)
} else {
log.Printf("Backend check reply: %v", pong)
}
if db, err = mysql.New(&cfg.Mysql, cfg.Proxy.Difficulty, backend); err != nil {
log.Printf("Can't establish connection to mysql: %v", err)
os.Exit(1)
}
backend.SetDB(db)
log.Printf("connected mysql host:%v",cfg.Mysql.Endpoint)
hook.RegistryMainHook(func() {
logger.Close() // Save all logs.
})
// logger is pooling
logger = plogger.New(db, cfg.Coin, cfg.Mysql.LogTableName)
if cfg.Proxy.Enabled {
go startProxy()
}
if cfg.Api.Enabled {
go startApi()
}
if cfg.BlockUnlocker.Enabled {
go startBlockUnlocker()
}
if cfg.Payouts.Enabled {
go startPayoutsProcessor()
}
hook.Listen()
}