Skip to content

Commit b935998

Browse files
committed
xeth: fixed nil pointer of filter retrieval
This fix addresses an issue with filters that were (possibly) not yet added to the filter queues but were expected. I've added additional nil checks making sure it doesn't crash and swapped the installation of the filter around so it's installed before use. Closes ethereum#1665
1 parent e56cbc2 commit b935998

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

xeth/xeth.go

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,10 @@ func (self *XEth) NewLogFilter(earliest, latest int64, skip, max int, address []
532532
self.logMu.Lock()
533533
defer self.logMu.Unlock()
534534

535-
var id int
536535
filter := core.NewFilter(self.backend)
536+
id := self.filterManager.InstallFilter(filter)
537+
self.logQueue[id] = &logQueue{timeout: time.Now()}
538+
537539
filter.SetEarliestBlock(earliest)
538540
filter.SetLatestBlock(latest)
539541
filter.SetSkip(skip)
@@ -544,10 +546,10 @@ func (self *XEth) NewLogFilter(earliest, latest int64, skip, max int, address []
544546
self.logMu.Lock()
545547
defer self.logMu.Unlock()
546548

547-
self.logQueue[id].add(logs...)
549+
if queue := self.logQueue[id]; queue != nil {
550+
queue.add(logs...)
551+
}
548552
}
549-
id = self.filterManager.InstallFilter(filter)
550-
self.logQueue[id] = &logQueue{timeout: time.Now()}
551553

552554
return id
553555
}
@@ -556,33 +558,37 @@ func (self *XEth) NewTransactionFilter() int {
556558
self.transactionMu.Lock()
557559
defer self.transactionMu.Unlock()
558560

559-
var id int
560561
filter := core.NewFilter(self.backend)
562+
id := self.filterManager.InstallFilter(filter)
563+
self.transactionQueue[id] = &hashQueue{timeout: time.Now()}
564+
561565
filter.TransactionCallback = func(tx *types.Transaction) {
562566
self.transactionMu.Lock()
563567
defer self.transactionMu.Unlock()
564568

565-
self.transactionQueue[id].add(tx.Hash())
569+
if queue := self.transactionQueue[id]; queue != nil {
570+
queue.add(tx.Hash())
571+
}
566572
}
567-
id = self.filterManager.InstallFilter(filter)
568-
self.transactionQueue[id] = &hashQueue{timeout: time.Now()}
569573
return id
570574
}
571575

572576
func (self *XEth) NewBlockFilter() int {
573577
self.blockMu.Lock()
574578
defer self.blockMu.Unlock()
575579

576-
var id int
577580
filter := core.NewFilter(self.backend)
581+
id := self.filterManager.InstallFilter(filter)
582+
self.blockQueue[id] = &hashQueue{timeout: time.Now()}
583+
578584
filter.BlockCallback = func(block *types.Block, logs state.Logs) {
579585
self.blockMu.Lock()
580586
defer self.blockMu.Unlock()
581587

582-
self.blockQueue[id].add(block.Hash())
588+
if queue := self.blockQueue[id]; queue != nil {
589+
queue.add(block.Hash())
590+
}
583591
}
584-
id = self.filterManager.InstallFilter(filter)
585-
self.blockQueue[id] = &hashQueue{timeout: time.Now()}
586592
return id
587593
}
588594

@@ -1022,33 +1028,49 @@ func (m callmsg) Value() *big.Int { return m.value }
10221028
func (m callmsg) Data() []byte { return m.data }
10231029

10241030
type logQueue struct {
1031+
mu sync.Mutex
1032+
10251033
logs state.Logs
10261034
timeout time.Time
10271035
id int
10281036
}
10291037

10301038
func (l *logQueue) add(logs ...*state.Log) {
1039+
l.mu.Lock()
1040+
defer l.mu.Unlock()
1041+
10311042
l.logs = append(l.logs, logs...)
10321043
}
10331044

10341045
func (l *logQueue) get() state.Logs {
1046+
l.mu.Lock()
1047+
defer l.mu.Unlock()
1048+
10351049
l.timeout = time.Now()
10361050
tmp := l.logs
10371051
l.logs = nil
10381052
return tmp
10391053
}
10401054

10411055
type hashQueue struct {
1056+
mu sync.Mutex
1057+
10421058
hashes []common.Hash
10431059
timeout time.Time
10441060
id int
10451061
}
10461062

10471063
func (l *hashQueue) add(hashes ...common.Hash) {
1064+
l.mu.Lock()
1065+
defer l.mu.Unlock()
1066+
10481067
l.hashes = append(l.hashes, hashes...)
10491068
}
10501069

10511070
func (l *hashQueue) get() []common.Hash {
1071+
l.mu.Lock()
1072+
defer l.mu.Unlock()
1073+
10521074
l.timeout = time.Now()
10531075
tmp := l.hashes
10541076
l.hashes = nil

0 commit comments

Comments
 (0)