mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 06:06:44 +00:00
filter 2
This commit is contained in:
parent
8a2044789a
commit
9db3551545
2 changed files with 27 additions and 25 deletions
|
|
@ -80,7 +80,7 @@ func NewPublicFilterAPI(apiBackend ethapi.Backend) *PublicFilterAPI {
|
||||||
apiBackend: apiBackend,
|
apiBackend: apiBackend,
|
||||||
mux: apiBackend.EventMux(),
|
mux: apiBackend.EventMux(),
|
||||||
chainDb: apiBackend.ChainDb(),
|
chainDb: apiBackend.ChainDb(),
|
||||||
filterManager: NewFilterSystem(mux),
|
filterManager: NewFilterSystem(apiBackend.EventMux()),
|
||||||
filterMapping: make(map[string]int),
|
filterMapping: make(map[string]int),
|
||||||
logQueue: make(map[int]*logQueue),
|
logQueue: make(map[int]*logQueue),
|
||||||
blockQueue: make(map[int]*hashQueue),
|
blockQueue: make(map[int]*hashQueue),
|
||||||
|
|
@ -434,14 +434,15 @@ func (s *PublicFilterAPI) NewFilter(args NewFilterArgs) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLogs returns the logs matching the given argument.
|
// GetLogs returns the logs matching the given argument.
|
||||||
func (s *PublicFilterAPI) GetLogs(args NewFilterArgs) []vmlog {
|
func (s *PublicFilterAPI) GetLogs(ctx context.Context, args NewFilterArgs) ([]vmlog, error) {
|
||||||
filter := New(s.apiBackend)
|
filter := New(s.apiBackend)
|
||||||
filter.SetBeginBlock(args.FromBlock.Int64())
|
filter.SetBeginBlock(args.FromBlock.Int64())
|
||||||
filter.SetEndBlock(args.ToBlock.Int64())
|
filter.SetEndBlock(args.ToBlock.Int64())
|
||||||
filter.SetAddresses(args.Addresses)
|
filter.SetAddresses(args.Addresses)
|
||||||
filter.SetTopics(args.Topics)
|
filter.SetTopics(args.Topics)
|
||||||
|
|
||||||
return toRPCLogs(filter.Find(), false)
|
logs, err := filter.Find(ctx)
|
||||||
|
return toRPCLogs(logs, false), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UninstallFilter removes the filter with the given filter id.
|
// UninstallFilter removes the filter with the given filter id.
|
||||||
|
|
@ -527,17 +528,18 @@ func (s *PublicFilterAPI) logFilterChanged(id int) []vmlog {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFilterLogs returns the logs for the filter with the given id.
|
// GetFilterLogs returns the logs for the filter with the given id.
|
||||||
func (s *PublicFilterAPI) GetFilterLogs(filterId string) []vmlog {
|
func (s *PublicFilterAPI) GetFilterLogs(ctx context.Context, filterId string) ([]vmlog, error) {
|
||||||
id, ok := s.filterMapping[filterId]
|
id, ok := s.filterMapping[filterId]
|
||||||
if !ok {
|
if !ok {
|
||||||
return toRPCLogs(nil, false)
|
return toRPCLogs(nil, false), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if filter := s.filterManager.Get(id); filter != nil {
|
if filter := s.filterManager.Get(id); filter != nil {
|
||||||
return toRPCLogs(filter.Find(), false)
|
logs, err := filter.Find(ctx)
|
||||||
|
return toRPCLogs(logs, false), err
|
||||||
}
|
}
|
||||||
|
|
||||||
return toRPCLogs(nil, false)
|
return toRPCLogs(nil, false), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFilterChanges returns the logs for the filter with the given id since last time is was called.
|
// GetFilterChanges returns the logs for the filter with the given id since last time is was called.
|
||||||
|
|
|
||||||
|
|
@ -17,16 +17,17 @@
|
||||||
package filters
|
package filters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
// "math"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core"
|
// "github.com/ethereum/go-ethereum/core"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/core/vm"
|
"github.com/ethereum/go-ethereum/core/vm"
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
"github.com/ethereum/go-ethereum/internal/ethapi"
|
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AccountChange struct {
|
type AccountChange struct {
|
||||||
|
|
@ -78,7 +79,7 @@ func (self *Filter) SetTopics(topics [][]common.Hash) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run filters logs with the current parameters set
|
// Run filters logs with the current parameters set
|
||||||
func (self *Filter) Find() vm.Logs {
|
func (self *Filter) Find(ctx context.Context) (vm.Logs, error) {
|
||||||
headBlockNumber := self.apiBackend.HeaderByNumber(rpc.LatestBlockNumber).Number.Uint64()
|
headBlockNumber := self.apiBackend.HeaderByNumber(rpc.LatestBlockNumber).Number.Uint64()
|
||||||
|
|
||||||
var beginBlockNo uint64 = uint64(self.begin)
|
var beginBlockNo uint64 = uint64(self.begin)
|
||||||
|
|
@ -93,13 +94,13 @@ func (self *Filter) Find() vm.Logs {
|
||||||
// if no addresses are present we can't make use of fast search which
|
// 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
|
// uses the mipmap bloom filters to check for fast inclusion and uses
|
||||||
// higher range probability in order to ensure at least a false positive
|
// higher range probability in order to ensure at least a false positive
|
||||||
if len(self.addresses) == 0 {
|
// if len(self.addresses) == 0 {
|
||||||
return self.getLogs(beginBlockNo, endBlockNo)
|
return self.getLogs(ctx, beginBlockNo, endBlockNo)
|
||||||
}
|
// }
|
||||||
return self.mipFind(beginBlockNo, endBlockNo, 0)
|
// return self.mipFind(beginBlockNo, endBlockNo, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Filter) mipFind(start, end uint64, depth int) (logs vm.Logs) {
|
/*func (self *Filter) mipFind(start, end uint64, depth int) (logs vm.Logs) {
|
||||||
level := core.MIPMapLevels[depth]
|
level := core.MIPMapLevels[depth]
|
||||||
// normalise numerator so we can work in level specific batches and
|
// normalise numerator so we can work in level specific batches and
|
||||||
// work with the proper range checks
|
// work with the proper range checks
|
||||||
|
|
@ -127,25 +128,24 @@ func (self *Filter) mipFind(start, end uint64, depth int) (logs vm.Logs) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return logs
|
return logs
|
||||||
}
|
}*/
|
||||||
|
|
||||||
func (self *Filter) getLogs(start, end uint64) (logs vm.Logs) {
|
|
||||||
var block *types.Block
|
|
||||||
|
|
||||||
|
func (self *Filter) getLogs(ctx context.Context, start, end uint64) (logs vm.Logs, err error) {
|
||||||
for i := start; i <= end; i++ {
|
for i := start; i <= end; i++ {
|
||||||
header := self.apiBackend.HeaderByNumber(rpc.BlockNumber(i))
|
header := self.apiBackend.HeaderByNumber(rpc.BlockNumber(i))
|
||||||
if header == nil {
|
if header == nil {
|
||||||
return logs
|
return logs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use bloom filtering to see if this block is interesting given the
|
// Use bloom filtering to see if this block is interesting given the
|
||||||
// current parameters
|
// current parameters
|
||||||
if self.bloomFilter(header.Bloom) {
|
if self.bloomFilter(header.Bloom) {
|
||||||
// Get the logs of the block
|
// Get the logs of the block
|
||||||
var (
|
receipts, err := self.apiBackend.GetReceipts(ctx, header.Hash())
|
||||||
receipts = self.apiBackend.GetReceipts(ctx, header.Hash())
|
if err != nil {
|
||||||
unfiltered vm.Logs
|
return nil, err
|
||||||
)
|
}
|
||||||
|
var unfiltered vm.Logs
|
||||||
for _, receipt := range receipts {
|
for _, receipt := range receipts {
|
||||||
unfiltered = append(unfiltered, receipt.Logs...)
|
unfiltered = append(unfiltered, receipt.Logs...)
|
||||||
}
|
}
|
||||||
|
|
@ -153,7 +153,7 @@ func (self *Filter) getLogs(start, end uint64) (logs vm.Logs) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return logs
|
return logs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func includes(addresses []common.Address, a common.Address) bool {
|
func includes(addresses []common.Address, a common.Address) bool {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue