mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 17:33:47 +00:00
whisper: add comments
This commit is contained in:
parent
c58ecf1a0b
commit
db2baf8886
2 changed files with 29 additions and 10 deletions
|
|
@ -27,7 +27,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ALL_TOPICS = ""
|
ALL_TOPICS = ""
|
||||||
MAX_POOL_CAPACITY = 1000
|
MAX_POOL_CAPACITY = 1000
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -45,17 +45,17 @@ type Filter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Filters struct {
|
type Filters struct {
|
||||||
watchers map[string]*Filter
|
watchers map[string]*Filter
|
||||||
whisper *Whisper
|
whisper *Whisper
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
topicMatcher *topicMatcher
|
topicMatcher *topicMatcher
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFilters(w *Whisper) *Filters {
|
func NewFilters(w *Whisper) *Filters {
|
||||||
fs := &Filters{
|
fs := &Filters{
|
||||||
watchers: make(map[string]*Filter),
|
watchers: make(map[string]*Filter),
|
||||||
whisper: w,
|
whisper: w,
|
||||||
topicMatcher: newTopicMatcher(),
|
topicMatcher: newTopicMatcher(),
|
||||||
}
|
}
|
||||||
return fs
|
return fs
|
||||||
}
|
}
|
||||||
|
|
@ -228,6 +228,7 @@ func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool {
|
||||||
return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
|
return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//topicMatcher keeps topic->watcher mapping
|
||||||
type topicMatcher struct {
|
type topicMatcher struct {
|
||||||
//structure - map[topic]map[filterID]
|
//structure - map[topic]map[filterID]
|
||||||
//mapping for topics
|
//mapping for topics
|
||||||
|
|
@ -237,6 +238,7 @@ type topicMatcher struct {
|
||||||
pool sync.Pool
|
pool sync.Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//newTopicMatcher returns a newly created topic matcher
|
||||||
func newTopicMatcher() *topicMatcher {
|
func newTopicMatcher() *topicMatcher {
|
||||||
tm := new(topicMatcher)
|
tm := new(topicMatcher)
|
||||||
tm.mapper = make(map[string]map[string]struct{})
|
tm.mapper = make(map[string]map[string]struct{})
|
||||||
|
|
@ -247,9 +249,12 @@ func newTopicMatcher() *topicMatcher {
|
||||||
return tm
|
return tm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//take returns []string from pool
|
||||||
func (fs *topicMatcher) take() []string {
|
func (fs *topicMatcher) take() []string {
|
||||||
return fs.pool.Get().([]string)
|
return fs.pool.Get().([]string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//resolve put []string to pool
|
||||||
func (fs *topicMatcher) resolve(s []string) {
|
func (fs *topicMatcher) resolve(s []string) {
|
||||||
if cap(s) > MAX_POOL_CAPACITY {
|
if cap(s) > MAX_POOL_CAPACITY {
|
||||||
return
|
return
|
||||||
|
|
@ -257,6 +262,7 @@ func (fs *topicMatcher) resolve(s []string) {
|
||||||
fs.pool.Put(s[:0])
|
fs.pool.Put(s[:0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//addFilterToTopicsMapping fill topic->watcher mapping for current watcher
|
||||||
func (fs *topicMatcher) addFilterToTopicsMapping(watcher *Filter, id string) {
|
func (fs *topicMatcher) addFilterToTopicsMapping(watcher *Filter, id string) {
|
||||||
fs.mx.Lock()
|
fs.mx.Lock()
|
||||||
defer fs.mx.Unlock()
|
defer fs.mx.Unlock()
|
||||||
|
|
@ -271,6 +277,7 @@ func (fs *topicMatcher) addFilterToTopicsMapping(watcher *Filter, id string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//removeTopicFromTopicMapping removes mapping info by filterID
|
||||||
func (fs *topicMatcher) removeTopicFromTopicMapping(id string) {
|
func (fs *topicMatcher) removeTopicFromTopicMapping(id string) {
|
||||||
fs.mx.Lock()
|
fs.mx.Lock()
|
||||||
defer fs.mx.Unlock()
|
defer fs.mx.Unlock()
|
||||||
|
|
@ -279,6 +286,7 @@ func (fs *topicMatcher) removeTopicFromTopicMapping(id string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//prepareTopicsMapping returns set of topics for watcher
|
||||||
func (fs *topicMatcher) prepareTopicsMapping(watcher *Filter) map[string]struct{} {
|
func (fs *topicMatcher) prepareTopicsMapping(watcher *Filter) map[string]struct{} {
|
||||||
topics := make(map[string]struct{}, len(watcher.Topics))
|
topics := make(map[string]struct{}, len(watcher.Topics))
|
||||||
|
|
||||||
|
|
@ -294,6 +302,7 @@ func (fs *topicMatcher) prepareTopicsMapping(watcher *Filter) map[string]struct{
|
||||||
return topics
|
return topics
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//matchedTopics write all matched topics to matched
|
||||||
func (fs *topicMatcher) matchedTopics(topic TopicType, matched *[]string) {
|
func (fs *topicMatcher) matchedTopics(topic TopicType, matched *[]string) {
|
||||||
fs.mx.RLock()
|
fs.mx.RLock()
|
||||||
defer fs.mx.RUnlock()
|
defer fs.mx.RUnlock()
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,7 @@ func (fs *Filters) Install(watcher *Filter) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.watchers[id] = watcher
|
fs.watchers[id] = watcher
|
||||||
|
//add topic matching for watcher
|
||||||
fs.topicMatcher.addFilterToTopicsMapping(watcher, id)
|
fs.topicMatcher.addFilterToTopicsMapping(watcher, id)
|
||||||
return id, err
|
return id, err
|
||||||
}
|
}
|
||||||
|
|
@ -246,6 +247,7 @@ func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool {
|
||||||
return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
|
return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//topicMatcher keeps topic->watcher mapping
|
||||||
type topicMatcher struct {
|
type topicMatcher struct {
|
||||||
//structure - map[topic]map[filterID]
|
//structure - map[topic]map[filterID]
|
||||||
//mapping for topics
|
//mapping for topics
|
||||||
|
|
@ -255,6 +257,7 @@ type topicMatcher struct {
|
||||||
pool sync.Pool
|
pool sync.Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//newTopicMatcher returns a newly created topic matcher
|
||||||
func newTopicMatcher() *topicMatcher {
|
func newTopicMatcher() *topicMatcher {
|
||||||
tm := new(topicMatcher)
|
tm := new(topicMatcher)
|
||||||
tm.mapper = make(map[string]map[string]struct{})
|
tm.mapper = make(map[string]map[string]struct{})
|
||||||
|
|
@ -265,9 +268,12 @@ func newTopicMatcher() *topicMatcher {
|
||||||
return tm
|
return tm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//take returns []string from pool
|
||||||
func (fs *topicMatcher) take() []string {
|
func (fs *topicMatcher) take() []string {
|
||||||
return fs.pool.Get().([]string)
|
return fs.pool.Get().([]string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//resolve put []string to pool
|
||||||
func (fs *topicMatcher) resolve(s []string) {
|
func (fs *topicMatcher) resolve(s []string) {
|
||||||
if cap(s) > MAX_POOL_CAPACITY {
|
if cap(s) > MAX_POOL_CAPACITY {
|
||||||
return
|
return
|
||||||
|
|
@ -275,6 +281,7 @@ func (fs *topicMatcher) resolve(s []string) {
|
||||||
fs.pool.Put(s[:0])
|
fs.pool.Put(s[:0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//addFilterToTopicsMapping fill topic->watcher mapping for current watcher
|
||||||
func (fs *topicMatcher) addFilterToTopicsMapping(watcher *Filter, id string) {
|
func (fs *topicMatcher) addFilterToTopicsMapping(watcher *Filter, id string) {
|
||||||
fs.mx.Lock()
|
fs.mx.Lock()
|
||||||
defer fs.mx.Unlock()
|
defer fs.mx.Unlock()
|
||||||
|
|
@ -289,14 +296,16 @@ func (fs *topicMatcher) addFilterToTopicsMapping(watcher *Filter, id string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs *topicMatcher) removeTopicFromTopicMapping(id string) {
|
//removeTopicFromTopicMapping removes mapping info by filterID
|
||||||
|
func (fs *topicMatcher) removeTopicFromTopicMapping(filterID string) {
|
||||||
fs.mx.Lock()
|
fs.mx.Lock()
|
||||||
defer fs.mx.Unlock()
|
defer fs.mx.Unlock()
|
||||||
for i := range fs.mapper {
|
for i := range fs.mapper {
|
||||||
delete(fs.mapper[i], id)
|
delete(fs.mapper[i], filterID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//prepareTopicsMapping returns set of topics for watcher
|
||||||
func (fs *topicMatcher) prepareTopicsMapping(watcher *Filter) map[string]struct{} {
|
func (fs *topicMatcher) prepareTopicsMapping(watcher *Filter) map[string]struct{} {
|
||||||
topics := make(map[string]struct{}, len(watcher.Topics))
|
topics := make(map[string]struct{}, len(watcher.Topics))
|
||||||
|
|
||||||
|
|
@ -312,6 +321,7 @@ func (fs *topicMatcher) prepareTopicsMapping(watcher *Filter) map[string]struct{
|
||||||
return topics
|
return topics
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//matchedTopics write all matched topics to matched
|
||||||
func (fs *topicMatcher) matchedTopics(topic TopicType, matched *[]string) {
|
func (fs *topicMatcher) matchedTopics(topic TopicType, matched *[]string) {
|
||||||
fs.mx.RLock()
|
fs.mx.RLock()
|
||||||
defer fs.mx.RUnlock()
|
defer fs.mx.RUnlock()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue