2018-10-23 05:11:11 +00:00
|
|
|
package rpcfilters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
ethereum "github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ContextCaller provides CallContext method as ethereums rpc.Client.
|
|
|
|
type ContextCaller interface {
|
|
|
|
CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func pollLogs(client ContextCaller, f *logsFilter, timeout, period time.Duration) {
|
|
|
|
query := func() {
|
|
|
|
ctx, cancel := context.WithTimeout(f.ctx, timeout)
|
2018-10-28 16:12:16 +00:00
|
|
|
defer cancel()
|
|
|
|
logs, err := getLogs(ctx, client, f.criteria())
|
2018-10-23 05:11:11 +00:00
|
|
|
if err != nil {
|
2018-10-28 16:12:16 +00:00
|
|
|
log.Error("Error fetch logs", "criteria", f.crit, "error", err)
|
|
|
|
return
|
2018-10-23 05:11:11 +00:00
|
|
|
}
|
|
|
|
if err := f.add(logs); err != nil {
|
2018-10-28 16:12:16 +00:00
|
|
|
log.Error("Error adding logs", "logs", logs, "error", err)
|
2018-10-23 05:11:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
query()
|
|
|
|
latest := time.NewTicker(period)
|
|
|
|
defer latest.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-latest.C:
|
|
|
|
query()
|
|
|
|
case <-f.done:
|
2018-10-28 16:12:16 +00:00
|
|
|
log.Debug("Filter was stopped", "ID", f.id, "crit", f.crit)
|
2018-10-23 05:11:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func getLogs(ctx context.Context, client ContextCaller, crit ethereum.FilterQuery) (rst []types.Log, err error) {
|
|
|
|
return rst, client.CallContext(ctx, &rst, "eth_getLogs", toFilterArg(crit))
|
|
|
|
}
|
|
|
|
|
|
|
|
func toFilterArg(q ethereum.FilterQuery) interface{} {
|
|
|
|
arg := map[string]interface{}{
|
|
|
|
"fromBlock": toBlockNumArg(q.FromBlock),
|
|
|
|
"toBlock": toBlockNumArg(q.ToBlock),
|
|
|
|
"address": q.Addresses,
|
|
|
|
"topics": q.Topics,
|
|
|
|
}
|
2018-11-06 06:41:36 +00:00
|
|
|
if q.FromBlock == nil {
|
|
|
|
arg["fromBlock"] = "0x0"
|
|
|
|
}
|
2018-10-23 05:11:11 +00:00
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
|
|
|
func toBlockNumArg(number *big.Int) string {
|
|
|
|
if number == nil || number.Int64() == rpc.LatestBlockNumber.Int64() {
|
|
|
|
return "latest"
|
|
|
|
} else if number.Int64() == rpc.PendingBlockNumber.Int64() {
|
|
|
|
return "pending"
|
|
|
|
}
|
|
|
|
return hexutil.EncodeBig(number)
|
|
|
|
}
|