/
gossip.go
340 lines (289 loc) · 9.59 KB
/
gossip.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package core
import (
"context"
"math/big"
"sync"
"time"
"github.com/ellcrys/elld/types"
"github.com/ellcrys/elld/util"
net "github.com/libp2p/go-libp2p-net"
"github.com/vmihailenco/msgpack"
)
// Handshake represents the first
// message between peers
type Handshake struct {
Version string `json:"version" msgpack:"version"`
BestBlockHash util.Hash `json:"bestBlockHash" msgpack:"bestBlockHash"`
BestBlockTotalDifficulty *big.Int `json:"bestBlockTD" msgpack:"bestBlockTD"`
BestBlockNumber uint64 `json:"bestBlockNumber" msgpack:"bestBlockNumber"`
Name string `json:"name" msgpack:"name"`
}
// EncodeMsgpack implements
// msgpack.CustomEncoder
func (h *Handshake) EncodeMsgpack(enc *msgpack.Encoder) error {
tdStr := h.BestBlockTotalDifficulty.String()
return enc.Encode(h.Version, h.Name, h.BestBlockHash, h.BestBlockNumber, tdStr)
}
// DecodeMsgpack implements
// msgpack.CustomDecoder
func (h *Handshake) DecodeMsgpack(dec *msgpack.Decoder) error {
var tdStr string
if err := dec.Decode(&h.Version, &h.Name, &h.BestBlockHash,
&h.BestBlockNumber, &tdStr); err != nil {
return err
}
h.BestBlockTotalDifficulty, _ = new(big.Int).SetString(tdStr, 10)
return nil
}
// GetAddr is used to request for peer
// addresses from other peers
type GetAddr struct {
}
// Addr is used to send peer addresses
// in response to a GetAddr
type Addr struct {
Addresses []*Address `json:"addresses" msgpack:"addresses"`
}
// Address represents a peer's address
type Address struct {
Address util.NodeAddr `json:"address" msgpack:"address"`
Timestamp int64 `json:"timestamp" msgpack:"timestamp"`
}
// Ping represents a ping message
type Ping struct {
BestBlockHash util.Hash `json:"bestBlockHash" msgpack:"bestBlockHash"`
BestBlockTotalDifficulty *big.Int `json:"bestBlockTD" msgpack:"bestBlockTD"`
BestBlockNumber uint64 `json:"bestBlockNumber" msgpack:"bestBlockNumber"`
}
// EncodeMsgpack implements msgpack.CustomEncoder
func (p *Ping) EncodeMsgpack(enc *msgpack.Encoder) error {
tdStr := p.BestBlockTotalDifficulty.String()
return enc.Encode(p.BestBlockHash, p.BestBlockNumber, tdStr)
}
// DecodeMsgpack implements msgpack.CustomDecoder
func (p *Ping) DecodeMsgpack(dec *msgpack.Decoder) error {
var tdStr string
if err := dec.Decode(&p.BestBlockHash, &p.BestBlockNumber, &tdStr); err != nil {
return err
}
p.BestBlockTotalDifficulty, _ = new(big.Int).SetString(tdStr, 10)
return nil
}
// Pong represents a pong message
type Pong struct {
BestBlockHash util.Hash `json:"bestBlockHash" msgpack:"bestBlockHash"`
BestBlockTotalDifficulty *big.Int `json:"bestBlockTD" msgpack:"bestBlockTD"`
BestBlockNumber uint64 `json:"bestBlockNumber" msgpack:"bestBlockNumber"`
}
// EncodeMsgpack implements msgpack.CustomEncoder
func (p *Pong) EncodeMsgpack(enc *msgpack.Encoder) error {
tdStr := p.BestBlockTotalDifficulty.String()
return enc.Encode(p.BestBlockHash, p.BestBlockNumber, tdStr)
}
// DecodeMsgpack implements msgpack.CustomDecoder
func (p *Pong) DecodeMsgpack(dec *msgpack.Decoder) error {
var tdStr string
if err := dec.Decode(&p.BestBlockHash, &p.BestBlockNumber, &tdStr); err != nil {
return err
}
p.BestBlockTotalDifficulty, _ = new(big.Int).SetString(tdStr, 10)
return nil
}
// Reject defines information about a rejected action
type Reject struct {
Message string `json:"message" msgpack:"message"`
Code int32 `json:"code" msgpack:"code"`
Reason string `json:"reason" msgpack:"reason"`
ExtraData []byte `json:"extraData" msgpack:"extraData"`
}
// RequestBlock represents a message requesting for a block
type RequestBlock struct {
Hash string `json:"hash" msgpack:"hash"`
}
// GetBlockHashes represents a message requesting
// for headers of blocks. The locator is used to
// compare with a remote node to determine which
// blocks to send back.
type GetBlockHashes struct {
Locators []util.Hash `json:"hash" msgpack:"hash"`
Seek util.Hash `json:"seek" msgpack:"seek"`
MaxBlocks int64 `json:"maxBlocks" msgpack:"maxBlocks"`
}
// BlockHashes represents a message containing
// block hashes as a response to GetBlockHeaders
type BlockHashes struct {
Hashes []util.Hash
}
// BlockBody represents the body of a block
type BlockBody struct {
Header *Header `json:"header" msgpack:"header"`
Transactions []*Transaction `json:"transactions" msgpack:"transactions"`
Hash util.Hash `json:"hash" msgpack:"hash"`
Sig []byte `json:"sig" msgpack:"sig"`
}
// BlockBodies represents a collection of block bodies
type BlockBodies struct {
Blocks []*BlockBody
}
// GetBlockBodies represents a message to fetch block bodies
// belonging to the given hashes
type GetBlockBodies struct {
Hashes []util.Hash
}
// Intro represents a message describing a peer's ID.
type Intro struct {
PeerID string `json:"id" msgpack:"id"`
}
// TxInfo describes a transaction
type TxInfo struct {
Hash util.Hash `json:"hash" msgpack:"hash"`
}
// TxOk describes a transaction hash received
// in TxInfo as accepted/ok or not
type TxOk struct {
Ok bool `json:"ok" msgpack:"ok"`
}
// BlockInfo describes a block
type BlockInfo struct {
Hash util.Hash `json:"hash" msgpack:"hash"`
}
// BlockOk describes a block hash received
// in BlockInfo as accepted/ok or not
type BlockOk struct {
Ok bool `json:"ok" msgpack:"ok"`
}
// Hash returns the hash representation
func (m *Intro) Hash() util.Hash {
bs := util.ObjectToBytes([]interface{}{m.PeerID})
return util.BytesToHash(util.Blake2b256(bs))
}
// WrappedStream encapsulates a stream along with
// extra data and behaviours
type WrappedStream struct {
Stream net.Stream
Extra map[string]interface{}
}
// Gossip represent messages and interactions between nodes
type Gossip interface {
// Address messages
OnAddr(s net.Stream, rp Engine) error
RelayAddresses(addrs []*Address) []error
// Block messages
BroadcastBlock(block types.Block, remotePeers []Engine) []error
OnBlockInfo(s net.Stream, rp Engine) error
OnBlockBody(s net.Stream, rp Engine) error
RequestBlock(rp Engine, blockHash util.Hash) error
OnRequestBlock(s net.Stream, rp Engine) error
SendGetBlockHashes(rp Engine, locators []util.Hash, seek util.Hash) (*BlockHashes, error)
OnGetBlockHashes(s net.Stream, rp Engine) error
SendGetBlockBodies(rp Engine, hashes []util.Hash) (*BlockBodies, error)
OnGetBlockBodies(s net.Stream, rp Engine) error
// Handshake messages
SendHandshake(rp Engine) error
OnHandshake(s net.Stream, rp Engine) error
// GetAddr messages
SendGetAddrToPeer(remotePeer Engine) ([]*Address, error)
SendGetAddr(remotePeers []Engine) error
OnGetAddr(s net.Stream, rp Engine) error
// Ping messages
SendPing(remotePeers []Engine)
SendPingToPeer(remotePeer Engine) error
OnPing(s net.Stream, rp Engine) error
// Node advertisement
SelfAdvertise(connectedPeers []Engine) int
// Transaction messages
BroadcastTx(tx types.Transaction, remotePeers []Engine) error
OnTx(s net.Stream, rp Engine) error
// PickBroadcasters selects N random addresses from
// the given slice of addresses and caches them to
// be used as broadcasters.
// They are returned on subsequent calls and only
// renewed when there are less than N addresses or the
// cache is over 24 hours since it was last updated.
PickBroadcasters(cache *BroadcastPeers, addresses []*Address, n int) *BroadcastPeers
// GetBroadcasters returns the broadcasters
GetBroadcasters() *BroadcastPeers
GetRandBroadcasters() *BroadcastPeers
// NewStream creates a stream for a given protocol
// ID and between the local peer and the given remote peer.
NewStream(remotePeer Engine, msgVersion string) (net.Stream,
context.CancelFunc, error)
// CheckRemotePeer performs validation against the remote peer.
CheckRemotePeer(ws *WrappedStream, rp Engine) error
// Handle wrappers a protocol handler providing an
// interface to perform pre and post handling operations.
Handle(handler func(s net.Stream, remotePeer Engine) error) func(net.Stream)
}
// BroadcastPeers is a type that contains
// randomly chosen peers that messages will be
// broadcast to.
type BroadcastPeers struct {
sync.RWMutex
peers map[string]Engine
lastUpdated time.Time
}
// NewBroadcastPeers creates a new BroadcastPeers instance
func NewBroadcastPeers() *BroadcastPeers {
return &BroadcastPeers{
peers: make(map[string]Engine),
lastUpdated: time.Now(),
}
}
// Has checks whether a peer exists
func (b *BroadcastPeers) Has(p Engine) bool {
b.RLock()
defer b.RUnlock()
_, has := b.peers[p.StringID()]
return has
}
// Add adds a peer
func (b *BroadcastPeers) Add(p Engine) {
b.Lock()
defer b.Unlock()
b.peers[p.StringID()] = p
b.lastUpdated = time.Now()
}
// Clear removes all peers
func (b *BroadcastPeers) Clear() {
b.Lock()
defer b.Unlock()
b.peers = make(map[string]Engine)
b.lastUpdated = time.Now()
}
// Len returns the number of peers
func (b *BroadcastPeers) Len() int {
b.RLock()
defer b.RUnlock()
return len(b.peers)
}
// Peers returns the stored peers
func (b *BroadcastPeers) Peers() (peers []Engine) {
b.RLock()
defer b.RUnlock()
for _, p := range b.peers {
peers = append(peers, p)
}
return
}
// PeersID returns the id of the stored peers
func (b *BroadcastPeers) PeersID() (ids []string) {
b.RLock()
defer b.RUnlock()
for id := range b.peers {
ids = append(ids, id)
}
return
}
// Remove removes a peer
func (b *BroadcastPeers) Remove(peer Engine) {
b.Lock()
defer b.Unlock()
delete(b.peers, peer.StringID())
b.lastUpdated = time.Now()
}
// LastUpdated is the last time the peers were updated
func (b *BroadcastPeers) LastUpdated() time.Time {
b.RLock()
defer b.RUnlock()
return b.lastUpdated
}