forked from thetatoken/theta-protocol-ledger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.go
266 lines (219 loc) · 6.79 KB
/
peer.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package peer
import (
"context"
"sync"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
cmn "github.com/thetatoken/theta/common"
p2ptypes "github.com/thetatoken/theta/p2p/types"
"github.com/thetatoken/theta/p2pl/transport"
"github.com/libp2p/go-libp2p-core/network"
"github.com/thetatoken/theta/rlp"
pr "github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
)
var logger *log.Entry = log.WithFields(log.Fields{"prefix": "p2pl"})
var Channels = []cmn.ChannelIDEnum{
cmn.ChannelIDCheckpoint,
cmn.ChannelIDHeader,
cmn.ChannelIDBlock,
cmn.ChannelIDProposal,
cmn.ChannelIDVote,
cmn.ChannelIDTransaction,
cmn.ChannelIDPeerDiscovery,
cmn.ChannelIDPing,
cmn.ChannelIDGuardian,
cmn.ChannelIDEliteEdgeNodeVote,
cmn.ChannelIDAggregatedEliteEdgeNodeVotes,
}
//
// Peer models a peer node in a network
//
type Peer struct {
addrInfo pr.AddrInfo
isOutbound bool
streamMap map[cmn.ChannelIDEnum](*transport.BufferedStream) // channelID -> stream
mutex *sync.Mutex
openStreamsTimer *time.Timer
onStream StreamCreator
onRawStream RawStreamCreator
onParse MessageParser
onEncode MessageEncoder
onReceive ReceiveHandler
onError ErrorHandler
// Life cycle
wg *sync.WaitGroup
quit chan struct{}
ctx context.Context
cancel context.CancelFunc
stopped bool
}
func CreatePeer(addrInfo pr.AddrInfo, isOutbound bool) *Peer {
peer := &Peer{
addrInfo: addrInfo,
isOutbound: isOutbound,
streamMap: make(map[cmn.ChannelIDEnum](*transport.BufferedStream)),
mutex: &sync.Mutex{},
onEncode: defaultMessageEncoder,
wg: &sync.WaitGroup{},
}
return peer
}
func (peer *Peer) OpenStreams() error {
if peer.isOutbound {
peer.openStreamsTimer = time.NewTimer(3 * time.Second)
go func() {
<-peer.openStreamsTimer.C
peer.openStreams()
}()
}
return nil
}
func (peer *Peer) openStreams() {
peer.mutex.Lock()
defer peer.mutex.Unlock()
for _, channel := range Channels {
stream, err := peer.onStream(channel)
if err != nil {
logger.Debugf("Failed to create stream with peer %v %v for channel %v, %v", peer.addrInfo.ID, peer.addrInfo.Addrs, channel, err)
continue
}
if s, ok := peer.streamMap[channel]; ok {
s.Stop()
}
peer.streamMap[channel] = stream
}
}
func (peer *Peer) AcceptStream(channel cmn.ChannelIDEnum, stream *transport.BufferedStream) {
if !peer.isOutbound {
peer.mutex.Lock()
defer peer.mutex.Unlock()
if s, ok := peer.streamMap[channel]; ok {
s.Stop()
}
peer.streamMap[channel] = stream
}
}
func (peer *Peer) StopStream(channel cmn.ChannelIDEnum) {
peer.mutex.Lock()
defer peer.mutex.Unlock()
if stream, ok := peer.streamMap[channel]; ok {
stream.Stop()
}
}
// Start is called when the peer starts
func (peer *Peer) Start(ctx context.Context) bool {
c, cancel := context.WithCancel(ctx)
peer.ctx = c
peer.cancel = cancel
return true
}
// Wait suspends the caller goroutine
func (peer *Peer) Wait() {
peer.wg.Wait()
}
// Stop is called when the peer stops
func (peer *Peer) Stop() {
if peer.openStreamsTimer != nil {
peer.openStreamsTimer.Stop()
}
peer.mutex.Lock()
defer peer.mutex.Unlock()
for _, stream := range peer.streamMap {
stream.Stop()
}
// peer.streamMap = nil
}
// Send sends the given message through the specified channel to the target peer
func (peer *Peer) Send(channelID cmn.ChannelIDEnum, message interface{}) bool {
msgBytes, err := peer.onEncode(channelID, message)
if err != nil {
logger.Errorf("Failed to encode message to bytes: %v, err: %v", message, err)
return false
}
var n int
reuseStream := viper.GetBool(cmn.CfgP2PReuseStream)
if reuseStream {
peer.mutex.Lock()
stream := peer.streamMap[channelID]
peer.mutex.Unlock()
if stream == nil {
logger.Debugf("Can't find stream for channel %v", channelID)
return false
}
n, err = stream.Write(msgBytes)
if err != nil {
logger.Errorf("Error writing stream to peer %v for channel %v, %v", peer.addrInfo.ID, channelID, err)
return false
}
} else {
rawStream, err := peer.onRawStream(channelID)
if rawStream != nil {
defer rawStream.Close()
}
if err != nil {
logger.Errorf("Stream open failed: %v. peer: %v, addrs: %v, channel: %v", err, peer.ID(), peer.Addrs(), channelID)
return false
}
n, err = rawStream.Write(msgBytes)
if err != nil {
logger.Errorf("Error writing stream to peer %v for channel %v, %v", peer.addrInfo.ID, channelID, err)
return false
}
}
if n != len(msgBytes) {
logger.Errorf("Didn't write expected bytes length")
return false
}
return true
}
// ID returns the unique idenitifier of the peer in the P2P network
func (peer *Peer) ID() pr.ID {
return peer.addrInfo.ID
}
// Addrs returns the Multiaddresses of the peer in the P2P network
func (peer *Peer) Addrs() []ma.Multiaddr {
return peer.addrInfo.Addrs
}
// AddrInfo returns the addrInfo of the peer in the P2P network
func (peer *Peer) AddrInfo() pr.AddrInfo {
return peer.addrInfo
}
// StreamCreator creates a buffered stream with this peer
type StreamCreator func(channelID cmn.ChannelIDEnum) (*transport.BufferedStream, error)
// RawStreamCreator creates a raw libp2p stream with this peer
type RawStreamCreator func(channelID cmn.ChannelIDEnum) (network.Stream, error)
// MessageParser parses the raw message bytes to type p2ptypes.Message
type MessageParser func(channelID cmn.ChannelIDEnum, rawMessageBytes cmn.Bytes) (p2ptypes.Message, error)
// MessageEncoder encodes type p2ptypes.Message to raw message bytes
type MessageEncoder func(channelID cmn.ChannelIDEnum, message interface{}) (cmn.Bytes, error)
var defaultMessageEncoder MessageEncoder = func(channelID cmn.ChannelIDEnum, message interface{}) (cmn.Bytes, error) {
return rlp.EncodeToBytes(message)
}
// ReceiveHandler is the callback function to handle received bytes from the given channel
type ReceiveHandler func(message p2ptypes.Message) error
// ErrorHandler is the callback function to handle channel read errors
type ErrorHandler func(interface{})
func (peer *Peer) SetStreamCreator(streamCreator StreamCreator) {
peer.onStream = streamCreator
}
func (peer *Peer) SetRawStreamCreator(rawStreamCreator RawStreamCreator) {
peer.onRawStream = rawStreamCreator
}
// SetMessageParser sets the message parser for the connection
func (peer *Peer) SetMessageParser(messageParser MessageParser) {
peer.onParse = messageParser
}
// SetMessageEncoder sets the message encoder for the connection
func (peer *Peer) SetMessageEncoder(messageEncoder MessageEncoder) {
peer.onEncode = messageEncoder
}
// SetReceiveHandler sets the receive handler for the connection
func (peer *Peer) SetReceiveHandler(receiveHandler ReceiveHandler) {
peer.onReceive = receiveHandler
}
// SetErrorHandler sets the error handler for the connection
func (peer *Peer) SetErrorHandler(errorHandler ErrorHandler) {
peer.onError = errorHandler
}