swarm/pss: Add error to outbox

This commit is contained in:
lash 2018-04-11 09:00:14 +02:00
parent 3e10f30bca
commit ecde73011d
2 changed files with 22 additions and 30 deletions

View file

@ -104,8 +104,6 @@ type Pss struct {
paddingByteSize int paddingByteSize int
capstring string capstring string
outbox chan *PssMsg outbox chan *PssMsg
outboxCapacity int
outboxCounter int
// keys and peers // keys and peers
pubKeyPool map[string]map[Topic]*pssPeer // mapping of hex public keys to peer address by topic. pubKeyPool map[string]map[Topic]*pssPeer // mapping of hex public keys to peer address by topic.
@ -152,7 +150,6 @@ func NewPss(k network.Overlay, params *PssParams) *Pss {
paddingByteSize: defaultPaddingByteSize, paddingByteSize: defaultPaddingByteSize,
capstring: cap.String(), capstring: cap.String(),
outbox: make(chan *PssMsg, defaultOutboxCapacity), outbox: make(chan *PssMsg, defaultOutboxCapacity),
outboxCapacity: defaultOutboxCapacity,
pubKeyPool: make(map[string]map[Topic]*pssPeer), pubKeyPool: make(map[string]map[Topic]*pssPeer),
symKeyPool: make(map[string]map[Topic]*pssPeer), symKeyPool: make(map[string]map[Topic]*pssPeer),
@ -331,15 +328,16 @@ func (self *Pss) handlePssMsg(msg interface{}) error {
var err error var err error
if !self.isSelfPossibleRecipient(pssmsg) { if !self.isSelfPossibleRecipient(pssmsg) {
log.Trace("pss was for someone else :'( ... forwarding", "pss", common.ToHex(self.BaseAddr())) log.Trace("pss was for someone else :'( ... forwarding", "pss", common.ToHex(self.BaseAddr()))
if !self.enqueue(pssmsg) { if err := self.enqueue(pssmsg); err != nil {
return errors.New("outbox full!") return err
} }
} }
log.Trace("pss for us, yay! ... let's process!", "pss", common.ToHex(self.BaseAddr())) log.Trace("pss for us, yay! ... let's process!", "pss", common.ToHex(self.BaseAddr()))
if !self.process(pssmsg) { if err := self.process(pssmsg); err != nil {
if !self.enqueue(pssmsg) { qerr := self.enqueue(pssmsg)
return errors.New("outbox full!") if qerr != nil {
err = fmt.Errorf("%s + %s", err, qerr)
} }
} }
return err return err
@ -351,7 +349,7 @@ func (self *Pss) handlePssMsg(msg interface{}) error {
// Entry point to processing a message for which the current node can be the intended recipient. // Entry point to processing a message for which the current node can be the intended recipient.
// Attempts symmetric and asymmetric decryption with stored keys. // Attempts symmetric and asymmetric decryption with stored keys.
// Dispatches message to all handlers matching the message topic // Dispatches message to all handlers matching the message topic
func (self *Pss) process(pssmsg *PssMsg) bool { func (self *Pss) process(pssmsg *PssMsg) error {
var err error var err error
var recvmsg *whisper.ReceivedMessage var recvmsg *whisper.ReceivedMessage
var from *PssAddress var from *PssAddress
@ -363,7 +361,7 @@ func (self *Pss) process(pssmsg *PssMsg) bool {
psstopic := Topic(envelope.Topic) psstopic := Topic(envelope.Topic)
if self.allowRaw && psstopic == rawTopic { if self.allowRaw && psstopic == rawTopic {
self.executeHandlers(rawTopic, envelope.Data, nil, false, "") self.executeHandlers(rawTopic, envelope.Data, nil, false, "")
return true return nil
} }
if len(envelope.AESNonce) > 0 { // detect symkey msg according to whisperv5/envelope.go:OpenSymmetric if len(envelope.AESNonce) > 0 { // detect symkey msg according to whisperv5/envelope.go:OpenSymmetric
@ -374,18 +372,17 @@ func (self *Pss) process(pssmsg *PssMsg) bool {
} }
recvmsg, keyid, from, err = keyFunc(envelope) recvmsg, keyid, from, err = keyFunc(envelope)
if err != nil { if err != nil {
return false return errors.New("Decryption failed")
} }
if len(pssmsg.To) < addressLength { if len(pssmsg.To) < addressLength {
if !self.enqueue(pssmsg) { if err := self.enqueue(pssmsg); err != nil {
log.Error("outbox full!") return err
return false
} }
} }
self.executeHandlers(psstopic, recvmsg.Payload, from, asymmetric, keyid) self.executeHandlers(psstopic, recvmsg.Payload, from, asymmetric, keyid)
return true return nil
} }
@ -606,13 +603,14 @@ func (self *Pss) cleanKeys() (count int) {
// SECTION: Message sending // SECTION: Message sending
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
func (self *Pss) enqueue(msg *PssMsg) bool { func (self *Pss) enqueue(msg *PssMsg) error {
select { select {
case self.outbox <- msg: case self.outbox <- msg:
return true return nil
default: default:
} }
return true
return errors.New("outbox full")
} }
// Send a raw message (any encryption is responsibility of calling client) // Send a raw message (any encryption is responsibility of calling client)
@ -631,10 +629,7 @@ func (self *Pss) SendRaw(msg []byte, address PssAddress) error {
}, },
} }
self.addFwdCache(pssmsg) self.addFwdCache(pssmsg)
if !self.enqueue(pssmsg) { return self.enqueue(pssmsg)
return errors.New("outbox full!")
}
return nil
} }
// Send a message using symmetric encryption // Send a message using symmetric encryption
@ -727,10 +722,7 @@ func (self *Pss) send(to []byte, topic Topic, msg []byte, asymmetric bool, key [
Expire: uint32(time.Now().Add(self.msgTTL).Unix()), Expire: uint32(time.Now().Add(self.msgTTL).Unix()),
Payload: envelope, Payload: envelope,
} }
if !self.enqueue(pssmsg) { return self.enqueue(pssmsg)
return errors.New("outbox full!")
}
return nil
} }
// Forwards a pss message to the peer(s) closest to the to recipient address in the PssMsg struct // Forwards a pss message to the peer(s) closest to the to recipient address in the PssMsg struct
@ -801,8 +793,8 @@ func (self *Pss) forward(msg *PssMsg) error {
if sent == 0 { if sent == 0 {
log.Debug("unable to forward to any peers") log.Debug("unable to forward to any peers")
time.Sleep(time.Millisecond) time.Sleep(time.Millisecond)
if !self.enqueue(msg) { if err := self.enqueue(msg); err != nil {
return errors.New("outbox full!") return err
} }
} }

View file

@ -1192,7 +1192,7 @@ func benchmarkSymkeyBruteforceChangeaddr(b *testing.B) {
} }
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if !ps.process(pssmsgs[len(pssmsgs)-(i%len(pssmsgs))-1]) { if err := ps.process(pssmsgs[len(pssmsgs)-(i%len(pssmsgs))-1]); err != nil {
b.Fatalf("pss processing failed: %v", err) b.Fatalf("pss processing failed: %v", err)
} }
} }
@ -1274,7 +1274,7 @@ func benchmarkSymkeyBruteforceSameaddr(b *testing.B) {
Payload: env, Payload: env,
} }
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if !ps.process(pssmsg) { if err := ps.process(pssmsg); err != nil {
b.Fatalf("pss processing failed: %v", err) b.Fatalf("pss processing failed: %v", err)
} }
} }