mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
5.0 KiB
175 lines
5.0 KiB
8 years ago
|
// Copyright 2016 The go-ethereum Authors
|
||
9 years ago
|
// This file is part of the go-ethereum library.
|
||
10 years ago
|
//
|
||
9 years ago
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||
10 years ago
|
// 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.
|
||
|
//
|
||
9 years ago
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||
10 years ago
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
9 years ago
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
10 years ago
|
// GNU Lesser General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU Lesser General Public License
|
||
9 years ago
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||
10 years ago
|
|
||
8 years ago
|
package whisperv5
|
||
10 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"time"
|
||
|
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/common"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/logger"
|
||
|
"github.com/ethereum/go-ethereum/logger/glog"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/p2p"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/rlp"
|
||
8 years ago
|
set "gopkg.in/fatih/set.v0"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
// peer represents a whisper protocol peer connection.
|
||
8 years ago
|
type Peer struct {
|
||
|
host *Whisper
|
||
|
peer *p2p.Peer
|
||
|
ws p2p.MsgReadWriter
|
||
|
trusted bool
|
||
10 years ago
|
|
||
10 years ago
|
known *set.Set // Messages already known by the peer to avoid wasting bandwidth
|
||
10 years ago
|
|
||
|
quit chan struct{}
|
||
|
}
|
||
|
|
||
10 years ago
|
// newPeer creates a new whisper peer object, but does not run the handshake itself.
|
||
8 years ago
|
func newPeer(host *Whisper, remote *p2p.Peer, rw p2p.MsgReadWriter) *Peer {
|
||
|
return &Peer{
|
||
|
host: host,
|
||
|
peer: remote,
|
||
|
ws: rw,
|
||
|
trusted: false,
|
||
|
known: set.New(),
|
||
|
quit: make(chan struct{}),
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
// start initiates the peer updater, periodically broadcasting the whisper packets
|
||
|
// into the network.
|
||
8 years ago
|
func (p *Peer) start() {
|
||
|
go p.update()
|
||
|
glog.V(logger.Debug).Infof("%v: whisper started", p.peer)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// stop terminates the peer updater, stopping message forwarding to it.
|
||
8 years ago
|
func (p *Peer) stop() {
|
||
|
close(p.quit)
|
||
|
glog.V(logger.Debug).Infof("%v: whisper stopped", p.peer)
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
// handshake sends the protocol initiation status message to the remote peer and
|
||
|
// verifies the remote status too.
|
||
8 years ago
|
func (p *Peer) handshake() error {
|
||
10 years ago
|
// Send the handshake status message asynchronously
|
||
|
errc := make(chan error, 1)
|
||
|
go func() {
|
||
8 years ago
|
errc <- p2p.Send(p.ws, statusCode, ProtocolVersion)
|
||
10 years ago
|
}()
|
||
|
// Fetch the remote status packet and verify protocol match
|
||
8 years ago
|
packet, err := p.ws.ReadMsg()
|
||
10 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if packet.Code != statusCode {
|
||
|
return fmt.Errorf("peer sent %x before status packet", packet.Code)
|
||
|
}
|
||
10 years ago
|
s := rlp.NewStream(packet.Payload, uint64(packet.Size))
|
||
10 years ago
|
peerVersion, err := s.Uint()
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("bad status message: %v", err)
|
||
|
}
|
||
8 years ago
|
if peerVersion != ProtocolVersion {
|
||
|
return fmt.Errorf("protocol version mismatch %d != %d", peerVersion, ProtocolVersion)
|
||
10 years ago
|
}
|
||
10 years ago
|
// Wait until out own status is consumed too
|
||
|
if err := <-errc; err != nil {
|
||
|
return fmt.Errorf("failed to send status packet: %v", err)
|
||
|
}
|
||
|
return nil
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// update executes periodic operations on the peer, including message transmission
|
||
|
// and expiration.
|
||
8 years ago
|
func (p *Peer) update() {
|
||
10 years ago
|
// Start the tickers for the updates
|
||
10 years ago
|
expire := time.NewTicker(expirationCycle)
|
||
|
transmit := time.NewTicker(transmissionCycle)
|
||
10 years ago
|
|
||
|
// Loop and transmit until termination is requested
|
||
10 years ago
|
for {
|
||
|
select {
|
||
10 years ago
|
case <-expire.C:
|
||
8 years ago
|
p.expire()
|
||
10 years ago
|
|
||
|
case <-transmit.C:
|
||
8 years ago
|
if err := p.broadcast(); err != nil {
|
||
|
glog.V(logger.Info).Infof("%v: broadcast failed: %v", p.peer, err)
|
||
10 years ago
|
return
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
case <-p.quit:
|
||
10 years ago
|
return
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
// mark marks an envelope known to the peer so that it won't be sent back.
|
||
8 years ago
|
func (peer *Peer) mark(envelope *Envelope) {
|
||
|
peer.known.Add(envelope.Hash())
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// marked checks if an envelope is already known to the remote peer.
|
||
8 years ago
|
func (peer *Peer) marked(envelope *Envelope) bool {
|
||
|
return peer.known.Has(envelope.Hash())
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// expire iterates over all the known envelopes in the host and removes all
|
||
|
// expired (unknown) ones from the known list.
|
||
8 years ago
|
func (peer *Peer) expire() {
|
||
10 years ago
|
// Assemble the list of available envelopes
|
||
|
available := set.NewNonTS()
|
||
8 years ago
|
for _, envelope := range peer.host.Envelopes() {
|
||
10 years ago
|
available.Add(envelope.Hash())
|
||
10 years ago
|
}
|
||
10 years ago
|
// Cross reference availability with known status
|
||
|
unmark := make(map[common.Hash]struct{})
|
||
8 years ago
|
peer.known.Each(func(v interface{}) bool {
|
||
10 years ago
|
if !available.Has(v.(common.Hash)) {
|
||
|
unmark[v.(common.Hash)] = struct{}{}
|
||
|
}
|
||
|
return true
|
||
|
})
|
||
|
// Dump all known but unavailable
|
||
|
for hash, _ := range unmark {
|
||
8 years ago
|
peer.known.Remove(hash)
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
// broadcast iterates over the collection of envelopes and transmits yet unknown
|
||
|
// ones over the network.
|
||
8 years ago
|
func (p *Peer) broadcast() error {
|
||
10 years ago
|
// Fetch the envelopes and collect the unknown ones
|
||
8 years ago
|
envelopes := p.host.Envelopes()
|
||
10 years ago
|
transmit := make([]*Envelope, 0, len(envelopes))
|
||
|
for _, envelope := range envelopes {
|
||
8 years ago
|
if !p.marked(envelope) {
|
||
10 years ago
|
transmit = append(transmit, envelope)
|
||
8 years ago
|
p.mark(envelope)
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
// Transmit the unknown batch (potentially empty)
|
||
8 years ago
|
if err := p2p.Send(p.ws, messagesCode, transmit); err != nil {
|
||
10 years ago
|
return err
|
||
10 years ago
|
}
|
||
8 years ago
|
glog.V(logger.Detail).Infoln(p.peer, "broadcasted", len(transmit), "message(s)")
|
||
10 years ago
|
return nil
|
||
10 years ago
|
}
|