Skip to content

Commit

Permalink
Listen to tx pre event and trigger 'pending'
Browse files Browse the repository at this point in the history
  • Loading branch information
obscuren committed Mar 20, 2015
1 parent b4a51de commit 55fdf3e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion core/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Filter struct {
topics [][][]byte

BlockCallback func(*types.Block, state.Logs)
PendingCallback func(*types.Block, state.Logs)
PendingCallback func(*types.Transaction)
LogsCallback func(state.Logs)
}

Expand Down
7 changes: 4 additions & 3 deletions event/filter/eth_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ func (self *FilterManager) GetFilter(id int) *core.Filter {
func (self *FilterManager) filterLoop() {
// Subscribe to events
events := self.eventMux.Subscribe(
core.PendingBlockEvent{},
//core.PendingBlockEvent{},
core.ChainEvent{},
core.TxPreEvent{},
state.Logs(nil))

out:
Expand All @@ -82,11 +83,11 @@ out:
}
self.filterMu.RUnlock()

case core.PendingBlockEvent:
case core.TxPreEvent:
self.filterMu.RLock()
for _, filter := range self.filters {
if filter.PendingCallback != nil {
filter.PendingCallback(event.Block, event.Logs)
filter.PendingCallback(event.Tx)
}
}
self.filterMu.RUnlock()
Expand Down
27 changes: 15 additions & 12 deletions rpc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,24 @@ func (self *EthereumApi) NewFilterString(args *FilterStringArgs, reply *interfac
var id int
filter := core.NewFilter(self.xeth().Backend())

callback := func(block *types.Block, logs state.Logs) {
self.logMut.Lock()
defer self.logMut.Unlock()

for _, log := range logs {
self.logs[id].add(log)
}
self.logs[id].add(&state.StateLog{})
}

switch args.Word {
case "pending":
filter.PendingCallback = callback
filter.PendingCallback = func(tx *types.Transaction) {
self.logMut.Lock()
defer self.logMut.Unlock()

self.logs[id].add(&state.StateLog{})
}
case "latest":
filter.BlockCallback = callback
filter.BlockCallback = func(block *types.Block, logs state.Logs) {
self.logMut.Lock()
defer self.logMut.Unlock()

for _, log := range logs {
self.logs[id].add(log)
}
self.logs[id].add(&state.StateLog{})
}
default:
return NewValidationError("Word", "Must be `latest` or `pending`")
}
Expand Down

0 comments on commit 55fdf3e

Please sign in to comment.