Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
014d8d98
Commit
014d8d98
authored
Feb 27, 2018
by
Vlad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
whisper: message filtering optimized
parent
4c845bdc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
11 deletions
+55
-11
filter.go
whisper/whisperv6/filter.go
+55
-11
No files found.
whisper/whisperv6/filter.go
View file @
014d8d98
...
...
@@ -35,6 +35,7 @@ type Filter struct {
PoW
float64
// Proof of work as described in the Whisper spec
AllowP2P
bool
// Indicates whether this filter is interested in direct peer-to-peer messages
SymKeyHash
common
.
Hash
// The Keccak256Hash of the symmetric key, needed for optimization
id
string
// unique identifier
Messages
map
[
common
.
Hash
]
*
ReceivedMessage
mutex
sync
.
RWMutex
...
...
@@ -42,16 +43,21 @@ type Filter struct {
// Filters represents a collection of filters
type
Filters
struct
{
watchers
map
[
string
]
*
Filter
whisper
*
Whisper
mutex
sync
.
RWMutex
watchers
map
[
string
]
*
Filter
topicMatcher
map
[
TopicType
]
map
[
*
Filter
]
struct
{}
allTopicsMatcher
map
[
*
Filter
]
struct
{}
whisper
*
Whisper
mutex
sync
.
RWMutex
}
// NewFilters returns a newly created filter collection
func
NewFilters
(
w
*
Whisper
)
*
Filters
{
return
&
Filters
{
watchers
:
make
(
map
[
string
]
*
Filter
),
whisper
:
w
,
watchers
:
make
(
map
[
string
]
*
Filter
),
topicMatcher
:
make
(
map
[
TopicType
]
map
[
*
Filter
]
struct
{}),
allTopicsMatcher
:
make
(
map
[
*
Filter
]
struct
{}),
whisper
:
w
,
}
}
...
...
@@ -81,7 +87,9 @@ func (fs *Filters) Install(watcher *Filter) (string, error) {
watcher
.
SymKeyHash
=
crypto
.
Keccak256Hash
(
watcher
.
KeySym
)
}
watcher
.
id
=
id
fs
.
watchers
[
id
]
=
watcher
fs
.
addTopicMatcher
(
watcher
)
return
id
,
err
}
...
...
@@ -91,12 +99,49 @@ func (fs *Filters) Uninstall(id string) bool {
fs
.
mutex
.
Lock
()
defer
fs
.
mutex
.
Unlock
()
if
fs
.
watchers
[
id
]
!=
nil
{
fs
.
removeFromTopicMatchers
(
fs
.
watchers
[
id
])
delete
(
fs
.
watchers
,
id
)
return
true
}
return
false
}
// addTopicMatcher adds a filter to the topic matchers
func
(
fs
*
Filters
)
addTopicMatcher
(
watcher
*
Filter
)
{
if
len
(
watcher
.
Topics
)
==
0
{
fs
.
allTopicsMatcher
[
watcher
]
=
struct
{}{}
}
else
{
for
_
,
t
:=
range
watcher
.
Topics
{
topic
:=
BytesToTopic
(
t
)
if
fs
.
topicMatcher
[
topic
]
==
nil
{
fs
.
topicMatcher
[
topic
]
=
make
(
map
[
*
Filter
]
struct
{})
}
fs
.
topicMatcher
[
topic
][
watcher
]
=
struct
{}{}
}
}
}
// removeFromTopicMatchers removes a filter from the topic matchers
func
(
fs
*
Filters
)
removeFromTopicMatchers
(
watcher
*
Filter
)
{
delete
(
fs
.
allTopicsMatcher
,
watcher
)
for
_
,
topic
:=
range
watcher
.
Topics
{
delete
(
fs
.
topicMatcher
[
BytesToTopic
(
topic
)],
watcher
)
}
}
// getWatchersByTopic returns a slice containing the filters that
// match a specific topic
func
(
fs
*
Filters
)
getWatchersByTopic
(
topic
TopicType
)
[]
*
Filter
{
res
:=
make
([]
*
Filter
,
0
,
len
(
fs
.
allTopicsMatcher
))
for
watcher
,
_
:=
range
fs
.
allTopicsMatcher
{
res
=
append
(
res
,
watcher
)
}
for
watcher
,
_
:=
range
fs
.
topicMatcher
[
topic
]
{
res
=
append
(
res
,
watcher
)
}
return
res
}
// Get returns a filter from the collection with a specific ID
func
(
fs
*
Filters
)
Get
(
id
string
)
*
Filter
{
fs
.
mutex
.
RLock
()
...
...
@@ -112,11 +157,10 @@ func (fs *Filters) NotifyWatchers(env *Envelope, p2pMessage bool) {
fs
.
mutex
.
RLock
()
defer
fs
.
mutex
.
RUnlock
()
i
:=
-
1
// only used for logging info
for
_
,
watcher
:=
range
fs
.
watchers
{
i
++
candidates
:=
fs
.
getWatchersByTopic
(
env
.
Topic
)
for
_
,
watcher
:=
range
candidates
{
if
p2pMessage
&&
!
watcher
.
AllowP2P
{
log
.
Trace
(
fmt
.
Sprintf
(
"msg [%x], filter [%
d]: p2p messages are not allowed"
,
env
.
Hash
(),
i
))
log
.
Trace
(
fmt
.
Sprintf
(
"msg [%x], filter [%
s]: p2p messages are not allowed"
,
env
.
Hash
(),
watcher
.
id
))
continue
}
...
...
@@ -128,10 +172,10 @@ func (fs *Filters) NotifyWatchers(env *Envelope, p2pMessage bool) {
if
match
{
msg
=
env
.
Open
(
watcher
)
if
msg
==
nil
{
log
.
Trace
(
"processing message: failed to open"
,
"message"
,
env
.
Hash
()
.
Hex
(),
"filter"
,
i
)
log
.
Trace
(
"processing message: failed to open"
,
"message"
,
env
.
Hash
()
.
Hex
(),
"filter"
,
watcher
.
id
)
}
}
else
{
log
.
Trace
(
"processing message: does not match"
,
"message"
,
env
.
Hash
()
.
Hex
(),
"filter"
,
i
)
log
.
Trace
(
"processing message: does not match"
,
"message"
,
env
.
Hash
()
.
Hex
(),
"filter"
,
watcher
.
id
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment