From 6d019710ef3f057d1b3b28694ba46b638fb648dd Mon Sep 17 00:00:00 2001 From: Nick Johnson Date: Mon, 24 Sep 2018 14:41:24 +0100 Subject: [PATCH] Fix tests --- accounts/abi/bind/backends/simulated.go | 24 ++++++++++++++++++------ eth/filters/bench_test.go | 6 ++++-- eth/filters/filter.go | 11 +++++++++++ eth/filters/filter_test.go | 18 ++++++++++-------- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index fc0ccbf52c..110bf18346 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -319,6 +319,18 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa return nil } +func (b *SimulatedBackend) getBlockHash(number *big.Int, hash *common.Hash) (common.Hash, error) { + if hash != nil { + return *hash, nil + } else if number != nil { + header := b.blockchain.GetHeaderByNumber(number.Uint64()) + return header.Hash(), nil + } else { + header := b.blockchain.CurrentHeader() + return header.Hash(), nil + } +} + // FilterLogs executes a log filter operation, blocking during execution and // returning all the results in one batch. // @@ -330,13 +342,13 @@ func (b *SimulatedBackend) FilterLogs(ctx context.Context, query ethereum.Filter filter = filters.NewBlockFilter(&filterBackend{b.database, b.blockchain}, *query.BlockHash, query.Addresses, query.Topics) } else { // Initialize unset filter boundaried to run from genesis to chain head - from := int64(0) - if query.FromBlock != nil { - from = query.FromBlock.Int64() + from, err := b.getBlockHash(query.FromBlock, query.FromBlockHash) + if err != nil { + return nil, err } - to := int64(-1) - if query.ToBlock != nil { - to = query.ToBlock.Int64() + to, err := b.getBlockHash(query.ToBlock, query.ToBlockHash) + if err != nil { + return nil, err } // Construct the range filter filter = filters.NewRangeFilter(&filterBackend{b.database, b.blockchain}, from, to, query.Addresses, query.Topics) diff --git a/eth/filters/bench_test.go b/eth/filters/bench_test.go index c5f681e024..f06953afda 100644 --- a/eth/filters/bench_test.go +++ b/eth/filters/bench_test.go @@ -116,6 +116,8 @@ func benchmarkBloomBits(b *testing.B, sectionSize uint64) { //} } + end := rawdb.ReadCanonicalHash(db, uint64(cnt*sectionSize-1)) + d := time.Since(start) fmt.Println("Finished generating bloombits data") fmt.Println(" ", d, "total ", d/time.Duration(cnt*sectionSize), "per block") @@ -135,7 +137,7 @@ func benchmarkBloomBits(b *testing.B, sectionSize uint64) { var addr common.Address addr[0] = byte(i) addr[1] = byte(i / 256) - filter := NewRangeFilter(backend, 0, int64(cnt*sectionSize-1), []common.Address{addr}, nil) + filter := NewRangeFilter(backend, head, end, []common.Address{addr}, nil) if _, err := filter.Logs(context.Background()); err != nil { b.Error("filter.Find error:", err) } @@ -192,7 +194,7 @@ func BenchmarkNoBloomBits(b *testing.B) { start := time.Now() mux := new(event.TypeMux) backend := &testBackend{mux, db, 0, new(event.Feed), new(event.Feed), new(event.Feed), new(event.Feed)} - filter := NewRangeFilter(backend, 0, int64(*headNum), []common.Address{{}}, nil) + filter := NewRangeFilter(backend, rawdb.ReadCanonicalHash(db, 0), head, []common.Address{{}}, nil) filter.Logs(context.Background()) d := time.Since(start) fmt.Println("Finished running filter benchmarks") diff --git a/eth/filters/filter.go b/eth/filters/filter.go index 492e1c32d9..0054ab72de 100644 --- a/eth/filters/filter.go +++ b/eth/filters/filter.go @@ -20,6 +20,7 @@ import ( "context" "errors" "math/big" + "sort" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" @@ -152,6 +153,14 @@ func (f *Filter) findCommonAncestor(ctx context.Context, begin, end *types.Heade return end, mainChain, nil } +type logList []*types.Log + +func (l logList) Len() int { return len(l) } +func (l logList) Swap(i, j int) { l[i], l[j] = l[j], l[i] } +func (l logList) Less(i, j int) bool { + return l[i].BlockNumber < l[j].BlockNumber || (l[i].BlockNumber == l[j].BlockNumber && l[i].Index < l[j].Index) +} + // Logs searches the blockchain for matching log entries, returning all from the // first block that contains matches, updating the start of the filter accordingly. func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) { @@ -223,6 +232,8 @@ func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) { } rest, err := f.unindexedLogs(ctx, begin.Hash(), end.Hash()) logs = append(logs, rest...) + sort.Sort(logList(logs)) + f.begin = end.Hash() return logs, err } diff --git a/eth/filters/filter_test.go b/eth/filters/filter_test.go index 396a03d611..c94d825da5 100644 --- a/eth/filters/filter_test.go +++ b/eth/filters/filter_test.go @@ -92,7 +92,7 @@ func BenchmarkFilters(b *testing.B) { } b.ResetTimer() - filter := NewRangeFilter(backend, 0, -1, []common.Address{addr1, addr2, addr3, addr4}, nil) + filter := NewRangeFilter(backend, genesis.Hash(), rawdb.ReadHeadBlockHash(db), []common.Address{addr1, addr2, addr3, addr4}, nil) for i := 0; i < b.N; i++ { logs, _ := filter.Logs(context.Background()) @@ -175,14 +175,16 @@ func TestFilters(t *testing.T) { rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts[i]) } - filter := NewRangeFilter(backend, 0, -1, []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}}) + head := rawdb.ReadHeadBlockHash(db) + + filter := NewRangeFilter(backend, genesis.Hash(), head, []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}}) logs, _ := filter.Logs(context.Background()) if len(logs) != 4 { t.Error("expected 4 log, got", len(logs)) } - filter = NewRangeFilter(backend, 900, 999, []common.Address{addr}, [][]common.Hash{{hash3}}) + filter = NewRangeFilter(backend, rawdb.ReadCanonicalHash(db, 900), rawdb.ReadCanonicalHash(db, 999), []common.Address{addr}, [][]common.Hash{{hash3}}) logs, _ = filter.Logs(context.Background()) if len(logs) != 1 { t.Error("expected 1 log, got", len(logs)) @@ -191,7 +193,7 @@ func TestFilters(t *testing.T) { t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0]) } - filter = NewRangeFilter(backend, 990, -1, []common.Address{addr}, [][]common.Hash{{hash3}}) + filter = NewRangeFilter(backend, rawdb.ReadCanonicalHash(db, 990), head, []common.Address{addr}, [][]common.Hash{{hash3}}) logs, _ = filter.Logs(context.Background()) if len(logs) != 1 { t.Error("expected 1 log, got", len(logs)) @@ -200,7 +202,7 @@ func TestFilters(t *testing.T) { t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0]) } - filter = NewRangeFilter(backend, 1, 10, nil, [][]common.Hash{{hash1, hash2}}) + filter = NewRangeFilter(backend, rawdb.ReadCanonicalHash(db, 1), rawdb.ReadCanonicalHash(db, 10), nil, [][]common.Hash{{hash1, hash2}}) logs, _ = filter.Logs(context.Background()) if len(logs) != 2 { @@ -208,7 +210,7 @@ func TestFilters(t *testing.T) { } failHash := common.BytesToHash([]byte("fail")) - filter = NewRangeFilter(backend, 0, -1, nil, [][]common.Hash{{failHash}}) + filter = NewRangeFilter(backend, genesis.Hash(), head, nil, [][]common.Hash{{failHash}}) logs, _ = filter.Logs(context.Background()) if len(logs) != 0 { @@ -216,14 +218,14 @@ func TestFilters(t *testing.T) { } failAddr := common.BytesToAddress([]byte("failmenow")) - filter = NewRangeFilter(backend, 0, -1, []common.Address{failAddr}, nil) + filter = NewRangeFilter(backend, genesis.Hash(), head, []common.Address{failAddr}, nil) logs, _ = filter.Logs(context.Background()) if len(logs) != 0 { t.Error("expected 0 log, got", len(logs)) } - filter = NewRangeFilter(backend, 0, -1, nil, [][]common.Hash{{failHash}, {hash1}}) + filter = NewRangeFilter(backend, genesis.Hash(), head, nil, [][]common.Hash{{failHash}, {hash1}}) logs, _ = filter.Logs(context.Background()) if len(logs) != 0 {