using core events, fixed light block filter

This commit is contained in:
zsfelfoldi 2016-06-25 18:34:53 +02:00
parent ce438acb08
commit 785439d088
5 changed files with 17 additions and 41 deletions

View file

@ -138,6 +138,7 @@ done:
// NewBlockFilter create a new filter that returns blocks that are included into the canonical chain. // NewBlockFilter create a new filter that returns blocks that are included into the canonical chain.
func (s *PublicFilterAPI) NewBlockFilter() (string, error) { func (s *PublicFilterAPI) NewBlockFilter() (string, error) {
fmt.Println("NewBlockFilter")
externalId, err := newFilterId() externalId, err := newFilterId()
if err != nil { if err != nil {
return "", err return "", err
@ -155,8 +156,10 @@ func (s *PublicFilterAPI) NewBlockFilter() (string, error) {
filter.BlockCallback = func(block *types.Block, logs vm.Logs) { filter.BlockCallback = func(block *types.Block, logs vm.Logs) {
s.blockMu.Lock() s.blockMu.Lock()
defer s.blockMu.Unlock() defer s.blockMu.Unlock()
fmt.Println("callback")
if queue := s.blockQueue[id]; queue != nil { if queue := s.blockQueue[id]; queue != nil {
fmt.Println(" add")
queue.add(block.Hash()) queue.add(block.Hash())
} }
} }
@ -498,8 +501,11 @@ func (s *PublicFilterAPI) blockFilterChanged(id int) []common.Hash {
s.blockMu.Lock() s.blockMu.Lock()
defer s.blockMu.Unlock() defer s.blockMu.Unlock()
fmt.Println("blockFilterChanged")
if s.blockQueue[id] != nil { if s.blockQueue[id] != nil {
return s.blockQueue[id].get() res := s.blockQueue[id].get()
fmt.Println(" len = ", len(res))
return res
} }
return nil return nil
} }

View file

@ -134,9 +134,12 @@ func (fs *FilterSystem) filterLoop() {
for event := range fs.sub.Chan() { for event := range fs.sub.Chan() {
switch ev := event.Data.(type) { switch ev := event.Data.(type) {
case core.ChainEvent: case core.ChainEvent:
fmt.Println("ChainEvent")
fs.filterMu.RLock() fs.filterMu.RLock()
for _, filter := range fs.chainFilters { for _, filter := range fs.chainFilters {
fmt.Println("1")
if filter.BlockCallback != nil && !filter.created.After(event.Time) { if filter.BlockCallback != nil && !filter.created.After(event.Time) {
fmt.Println("2")
filter.BlockCallback(ev.Block, ev.Logs) filter.BlockCallback(ev.Block, ev.Logs)
} }
} }

View file

@ -1,33 +0,0 @@
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package light
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
type LightChainSplitEvent struct{ Header *types.Header }
type LightChainEvent struct {
Header *types.Header
Hash common.Hash
}
type LightChainSideEvent struct{ Header *types.Header }
type LightChainHeadEvent struct{ Header *types.Header }

View file

@ -338,9 +338,9 @@ func (self *LightChain) Rollback(chain []common.Hash) {
// posts them into the event mux. // posts them into the event mux.
func (self *LightChain) postChainEvents(events []interface{}) { func (self *LightChain) postChainEvents(events []interface{}) {
for _, event := range events { for _, event := range events {
if event, ok := event.(LightChainEvent); ok { if event, ok := event.(core.ChainEvent); ok {
if self.LastBlockHash() == event.Hash { if self.LastBlockHash() == event.Hash {
self.eventMux.Post(LightChainHeadEvent{event.Header}) self.eventMux.Post(core.ChainHeadEvent{Block: event.Block})
} }
} }
// Fire the insertion events individually too // Fire the insertion events individually too
@ -379,16 +379,16 @@ func (self *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int)
if glog.V(logger.Debug) { if glog.V(logger.Debug) {
glog.Infof("[%v] inserted header #%d (%x...).\n", time.Now().UnixNano(), header.Number, header.Hash().Bytes()[0:4]) glog.Infof("[%v] inserted header #%d (%x...).\n", time.Now().UnixNano(), header.Number, header.Hash().Bytes()[0:4])
} }
events = append(events, LightChainEvent{header, header.Hash()}) events = append(events, core.ChainEvent{Block: types.NewBlockWithHeader(header), Hash: header.Hash()})
case core.SideStatTy: case core.SideStatTy:
if glog.V(logger.Detail) { if glog.V(logger.Detail) {
glog.Infof("inserted forked header #%d (TD=%v) (%x...).\n", header.Number, header.Difficulty, header.Hash().Bytes()[0:4]) glog.Infof("inserted forked header #%d (TD=%v) (%x...).\n", header.Number, header.Difficulty, header.Hash().Bytes()[0:4])
} }
events = append(events, LightChainSideEvent{header}) events = append(events, core.ChainSideEvent{Block: types.NewBlockWithHeader(header)})
case core.SplitStatTy: case core.SplitStatTy:
events = append(events, LightChainSplitEvent{header}) events = append(events, core.ChainSplitEvent{Block: types.NewBlockWithHeader(header)})
} }
return err return err

View file

@ -84,7 +84,7 @@ func NewTxPool(config *core.ChainConfig, eventMux *event.TypeMux, chain *LightCh
mined: make(map[common.Hash][]*types.Transaction), mined: make(map[common.Hash][]*types.Transaction),
quit: make(chan bool), quit: make(chan bool),
eventMux: eventMux, eventMux: eventMux,
events: eventMux.Subscribe(LightChainHeadEvent{}), events: eventMux.Subscribe(core.ChainHeadEvent{}),
chain: chain, chain: chain,
relay: relay, relay: relay,
odr: chain.Odr(), odr: chain.Odr(),
@ -274,7 +274,7 @@ const blockCheckTimeout = time.Second * 3
func (pool *TxPool) eventLoop() { func (pool *TxPool) eventLoop() {
for ev := range pool.events.Chan() { for ev := range pool.events.Chan() {
switch ev.Data.(type) { switch ev.Data.(type) {
case LightChainHeadEvent: case core.ChainHeadEvent:
pool.mu.Lock() pool.mu.Lock()
ctx, _ := context.WithTimeout(context.Background(), blockCheckTimeout) ctx, _ := context.WithTimeout(context.Background(), blockCheckTimeout)
head := pool.chain.CurrentHeader() head := pool.chain.CurrentHeader()