Skip to content

Commit 7a045af

Browse files
gballetfjl
authored andcommitted
whisper/whisperv5: set filter SymKeyHash on creation (#15165)
1 parent 36243c7 commit 7a045af

File tree

5 files changed

+331
-17
lines changed

5 files changed

+331
-17
lines changed

whisper/whisperv5/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (api *PublicWhisperAPI) Info(ctx context.Context) Info {
104104
stats := api.w.Stats()
105105
return Info{
106106
Memory: stats.memoryUsed,
107-
Messages: len(api.w.messageQueue) + len(api.w.p2pMsgQueue),
107+
Messages: len(api.w.messageQueue) + len(api.w.p2pMsgQueue),
108108
MinPow: api.w.MinPow(),
109109
MaxMessageSize: api.w.MaxMessageSize(),
110110
}

whisper/whisperv5/config.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,3 @@ var DefaultConfig = Config{
2525
MaxMessageSize: DefaultMaxMessageSize,
2626
MinimumAcceptedPOW: DefaultMinimumPoW,
2727
}
28-
29-
var ()

whisper/whisperv5/filter.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"sync"
2323

2424
"github.com/ethereum/go-ethereum/common"
25+
"github.com/ethereum/go-ethereum/crypto"
2526
"github.com/ethereum/go-ethereum/log"
2627
)
2728

@@ -68,6 +69,10 @@ func (fs *Filters) Install(watcher *Filter) (string, error) {
6869
return "", fmt.Errorf("failed to generate unique ID")
6970
}
7071

72+
if watcher.expectsSymmetricEncryption() {
73+
watcher.SymKeyHash = crypto.Keccak256Hash(watcher.KeySym)
74+
}
75+
7176
fs.watchers[id] = watcher
7277
return id, err
7378
}
@@ -119,7 +124,9 @@ func (fs *Filters) NotifyWatchers(env *Envelope, p2pMessage bool) {
119124

120125
if match && msg != nil {
121126
log.Trace("processing message: decrypted", "hash", env.Hash().Hex())
122-
watcher.Trigger(msg)
127+
if watcher.Src == nil || IsPubKeyEqual(msg.Src, watcher.Src) {
128+
watcher.Trigger(msg)
129+
}
123130
}
124131
}
125132
}
@@ -172,9 +179,6 @@ func (f *Filter) MatchMessage(msg *ReceivedMessage) bool {
172179
if f.PoW > 0 && msg.PoW < f.PoW {
173180
return false
174181
}
175-
if f.Src != nil && !IsPubKeyEqual(msg.Src, f.Src) {
176-
return false
177-
}
178182

179183
if f.expectsAsymmetricEncryption() && msg.isAsymmetricEncryption() {
180184
return IsPubKeyEqual(&f.KeyAsym.PublicKey, msg.Dst) && f.MatchTopic(msg.Topic)

whisper/whisperv5/filter_test.go

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,103 @@ func TestInstallFilters(t *testing.T) {
132132
}
133133
}
134134

135+
func TestInstallSymKeyGeneratesHash(t *testing.T) {
136+
InitSingleTest()
137+
138+
w := New(&Config{})
139+
filters := NewFilters(w)
140+
filter, _ := generateFilter(t, true)
141+
142+
// save the current SymKeyHash for comparison
143+
initialSymKeyHash := filter.SymKeyHash
144+
145+
// ensure the SymKeyHash is invalid, for Install to recreate it
146+
var invalid common.Hash
147+
filter.SymKeyHash = invalid
148+
149+
_, err := filters.Install(filter)
150+
151+
if err != nil {
152+
t.Fatalf("Error installing the filter: %s", err)
153+
}
154+
155+
for i, b := range filter.SymKeyHash {
156+
if b != initialSymKeyHash[i] {
157+
t.Fatalf("The filter's symmetric key hash was not properly generated by Install")
158+
}
159+
}
160+
}
161+
162+
func TestInstallIdenticalFilters(t *testing.T) {
163+
InitSingleTest()
164+
165+
w := New(&Config{})
166+
filters := NewFilters(w)
167+
filter1, _ := generateFilter(t, true)
168+
169+
// Copy the first filter since some of its fields
170+
// are randomly gnerated.
171+
filter2 := &Filter{
172+
KeySym: filter1.KeySym,
173+
Topics: filter1.Topics,
174+
PoW: filter1.PoW,
175+
AllowP2P: filter1.AllowP2P,
176+
Messages: make(map[common.Hash]*ReceivedMessage),
177+
}
178+
179+
_, err := filters.Install(filter1)
180+
181+
if err != nil {
182+
t.Fatalf("Error installing the first filter with seed %d: %s", seed, err)
183+
}
184+
185+
_, err = filters.Install(filter2)
186+
187+
if err != nil {
188+
t.Fatalf("Error installing the second filter with seed %d: %s", seed, err)
189+
}
190+
191+
params, err := generateMessageParams()
192+
if err != nil {
193+
t.Fatalf("Error generating message parameters with seed %d: %s", seed, err)
194+
}
195+
196+
params.KeySym = filter1.KeySym
197+
params.Topic = BytesToTopic(filter1.Topics[0])
198+
199+
filter1.Src = &params.Src.PublicKey
200+
filter2.Src = &params.Src.PublicKey
201+
202+
sentMessage, err := NewSentMessage(params)
203+
if err != nil {
204+
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
205+
}
206+
env, err := sentMessage.Wrap(params)
207+
if err != nil {
208+
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
209+
}
210+
msg := env.Open(filter1)
211+
if msg == nil {
212+
t.Fatalf("failed to Open with filter1")
213+
}
214+
215+
if !filter1.MatchEnvelope(env) {
216+
t.Fatalf("failed matching with the first filter")
217+
}
218+
219+
if !filter2.MatchEnvelope(env) {
220+
t.Fatalf("failed matching with the first filter")
221+
}
222+
223+
if !filter1.MatchMessage(msg) {
224+
t.Fatalf("failed matching with the second filter")
225+
}
226+
227+
if !filter2.MatchMessage(msg) {
228+
t.Fatalf("failed matching with the second filter")
229+
}
230+
}
231+
135232
func TestComparePubKey(t *testing.T) {
136233
InitSingleTest()
137234

@@ -345,11 +442,6 @@ func TestMatchMessageSym(t *testing.T) {
345442
t.Fatalf("failed Open with seed %d.", seed)
346443
}
347444

348-
// Src mismatch
349-
if f.MatchMessage(msg) {
350-
t.Fatalf("failed MatchMessage(src mismatch) with seed %d.", seed)
351-
}
352-
353445
// Src: match
354446
*f.Src.X = *params.Src.PublicKey.X
355447
*f.Src.Y = *params.Src.PublicKey.Y
@@ -443,11 +535,6 @@ func TestMatchMessageAsym(t *testing.T) {
443535
t.Fatalf("failed to open with seed %d.", seed)
444536
}
445537

446-
// Src mismatch
447-
if f.MatchMessage(msg) {
448-
t.Fatalf("failed MatchMessage(src mismatch) with seed %d.", seed)
449-
}
450-
451538
// Src: match
452539
*f.Src.X = *params.Src.PublicKey.X
453540
*f.Src.Y = *params.Src.PublicKey.Y

0 commit comments

Comments
 (0)