-
Notifications
You must be signed in to change notification settings - Fork 4
/
arbiter.go
228 lines (186 loc) · 6.39 KB
/
arbiter.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"flag"
"io"
"net/http"
"os"
"path/filepath"
"github.com/elastos/Elastos.ELA.Arbiter/arbitration/arbitrator"
"github.com/elastos/Elastos.ELA.Arbiter/arbitration/cs"
"github.com/elastos/Elastos.ELA.Arbiter/arbitration/mainchain"
"github.com/elastos/Elastos.ELA.Arbiter/arbitration/sidechain"
"github.com/elastos/Elastos.ELA.Arbiter/config"
"github.com/elastos/Elastos.ELA.Arbiter/log"
"github.com/elastos/Elastos.ELA.Arbiter/net/servers/httpjsonrpc"
"github.com/elastos/Elastos.ELA.Arbiter/password"
"github.com/elastos/Elastos.ELA.Arbiter/sideauxpow"
"github.com/elastos/Elastos.ELA.Arbiter/store"
"github.com/elastos/Elastos.ELA.SPV/interface"
"github.com/elastos/Elastos.ELA/account"
elatx "github.com/elastos/Elastos.ELA/core/transaction"
"github.com/elastos/Elastos.ELA/core/types/functions"
"github.com/elastos/Elastos.ELA/dpos/p2p/peer"
"github.com/elastos/Elastos.ELA/utils/elalog"
)
var (
LogsPath = filepath.Join(config.DataPath, config.LogDir)
ArbiterLogOutputPath = filepath.Join(LogsPath, config.ArbiterDir)
SpvLogOutputPath = filepath.Join(LogsPath, config.SpvDir)
)
const (
defaultSpvMaxPerLogFileSize int64 = elalog.MBSize * 20
defaultSpvMaxLogsFolderSize int64 = elalog.GBSize * 2
defaultArbiterMaxPerLogFileSize int64 = 20
defaultArbiterMaxLogsFolderSize int64 = 2 * 1024
)
var walletPath string
var pstr string
func init() {
v := versionFlag{}
flag.Var(&v, "v", "print version and exit")
flag.StringVar(&walletPath, "wallet", "", "wallet path, default: keystore.dat")
flag.StringVar(&walletPath, "w", "", "wallet path, default: keystore.dat")
flag.StringVar(&pstr, "p", "", "wallet password")
flag.Parse()
}
type versionFlag struct{}
func (versionFlag) IsBoolFlag() bool { return true }
func (versionFlag) Get() interface{} { return nil }
func (r *versionFlag) String() string { return config.Version }
func (r *versionFlag) Set(s string) error {
println("arbiter version", config.Version)
os.Exit(0)
return nil
}
func initialize() {
config.Initialize()
// Initialize functions
functions.GetTransactionByTxType = elatx.GetTransaction
functions.GetTransactionByBytes = elatx.GetTransactionByBytes
functions.CreateTransaction = elatx.CreateTransaction
functions.GetTransactionParameters = elatx.GetTransactionparameters
spvMaxPerLogFileSize := defaultSpvMaxPerLogFileSize
spvMaxLogsFolderSize := defaultSpvMaxLogsFolderSize
if config.Parameters.MaxPerLogSize > 0 {
spvMaxPerLogFileSize = int64(config.Parameters.MaxPerLogSize) * elalog.MBSize
}
if config.Parameters.MaxLogsSize > 0 {
spvMaxLogsFolderSize = int64(config.Parameters.MaxLogsSize) * elalog.MBSize
}
fileWriter := elalog.NewFileWriter(
SpvLogOutputPath,
spvMaxPerLogFileSize,
spvMaxLogsFolderSize,
)
logWriter := io.MultiWriter(os.Stdout, fileWriter)
level := elalog.Level(config.Parameters.SPVPrintLevel)
backend := elalog.NewBackend(logWriter, elalog.Llongfile)
spvslog := backend.Logger("SPVS", level)
_interface.UseLogger(spvslog)
arbiterMaxPerLogFileSize := defaultArbiterMaxPerLogFileSize
arbiterMaxLogsFolderSize := defaultArbiterMaxLogsFolderSize
if config.Parameters.MaxPerLogSize > 0 {
arbiterMaxPerLogFileSize = int64(config.Parameters.MaxPerLogSize)
}
if config.Parameters.MaxLogsSize > 0 {
arbiterMaxLogsFolderSize = int64(config.Parameters.MaxLogsSize)
}
log.Init(
ArbiterLogOutputPath,
config.Parameters.PrintLevel,
arbiterMaxPerLogFileSize,
arbiterMaxLogsFolderSize,
)
if walletPath != "" {
config.Parameters.WalletPath = walletPath
}
log.Info("Arbiter version:", config.Version)
log.Info("path:", walletPath)
log.Info("Init wallet.")
passwd, err := password.GetAccountPassword(pstr)
if err != nil {
log.Fatal("Get password error.")
os.Exit(1)
}
c, err := account.Open(config.Parameters.WalletPath, passwd)
if err != nil || c == nil {
log.Fatal("error: open wallet failed, ", err)
os.Exit(1)
}
sideauxpow.Init(c)
arbitrator.Init(c)
sidechain.Init()
}
func setSideChainAccountMonitor(arb arbitrator.Arbitrator) {
sidechain.SideChainAccountMonitor.ParentArbitrator = arb
for i, side := range arb.GetSideChainManager().GetAllChains() {
sidechain.SideChainAccountMonitor.AddListener(side)
go sidechain.SideChainAccountMonitor.SyncChainData(config.Parameters.SideNodeList[i], side, config.Parameters.SideNodeList[i].EffectiveHeight)
}
}
func initP2P(arbitrator arbitrator.Arbitrator) error {
pk, err := arbitrator.GetPublicKey().EncodePoint(true)
if err != nil {
return err
}
var id peer.PID
copy(id[:], pk)
if err := cs.InitP2PClient(id); err != nil {
return err
}
//register p2p client listener
if err := mainchain.InitMainChain(arbitrator); err != nil {
return err
}
cs.P2PClientSingleton.Start()
return nil
}
func main() {
initialize()
log.Info("1. Init chain utxo cache.")
dataStore, err := store.OpenDataStore()
if err != nil {
log.Fatalf("Data store open failed error: [s%]", err.Error())
os.Exit(1)
}
store.DbCache = *dataStore
log.Info("2. Init finished transaction cache.")
finishedDataStore, err := store.OpenFinishedTxsDataStore()
if err != nil {
log.Fatalf("Side chain monitor setup error: [s%]", err.Error())
os.Exit(1)
}
store.FinishedTxsDbCache = finishedDataStore
currentArbitrator := arbitrator.ArbitratorGroupSingleton.GetCurrentArbitrator()
log.Info("3. Start arbitrator P2P networks.")
if err := initP2P(currentArbitrator); err != nil {
log.Fatal(err)
os.Exit(1)
}
setSideChainAccountMonitor(currentArbitrator)
log.Info("4. Init configurations.")
if err := arbitrator.ArbitratorGroupSingleton.InitArbitrators(); err != nil {
log.Fatal(err)
os.Exit(1)
}
log.Info("5. Start arbitrator spv module.")
if err := currentArbitrator.StartSpvModule(); err != nil {
log.Fatal(err)
os.Exit(1)
}
log.Info("6. Start arbitrator group monitor.")
go arbitrator.ArbitratorGroupSingleton.SyncLoop()
log.Info("7. Start servers.")
pServer := new(http.Server)
go httpjsonrpc.StartRPCServer(pServer)
log.Info("8. Start check and remove cross chain transactions from db.")
go currentArbitrator.CheckAndRemoveCrossChainTransactionsFromDBLoop()
log.Info("9. Start side chain account divide.")
go sideauxpow.SidechainAccountDivide()
log.Info("10. Start small crosschain transfer monitor.")
go arbitrator.MonitorSmallCrossTransfer()
log.Info("11. Start invalid withdraw transaction monitor.")
go arbitrator.MonitorInvalidWithdrawTransaction()
sidechain.Initialized = true
select {}
}