whisper: send/receive protocol updated

This commit is contained in:
Vlad 2017-04-17 18:36:59 +02:00
parent 6bad91a259
commit 5a8562726a
2 changed files with 18 additions and 24 deletions

View file

@ -149,23 +149,20 @@ func (peer *Peer) expire() {
// broadcast iterates over the collection of envelopes and transmits yet unknown
// ones over the network.
func (p *Peer) broadcast() error {
// Fetch the envelopes and collect the unknown ones
var cnt int
envelopes := p.host.Envelopes()
transmit := make([]*Envelope, 0, len(envelopes))
for _, envelope := range envelopes {
if !p.marked(envelope) {
transmit = append(transmit, envelope)
p.mark(envelope)
}
}
if len(transmit) == 0 {
return nil
}
// Transmit the unknown batch (potentially empty)
if err := p2p.Send(p.ws, messagesCode, transmit); err != nil {
err := p2p.Send(p.ws, messagesCode, envelope)
if err != nil {
return err
} else {
p.mark(envelope)
cnt++
}
log.Trace("broadcast", "num. messages", len(transmit))
}
}
log.Trace("broadcast", "num. messages", cnt)
return nil
}

View file

@ -462,21 +462,18 @@ func (wh *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
log.Warn("unxepected status message received", "peer", p.peer.ID())
case messagesCode:
// decode the contained envelopes
var envelopes []*Envelope
if err := packet.Decode(&envelopes); err != nil {
var envelope Envelope
if err := packet.Decode(&envelope); err != nil {
log.Warn("failed to decode envelope, peer will be disconnected", "peer", p.peer.ID(), "err", err)
return errors.New("invalid envelope")
}
// inject all envelopes into the internal pool
for _, envelope := range envelopes {
cached, err := wh.add(envelope)
cached, err := wh.add(&envelope)
if err != nil {
log.Warn("bad envelope received, peer will be disconnected", "peer", p.peer.ID(), "err", err)
return errors.New("invalid envelope")
}
if cached {
p.mark(envelope)
}
p.mark(&envelope)
}
case p2pCode:
// peer-to-peer message, sent directly to peer bypassing PoW checks, etc.