-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
175 lines (144 loc) · 3.67 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"fmt"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
"github.com/rsksmart/liquidity-provider-server/connectors"
"github.com/rsksmart/liquidity-provider-server/http"
"github.com/rsksmart/liquidity-provider-server/storage"
"github.com/rsksmart/liquidity-provider/providers"
log "github.com/sirupsen/logrus"
"github.com/tkanos/gonfig"
)
var (
cfg config
srv http.Server
)
func loadConfig() {
err := gonfig.GetConf("config.json", &cfg)
if err != nil {
log.Fatalf("error loading config file: %v", err)
}
}
func initLogger() {
if cfg.Debug {
log.SetLevel(log.DebugLevel)
}
if cfg.LogFile == "" {
return
}
f, err := os.OpenFile(cfg.LogFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Error(fmt.Sprintf("cannot open file %v: ", cfg.LogFile), err)
} else {
log.SetOutput(f)
}
}
func startServer(rsk *connectors.RSK, btc *connectors.BTC, db *storage.DB) {
pwdFile, err := os.Open(cfg.Provider.PwdFilePath)
if err != nil {
log.Fatal("cannot read password file: ", err)
}
providerCfg := providers.ProviderConfig{
PwdFile: pwdFile,
Keydir: cfg.Provider.Keystore,
BtcAddr: cfg.Provider.BtcAddress,
AccountNum: cfg.Provider.RskAccountNum,
}
lp, err := providers.NewLocalProvider(providerCfg)
if err != nil {
log.Fatal("cannot create local provider: ", err)
}
srv = http.New(rsk, btc, db)
srv.AddProvider(lp)
port := cfg.Server.Port
if port == 0 {
port = 8080
}
go func() {
err := srv.Start(port)
if err != nil {
log.Error("server error: ", err.Error())
}
}()
}
func initFederation(rsk connectors.RSK) (*connectors.FedInfo, error) {
fedSize, err := rsk.GetFedSize()
if err != nil {
return nil, err
}
var pubKeys []string
for i := 0; i < fedSize; i++ {
pubKey, err := rsk.GetFedPublicKey(i)
if err != nil {
log.Error("error fetching fed public key: ", err.Error())
return nil, err
}
pubKeys = append(pubKeys, pubKey)
}
fedThreshold, err := rsk.GetFedThreshold()
if err != nil {
log.Error("error fetching federation size: ", err.Error())
return nil, err
}
fedAddress, err := rsk.GetFedAddress()
if err != nil {
return nil, err
}
activeFedBlockHeight, err := rsk.GetActiveFederationCreationBlockHeight()
if err != nil {
log.Error("error fetching federation address: ", err.Error())
return nil, err
}
return &connectors.FedInfo{
FedThreshold: fedThreshold,
FedSize: fedSize,
PubKeys: pubKeys,
FedAddress: fedAddress,
ActiveFedBlockHeight: activeFedBlockHeight,
IrisActivationHeight: cfg.IrisActivationHeight,
ErpKeys: cfg.ErpKeys,
}, nil
}
func main() {
loadConfig()
initLogger()
rand.Seed(time.Now().UnixNano())
log.Info("starting liquidity provider server")
log.Debugf("loaded config %+v", cfg)
db, err := storage.Connect(cfg.DB.Path)
if err != nil {
log.Fatal("error connecting to DB: ", err)
}
rsk, err := connectors.NewRSK(cfg.RSK.LBCAddr, cfg.RSK.BridgeAddr)
if err != nil {
log.Fatal("RSK error: ", err)
}
err = rsk.Connect(cfg.RSK.Endpoint)
if err != nil {
log.Fatal("error connecting to RSK: ", err)
}
fedInfo, err := initFederation(*rsk)
if err != nil {
log.Fatal("error initializing federation info: ", err)
}
btc, err := connectors.NewBTC(cfg.BTC.Network, *fedInfo)
if err != nil {
log.Fatal("error initializing BTC connector: ", err)
}
err = btc.Connect(cfg.BTC.Endpoint, cfg.BTC.Username, cfg.BTC.Password)
if err != nil {
log.Fatal("error connecting to BTC: ", err)
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
startServer(rsk, btc, db)
<-done
srv.Shutdown()
db.Close()
rsk.Close()
btc.Close()
}