Skip to content

Commit d7d0c41

Browse files
committed
accounts/abi/bind/backends, eth/filters: handle rpc safe block, restructure tests
1 parent 32c2d48 commit d7d0c41

File tree

3 files changed

+80
-81
lines changed

3 files changed

+80
-81
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -844,11 +844,28 @@ func (fb *filterBackend) ChainDb() ethdb.Database { return fb.db }
844844

845845
func (fb *filterBackend) EventMux() *event.TypeMux { panic("not supported") }
846846

847-
func (fb *filterBackend) HeaderByNumber(ctx context.Context, block rpc.BlockNumber) (*types.Header, error) {
848-
if block == rpc.LatestBlockNumber {
847+
func (fb *filterBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
848+
switch number {
849+
case rpc.PendingBlockNumber:
850+
if block := fb.backend.pendingBlock; block != nil {
851+
return block.Header(), nil
852+
}
853+
return nil, nil
854+
case rpc.LatestBlockNumber:
849855
return fb.bc.CurrentHeader(), nil
856+
case rpc.FinalizedBlockNumber:
857+
if block := fb.bc.CurrentFinalizedBlock(); block != nil {
858+
return block.Header(), nil
859+
}
860+
return nil, errors.New("finalized block not found")
861+
case rpc.SafeBlockNumber:
862+
if block := fb.bc.CurrentSafeBlock(); block != nil {
863+
return block.Header(), nil
864+
}
865+
return nil, errors.New("safe block not found")
866+
default:
867+
return fb.bc.GetHeaderByNumber(uint64(number.Int64())), nil
850868
}
851-
return fb.bc.GetHeaderByNumber(uint64(block.Int64())), nil
852869
}
853870

854871
func (fb *filterBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {

eth/filters/filter_system_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package filters
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"math/big"
2324
"math/rand"
@@ -58,21 +59,24 @@ func (b *testBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumbe
5859
hash common.Hash
5960
num uint64
6061
)
61-
if blockNr == rpc.LatestBlockNumber {
62+
switch blockNr {
63+
case rpc.LatestBlockNumber:
6264
hash = rawdb.ReadHeadBlockHash(b.db)
6365
number := rawdb.ReadHeaderNumber(b.db, hash)
6466
if number == nil {
6567
return nil, nil
6668
}
6769
num = *number
68-
} else if blockNr == rpc.FinalizedBlockNumber {
70+
case rpc.FinalizedBlockNumber:
6971
hash = rawdb.ReadFinalizedBlockHash(b.db)
7072
number := rawdb.ReadHeaderNumber(b.db, hash)
7173
if number == nil {
7274
return nil, nil
7375
}
7476
num = *number
75-
} else {
77+
case rpc.SafeBlockNumber:
78+
return nil, errors.New("safe block not found")
79+
default:
7680
num = uint64(blockNr)
7781
hash = rawdb.ReadCanonicalHash(b.db, num)
7882
}

eth/filters/filter_test.go

Lines changed: 53 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package filters
1919
import (
2020
"context"
2121
"math/big"
22+
"reflect"
2223
"testing"
2324

2425
"github.com/ethereum/go-ethereum/common"
@@ -174,85 +175,62 @@ func TestFilters(t *testing.T) {
174175
rawdb.WriteFinalizedBlockHash(db, chain[998].Hash())
175176

176177
filter := sys.NewRangeFilter(0, -1, []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}})
177-
178178
logs, _ := filter.Logs(context.Background())
179179
if len(logs) != 4 {
180180
t.Error("expected 4 log, got", len(logs))
181181
}
182182

183-
filter = sys.NewRangeFilter(900, 999, []common.Address{addr}, [][]common.Hash{{hash3}})
184-
logs, _ = filter.Logs(context.Background())
185-
if len(logs) != 1 {
186-
t.Error("expected 1 log, got", len(logs))
187-
}
188-
if len(logs) > 0 && logs[0].Topics[0] != hash3 {
189-
t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0])
190-
}
191-
192-
filter = sys.NewRangeFilter(990, -1, []common.Address{addr}, [][]common.Hash{{hash3}})
193-
logs, _ = filter.Logs(context.Background())
194-
if len(logs) != 1 {
195-
t.Error("expected 1 log, got", len(logs))
196-
}
197-
if len(logs) > 0 && logs[0].Topics[0] != hash3 {
198-
t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0])
199-
}
200-
201-
filter = sys.NewRangeFilter(1, 10, nil, [][]common.Hash{{hash1, hash2}})
202-
203-
logs, _ = filter.Logs(context.Background())
204-
if len(logs) != 2 {
205-
t.Error("expected 2 log, got", len(logs))
206-
}
207-
208-
failHash := common.BytesToHash([]byte("fail"))
209-
filter = sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{failHash}})
210-
211-
logs, _ = filter.Logs(context.Background())
212-
if len(logs) != 0 {
213-
t.Error("expected 0 log, got", len(logs))
214-
}
215-
216-
failAddr := common.BytesToAddress([]byte("failmenow"))
217-
filter = sys.NewRangeFilter(0, -1, []common.Address{failAddr}, nil)
218-
219-
logs, _ = filter.Logs(context.Background())
220-
if len(logs) != 0 {
221-
t.Error("expected 0 log, got", len(logs))
222-
}
223-
224-
filter = sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{failHash}, {hash1}})
225-
226-
logs, _ = filter.Logs(context.Background())
227-
if len(logs) != 0 {
228-
t.Error("expected 0 log, got", len(logs))
229-
}
230-
231-
filter = sys.NewRangeFilter(-1, -1, nil, nil)
232-
233-
logs, _ = filter.Logs(context.Background())
234-
if len(logs) != 1 {
235-
t.Error("expected 1 log, got", len(logs))
236-
}
237-
238-
filter = sys.NewRangeFilter(-3, -1, nil, nil)
239-
240-
logs, _ = filter.Logs(context.Background())
241-
if len(logs) != 2 {
242-
t.Error("expected 2 log, got", len(logs))
243-
}
244-
245-
filter = sys.NewRangeFilter(-3, -3, nil, nil)
246-
247-
logs, _ = filter.Logs(context.Background())
248-
if len(logs) != 1 {
249-
t.Error("expected 1 log, got", len(logs))
250-
}
251-
252-
filter = sys.NewRangeFilter(-1, -3, nil, nil)
253-
254-
logs, _ = filter.Logs(context.Background())
255-
if len(logs) != 0 {
256-
t.Error("expected 0 log, got", len(logs))
183+
for i, tc := range []struct {
184+
f *Filter
185+
wantHashes []common.Hash
186+
}{
187+
{
188+
sys.NewRangeFilter(900, 999, []common.Address{addr}, [][]common.Hash{{hash3}}),
189+
[]common.Hash{hash3},
190+
}, {
191+
sys.NewRangeFilter(990, -1, []common.Address{addr}, [][]common.Hash{{hash3}}),
192+
[]common.Hash{hash3},
193+
}, {
194+
sys.NewRangeFilter(1, 10, nil, [][]common.Hash{{hash1, hash2}}),
195+
[]common.Hash{hash1, hash2},
196+
}, {
197+
sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}}),
198+
nil,
199+
}, {
200+
sys.NewRangeFilter(0, -1, []common.Address{common.BytesToAddress([]byte("failmenow"))}, nil),
201+
nil,
202+
}, {
203+
sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}, {hash1}}),
204+
nil,
205+
}, {
206+
sys.NewRangeFilter(-1, -1, nil, nil), []common.Hash{hash4},
207+
}, {
208+
sys.NewRangeFilter(-3, -1, nil, nil), []common.Hash{hash3, hash4},
209+
}, {
210+
sys.NewRangeFilter(-3, -3, nil, nil), []common.Hash{hash3},
211+
}, {
212+
sys.NewRangeFilter(-1, -3, nil, nil), nil,
213+
}, {
214+
sys.NewRangeFilter(-4, -1, nil, nil), nil,
215+
}, {
216+
sys.NewRangeFilter(-4, -4, nil, nil), nil,
217+
}, {
218+
sys.NewRangeFilter(-1, -4, nil, nil), nil,
219+
},
220+
} {
221+
logs, _ := tc.f.Logs(context.Background())
222+
var haveHashes []common.Hash
223+
for _, l := range logs {
224+
haveHashes = append(haveHashes, l.Topics[0])
225+
}
226+
if have, want := len(haveHashes), len(tc.wantHashes); have != want {
227+
t.Fatalf("test %d, have %d logs, want %d", i, have, want)
228+
}
229+
if len(haveHashes) == 0 {
230+
continue
231+
}
232+
if !reflect.DeepEqual(tc.wantHashes, haveHashes) {
233+
t.Fatalf("test %d, have %v want %v", i, haveHashes, tc.wantHashes)
234+
}
257235
}
258236
}

0 commit comments

Comments
 (0)