-
Notifications
You must be signed in to change notification settings - Fork 0
/
simnet.go
236 lines (202 loc) · 5.66 KB
/
simnet.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
package simulation
import (
"context"
"sync"
"time"
"github.com/spf13/viper"
"github.com/dnerochain/dnero/common"
"github.com/dnerochain/dnero/p2p"
p2ptypes "github.com/dnerochain/dnero/p2p/types"
)
// Envelope wraps a message with network information for delivery.
type Envelope struct {
From string
To string
Content interface{}
}
// Simnet represents an instance of simulated network.
type Simnet struct {
Endpoints []*SimnetEndpoint
msgHandler p2p.MessageHandler
messages chan Envelope
MsgLogs []Envelope
// Life cycle.
wg *sync.WaitGroup
mu *sync.Mutex
ctx context.Context
cancel context.CancelFunc
stopped bool
}
// NewSimnet creates a new instance of Simnet.
func NewSimnet() *Simnet {
return &Simnet{
messages: make(chan Envelope, viper.GetInt(common.CfgP2PMessageQueueSize)),
MsgLogs: []Envelope{},
wg: &sync.WaitGroup{},
mu: &sync.Mutex{},
}
}
// NewSimnetWithHandler creates a new instance of Simnet with given MessageHandler as the default handler.
func NewSimnetWithHandler(msgHandler p2p.MessageHandler) *Simnet {
return &Simnet{
msgHandler: msgHandler,
messages: make(chan Envelope, viper.GetInt(common.CfgP2PMessageQueueSize)),
wg: &sync.WaitGroup{},
mu: &sync.Mutex{},
}
}
// AddEndpoint adds an endpoint with given ID to the Simnet instance.
func (sn *Simnet) AddEndpoint(id string) *SimnetEndpoint {
endpoint := &SimnetEndpoint{
id: id,
network: sn,
incoming: make(chan Envelope, viper.GetInt(common.CfgP2PMessageQueueSize)),
outgoing: make(chan Envelope, viper.GetInt(common.CfgP2PMessageQueueSize)),
}
sn.Endpoints = append(sn.Endpoints, endpoint)
return endpoint
}
// Start is the main entry point for Simnet. It starts all endpoints and start a goroutine to handle message dlivery.
func (sn *Simnet) Start(ctx context.Context) {
c, cancel := context.WithCancel(ctx)
sn.ctx = c
sn.cancel = cancel
for _, endpoint := range sn.Endpoints {
endpoint.Start(ctx)
}
go sn.mainLoop()
}
// Stop notifies all goroutines to stop without blocking.
func (sn *Simnet) Stop() {
sn.cancel()
}
// Wait blocks until all goroutines have stopped.
func (sn *Simnet) Wait() {
sn.wg.Wait()
}
func (sn *Simnet) mainLoop() {
sn.wg.Add(1)
defer sn.wg.Done()
for {
select {
case <-sn.ctx.Done():
sn.mu.Lock()
sn.stopped = true
sn.mu.Unlock()
return
case envelope := <-sn.messages:
time.Sleep(1 * time.Microsecond)
for _, endpoint := range sn.Endpoints {
if (envelope.To == "" && envelope.From != endpoint.ID()) || envelope.To == endpoint.ID() {
go func(endpoint *SimnetEndpoint, envelope Envelope) {
// Simulate network delay except for messages to self.
if envelope.From != endpoint.ID() {
// time.Sleep(100 * time.Millisecond)
}
endpoint.incoming <- envelope
}(endpoint, envelope)
}
}
}
}
}
// AddMessage send a message through the network.
func (sn *Simnet) AddMessage(msg Envelope) {
sn.mu.Lock()
defer sn.mu.Unlock()
if sn.stopped {
return
}
sn.MsgLogs = append(sn.MsgLogs, msg)
sn.messages <- msg
}
// SimnetEndpoint is the implementation of Network interface for Simnet.
type SimnetEndpoint struct {
id string
network *Simnet
handlers []p2p.MessageHandler
incoming chan Envelope
outgoing chan Envelope
}
var _ p2p.Network = &SimnetEndpoint{}
// Start implements the Network interface. It starts goroutines to receive/send message from network.
func (se *SimnetEndpoint) Start(ctx context.Context) error {
go func() {
for {
select {
case envelope := <-se.incoming:
message := p2ptypes.Message{
PeerID: envelope.From,
Content: envelope.Content,
}
se.HandleMessage(message)
}
}
}()
go func() {
for {
select {
case envelope := <-se.outgoing:
se.network.messages <- envelope
}
}
}()
return nil
}
// Stop implements the Network interface.
func (se *SimnetEndpoint) Stop() {
}
// Wait blocks until all goroutines have stopped.
func (se *SimnetEndpoint) Wait() {
}
// Broadcast implements the Network interface.
func (se *SimnetEndpoint) Broadcast(message p2ptypes.Message) (successes chan bool) {
successes = make(chan bool, 10)
go func() {
se.network.AddMessage(Envelope{From: se.ID(), Content: message.Content})
successes <- true
}()
return successes
}
// BroadcastToNeighbors implements the Network interface.
func (se *SimnetEndpoint) BroadcastToNeighbors(message p2ptypes.Message, maxNumPeersToBroadcast int) (successes chan bool) {
successes = make(chan bool, 10)
go func() {
se.network.AddMessage(Envelope{From: se.ID(), Content: message.Content})
successes <- true
}()
return successes
}
// Send implements the Network interface.
func (se *SimnetEndpoint) Send(id string, message p2ptypes.Message) bool {
go func() {
se.network.AddMessage(Envelope{From: se.ID(), To: id, Content: message.Content})
}()
return true
}
// Peers returns the IDs of all peers
func (se *SimnetEndpoint) Peers() []string {
return []string{}
}
// PeerExists indicates if the given peerID is a neighboring peer
func (se *SimnetEndpoint) PeerExists(peerID string) bool {
return false
}
// RegisterMessageHandler implements the Network interface.
func (se *SimnetEndpoint) RegisterMessageHandler(handler p2p.MessageHandler) {
se.handlers = append(se.handlers, handler)
}
// ID implements the Network interface.
func (se *SimnetEndpoint) ID() string {
return se.id
}
// HandleMessage implements the MessageHandler interface.
func (se *SimnetEndpoint) HandleMessage(message p2ptypes.Message) error {
for _, handler := range se.handlers {
handler.HandleMessage(message)
}
if se.network.msgHandler != nil {
se.network.msgHandler.HandleMessage(message)
}
return nil
}