Skip to content

Commit 637b241

Browse files
committed
Merge pull request #864 from obscuren/filter_changes
xeth, core, event/filter, rpc: new block and transaction filters
2 parents 69aac4d + 60b5a94 commit 637b241

File tree

6 files changed

+221
-83
lines changed

6 files changed

+221
-83
lines changed

core/filter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ type Filter struct {
2222
max int
2323
topics [][]common.Hash
2424

25-
BlockCallback func(*types.Block, state.Logs)
26-
PendingCallback func(*types.Transaction)
27-
LogsCallback func(state.Logs)
25+
BlockCallback func(*types.Block, state.Logs)
26+
TransactionCallback func(*types.Transaction)
27+
LogsCallback func(state.Logs)
2828
}
2929

3030
// Create a new filter which uses a bloom filter on blocks to figure out whether a particular block

core/transaction_pool.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,27 @@ func (self *TxPool) AddTransactions(txs []*types.Transaction) {
204204
}
205205
}
206206

207+
// GetTransaction allows you to check the pending and queued transaction in the
208+
// transaction pool.
209+
// It has two stategies, first check the pool (map) then check the queue
210+
func (tp *TxPool) GetTransaction(hash common.Hash) *types.Transaction {
211+
// check the txs first
212+
if tx, ok := tp.txs[hash]; ok {
213+
return tx
214+
}
215+
216+
// check queue
217+
for _, txs := range tp.queue {
218+
for _, tx := range txs {
219+
if tx.Hash() == hash {
220+
return tx
221+
}
222+
}
223+
}
224+
225+
return nil
226+
}
227+
207228
func (self *TxPool) GetTransactions() (txs types.Transactions) {
208229
self.mu.RLock()
209230
defer self.mu.RUnlock()

event/filter/eth_filter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ out:
8888
case core.TxPreEvent:
8989
self.filterMu.RLock()
9090
for _, filter := range self.filters {
91-
if filter.PendingCallback != nil {
92-
filter.PendingCallback(event.Tx)
91+
if filter.TransactionCallback != nil {
92+
filter.TransactionCallback(event.Tx)
9393
}
9494
}
9595
self.filterMu.RUnlock()

rpc/api.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,13 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
322322
return err
323323
}
324324

325-
id := api.xeth().RegisterFilter(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics)
325+
id := api.xeth().NewLogFilter(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics)
326326
*reply = newHexNum(big.NewInt(int64(id)).Bytes())
327+
327328
case "eth_newBlockFilter":
328-
args := new(FilterStringArgs)
329-
if err := json.Unmarshal(req.Params, &args); err != nil {
330-
return err
331-
}
332-
*reply = newHexNum(api.xeth().NewFilterString(args.Word))
329+
*reply = newHexNum(api.xeth().NewBlockFilter())
330+
case "eth_newPendingTransactionFilter":
331+
*reply = newHexNum(api.xeth().NewTransactionFilter())
333332
case "eth_uninstallFilter":
334333
args := new(FilterIdArgs)
335334
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -341,7 +340,17 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
341340
if err := json.Unmarshal(req.Params, &args); err != nil {
342341
return err
343342
}
344-
*reply = NewLogsRes(api.xeth().FilterChanged(args.Id))
343+
344+
switch api.xeth().GetFilterType(args.Id) {
345+
case xeth.BlockFilterTy:
346+
*reply = NewHashesRes(api.xeth().BlockFilterChanged(args.Id))
347+
case xeth.TransactionFilterTy:
348+
*reply = NewHashesRes(api.xeth().TransactionFilterChanged(args.Id))
349+
case xeth.LogFilterTy:
350+
*reply = NewLogsRes(api.xeth().LogFilterChanged(args.Id))
351+
default:
352+
*reply = []string{} // reply empty string slice
353+
}
345354
case "eth_getFilterLogs":
346355
args := new(FilterIdArgs)
347356
if err := json.Unmarshal(req.Params, &args); err != nil {

rpc/responses.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rpc
33
import (
44
"encoding/json"
55

6+
"github.com/ethereum/go-ethereum/common"
67
"github.com/ethereum/go-ethereum/core/state"
78
"github.com/ethereum/go-ethereum/core/types"
89
)
@@ -303,3 +304,13 @@ func NewLogsRes(logs state.Logs) (ls []LogRes) {
303304

304305
return
305306
}
307+
308+
func NewHashesRes(hs []common.Hash) []string {
309+
hashes := make([]string, len(hs))
310+
311+
for i, hash := range hs {
312+
hashes[i] = hash.Hex()
313+
}
314+
315+
return hashes
316+
}

0 commit comments

Comments
 (0)