Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func NewPublicFilterAPI(backend Backend, lightMode bool) *PublicFilterAPI {
filters: make(map[rpc.ID]*filter),
}
go api.timeoutLoop()

return api
}

Expand Down Expand Up @@ -141,7 +140,6 @@ func (api *PublicFilterAPI) NewPendingTransactions(ctx context.Context) (*rpc.Su
if !supported {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}

rpcSub := notifier.CreateSubscription()

go func() {
Expand Down Expand Up @@ -206,7 +204,6 @@ func (api *PublicFilterAPI) NewHeads(ctx context.Context) (*rpc.Subscription, er
if !supported {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}

rpcSub := notifier.CreateSubscription()

go func() {
Expand Down Expand Up @@ -248,7 +245,6 @@ func (api *PublicFilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc
}

go func() {

for {
select {
case logs := <-matchedLogs:
Expand Down Expand Up @@ -336,11 +332,14 @@ func (api *PublicFilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([
}
// Create and run the filter to get all the logs
filter := New(api.backend, crit.FromBlock.Int64(), crit.ToBlock.Int64(), crit.Addresses, crit.Topics)

logs, err := filter.Logs(ctx)
if err != nil {
return nil, err
}
for _, log := range logs {
// update BlockHash to fix #1
log.BlockHash = core.GetCanonicalHash(api.chainDb, log.BlockNumber)
}
return returnLogs(logs), err
}

Expand All @@ -357,7 +356,6 @@ func (api *PublicFilterAPI) UninstallFilter(id rpc.ID) bool {
if found {
f.s.Unsubscribe()
}

return found
}

Expand All @@ -369,11 +367,9 @@ func (api *PublicFilterAPI) GetFilterLogs(ctx context.Context, id rpc.ID) ([]*ty
api.filtersMu.Lock()
f, found := api.filters[id]
api.filtersMu.Unlock()

if !found || f.typ != LogsSubscription {
return nil, fmt.Errorf("filter not found")
}

begin := rpc.LatestBlockNumber.Int64()
if f.crit.FromBlock != nil {
begin = f.crit.FromBlock.Int64()
Expand All @@ -384,11 +380,14 @@ func (api *PublicFilterAPI) GetFilterLogs(ctx context.Context, id rpc.ID) ([]*ty
}
// Create and run the filter to get all the logs
filter := New(api.backend, begin, end, f.crit.Addresses, f.crit.Topics)

logs, err := filter.Logs(ctx)
if err != nil {
return nil, err
}
for _, log := range logs {
// update BlockHash to fix #1
log.BlockHash = core.GetCanonicalHash(api.chainDb, log.BlockNumber)
}
return returnLogs(logs), nil
}

Expand Down Expand Up @@ -426,7 +425,6 @@ func (api *PublicFilterAPI) GetFilterChanges(id rpc.ID) (interface{}, error) {
return returnLogs(logs), nil
}
}

return []interface{}{}, fmt.Errorf("filter not found")
}

Expand Down Expand Up @@ -465,13 +463,11 @@ func (args *FilterCriteria) UnmarshalJSON(data []byte) error {
if raw.From != nil {
args.FromBlock = big.NewInt(raw.From.Int64())
}

if raw.ToBlock != nil {
args.ToBlock = big.NewInt(raw.ToBlock.Int64())
}

args.Addresses = []common.Address{}

if raw.Addresses != nil {
// raw.Address can contain a single address or an array of addresses
switch rawAddr := raw.Addresses.(type) {
Expand Down Expand Up @@ -506,15 +502,13 @@ func (args *FilterCriteria) UnmarshalJSON(data []byte) error {
switch topic := t.(type) {
case nil:
// ignore topic when matching logs

case string:
// match specific topic
top, err := decodeTopic(topic)
if err != nil {
return err
}
args.Topics[i] = []common.Hash{top}

case []interface{}:
// or case e.g. [null, "topic0", "topic1"]
for _, rawTopic := range topic {
Expand All @@ -541,9 +535,11 @@ func (args *FilterCriteria) UnmarshalJSON(data []byte) error {

return nil
}

func hasXDCPrefix(str string) bool {
return len(str) >= 3 && (str[0] == 'x' || str[0] == 'X') && (str[1] == 'd' || str[1] == 'D') && (str[2] == 'c' || str[2] == 'C')
}

func decodeAddress(s string) (common.Address, error) {
if hasXDCPrefix(s) {
s = "0x" + s[3:]
Expand All @@ -561,4 +557,4 @@ func decodeTopic(s string) (common.Hash, error) {
err = fmt.Errorf("hex has invalid length %d after decoding", len(b))
}
return common.BytesToHash(b), err
}
}