aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/peer.go
blob: 338166c25f3fe9e5d5d4b944e3ce68c1d347c2d0 (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
package whisper

import (
    "fmt"
    "time"

    "github.com/ethereum/go-ethereum/p2p"
    "github.com/ethereum/go-ethereum/rlp"
    "gopkg.in/fatih/set.v0"
)

const (
    protocolVersion uint64 = 0x02
)

type peer struct {
    host *Whisper
    peer *p2p.Peer
    ws   p2p.MsgReadWriter

    // XXX Eventually this is going to reach exceptional large space. We need an expiry here
    known *set.Set

    quit chan struct{}
}

func NewPeer(host *Whisper, p *p2p.Peer, ws p2p.MsgReadWriter) *peer {
    return &peer{host, p, ws, set.New(), make(chan struct{})}
}

func (self *peer) init() error {
    if err := self.handleStatus(); err != nil {
        return err
    }

    return nil
}

func (self *peer) start() {
    go self.update()
    self.peer.Debugln("whisper started")
}

func (self *peer) stop() {
    self.peer.Debugln("whisper stopped")

    close(self.quit)
}

func (self *peer) update() {
    relay := time.NewTicker(300 * time.Millisecond)
out:
    for {
        select {
        case <-relay.C:
            err := self.broadcast(self.host.envelopes())
            if err != nil {
                self.peer.Infoln("broadcast err:", err)
                break out
            }

        case <-self.quit:
            break out
        }
    }
}

func (self *peer) broadcast(envelopes []*Envelope) error {
    envs := make([]*Envelope, 0, len(envelopes))
    for _, env := range envelopes {
        if !self.known.Has(env.Hash()) {
            envs = append(envs, env)
            self.known.Add(env.Hash())
        }
    }
    if len(envs) > 0 {
        if err := p2p.Send(self.ws, envelopesMsg, envs); err != nil {
            return err
        }
        self.peer.DebugDetailln("broadcasted", len(envs), "message(s)")
    }
    return nil
}

func (self *peer) addKnown(envelope *Envelope) {
    self.known.Add(envelope.Hash())
}

func (self *peer) handleStatus() error {
    ws := self.ws
    if err := p2p.SendItems(ws, statusMsg, protocolVersion); err != nil {
        return err
    }
    msg, err := ws.ReadMsg()
    if err != nil {
        return err
    }
    if msg.Code != statusMsg {
        return fmt.Errorf("peer send %x before status msg", msg.Code)
    }
    s := rlp.NewStream(msg.Payload)
    if _, err := s.List(); err != nil {
        return fmt.Errorf("bad status message: %v", err)
    }
    pv, err := s.Uint()
    if err != nil {
        return fmt.Errorf("bad status message: %v", err)
    }
    if pv != protocolVersion {
        return fmt.Errorf("protocol version mismatch %d != %d", pv, protocolVersion)
    }
    return msg.Discard() // ignore anything after protocol version
}