cmd, eth: added light client and light server modes
This commit is contained in:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user