aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-01-13 20:36:44 +0800
committerobscuren <geffobscura@gmail.com>2015-01-13 20:36:44 +0800
commite3cad04decbbc83a0c956850717cb0ae0b2b3eec (patch)
tree81e0f1a57344a7c0d6a2ae2f1481c2e26880d1c2
parent1e5353824a7859d5cfa98565b5a879d5ca7e582a (diff)
downloaddexon-e3cad04decbbc83a0c956850717cb0ae0b2b3eec.tar
dexon-e3cad04decbbc83a0c956850717cb0ae0b2b3eec.tar.gz
dexon-e3cad04decbbc83a0c956850717cb0ae0b2b3eec.tar.bz2
dexon-e3cad04decbbc83a0c956850717cb0ae0b2b3eec.tar.lz
dexon-e3cad04decbbc83a0c956850717cb0ae0b2b3eec.tar.xz
dexon-e3cad04decbbc83a0c956850717cb0ae0b2b3eec.tar.zst
dexon-e3cad04decbbc83a0c956850717cb0ae0b2b3eec.zip
Fixed whisper messages
* Whisper protocol wasn't properly suppling envelope slices * Message history wasn't properly propagated * Added 'Messages' method, filtering any current envelope with the supplied filter.
-rw-r--r--cmd/mist/assets/qml/browser.qml4
-rw-r--r--ui/qt/qwhisper/message.go6
-rw-r--r--ui/qt/qwhisper/whisper.go12
-rw-r--r--whisper/envelope.go42
-rw-r--r--whisper/message.go6
-rw-r--r--whisper/whisper.go22
6 files changed, 58 insertions, 34 deletions
diff --git a/cmd/mist/assets/qml/browser.qml b/cmd/mist/assets/qml/browser.qml
index 7e8ed75bf..34a58f935 100644
--- a/cmd/mist/assets/qml/browser.qml
+++ b/cmd/mist/assets/qml/browser.qml
@@ -357,7 +357,9 @@ Rectangle {
case "shh_getMessages":
require(1);
- shh.trigger(data.args[0]);
+ var m = shh.messages(data.args[0]);
+ var messages = JSON.parse(JSON.parse(JSON.stringify(m)));
+ postData(data._id, messages);
break;
}
diff --git a/ui/qt/qwhisper/message.go b/ui/qt/qwhisper/message.go
index c87647399..3a80381ff 100644
--- a/ui/qt/qwhisper/message.go
+++ b/ui/qt/qwhisper/message.go
@@ -8,9 +8,9 @@ import (
type Message struct {
ref *whisper.Message
- Flags int32
- Payload string
- From string
+ Flags int32 `json:"flags"`
+ Payload string `json:"payload"`
+ From string `json:"from"`
}
func ToQMessage(msg *whisper.Message) *Message {
diff --git a/ui/qt/qwhisper/whisper.go b/ui/qt/qwhisper/whisper.go
index becb2a29b..b904678f4 100644
--- a/ui/qt/qwhisper/whisper.go
+++ b/ui/qt/qwhisper/whisper.go
@@ -43,7 +43,7 @@ func (self *Whisper) Post(payload []string, to, from string, topics []string, pr
msg := whisper.NewMessage(data)
envelope, err := msg.Seal(time.Duration(priority*100000), whisper.Opts{
- Ttl: time.Duration(ttl),
+ Ttl: time.Duration(ttl) * time.Second,
To: crypto.ToECDSAPub(fromHex(to)),
From: crypto.ToECDSA(fromHex(from)),
Topics: whisper.TopicsFromString(topics...),
@@ -84,8 +84,14 @@ func (self *Whisper) Watch(opts map[string]interface{}, view *qml.Common) int {
return i
}
-func (self *Whisper) Trigger(id int) {
- go self.Whisper.Trigger(id)
+func (self *Whisper) Messages(id int) (messages *ethutil.List) {
+ msgs := self.Whisper.Messages(id)
+ messages = ethutil.EmptyList()
+ for _, message := range msgs {
+ messages.Append(ToQMessage(message))
+ }
+
+ return
}
func filterFromMap(opts map[string]interface{}) (f whisper.Filter) {
diff --git a/whisper/envelope.go b/whisper/envelope.go
index 9d28dfa6b..3c477ad9f 100644
--- a/whisper/envelope.go
+++ b/whisper/envelope.go
@@ -1,11 +1,9 @@
package whisper
import (
- "bytes"
"crypto/ecdsa"
"encoding/binary"
"fmt"
- "io"
"time"
"github.com/ethereum/go-ethereum/crypto"
@@ -28,22 +26,6 @@ type Envelope struct {
hash Hash
}
-func NewEnvelopeFromReader(reader io.Reader) (*Envelope, error) {
- var envelope Envelope
-
- buf := new(bytes.Buffer)
- buf.ReadFrom(reader)
-
- h := H(crypto.Sha3(buf.Bytes()))
- if err := rlp.Decode(buf, &envelope); err != nil {
- return nil, err
- }
-
- envelope.hash = h
-
- return &envelope, nil
-}
-
func (self *Envelope) Hash() Hash {
if self.hash == EmptyHash {
self.hash = H(crypto.Sha3(ethutil.Encode(self)))
@@ -126,3 +108,27 @@ func (self *Envelope) withoutNonce() interface{} {
func (self *Envelope) RlpData() interface{} {
return []interface{}{self.Expiry, self.Ttl, ethutil.ByteSliceToInterface(self.Topics), self.Data, self.Nonce}
}
+
+func (self *Envelope) DecodeRLP(s *rlp.Stream) error {
+ var extenv struct {
+ Expiry uint32
+ Ttl uint32
+ Topics [][]byte
+ Data []byte
+ Nonce uint32
+ }
+ if err := s.Decode(&extenv); err != nil {
+ return err
+ }
+
+ self.Expiry = extenv.Expiry
+ self.Ttl = extenv.Ttl
+ self.Topics = extenv.Topics
+ self.Data = extenv.Data
+ self.Nonce = extenv.Nonce
+
+ // TODO We should use the stream directly here.
+ self.hash = H(crypto.Sha3(ethutil.Encode(self)))
+
+ return nil
+}
diff --git a/whisper/message.go b/whisper/message.go
index db0110b4a..bbad8e6a3 100644
--- a/whisper/message.go
+++ b/whisper/message.go
@@ -67,7 +67,11 @@ func (self *Message) Seal(pow time.Duration, opts Opts) (*Envelope, error) {
}
}
- envelope := NewEnvelope(DefaultTtl, opts.Topics, self)
+ if opts.Ttl == 0 {
+ opts.Ttl = DefaultTtl
+ }
+
+ envelope := NewEnvelope(opts.Ttl, opts.Topics, self)
envelope.Seal(pow)
return envelope, nil
diff --git a/whisper/whisper.go b/whisper/whisper.go
index bdc69f199..ece2dd6d4 100644
--- a/whisper/whisper.go
+++ b/whisper/whisper.go
@@ -126,18 +126,20 @@ func (self *Whisper) Watch(opts Filter) int {
})
}
-func (self *Whisper) Trigger(id int) {
+func (self *Whisper) Messages(id int) (messages []*Message) {
filter := self.filters.Get(id)
if filter != nil {
for _, e := range self.messages {
if msg, key := self.open(e); msg != nil {
f := createFilter(msg, e.Topics, key)
if self.filters.Match(filter, f) {
- self.filters.Notify(f, msg)
+ messages = append(messages, msg)
}
}
}
}
+
+ return
}
// Main handler for passing whisper messages to whisper peer objects
@@ -158,17 +160,19 @@ func (self *Whisper) msgHandler(peer *p2p.Peer, ws p2p.MsgReadWriter) error {
return err
}
- envelope, err := NewEnvelopeFromReader(msg.Payload)
- if err != nil {
+ var envelopes []*Envelope
+ if err := msg.Decode(&envelopes); err != nil {
peer.Infoln(err)
continue
}
- if err := self.add(envelope); err != nil {
- // TODO Punish peer here. Invalid envelope.
- peer.Infoln(err)
+ for _, envelope := range envelopes {
+ if err := self.add(envelope); err != nil {
+ // TODO Punish peer here. Invalid envelope.
+ peer.Infoln(err)
+ }
+ wpeer.addKnown(envelope)
}
- wpeer.addKnown(envelope)
}
}
@@ -192,6 +196,8 @@ func (self *Whisper) add(envelope *Envelope) error {
go self.postEvent(envelope)
}
+ wlogger.DebugDetailln("added whisper message")
+
return nil
}