2015-10-15 09:07:19 -05:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2016-04-14 11:18:24 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-10-15 09:07:19 -05:00
|
|
|
//
|
2016-04-14 11:18:24 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
2015-10-15 09:07:19 -05:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2016-04-14 11:18:24 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-10-15 09:07:19 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-04-14 11:18:24 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
2015-10-15 09:07:19 -05:00
|
|
|
//
|
2016-04-14 11:18:24 -05:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-10-15 09:07:19 -05:00
|
|
|
|
|
|
|
package filters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2016-03-29 08:07:40 -05:00
|
|
|
"errors"
|
2015-10-15 09:07:19 -05:00
|
|
|
"fmt"
|
2016-07-27 10:47:46 -05:00
|
|
|
"math/big"
|
2016-03-29 08:07:40 -05:00
|
|
|
"sync"
|
|
|
|
"time"
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-10-15 09:07:19 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2015-12-16 03:58:01 -06:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2015-10-15 09:07:19 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-07-27 10:47:46 -05:00
|
|
|
deadline = 5 * time.Minute // consider a filter inactive if it has not been polled for within deadline
|
2015-10-15 09:07:19 -05:00
|
|
|
)
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// filter is a helper struct that holds meta information over the filter type
|
|
|
|
// and associated subscription in the event system.
|
|
|
|
type filter struct {
|
|
|
|
typ Type
|
|
|
|
deadline *time.Timer // filter is inactiv when deadline triggers
|
|
|
|
hashes []common.Hash
|
|
|
|
crit FilterCriteria
|
|
|
|
logs []Log
|
|
|
|
s *Subscription // associated subscription in event system
|
|
|
|
}
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-03-15 13:27:49 -05:00
|
|
|
// PublicFilterAPI offers support to create and manage filters. This will allow external clients to retrieve various
|
2015-10-15 09:07:19 -05:00
|
|
|
// information related to the Ethereum protocol such als blocks, transactions and logs.
|
|
|
|
type PublicFilterAPI struct {
|
2016-07-27 10:47:46 -05:00
|
|
|
mux *event.TypeMux
|
|
|
|
quit chan struct{}
|
|
|
|
chainDb ethdb.Database
|
|
|
|
events *EventSystem
|
|
|
|
filtersMu sync.Mutex
|
|
|
|
filters map[rpc.ID]*filter
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPublicFilterAPI returns a new PublicFilterAPI instance.
|
|
|
|
func NewPublicFilterAPI(chainDb ethdb.Database, mux *event.TypeMux) *PublicFilterAPI {
|
2016-07-27 10:47:46 -05:00
|
|
|
api := &PublicFilterAPI{
|
|
|
|
mux: mux,
|
|
|
|
chainDb: chainDb,
|
|
|
|
events: NewEventSystem(mux),
|
|
|
|
filters: make(map[rpc.ID]*filter),
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
go api.timeoutLoop()
|
|
|
|
|
|
|
|
return api
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// timeoutLoop runs every 5 minutes and deletes filters that have not been recently used.
|
|
|
|
// Tt is started when the api is created.
|
|
|
|
func (api *PublicFilterAPI) timeoutLoop() {
|
|
|
|
ticker := time.NewTicker(5 * time.Minute)
|
2015-10-15 09:07:19 -05:00
|
|
|
for {
|
2016-07-27 10:47:46 -05:00
|
|
|
<-ticker.C
|
|
|
|
api.filtersMu.Lock()
|
|
|
|
for id, f := range api.filters {
|
|
|
|
select {
|
|
|
|
case <-f.deadline.C:
|
|
|
|
f.s.Unsubscribe()
|
|
|
|
delete(api.filters, id)
|
|
|
|
default:
|
|
|
|
continue
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
2016-07-27 10:47:46 -05:00
|
|
|
}
|
|
|
|
api.filtersMu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// NewPendingTransactionFilter creates a filter that fetches pending transaction hashes
|
|
|
|
// as transactions enter the pending state.
|
|
|
|
//
|
|
|
|
// It is part of the filter package because this filter can be used throug the
|
|
|
|
// `eth_getFilterChanges` polling method that is also used for log filters.
|
|
|
|
//
|
|
|
|
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newpendingtransactionfilter
|
|
|
|
func (api *PublicFilterAPI) NewPendingTransactionFilter() rpc.ID {
|
|
|
|
var (
|
|
|
|
pendingTxs = make(chan common.Hash)
|
|
|
|
pendingTxSub = api.events.SubscribePendingTxEvents(pendingTxs)
|
|
|
|
)
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
api.filtersMu.Lock()
|
|
|
|
api.filters[pendingTxSub.ID] = &filter{typ: PendingTransactionsSubscription, deadline: time.NewTimer(deadline), hashes: make([]common.Hash, 0), s: pendingTxSub}
|
|
|
|
api.filtersMu.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ph := <-pendingTxs:
|
|
|
|
api.filtersMu.Lock()
|
|
|
|
if f, found := api.filters[pendingTxSub.ID]; found {
|
|
|
|
f.hashes = append(f.hashes, ph)
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
2016-07-27 10:47:46 -05:00
|
|
|
api.filtersMu.Unlock()
|
|
|
|
case <-pendingTxSub.Err():
|
|
|
|
api.filtersMu.Lock()
|
|
|
|
delete(api.filters, pendingTxSub.ID)
|
|
|
|
api.filtersMu.Unlock()
|
|
|
|
return
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
}
|
2016-07-27 10:47:46 -05:00
|
|
|
}()
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
return pendingTxSub.ID
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// NewPendingTransactions creates a subscription that is triggered each time a transaction
|
|
|
|
// enters the transaction pool and was signed from one of the transactions this nodes manages.
|
|
|
|
func (api *PublicFilterAPI) NewPendingTransactions(ctx context.Context) (*rpc.Subscription, error) {
|
|
|
|
notifier, supported := rpc.NotifierFromContext(ctx)
|
|
|
|
if !supported {
|
|
|
|
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
rpcSub := notifier.CreateSubscription()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
txHashes := make(chan common.Hash)
|
|
|
|
pendingTxSub := api.events.SubscribePendingTxEvents(txHashes)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case h := <-txHashes:
|
|
|
|
notifier.Notify(rpcSub.ID, h)
|
|
|
|
case <-rpcSub.Err():
|
|
|
|
pendingTxSub.Unsubscribe()
|
|
|
|
return
|
|
|
|
case <-notifier.Closed():
|
|
|
|
pendingTxSub.Unsubscribe()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-02-12 18:40:44 -06:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
return rpcSub, nil
|
|
|
|
}
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// NewBlockFilter creates a filter that fetches blocks that are imported into the chain.
|
|
|
|
// It is part of the filter package since polling goes with eth_getFilterChanges.
|
|
|
|
//
|
|
|
|
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newblockfilter
|
|
|
|
func (api *PublicFilterAPI) NewBlockFilter() rpc.ID {
|
|
|
|
var (
|
|
|
|
headers = make(chan *types.Header)
|
|
|
|
headerSub = api.events.SubscribeNewHeads(headers)
|
|
|
|
)
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
api.filtersMu.Lock()
|
|
|
|
api.filters[headerSub.ID] = &filter{typ: BlocksSubscription, deadline: time.NewTimer(deadline), hashes: make([]common.Hash, 0), s: headerSub}
|
|
|
|
api.filtersMu.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case h := <-headers:
|
|
|
|
api.filtersMu.Lock()
|
|
|
|
if f, found := api.filters[headerSub.ID]; found {
|
|
|
|
f.hashes = append(f.hashes, h.Hash())
|
|
|
|
}
|
|
|
|
api.filtersMu.Unlock()
|
|
|
|
case <-headerSub.Err():
|
|
|
|
api.filtersMu.Lock()
|
|
|
|
delete(api.filters, headerSub.ID)
|
|
|
|
api.filtersMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
2016-07-27 10:47:46 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
return headerSub.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHeads send a notification each time a new (header) block is appended to the chain.
|
|
|
|
func (api *PublicFilterAPI) NewHeads(ctx context.Context) (*rpc.Subscription, error) {
|
|
|
|
notifier, supported := rpc.NotifierFromContext(ctx)
|
|
|
|
if !supported {
|
|
|
|
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
rpcSub := notifier.CreateSubscription()
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
go func() {
|
|
|
|
headers := make(chan *types.Header)
|
|
|
|
headersSub := api.events.SubscribeNewHeads(headers)
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case h := <-headers:
|
|
|
|
notifier.Notify(rpcSub.ID, h)
|
|
|
|
case <-rpcSub.Err():
|
|
|
|
headersSub.Unsubscribe()
|
|
|
|
return
|
|
|
|
case <-notifier.Closed():
|
|
|
|
headersSub.Unsubscribe()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-06-17 02:53:54 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
return rpcSub, nil
|
|
|
|
}
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// Logs creates a subscription that fires for all new log that match the given filter criteria.
|
|
|
|
func (api *PublicFilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subscription, error) {
|
|
|
|
notifier, supported := rpc.NotifierFromContext(ctx)
|
|
|
|
if !supported {
|
|
|
|
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
|
2016-02-12 18:40:44 -06:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
rpcSub := notifier.CreateSubscription()
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
go func() {
|
|
|
|
matchedLogs := make(chan []Log)
|
|
|
|
logsSub := api.events.SubscribeLogs(crit, matchedLogs)
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case logs := <-matchedLogs:
|
|
|
|
for _, log := range logs {
|
|
|
|
notifier.Notify(rpcSub.ID, &log)
|
|
|
|
}
|
|
|
|
case <-rpcSub.Err(): // client send an unsubscribe request
|
|
|
|
logsSub.Unsubscribe()
|
|
|
|
return
|
|
|
|
case <-notifier.Closed(): // connection dropped
|
|
|
|
logsSub.Unsubscribe()
|
|
|
|
return
|
|
|
|
}
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
2016-07-27 10:47:46 -05:00
|
|
|
}()
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
return rpcSub, nil
|
|
|
|
}
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// FilterCriteria represents a request to create a new filter.
|
|
|
|
type FilterCriteria struct {
|
|
|
|
FromBlock *big.Int
|
|
|
|
ToBlock *big.Int
|
|
|
|
Addresses []common.Address
|
|
|
|
Topics [][]common.Hash
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// NewFilter creates a new filter and returns the filter id. It can be
|
|
|
|
// used to retrieve logs when the state changes. This method cannot be
|
|
|
|
// used to fetch logs that are already stored in the state.
|
|
|
|
//
|
|
|
|
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newfilter
|
|
|
|
func (api *PublicFilterAPI) NewFilter(crit FilterCriteria) rpc.ID {
|
|
|
|
var (
|
|
|
|
logs = make(chan []Log)
|
|
|
|
logsSub = api.events.SubscribeLogs(crit, logs)
|
|
|
|
)
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
if crit.FromBlock == nil {
|
|
|
|
crit.FromBlock = big.NewInt(rpc.LatestBlockNumber.Int64())
|
|
|
|
}
|
|
|
|
if crit.ToBlock == nil {
|
|
|
|
crit.ToBlock = big.NewInt(rpc.LatestBlockNumber.Int64())
|
2016-02-12 18:40:44 -06:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
api.filtersMu.Lock()
|
|
|
|
api.filters[logsSub.ID] = &filter{typ: LogsSubscription, crit: crit, deadline: time.NewTimer(deadline), logs: make([]Log, 0), s: logsSub}
|
|
|
|
api.filtersMu.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case l := <-logs:
|
|
|
|
api.filtersMu.Lock()
|
|
|
|
if f, found := api.filters[logsSub.ID]; found {
|
|
|
|
f.logs = append(f.logs, l...)
|
|
|
|
}
|
|
|
|
api.filtersMu.Unlock()
|
|
|
|
case <-logsSub.Err():
|
|
|
|
api.filtersMu.Lock()
|
|
|
|
delete(api.filters, logsSub.ID)
|
|
|
|
api.filtersMu.Unlock()
|
|
|
|
return
|
2016-03-29 08:07:40 -05:00
|
|
|
}
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
2016-07-27 10:47:46 -05:00
|
|
|
}()
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
return logsSub.ID
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// GetLogs returns logs matching the given argument that are stored within the state.
|
|
|
|
//
|
|
|
|
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getlogs
|
|
|
|
func (api *PublicFilterAPI) GetLogs(crit FilterCriteria) []Log {
|
|
|
|
if crit.FromBlock == nil {
|
|
|
|
crit.FromBlock = big.NewInt(rpc.LatestBlockNumber.Int64())
|
|
|
|
}
|
|
|
|
if crit.ToBlock == nil {
|
|
|
|
crit.ToBlock = big.NewInt(rpc.LatestBlockNumber.Int64())
|
2016-03-29 08:07:40 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
filter := New(api.chainDb)
|
|
|
|
filter.SetBeginBlock(crit.FromBlock.Int64())
|
|
|
|
filter.SetEndBlock(crit.ToBlock.Int64())
|
|
|
|
filter.SetAddresses(crit.Addresses)
|
|
|
|
filter.SetTopics(crit.Topics)
|
2016-03-29 08:07:40 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
return returnLogs(filter.Find())
|
|
|
|
}
|
2016-03-29 08:07:40 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// UninstallFilter removes the filter with the given filter id.
|
|
|
|
//
|
|
|
|
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_uninstallfilter
|
|
|
|
func (api *PublicFilterAPI) UninstallFilter(id rpc.ID) bool {
|
|
|
|
api.filtersMu.Lock()
|
|
|
|
f, found := api.filters[id]
|
|
|
|
if found {
|
|
|
|
delete(api.filters, id)
|
2016-03-29 08:07:40 -05:00
|
|
|
}
|
2016-07-27 10:47:46 -05:00
|
|
|
api.filtersMu.Unlock()
|
|
|
|
if found {
|
|
|
|
f.s.Unsubscribe()
|
2016-03-29 08:07:40 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFilterLogs returns the logs for the filter with the given id.
|
|
|
|
// If the filter could not be found an empty array of logs is returned.
|
|
|
|
//
|
|
|
|
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getfilterlogs
|
|
|
|
func (api *PublicFilterAPI) GetFilterLogs(id rpc.ID) []Log {
|
|
|
|
api.filtersMu.Lock()
|
|
|
|
f, found := api.filters[id]
|
|
|
|
api.filtersMu.Unlock()
|
|
|
|
|
|
|
|
if !found || f.typ != LogsSubscription {
|
|
|
|
return []Log{}
|
2016-03-29 08:07:40 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
filter := New(api.chainDb)
|
|
|
|
filter.SetBeginBlock(f.crit.FromBlock.Int64())
|
|
|
|
filter.SetEndBlock(f.crit.ToBlock.Int64())
|
|
|
|
filter.SetAddresses(f.crit.Addresses)
|
|
|
|
filter.SetTopics(f.crit.Topics)
|
|
|
|
|
|
|
|
return returnLogs(filter.Find())
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFilterChanges returns the logs for the filter with the given id since
|
|
|
|
// last time is was called. This can be used for polling.
|
|
|
|
//
|
|
|
|
// For pending transaction and block filters the result is []common.Hash.
|
|
|
|
// (pending)Log filters return []Log. If the filter could not be found
|
|
|
|
// []interface{}{} is returned.
|
|
|
|
//
|
|
|
|
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getfilterchanges
|
|
|
|
func (api *PublicFilterAPI) GetFilterChanges(id rpc.ID) interface{} {
|
|
|
|
api.filtersMu.Lock()
|
|
|
|
defer api.filtersMu.Unlock()
|
|
|
|
|
|
|
|
if f, found := api.filters[id]; found {
|
|
|
|
if !f.deadline.Stop() {
|
|
|
|
// timer expired but filter is not yet removed in timeout loop
|
|
|
|
// receive timer value and reset timer
|
|
|
|
<-f.deadline.C
|
|
|
|
}
|
|
|
|
f.deadline.Reset(deadline)
|
|
|
|
|
|
|
|
switch f.typ {
|
|
|
|
case PendingTransactionsSubscription, BlocksSubscription:
|
|
|
|
hashes := f.hashes
|
|
|
|
f.hashes = nil
|
|
|
|
return returnHashes(hashes)
|
|
|
|
case PendingLogsSubscription, LogsSubscription:
|
|
|
|
logs := f.logs
|
|
|
|
f.logs = nil
|
|
|
|
return returnLogs(logs)
|
|
|
|
}
|
2016-03-29 08:07:40 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
return []interface{}{}
|
|
|
|
}
|
2016-03-29 08:07:40 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// returnHashes is a helper that will return an empty hash array case the given hash array is nil,
|
|
|
|
// otherwise the given hashes array is returned.
|
|
|
|
func returnHashes(hashes []common.Hash) []common.Hash {
|
|
|
|
if hashes == nil {
|
|
|
|
return []common.Hash{}
|
|
|
|
}
|
|
|
|
return hashes
|
2016-03-29 08:07:40 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// returnLogs is a helper that will return an empty log array in case the given logs array is nil,
|
|
|
|
// otherwise the given logs array is returned.
|
|
|
|
func returnLogs(logs []Log) []Log {
|
|
|
|
if logs == nil {
|
|
|
|
return []Log{}
|
|
|
|
}
|
|
|
|
return logs
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
2016-05-17 09:05:12 -05:00
|
|
|
// UnmarshalJSON sets *args fields with given data.
|
2016-07-27 10:47:46 -05:00
|
|
|
func (args *FilterCriteria) UnmarshalJSON(data []byte) error {
|
2015-10-15 09:07:19 -05:00
|
|
|
type input struct {
|
|
|
|
From *rpc.BlockNumber `json:"fromBlock"`
|
|
|
|
ToBlock *rpc.BlockNumber `json:"toBlock"`
|
|
|
|
Addresses interface{} `json:"address"`
|
2016-05-17 09:05:12 -05:00
|
|
|
Topics []interface{} `json:"topics"`
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var raw input
|
|
|
|
if err := json.Unmarshal(data, &raw); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-27 10:01:30 -06:00
|
|
|
if raw.From == nil || raw.From.Int64() < 0 {
|
2016-07-27 10:47:46 -05:00
|
|
|
args.FromBlock = big.NewInt(rpc.LatestBlockNumber.Int64())
|
2015-10-15 09:07:19 -05:00
|
|
|
} else {
|
2016-07-27 10:47:46 -05:00
|
|
|
args.FromBlock = big.NewInt(raw.From.Int64())
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
2016-01-27 10:01:30 -06:00
|
|
|
if raw.ToBlock == nil || raw.ToBlock.Int64() < 0 {
|
2016-07-27 10:47:46 -05:00
|
|
|
args.ToBlock = big.NewInt(rpc.LatestBlockNumber.Int64())
|
2015-10-15 09:07:19 -05:00
|
|
|
} else {
|
2016-07-27 10:47:46 -05:00
|
|
|
args.ToBlock = big.NewInt(raw.ToBlock.Int64())
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
args.Addresses = []common.Address{}
|
|
|
|
|
|
|
|
if raw.Addresses != nil {
|
|
|
|
// raw.Address can contain a single address or an array of addresses
|
|
|
|
var addresses []common.Address
|
|
|
|
if strAddrs, ok := raw.Addresses.([]interface{}); ok {
|
|
|
|
for i, addr := range strAddrs {
|
|
|
|
if strAddr, ok := addr.(string); ok {
|
|
|
|
if len(strAddr) >= 2 && strAddr[0] == '0' && (strAddr[1] == 'x' || strAddr[1] == 'X') {
|
|
|
|
strAddr = strAddr[2:]
|
|
|
|
}
|
|
|
|
if decAddr, err := hex.DecodeString(strAddr); err == nil {
|
|
|
|
addresses = append(addresses, common.BytesToAddress(decAddr))
|
|
|
|
} else {
|
2016-04-15 04:06:57 -05:00
|
|
|
return fmt.Errorf("invalid address given")
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("invalid address on index %d", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if singleAddr, ok := raw.Addresses.(string); ok {
|
|
|
|
if len(singleAddr) >= 2 && singleAddr[0] == '0' && (singleAddr[1] == 'x' || singleAddr[1] == 'X') {
|
|
|
|
singleAddr = singleAddr[2:]
|
|
|
|
}
|
|
|
|
if decAddr, err := hex.DecodeString(singleAddr); err == nil {
|
|
|
|
addresses = append(addresses, common.BytesToAddress(decAddr))
|
|
|
|
} else {
|
2016-04-15 04:06:57 -05:00
|
|
|
return fmt.Errorf("invalid address given")
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
} else {
|
2016-04-15 04:06:57 -05:00
|
|
|
return errors.New("invalid address(es) given")
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
args.Addresses = addresses
|
|
|
|
}
|
|
|
|
|
2016-05-17 09:05:12 -05:00
|
|
|
// helper function which parses a string to a topic hash
|
2015-10-15 09:07:19 -05:00
|
|
|
topicConverter := func(raw string) (common.Hash, error) {
|
|
|
|
if len(raw) == 0 {
|
|
|
|
return common.Hash{}, nil
|
|
|
|
}
|
|
|
|
if len(raw) >= 2 && raw[0] == '0' && (raw[1] == 'x' || raw[1] == 'X') {
|
|
|
|
raw = raw[2:]
|
|
|
|
}
|
2016-04-05 08:22:04 -05:00
|
|
|
if len(raw) != 2*common.HashLength {
|
2016-05-17 09:05:12 -05:00
|
|
|
return common.Hash{}, errors.New("invalid topic(s)")
|
|
|
|
}
|
2015-10-15 09:07:19 -05:00
|
|
|
if decAddr, err := hex.DecodeString(raw); err == nil {
|
|
|
|
return common.BytesToHash(decAddr), nil
|
|
|
|
}
|
2016-05-17 09:05:12 -05:00
|
|
|
return common.Hash{}, errors.New("invalid topic(s)")
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
2016-05-17 09:05:12 -05:00
|
|
|
// topics is an array consisting of strings and/or arrays of strings.
|
|
|
|
// JSON null values are converted to common.Hash{} and ignored by the filter manager.
|
|
|
|
if len(raw.Topics) > 0 {
|
|
|
|
args.Topics = make([][]common.Hash, len(raw.Topics))
|
|
|
|
for i, t := range raw.Topics {
|
|
|
|
if t == nil { // ignore topic when matching logs
|
|
|
|
args.Topics[i] = []common.Hash{common.Hash{}}
|
|
|
|
} else if topic, ok := t.(string); ok { // match specific topic
|
|
|
|
top, err := topicConverter(topic)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args.Topics[i] = []common.Hash{top}
|
|
|
|
} else if topics, ok := t.([]interface{}); ok { // or case e.g. [null, "topic0", "topic1"]
|
|
|
|
for _, rawTopic := range topics {
|
|
|
|
if rawTopic == nil {
|
|
|
|
args.Topics[i] = append(args.Topics[i], common.Hash{})
|
|
|
|
} else if topic, ok := rawTopic.(string); ok {
|
|
|
|
parsed, err := topicConverter(topic)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
2016-05-17 09:05:12 -05:00
|
|
|
args.Topics[i] = append(args.Topics[i], parsed)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("invalid topic(s)")
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-17 09:05:12 -05:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("invalid topic(s)")
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|