Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e3: trace_filter on iterators #6706

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions cmd/rpcdaemon/commands/trace_filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/bitmapdb"
"github.com/ledgerwatch/erigon-lib/kv/iter"
"github.com/ledgerwatch/erigon-lib/kv/order"
"github.com/ledgerwatch/erigon-lib/kv/rawdbv3"
"github.com/ledgerwatch/erigon/common"
Expand Down Expand Up @@ -289,23 +290,22 @@ func traceFilterBitmaps(tx kv.Tx, req TraceFilterRequest, from, to uint64) (from
return fromAddresses, toAddresses, allBlocks, nil
}

func traceFilterBitmapsV3(tx kv.TemporalTx, req TraceFilterRequest, from, to uint64) (fromAddresses, toAddresses map[libcommon.Address]struct{}, allBlocks *roaring64.Bitmap, err error) {
func traceFilterBitmapsV3(tx kv.TemporalTx, req TraceFilterRequest, from, to uint64) (fromAddresses, toAddresses map[libcommon.Address]struct{}, allBlocks iter.U64, err error) {
fromAddresses = make(map[libcommon.Address]struct{}, len(req.FromAddress))
toAddresses = make(map[libcommon.Address]struct{}, len(req.ToAddress))
allBlocks = roaring64.New()
var blocksTo roaring64.Bitmap
var blocksTo iter.U64

for _, addr := range req.FromAddress {
if addr != nil {
it, err := tx.IndexRange(temporal.TracesFromIdx, addr.Bytes(), int(from), int(to), order.Asc, -1)
if errors.Is(err, ethdb.ErrKeyNotFound) {
continue
}
b, err := it.(bitmapdb.ToBitmap).ToBitmap()
if err != nil {
return nil, nil, nil, err
if allBlocks == nil {
allBlocks = it
} else {
allBlocks = iter.Union[uint64](allBlocks, it)
}
allBlocks.Or(b)
fromAddresses[*addr] = struct{}{}
}
}
Expand All @@ -316,30 +316,38 @@ func traceFilterBitmapsV3(tx kv.TemporalTx, req TraceFilterRequest, from, to uin
if errors.Is(err, ethdb.ErrKeyNotFound) {
continue
}
b, err := it.(bitmapdb.ToBitmap).ToBitmap()
if err != nil {
return nil, nil, nil, err
if blocksTo == nil {
blocksTo = it
} else {
blocksTo = iter.Union[uint64](blocksTo, it)
}
blocksTo.Or(b)
toAddresses[*addr] = struct{}{}
}
}

switch req.Mode {
case TraceFilterModeIntersection:
allBlocks.And(&blocksTo)
if allBlocks == nil {
allBlocks = blocksTo
} else {
allBlocks = iter.Intersect[uint64](allBlocks, blocksTo)
}
case TraceFilterModeUnion:
fallthrough
default:
allBlocks.Or(&blocksTo)
if allBlocks == nil {
allBlocks = blocksTo
} else {
allBlocks = iter.Union[uint64](allBlocks, blocksTo)
}
}

// Special case - if no addresses specified, take all traces
if len(req.FromAddress) == 0 && len(req.ToAddress) == 0 {
allBlocks.AddRange(from, to)
allBlocks = iter.Range[uint64](from, to)
} else {
allBlocks.RemoveRange(0, from)
allBlocks.RemoveRange(to, uint64(0x100000000))
//allBlocks.RemoveRange(0, from)
//allBlocks.RemoveRange(to, uint64(0x100000000))
}

return fromAddresses, toAddresses, allBlocks, nil
Expand Down Expand Up @@ -631,7 +639,7 @@ func (api *TraceAPIImpl) filterV3(ctx context.Context, dbtx kv.TemporalTx, fromB
nSeen := uint64(0)
nExported := uint64(0)
includeAll := len(fromAddresses) == 0 && len(toAddresses) == 0
it := MapTxNum2BlockNum(dbtx, bitmapdb.ToIter(allTxs.Iterator()))
it := MapTxNum2BlockNum(dbtx, allTxs)

var lastBlockHash libcommon.Hash
var lastHeader *types.Header
Expand Down