2018-10-23 05:11:11 +00:00
|
|
|
package rpcfilters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-10-23 05:11:11 +00:00
|
|
|
ethereum "github.com/ethereum/go-ethereum"
|
2018-10-28 16:12:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-10-23 05:11:11 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type callTracker struct {
|
2019-06-12 09:16:00 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
calls int
|
|
|
|
reply [][]types.Log
|
|
|
|
criteria []map[string]interface{}
|
2018-10-23 05:11:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *callTracker) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
c.calls++
|
|
|
|
if len(args) != 1 {
|
|
|
|
return errors.New("unexpected length of args")
|
|
|
|
}
|
|
|
|
crit := args[0].(map[string]interface{})
|
2019-06-12 09:16:00 +00:00
|
|
|
c.criteria = append(c.criteria, crit)
|
2018-10-23 05:11:11 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return errors.New("context canceled")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if c.calls <= len(c.reply) {
|
|
|
|
rst := result.(*[]types.Log)
|
|
|
|
*rst = c.reply[c.calls-1]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-28 16:12:16 +00:00
|
|
|
func runLogsFetcherTest(t *testing.T, f *logsFilter, replies [][]types.Log, queries int) *callTracker {
|
|
|
|
c := callTracker{reply: replies}
|
2018-10-23 05:11:11 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
pollLogs(&c, f, time.Second, 100*time.Millisecond)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
tick := time.Tick(10 * time.Millisecond)
|
|
|
|
after := time.After(time.Second)
|
|
|
|
func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-after:
|
|
|
|
f.stop()
|
|
|
|
assert.FailNow(t, "failed waiting for requests")
|
|
|
|
return
|
|
|
|
case <-tick:
|
|
|
|
c.mu.Lock()
|
|
|
|
num := c.calls
|
|
|
|
c.mu.Unlock()
|
2018-10-28 16:12:16 +00:00
|
|
|
if num >= queries {
|
2018-10-23 05:11:11 +00:00
|
|
|
f.stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
2019-06-12 09:16:00 +00:00
|
|
|
require.Len(t, c.criteria, queries)
|
2018-10-23 05:11:11 +00:00
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogsFetcherAdjusted(t *testing.T) {
|
|
|
|
f := &logsFilter{
|
|
|
|
ctx: context.TODO(),
|
|
|
|
crit: ethereum.FilterQuery{
|
|
|
|
FromBlock: big.NewInt(10),
|
|
|
|
},
|
2018-10-28 16:12:16 +00:00
|
|
|
done: make(chan struct{}),
|
|
|
|
logsCache: newCache(defaultCacheSize),
|
2018-10-23 05:11:11 +00:00
|
|
|
}
|
2018-10-28 16:12:16 +00:00
|
|
|
logs := []types.Log{
|
|
|
|
{BlockNumber: 11}, {BlockNumber: 12},
|
|
|
|
}
|
|
|
|
c := runLogsFetcherTest(t, f, [][]types.Log{logs}, 2)
|
2019-06-12 09:16:00 +00:00
|
|
|
require.Equal(t, hexutil.EncodeBig(big.NewInt(10)), c.criteria[0]["fromBlock"])
|
|
|
|
require.Equal(t, c.criteria[1]["fromBlock"], "latest")
|
2018-10-23 05:11:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-28 16:12:16 +00:00
|
|
|
func TestAdjustedDueToReorg(t *testing.T) {
|
|
|
|
f := &logsFilter{
|
|
|
|
ctx: context.TODO(),
|
|
|
|
crit: ethereum.FilterQuery{
|
|
|
|
FromBlock: big.NewInt(10),
|
|
|
|
},
|
|
|
|
done: make(chan struct{}),
|
|
|
|
logsCache: newCache(defaultCacheSize),
|
|
|
|
}
|
|
|
|
logs := []types.Log{
|
|
|
|
{BlockNumber: 11, BlockHash: common.Hash{1}}, {BlockNumber: 12, BlockHash: common.Hash{2}},
|
|
|
|
}
|
|
|
|
reorg := []types.Log{
|
|
|
|
{BlockNumber: 12, BlockHash: common.Hash{2, 2}},
|
|
|
|
}
|
|
|
|
c := runLogsFetcherTest(t, f, [][]types.Log{logs, reorg}, 3)
|
2019-06-12 09:16:00 +00:00
|
|
|
require.Equal(t, hexutil.EncodeBig(big.NewInt(10)), c.criteria[0]["fromBlock"])
|
|
|
|
require.Equal(t, "latest", c.criteria[1]["fromBlock"])
|
|
|
|
require.Equal(t, hexutil.EncodeBig(big.NewInt(11)), c.criteria[2]["fromBlock"])
|
2018-10-28 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 05:11:11 +00:00
|
|
|
func TestLogsFetcherCanceledContext(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
f := &logsFilter{
|
|
|
|
ctx: ctx,
|
|
|
|
crit: ethereum.FilterQuery{
|
|
|
|
FromBlock: big.NewInt(10),
|
|
|
|
},
|
2018-10-28 16:12:16 +00:00
|
|
|
done: make(chan struct{}),
|
|
|
|
logsCache: newCache(defaultCacheSize),
|
2018-10-23 05:11:11 +00:00
|
|
|
}
|
|
|
|
cancel()
|
2018-10-28 16:12:16 +00:00
|
|
|
c := runLogsFetcherTest(t, f, [][]types.Log{make([]types.Log, 2)}, 2)
|
2019-06-12 09:16:00 +00:00
|
|
|
require.Equal(t, hexutil.EncodeBig(big.NewInt(10)), c.criteria[0]["fromBlock"])
|
|
|
|
require.Equal(t, hexutil.EncodeBig(big.NewInt(10)), c.criteria[1]["fromBlock"])
|
2018-10-23 05:11:11 +00:00
|
|
|
}
|