aboutsummaryrefslogtreecommitdiffstats
path: root/eth/filters/filter.go
diff options
context:
space:
mode:
authorzsfelfoldi <zsfelfoldi@gmail.com>2016-01-14 02:35:48 +0800
committerFelix Lange <fjl@twurst.com>2016-11-09 09:12:53 +0800
commit7db7109a5b53c339f00e9c05ac826b3dbd1f98e1 (patch)
treec447d9816c4490e71cb2a8d2420d82f938d8757a /eth/filters/filter.go
parent9f8d192991c4f68fa14c91366722bbca601da117 (diff)
downloadgo-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.tar
go-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.tar.gz
go-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.tar.bz2
go-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.tar.lz
go-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.tar.xz
go-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.tar.zst
go-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.zip
cmd, eth: added light client and light server modes
Diffstat (limited to 'eth/filters/filter.go')
-rw-r--r--eth/filters/filter.go77
1 files changed, 44 insertions, 33 deletions
diff --git a/eth/filters/filter.go b/eth/filters/filter.go
index d181d0892..4004af300 100644
--- a/eth/filters/filter.go
+++ b/eth/filters/filter.go
@@ -24,10 +24,23 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
+ "github.com/ethereum/go-ethereum/event"
+ "github.com/ethereum/go-ethereum/rpc"
+ "golang.org/x/net/context"
)
+type Backend interface {
+ ChainDb() ethdb.Database
+ EventMux() *event.TypeMux
+ HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
+ GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
+}
+
// Filter can be used to retrieve and filter logs
type Filter struct {
+ backend Backend
+ useMipMap bool
+
created time.Time
db ethdb.Database
@@ -38,8 +51,12 @@ type Filter struct {
// New creates a new filter which uses a bloom filter on blocks to figure out whether
// a particular block is interesting or not.
-func New(db ethdb.Database) *Filter {
- return &Filter{db: db}
+func New(backend Backend, useMipMap bool) *Filter {
+ return &Filter{
+ backend: backend,
+ useMipMap: useMipMap,
+ db: backend.ChainDb(),
+ }
}
// SetBeginBlock sets the earliest block for filtering.
@@ -66,30 +83,29 @@ func (f *Filter) SetTopics(topics [][]common.Hash) {
}
// Run filters logs with the current parameters set
-func (f *Filter) Find() []Log {
- latestHash := core.GetHeadBlockHash(f.db)
- latestBlock := core.GetBlock(f.db, latestHash, core.GetBlockNumber(f.db, latestHash))
- if latestBlock == nil {
- return []Log{}
+func (f *Filter) Find(ctx context.Context) ([]Log, error) {
+ head, _ := f.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber)
+ if head == nil {
+ return nil, nil
}
+ headBlockNumber := head.Number.Uint64()
var beginBlockNo uint64 = uint64(f.begin)
if f.begin == -1 {
- beginBlockNo = latestBlock.NumberU64()
+ beginBlockNo = headBlockNumber
}
-
- endBlockNo := uint64(f.end)
+ var endBlockNo uint64 = uint64(f.end)
if f.end == -1 {
- endBlockNo = latestBlock.NumberU64()
+ endBlockNo = headBlockNumber
}
// if no addresses are present we can't make use of fast search which
// uses the mipmap bloom filters to check for fast inclusion and uses
// higher range probability in order to ensure at least a false positive
- if len(f.addresses) == 0 {
- return f.getLogs(beginBlockNo, endBlockNo)
+ if !f.useMipMap || len(f.addresses) == 0 {
+ return f.getLogs(ctx, beginBlockNo, endBlockNo)
}
- return f.mipFind(beginBlockNo, endBlockNo, 0)
+ return f.mipFind(beginBlockNo, endBlockNo, 0), nil
}
func (f *Filter) mipFind(start, end uint64, depth int) (logs []Log) {
@@ -107,7 +123,8 @@ func (f *Filter) mipFind(start, end uint64, depth int) (logs []Log) {
start := uint64(math.Max(float64(num), float64(start)))
end := uint64(math.Min(float64(num+level-1), float64(end)))
if depth+1 == len(core.MIPMapLevels) {
- logs = append(logs, f.getLogs(start, end)...)
+ l, _ := f.getLogs(context.Background(), start, end)
+ logs = append(logs, l...)
} else {
logs = append(logs, f.mipFind(start, end, depth+1)...)
}
@@ -122,28 +139,22 @@ func (f *Filter) mipFind(start, end uint64, depth int) (logs []Log) {
return logs
}
-func (f *Filter) getLogs(start, end uint64) (logs []Log) {
- var block *types.Block
-
+func (f *Filter) getLogs(ctx context.Context, start, end uint64) (logs []Log, err error) {
for i := start; i <= end; i++ {
- hash := core.GetCanonicalHash(f.db, i)
- if hash != (common.Hash{}) {
- block = core.GetBlock(f.db, hash, i)
- } else { // block not found
- return logs
- }
- if block == nil { // block not found/written
- return logs
+ header, err := f.backend.HeaderByNumber(ctx, rpc.BlockNumber(i))
+ if header == nil || err != nil {
+ return logs, err
}
// Use bloom filtering to see if this block is interesting given the
// current parameters
- if f.bloomFilter(block) {
+ if f.bloomFilter(header.Bloom) {
// Get the logs of the block
- var (
- receipts = core.GetBlockReceipts(f.db, block.Hash(), i)
- unfiltered []Log
- )
+ receipts, err := f.backend.GetReceipts(ctx, header.Hash())
+ if err != nil {
+ return nil, err
+ }
+ var unfiltered []Log
for _, receipt := range receipts {
rl := make([]Log, len(receipt.Logs))
for i, l := range receipt.Logs {
@@ -155,7 +166,7 @@ func (f *Filter) getLogs(start, end uint64) (logs []Log) {
}
}
- return logs
+ return logs, nil
}
func includes(addresses []common.Address, a common.Address) bool {
@@ -229,7 +240,7 @@ func bloomFilter(bloom types.Bloom, addresses []common.Address, topics [][]commo
for _, sub := range topics {
var included bool
for _, topic := range sub {
- if (topic == common.Hash{}) || types.BloomLookup(block.Bloom(), topic) {
+ if (topic == common.Hash{}) || types.BloomLookup(bloom, topic) {
included = true
break
}