mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 06:06:44 +00:00
using core events, fixed light block filter
This commit is contained in:
parent
ce438acb08
commit
785439d088
5 changed files with 17 additions and 41 deletions
|
|
@ -138,6 +138,7 @@ done:
|
|||
|
||||
// NewBlockFilter create a new filter that returns blocks that are included into the canonical chain.
|
||||
func (s *PublicFilterAPI) NewBlockFilter() (string, error) {
|
||||
fmt.Println("NewBlockFilter")
|
||||
externalId, err := newFilterId()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -155,8 +156,10 @@ func (s *PublicFilterAPI) NewBlockFilter() (string, error) {
|
|||
filter.BlockCallback = func(block *types.Block, logs vm.Logs) {
|
||||
s.blockMu.Lock()
|
||||
defer s.blockMu.Unlock()
|
||||
fmt.Println("callback")
|
||||
|
||||
if queue := s.blockQueue[id]; queue != nil {
|
||||
fmt.Println(" add")
|
||||
queue.add(block.Hash())
|
||||
}
|
||||
}
|
||||
|
|
@ -498,8 +501,11 @@ func (s *PublicFilterAPI) blockFilterChanged(id int) []common.Hash {
|
|||
s.blockMu.Lock()
|
||||
defer s.blockMu.Unlock()
|
||||
|
||||
fmt.Println("blockFilterChanged")
|
||||
if s.blockQueue[id] != nil {
|
||||
return s.blockQueue[id].get()
|
||||
res := s.blockQueue[id].get()
|
||||
fmt.Println(" len = ", len(res))
|
||||
return res
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -134,9 +134,12 @@ func (fs *FilterSystem) filterLoop() {
|
|||
for event := range fs.sub.Chan() {
|
||||
switch ev := event.Data.(type) {
|
||||
case core.ChainEvent:
|
||||
fmt.Println("ChainEvent")
|
||||
fs.filterMu.RLock()
|
||||
for _, filter := range fs.chainFilters {
|
||||
fmt.Println("1")
|
||||
if filter.BlockCallback != nil && !filter.created.After(event.Time) {
|
||||
fmt.Println("2")
|
||||
filter.BlockCallback(ev.Block, ev.Logs)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
@ -338,9 +338,9 @@ func (self *LightChain) Rollback(chain []common.Hash) {
|
|||
// posts them into the event mux.
|
||||
func (self *LightChain) postChainEvents(events []interface{}) {
|
||||
for _, event := range events {
|
||||
if event, ok := event.(LightChainEvent); ok {
|
||||
if event, ok := event.(core.ChainEvent); ok {
|
||||
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
|
||||
|
|
@ -379,16 +379,16 @@ func (self *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int)
|
|||
if glog.V(logger.Debug) {
|
||||
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:
|
||||
if glog.V(logger.Detail) {
|
||||
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:
|
||||
events = append(events, LightChainSplitEvent{header})
|
||||
events = append(events, core.ChainSplitEvent{Block: types.NewBlockWithHeader(header)})
|
||||
}
|
||||
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ func NewTxPool(config *core.ChainConfig, eventMux *event.TypeMux, chain *LightCh
|
|||
mined: make(map[common.Hash][]*types.Transaction),
|
||||
quit: make(chan bool),
|
||||
eventMux: eventMux,
|
||||
events: eventMux.Subscribe(LightChainHeadEvent{}),
|
||||
events: eventMux.Subscribe(core.ChainHeadEvent{}),
|
||||
chain: chain,
|
||||
relay: relay,
|
||||
odr: chain.Odr(),
|
||||
|
|
@ -274,7 +274,7 @@ const blockCheckTimeout = time.Second * 3
|
|||
func (pool *TxPool) eventLoop() {
|
||||
for ev := range pool.events.Chan() {
|
||||
switch ev.Data.(type) {
|
||||
case LightChainHeadEvent:
|
||||
case core.ChainHeadEvent:
|
||||
pool.mu.Lock()
|
||||
ctx, _ := context.WithTimeout(context.Background(), blockCheckTimeout)
|
||||
head := pool.chain.CurrentHeader()
|
||||
|
|
|
|||
Loading…
Reference in a new issue