aboutsummaryrefslogtreecommitdiffstats
path: root/core/test/fake-transport.go
blob: 2f1686e7bec3550bbaeca9395c09951eb5dc8831 (plain) (blame)
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
// Copyright 2018 The dexon-consensus-core Authors
// This file is part of the dexon-consensus-core library.
//
// The dexon-consensus-core library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus-core library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus-core library. If not, see
// <http://www.gnu.org/licenses/>.

package test

import (
    "fmt"
    "time"

    "github.com/dexon-foundation/dexon-consensus-core/core/types"
)

// FakeTransport implement TransportServer and TransportClient interface
// by using golang channel.
type FakeTransport struct {
    peerType      TransportPeerType
    nID           types.NodeID
    recvChannel   chan *TransportEnvelope
    serverChannel chan<- *TransportEnvelope
    peers         map[types.NodeID]chan<- *TransportEnvelope
    latency       LatencyModel
}

// NewFakeTransportServer constructs FakeTransport instance for peer server.
func NewFakeTransportServer() TransportServer {
    return &FakeTransport{
        peerType:    TransportPeerServer,
        recvChannel: make(chan *TransportEnvelope, 1000),
    }
}

// NewFakeTransportClient constructs FakeTransport instance for peer.
func NewFakeTransportClient(
    nID types.NodeID, latency LatencyModel) TransportClient {

    return &FakeTransport{
        peerType:    TransportPeer,
        recvChannel: make(chan *TransportEnvelope, 1000),
        nID:         nID,
        latency:     latency,
    }
}

// Send implements Transport.Send method.
func (t *FakeTransport) Send(
    endpoint types.NodeID, msg interface{}) (err error) {

    ch, exists := t.peers[endpoint]
    if !exists {
        err = fmt.Errorf("the endpoint does not exists: %v", endpoint)
        return
    }
    go func(ch chan<- *TransportEnvelope) {
        if t.latency != nil {
            time.Sleep(t.latency.Delay())
        }
        ch <- &TransportEnvelope{
            PeerType: t.peerType,
            From:     t.nID,
            Msg:      msg,
        }
    }(ch)
    return
}

// Report implements Transport.Report method.
func (t *FakeTransport) Report(msg interface{}) (err error) {
    go func() {
        t.serverChannel <- &TransportEnvelope{
            PeerType: TransportPeer,
            From:     t.nID,
            Msg:      msg,
        }
    }()
    return
}

// Broadcast implements Transport.Broadcast method.
func (t *FakeTransport) Broadcast(msg interface{}) (err error) {
    for k := range t.peers {
        if k == t.nID {
            continue
        }
        t.Send(k, msg)
    }
    return
}

// Close implements Transport.Close method.
func (t *FakeTransport) Close() (err error) {
    close(t.recvChannel)
    return
}

// Peers implements Transport.Peers method.
func (t *FakeTransport) Peers() (peers map[types.NodeID]struct{}) {
    peers = make(map[types.NodeID]struct{})
    for nID := range t.peers {
        peers[nID] = struct{}{}
    }
    return
}

// Join implements TransportClient.Join method.
func (t *FakeTransport) Join(
    serverEndpoint interface{}) (<-chan *TransportEnvelope, error) {

    var (
        envelopes = []*TransportEnvelope{}
        ok        bool
    )
    if t.serverChannel, ok = serverEndpoint.(chan *TransportEnvelope); !ok {
        return nil, fmt.Errorf("accept channel of *TransportEnvelope when join")
    }
    t.Report(t)
    // Wait for peers info.
    for {
        envelope := <-t.recvChannel
        if envelope.PeerType != TransportPeerServer {
            envelopes = append(envelopes, envelope)
            continue
        }
        if t.peers, ok =
            envelope.Msg.(map[types.NodeID]chan<- *TransportEnvelope); !ok {

            envelopes = append(envelopes, envelope)
            continue
        }
        for _, envelope := range envelopes {
            t.recvChannel <- envelope
        }
        break
    }
    return t.recvChannel, nil
}

// Host implements TransportServer.Host method.
func (t *FakeTransport) Host() (chan *TransportEnvelope, error) {
    return t.recvChannel, nil
}

// WaitForPeers implements TransportServer.WaitForPeers method.
func (t *FakeTransport) WaitForPeers(numPeers int) (err error) {
    t.peers = make(map[types.NodeID]chan<- *TransportEnvelope)
    for {
        envelope := <-t.recvChannel
        // Panic here if some peer send other stuffs before
        // receiving peer lists.
        newPeer := envelope.Msg.(*FakeTransport)
        t.peers[envelope.From] = newPeer.recvChannel
        if len(t.peers) == numPeers {
            break
        }
    }
    // The collected peer channels are shared for all peers.
    if err = t.Broadcast(t.peers); err != nil {
        return
    }
    return
}