mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
Fix tests
This commit is contained in:
parent
6ba9b3b775
commit
6d019710ef
4 changed files with 43 additions and 16 deletions
|
|
@ -319,6 +319,18 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
|
||||||
return nil
|
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
|
// FilterLogs executes a log filter operation, blocking during execution and
|
||||||
// returning all the results in one batch.
|
// 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)
|
filter = filters.NewBlockFilter(&filterBackend{b.database, b.blockchain}, *query.BlockHash, query.Addresses, query.Topics)
|
||||||
} else {
|
} else {
|
||||||
// Initialize unset filter boundaried to run from genesis to chain head
|
// Initialize unset filter boundaried to run from genesis to chain head
|
||||||
from := int64(0)
|
from, err := b.getBlockHash(query.FromBlock, query.FromBlockHash)
|
||||||
if query.FromBlock != nil {
|
if err != nil {
|
||||||
from = query.FromBlock.Int64()
|
return nil, err
|
||||||
}
|
}
|
||||||
to := int64(-1)
|
to, err := b.getBlockHash(query.ToBlock, query.ToBlockHash)
|
||||||
if query.ToBlock != nil {
|
if err != nil {
|
||||||
to = query.ToBlock.Int64()
|
return nil, err
|
||||||
}
|
}
|
||||||
// Construct the range filter
|
// Construct the range filter
|
||||||
filter = filters.NewRangeFilter(&filterBackend{b.database, b.blockchain}, from, to, query.Addresses, query.Topics)
|
filter = filters.NewRangeFilter(&filterBackend{b.database, b.blockchain}, from, to, query.Addresses, query.Topics)
|
||||||
|
|
|
||||||
|
|
@ -116,6 +116,8 @@ func benchmarkBloomBits(b *testing.B, sectionSize uint64) {
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
end := rawdb.ReadCanonicalHash(db, uint64(cnt*sectionSize-1))
|
||||||
|
|
||||||
d := time.Since(start)
|
d := time.Since(start)
|
||||||
fmt.Println("Finished generating bloombits data")
|
fmt.Println("Finished generating bloombits data")
|
||||||
fmt.Println(" ", d, "total ", d/time.Duration(cnt*sectionSize), "per block")
|
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
|
var addr common.Address
|
||||||
addr[0] = byte(i)
|
addr[0] = byte(i)
|
||||||
addr[1] = byte(i / 256)
|
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 {
|
if _, err := filter.Logs(context.Background()); err != nil {
|
||||||
b.Error("filter.Find error:", err)
|
b.Error("filter.Find error:", err)
|
||||||
}
|
}
|
||||||
|
|
@ -192,7 +194,7 @@ func BenchmarkNoBloomBits(b *testing.B) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
mux := new(event.TypeMux)
|
mux := new(event.TypeMux)
|
||||||
backend := &testBackend{mux, db, 0, new(event.Feed), new(event.Feed), new(event.Feed), new(event.Feed)}
|
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())
|
filter.Logs(context.Background())
|
||||||
d := time.Since(start)
|
d := time.Since(start)
|
||||||
fmt.Println("Finished running filter benchmarks")
|
fmt.Println("Finished running filter benchmarks")
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"sort"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"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
|
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
|
// Logs searches the blockchain for matching log entries, returning all from the
|
||||||
// first block that contains matches, updating the start of the filter accordingly.
|
// first block that contains matches, updating the start of the filter accordingly.
|
||||||
func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) {
|
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())
|
rest, err := f.unindexedLogs(ctx, begin.Hash(), end.Hash())
|
||||||
logs = append(logs, rest...)
|
logs = append(logs, rest...)
|
||||||
|
sort.Sort(logList(logs))
|
||||||
|
|
||||||
f.begin = end.Hash()
|
f.begin = end.Hash()
|
||||||
return logs, err
|
return logs, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -92,7 +92,7 @@ func BenchmarkFilters(b *testing.B) {
|
||||||
}
|
}
|
||||||
b.ResetTimer()
|
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++ {
|
for i := 0; i < b.N; i++ {
|
||||||
logs, _ := filter.Logs(context.Background())
|
logs, _ := filter.Logs(context.Background())
|
||||||
|
|
@ -175,14 +175,16 @@ func TestFilters(t *testing.T) {
|
||||||
rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts[i])
|
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())
|
logs, _ := filter.Logs(context.Background())
|
||||||
if len(logs) != 4 {
|
if len(logs) != 4 {
|
||||||
t.Error("expected 4 log, got", len(logs))
|
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())
|
logs, _ = filter.Logs(context.Background())
|
||||||
if len(logs) != 1 {
|
if len(logs) != 1 {
|
||||||
t.Error("expected 1 log, got", len(logs))
|
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])
|
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())
|
logs, _ = filter.Logs(context.Background())
|
||||||
if len(logs) != 1 {
|
if len(logs) != 1 {
|
||||||
t.Error("expected 1 log, got", len(logs))
|
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])
|
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())
|
logs, _ = filter.Logs(context.Background())
|
||||||
if len(logs) != 2 {
|
if len(logs) != 2 {
|
||||||
|
|
@ -208,7 +210,7 @@ func TestFilters(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
failHash := common.BytesToHash([]byte("fail"))
|
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())
|
logs, _ = filter.Logs(context.Background())
|
||||||
if len(logs) != 0 {
|
if len(logs) != 0 {
|
||||||
|
|
@ -216,14 +218,14 @@ func TestFilters(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
failAddr := common.BytesToAddress([]byte("failmenow"))
|
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())
|
logs, _ = filter.Logs(context.Background())
|
||||||
if len(logs) != 0 {
|
if len(logs) != 0 {
|
||||||
t.Error("expected 0 log, got", len(logs))
|
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())
|
logs, _ = filter.Logs(context.Background())
|
||||||
if len(logs) != 0 {
|
if len(logs) != 0 {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue