/
relay.go
190 lines (164 loc) · 4.43 KB
/
relay.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
package server
import (
"encoding/hex"
"errors"
"fmt"
"time"
"github.com/gogo/protobuf/proto"
"github.com/nknorg/nkn/pb"
"github.com/nknorg/nkn/por"
"github.com/nknorg/nkn/util/address"
"github.com/nknorg/nkn/util/log"
)
type sigChainInfo struct {
blockHash []byte
sigChainLen int
}
func (ws *WsServer) sendOutboundRelayMessage(srcAddrStrPtr *string, msg *pb.OutboundMessage) {
if srcAddrStrPtr == nil {
log.Warningf("src addr is nil")
return
}
dests := msg.Dests
if len(dests) == 0 && len(msg.Dest) > 0 {
dests = append(dests, msg.Dest)
}
if len(dests) == 0 {
log.Warningf("no destination")
return
}
payloads := msg.Payloads
if len(payloads) == 0 && len(msg.Payload) > 0 {
payloads = append(payloads, msg.Payload)
}
if len(payloads) == 0 {
log.Warningf("no payload")
return
}
if len(payloads) > 1 && len(payloads) != len(dests) {
log.Warningf("payloads length %d is different from dests length %d", len(payloads), len(dests))
return
}
var payload []byte
for i, dest := range dests {
if len(payloads) > 1 {
payload = payloads[i]
} else {
payload = payloads[0]
}
err := ws.localNode.SendRelayMessage(*srcAddrStrPtr, dest, payload, msg.Signatures[i], msg.BlockHash, msg.Nonce, msg.MaxHoldingSeconds)
if err != nil {
log.Error("Send relay message error:", err)
}
}
}
func (ws *WsServer) sendInboundMessage(clientID string, inboundMsg *pb.InboundMessage) bool {
clients := ws.SessionList.GetSessionsById(clientID)
if clients == nil {
log.Debugf("Client Not Online: %s", clientID)
return false
}
buf, err := proto.Marshal(inboundMsg)
if err != nil {
log.Errorf("Marshal inbound message error: %v", err)
return false
}
msg := &pb.ClientMessage{
MessageType: pb.INBOUND_MESSAGE,
Message: buf,
}
buf, err = proto.Marshal(msg)
if err != nil {
log.Errorf("Marshal client message error: %v", err)
return false
}
success := false
for _, client := range clients {
if !client.IsClient() {
log.Error("Session is not client")
continue
}
err = client.SendBinary(buf)
if err != nil {
log.Error("Send to client error: ", err)
continue
}
success = true
}
return success
}
func (ws *WsServer) sendInboundRelayMessage(relayMessage *pb.Relay) {
clientID := relayMessage.DestId
msg := &pb.InboundMessage{
Src: address.AssembleClientAddress(relayMessage.SrcIdentifier, relayMessage.SrcPubkey),
Payload: relayMessage.Payload,
}
shouldSign := por.GetPorServer().ShouldSignDestSigChainElem(relayMessage.BlockHash, relayMessage.LastHash, int(relayMessage.SigChainLen))
if shouldSign {
msg.PrevHash = relayMessage.LastHash
}
success := ws.sendInboundMessage(hex.EncodeToString(clientID), msg)
if success {
if shouldSign {
ws.sigChainCache.Add(relayMessage.LastHash, &sigChainInfo{
blockHash: relayMessage.BlockHash,
sigChainLen: int(relayMessage.SigChainLen),
})
}
if time.Duration(relayMessage.MaxHoldingSeconds) > pongTimeout/time.Second {
ok := ws.messageDeliveredCache.Push(relayMessage)
if !ok {
log.Warningf("MessageDeliveredCache full, discarding messages.")
}
}
} else if relayMessage.MaxHoldingSeconds > 0 {
ws.messageBuffer.AddMessage(clientID, relayMessage)
}
}
func (ws *WsServer) startCheckingLostMessages() {
for {
v, ok := ws.messageDeliveredCache.Pop()
if !ok {
break
}
if relayMessage, ok := v.(*pb.Relay); ok {
clientID := relayMessage.DestId
clients := ws.SessionList.GetSessionsById(hex.EncodeToString(clientID))
if len(clients) > 0 {
threshold := time.Now().Add(-pongTimeout)
success := false
for _, client := range clients {
if client.GetLastReadTime().After(threshold) {
success = true
break
}
}
if success {
continue
}
}
ws.messageBuffer.AddMessage(clientID, relayMessage)
}
}
}
func (ws *WsServer) handleReceipt(receipt *pb.Receipt) error {
v, ok := ws.sigChainCache.Get(receipt.PrevHash)
if !ok {
return fmt.Errorf("sigchain info with last hash %x not found in cache", receipt.PrevHash)
}
sci, ok := v.(*sigChainInfo)
if !ok {
return errors.New("convert to sigchain info failed")
}
if !por.GetPorServer().ShouldSignDestSigChainElem(sci.blockHash, receipt.PrevHash, sci.sigChainLen) {
return nil
}
destSigChainElem := pb.NewSigChainElem(nil, nil, receipt.Signature, nil, nil, false, pb.SIGNATURE)
por.GetPorServer().AddDestSigChainElem(
sci.blockHash,
receipt.PrevHash,
sci.sigChainLen,
destSigChainElem,
)
return nil
}