/
server.go
242 lines (185 loc) · 6.37 KB
/
server.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package beam
import (
"encoding/json"
"github.com/blocktree/openwallet/log"
"github.com/blocktree/openwallet/owtp"
)
type Server struct {
wm *WalletManager
node *owtp.OWTPNode
config *WalletConfig
disconnectHandler func(node *Server, nodeID string) //托管节点断开连接后的通知
connectHandler func(node *Server, nodeInfo *TrustNodeInfo) //托管节点连接成功的通知
}
func NewServer(wm *WalletManager) (*Server, error) {
config := wm.Config
cert := owtp.NewRandomCertificate()
connectCfg := owtp.ConnectConfig{}
connectCfg.Address = config.remoteserver
connectCfg.EnableSSL = config.enablessl
connectCfg.ConnectType = config.connecttype
node := owtp.NewNode(owtp.NodeConfig{
Cert: cert,
TimeoutSEC: config.requesttimeout,
})
t := &Server{
node: node,
config: config,
wm: wm,
}
node.HandleFunc("newNodeJoin", t.newNodeJoin)
node.HandleFunc("getTransactionsByHeight", t.getTransactionsByHeight)
node.HandleFunc("getTransaction", t.getTransaction)
node.HandleFunc("createBatchAddress", t.createBatchAddress)
node.HandleFunc("getWalletBalance", t.getWalletBalance)
node.HandleFunc("getWalletAddress", t.getWalletAddress)
node.HandleFunc("getBlockByHeight", t.getBlockByHeight)
node.SetCloseHandler(func(n *owtp.OWTPNode, peer owtp.PeerInfo) {
if t.disconnectHandler != nil {
t.disconnectHandler(t, peer.ID)
}
})
return t, nil
}
//Listen 启动监听
func (server *Server) Listen() {
//开启监听
server.wm.Log.Infof("Transmit node IP %s start to listen [%s] connection...", server.config.remoteserver, server.config.connecttype)
server.node.Listen(owtp.ConnectConfig{
Address: server.config.remoteserver,
ConnectType: server.config.connecttype,
})
}
//Close 关闭监听
func (server *Server) Close() {
server.node.Close()
}
//SetConnectHandler 设置托管节点断开连接后的通知
func (server *Server) SetConnectHandler(h func(node *Server, nodeInfo *TrustNodeInfo)) {
server.connectHandler = h
}
//SetDisconnectHandler 设置托管节点连接成功的通知
func (server *Server) SetDisconnectHandler(h func(node *Server, nodeID string)) {
server.disconnectHandler = h
}
//checkTrustNode 检查是否授信节点
func (server *Server) checkTrustNode(nodeID string) bool {
//判断连接的客户端NodeID是否授信
trustNodeID := server.wm.Config.trustnodeid
if len(trustNodeID) > 0 {
if trustNodeID != nodeID {
log.Warningf("The Joining Node: %s is not trusted", nodeID)
server.node.ClosePeer(nodeID)
return false
}
}
return true
}
/*********** 本地路由方法实现 ***********/
func (server *Server) newNodeJoin(ctx *owtp.Context) {
if !server.checkTrustNode(ctx.PID) {
ctx.Response(nil, owtp.ErrDenialOfService, "the node is not trusted")
return
}
if server.connectHandler != nil {
var nodeInfo TrustNodeInfo
err := json.Unmarshal([]byte(ctx.Params().Get("nodeInfo").Raw), &nodeInfo)
if err != nil {
ctx.Response(nil, owtp.ErrCustomError, err.Error())
return
}
server.connectHandler(server, &nodeInfo)
}
ctx.Response(nil, owtp.StatusSuccess, "success")
}
func (server *Server) getTransactionsByHeight(ctx *owtp.Context) {
//server.wm.Log.Infof("Client call [getTransactionsByHeight]")
if !server.checkTrustNode(ctx.PID) {
ctx.Response(nil, owtp.ErrDenialOfService, "the node is not trusted")
return
}
height := ctx.Params().Get("height").Uint()
txs, err := server.wm.walletClient.GetTransactionsByHeight(height)
if err != nil {
ctx.Response(nil, owtp.ErrCustomError, err.Error())
return
}
ctx.Response(txs, owtp.StatusSuccess, "success")
//server.wm.Log.Infof("---------------------------------------")
}
func (server *Server) getTransaction(ctx *owtp.Context) {
server.wm.Log.Infof("Client call [getTransaction]")
if !server.checkTrustNode(ctx.PID) {
ctx.Response(nil, owtp.ErrDenialOfService, "the node is not trusted")
return
}
txid := ctx.Params().Get("txid").String()
tx, err := server.wm.walletClient.GetTransaction(txid)
if err != nil {
ctx.Response(nil, owtp.ErrCustomError, err.Error())
return
}
ctx.Response(tx, owtp.StatusSuccess, "success")
server.wm.Log.Infof("---------------------------------------")
}
func (server *Server) createBatchAddress(ctx *owtp.Context) {
count := ctx.Params().Get("count").Uint()
workerSize := ctx.Params().Get("workerSize").Uint()
server.wm.Log.Infof("Client call [createBatchAddress]")
server.wm.Log.Infof("count: %d", count)
server.wm.Log.Infof("workerSize: %d", workerSize)
addrs, err := server.wm.CreateLocalWalletAddress(count, workerSize)
if err != nil {
ctx.Response(nil, owtp.ErrCustomError, err.Error())
return
}
ctx.Response(addrs, owtp.StatusSuccess, "success")
server.wm.Log.Infof("---------------------------------------")
}
func (server *Server) getWalletBalance(ctx *owtp.Context) {
server.wm.Log.Infof("Client call [getWalletBalance]")
if !server.checkTrustNode(ctx.PID) {
ctx.Response(nil, owtp.ErrDenialOfService, "the node is not trusted")
return
}
balance, err := server.wm.GetLocalWalletBalance()
if err != nil {
ctx.Response(nil, owtp.ErrCustomError, err.Error())
return
}
result := map[string]interface{} {
"balance": balance,
}
server.wm.Log.Infof("balance: %+v", balance)
ctx.Response(result, owtp.StatusSuccess, "success")
server.wm.Log.Infof("---------------------------------------")
}
func (server *Server) getWalletAddress(ctx *owtp.Context) {
server.wm.Log.Infof("Client call [getWalletAddress]")
if !server.checkTrustNode(ctx.PID) {
ctx.Response(nil, owtp.ErrDenialOfService, "the node is not trusted")
return
}
addrs, err := server.wm.GetLocalWalletAddress()
if err != nil {
ctx.Response(nil, owtp.ErrCustomError, err.Error())
return
}
ctx.Response(addrs, owtp.StatusSuccess, "success")
server.wm.Log.Infof("---------------------------------------")
}
func (server *Server) getBlockByHeight(ctx *owtp.Context) {
//server.wm.Log.Infof("Client call [getTransactionsByHeight]")
if !server.checkTrustNode(ctx.PID) {
ctx.Response(nil, owtp.ErrDenialOfService, "the node is not trusted")
return
}
height := ctx.Params().Get("height").Uint()
block, err := server.wm.walletClient.GetBlockByHeight(height)
if err != nil {
ctx.Response(nil, owtp.ErrCustomError, err.Error())
return
}
ctx.Response(block, owtp.StatusSuccess, "success")
//server.wm.Log.Infof("---------------------------------------")
}