aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeffrey Wilcke <obscuren@users.noreply.github.com>2014-10-10 22:57:54 +0800
committerJeffrey Wilcke <obscuren@users.noreply.github.com>2014-10-10 22:57:54 +0800
commit3db6a8e92d7929bafa2d66e753ed365cf0822187 (patch)
tree540d6da2c5790efa48f1bdde8c01dd927aacbd82
parent6fec5bd32e64e15d16085591e732b26a243297fa (diff)
parente83a99903994eaadec3b58822dd18682649ac9dc (diff)
downloaddexon-3db6a8e92d7929bafa2d66e753ed365cf0822187.tar.gz
dexon-3db6a8e92d7929bafa2d66e753ed365cf0822187.tar.zst
dexon-3db6a8e92d7929bafa2d66e753ed365cf0822187.zip
Merge pull request #57 from fjl/feature/raceless-ethereum-filters
Fix filter map race
-rw-r--r--ethereum.go33
1 files changed, 23 insertions, 10 deletions
diff --git a/ethereum.go b/ethereum.go
index b83ceb12f..204f30bec 100644
--- a/ethereum.go
+++ b/ethereum.go
@@ -95,7 +95,9 @@ type Ethereum struct {
isUpToDate bool
- filters map[int]*ethchain.Filter
+ filterMu sync.RWMutex
+ filterId int
+ filters map[int]*ethchain.Filter
}
func New(db ethutil.Database, clientIdentity ethwire.ClientIdentity, keyManager *ethcrypto.KeyManager, caps Caps, usePnp bool) (*Ethereum, error) {
@@ -594,22 +596,29 @@ out:
}
}
-var filterId = 0
-
-func (self *Ethereum) InstallFilter(object map[string]interface{}) (*ethchain.Filter, int) {
- defer func() { filterId++ }()
-
- filter := ethchain.NewFilterFromMap(object, self)
- self.filters[filterId] = filter
-
- return filter, filterId
+// InstallFilter adds filter for blockchain events.
+// The filter's callbacks will run for matching blocks and messages.
+// The filter should not be modified after it has been installed.
+func (self *Ethereum) InstallFilter(filter *ethchain.Filter) (id int) {
+ self.filterMu.Lock()
+ id = self.filterId
+ self.filters[id] = filter
+ self.filterId++
+ self.filterMu.Unlock()
+ return id
}
func (self *Ethereum) UninstallFilter(id int) {
+ self.filterMu.Lock()
delete(self.filters, id)
+ self.filterMu.Unlock()
}
+// GetFilter retrieves a filter installed using InstallFilter.
+// The filter may not be modified.
func (self *Ethereum) GetFilter(id int) *ethchain.Filter {
+ self.filterMu.RLock()
+ defer self.filterMu.RUnlock()
return self.filters[id]
}
@@ -627,14 +636,17 @@ out:
break out
case block := <-blockChan:
if block, ok := block.Resource.(*ethchain.Block); ok {
+ self.filterMu.RLock()
for _, filter := range self.filters {
if filter.BlockCallback != nil {
filter.BlockCallback(block)
}
}
+ self.filterMu.RUnlock()
}
case msg := <-messageChan:
if messages, ok := msg.Resource.(ethstate.Messages); ok {
+ self.filterMu.RLock()
for _, filter := range self.filters {
if filter.MessageCallback != nil {
msgs := filter.FilterMessages(messages)
@@ -643,6 +655,7 @@ out:
}
}
}
+ self.filterMu.RUnlock()
}
}
}