-
Notifications
You must be signed in to change notification settings - Fork 5
/
gossip.go
343 lines (271 loc) · 5.46 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
341
342
343
package ozcoin
import (
"encoding/json"
"errors"
"log"
"net"
"net/rpc"
"time"
)
const (
NUM_PEERS = 10
BLOCK_RETRIEVAL_LIMIT = 25
)
type GossipCore struct {
c *Client
}
func (s *Client) Serve() error {
rpc.Register(&GossipCore{s})
l, err := net.Listen("tcp", s.Address)
if err != nil {
return err
}
go rpc.Accept(l)
return nil
}
type RPCHeader struct {
Address string
}
/*
* HashMsg
*
* Primary identifier for broadcasting new information.
*/
type HashMsg struct {
RPCHeader
Hash SHA256Sum
}
func (c *Client) NewHashMsg(hash SHA256Sum) HashMsg {
return HashMsg{
RPCHeader: RPCHeader{
Address: c.Address,
},
Hash: hash,
}
}
func (hm HashMsg) NewHash(hash SHA256Sum) HashMsg {
return HashMsg{
RPCHeader: hm.RPCHeader,
Hash: hash,
}
}
type HeaderMsg struct {
RPCHeader
Header BlockHeader
}
type BlockMsg struct {
RPCHeader
Block
}
type TxnMsg struct {
RPCHeader
Txn
}
type OutputMsg struct {
RPCHeader
Output
}
/*
* Updates peers with incoming requests
*/
func (c *Client) HandleRPC(request RPCHeader, response *RPCHeader) error {
err := c.PutPeer(request.Address)
if err != nil {
log.Println(err)
return err
}
log.Println("Peer Added:", request.Address)
response.Address = c.Address
return nil
}
/*
* Sends the incoming block hash to the client to be resolved.
*/
func (gc *GossipCore) BcastBlockRPC(req HashMsg, res *RPCHeader) error {
err := gc.c.HandleRPC(req.RPCHeader, res)
if err != nil {
return err
}
if (req.Hash != SHA256Sum{}) {
gc.c.BlockHashChan <- req
}
return nil
}
/*
* Sends the incoming txn hash to the client to be resolved.
*/
func (gc *GossipCore) BcastTxnRPC(req HashMsg, res *RPCHeader) error {
err := gc.c.HandleRPC(req.RPCHeader, res)
if err != nil {
return err
}
if (req.Hash != SHA256Sum{}) {
gc.c.TxnHashChan <- req
}
return nil
}
/*
* Fetch Header
*/
func (s *Client) FetchHeader(hash SHA256Sum, address string) (*BlockHeader, error) {
peer, err := s.dialPeer(address)
if err != nil {
return nil, err
}
req := s.NewHashMsg(hash)
res := HeaderMsg{}
err = peer.Call("GossipCore.FetchHeaderRPC", &req, &res)
if err != nil {
return nil, err
}
header := &BlockHeader{}
*header = res.Header
if header.Hash() != hash {
return nil, errors.New("Wrong header")
}
return header, nil
}
func (gc *GossipCore) FetchHeaderRPC(req HashMsg, res *HeaderMsg) error {
err := gc.c.HandleRPC(req.RPCHeader, &res.RPCHeader)
if err != nil {
return err
}
headerBytes, err := gc.c.dbm.headerDB.Get(req.Hash[:], nil)
if err != nil {
return err
}
var header BlockHeader
err = json.Unmarshal(headerBytes, &header)
if err != nil {
return err
}
res.Header = header
return nil
}
/*
* Fetch Block
*/
func (s *Client) FetchBlock(hash SHA256Sum, address string) (*Block, error) {
peer, err := s.dialPeer(address)
if err != nil {
return nil, err
}
req := s.NewHashMsg(hash)
res := BlockMsg{}
err = peer.Call("GossipCore.FetchBlockRPC", &req, &res)
if err != nil {
return nil, err
}
block := &Block{}
*block = res.Block
if block.Header.Hash() != hash {
return nil, errors.New("Wrong block")
}
if !block.VerifyMerkleHash() {
return nil, errors.New("Wrong block")
}
return block, nil
}
func (gc *GossipCore) FetchBlockRPC(req HashMsg, res *BlockMsg) error {
err := gc.c.HandleRPC(req.RPCHeader, &res.RPCHeader)
if err != nil {
return err
}
block, err := gc.c.LoadBlock(req.Hash)
if err != nil {
return err
}
res.Block = *block
return nil
}
/*
* Fetch Txn
*/
func (c *Client) FetchTxn(hash SHA256Sum, address string) (*Txn, error) {
peer, err := c.dialPeer(address)
if err != nil {
return nil, err
}
req := c.NewHashMsg(hash)
res := TxnMsg{}
err = peer.Call("GossipCore.FetchTxnRPC", &req, &res)
if err != nil {
return nil, err
}
txn := &Txn{}
*txn = res.Txn
if txn.Hash() != hash {
return nil, errors.New("Wrong txn")
}
return txn, nil
}
func (gc *GossipCore) FetchTxnRPC(req HashMsg, res *TxnMsg) error {
err := gc.c.HandleRPC(req.RPCHeader, &res.RPCHeader)
if err != nil {
return err
}
txn, err := gc.c.LoadTxn(req.Hash)
if err != nil {
return err
}
res.Txn = *txn
return nil
}
/*
* Fetch Output
*/
func (c *Client) FetchOutput(hash SHA256Sum, address string) (*Output, error) {
peer, err := c.dialPeer(address)
if err != nil {
return nil, err
}
req := c.NewHashMsg(hash)
res := OutputMsg{}
err = peer.Call("GossipCore.FetchOutputRPC", &req, &res)
if err != nil {
return nil, err
}
output := &Output{}
*output = res.Output
if output.Hash() != hash {
return nil, errors.New("Wrong output")
}
return output, nil
}
func (gc *GossipCore) FetchOutputRPC(req HashMsg, res *OutputMsg) error {
err := gc.c.HandleRPC(req.RPCHeader, &res.RPCHeader)
if err != nil {
return err
}
output, err := gc.c.LoadOutput(req.Hash)
if err != nil {
return err
}
res.Output = *output
return nil
}
/*
* Returns a new rpc connection with the address.
*/
func (s *Client) dialPeer(address string) (*rpc.Client, error) {
conn, err := net.DialTimeout("tcp", address, 5*time.Second)
if err != nil {
return nil, err
}
return rpc.NewClient(conn), nil
}
func (c *Client) sendBcast(method, address string, hash SHA256Sum) {
peer, err := c.dialPeer(address)
if err != nil {
err = c.dbm.peerDB.Delete([]byte(address), nil)
if err != nil {
log.Println(err)
}
return
}
req := c.NewHashMsg(hash)
err = peer.Call(method, &req, &RPCHeader{})
if err != nil {
log.Println("Failed to broadcast", err)
}
}