Skip to content

Commit

Permalink
Added manual triggering of filters
Browse files Browse the repository at this point in the history
  • Loading branch information
obscuren committed Jan 12, 2015
1 parent 8a1b51c commit 34689cb
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/mist/assets/ext/ethereum.js/lib/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ var shhWatchMethods = function () {
return [
{ name: 'newFilter', call: 'shh_newFilter' },
{ name: 'uninstallFilter', call: 'shh_uninstallFilter' },
{ name: 'getMessage', call: 'shh_getMessages' }
{ name: 'getMessages', call: 'shh_getMessages' }
];
};

Expand Down
9 changes: 8 additions & 1 deletion cmd/mist/assets/qml/browser.qml
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ Rectangle {
postData(data._id, id);
break;

case "eth_messages":
case "eth_filterLogs":
require(1);

var messages = eth.messages(data.args[0]);
Expand Down Expand Up @@ -352,6 +352,13 @@ Rectangle {

shh.post(params.payload, params.to, params.from, params.topics, params.priority, params.ttl);

break;

case "shh_getMessages":
require(1);

shh.trigger(data.args[0]);

break;
}
} catch(e) {
Expand Down
8 changes: 8 additions & 0 deletions event/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,11 @@ out:
}
}
}

func (self *Filters) Match(a, b Filter) bool {
return reflect.TypeOf(a) == reflect.TypeOf(b) && a.Compare(b)
}

func (self *Filters) Get(i int) Filter {
return self.watchers[i]
}
4 changes: 4 additions & 0 deletions ui/qt/qwhisper/whisper.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ 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 filterFromMap(opts map[string]interface{}) (f whisper.Filter) {
if to, ok := opts["to"].(string); ok {
f.To = crypto.ToECDSA(fromHex(to))
Expand Down
37 changes: 30 additions & 7 deletions whisper/whisper.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ func (self *Whisper) Watch(opts Filter) int {
})
}

func (self *Whisper) Trigger(id int) {
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)
}
}
}
}
}

// Main handler for passing whisper messages to whisper peer objects
func (self *Whisper) msgHandler(peer *p2p.Peer, ws p2p.MsgReadWriter) error {
wpeer := NewPeer(self, peer, ws)
Expand Down Expand Up @@ -227,19 +241,28 @@ func (self *Whisper) envelopes() (envelopes []*Envelope) {
}

func (self *Whisper) postEvent(envelope *Envelope) {
if message, key := self.open(envelope); message != nil {
self.filters.Notify(createFilter(message, envelope.Topics, key), message)
}
}

func (self *Whisper) open(envelope *Envelope) (*Message, *ecdsa.PrivateKey) {
for _, key := range self.keys {
if message, err := envelope.Open(key); err == nil || (err != nil && err == ecies.ErrInvalidPublicKey) {
self.filters.Notify(filter.Generic{
Str1: string(crypto.FromECDSA(key)), Str2: string(crypto.FromECDSAPub(message.Recover())),
Data: bytesToMap(envelope.Topics),
}, message)
break
} else {
wlogger.Infoln(err)
return message, key
}
}

return nil, nil
}

func (self *Whisper) Protocol() p2p.Protocol {
return self.protocol
}

func createFilter(message *Message, topics [][]byte, key *ecdsa.PrivateKey) filter.Filter {
return filter.Generic{
Str1: string(crypto.FromECDSA(key)), Str2: string(crypto.FromECDSAPub(message.Recover())),
Data: bytesToMap(topics),
}
}

0 comments on commit 34689cb

Please sign in to comment.