@@ -30,11 +30,15 @@ import (
3030 "github.com/ethereum/go-ethereum/common/math"
3131 "github.com/ethereum/go-ethereum/consensus/ethash"
3232 "github.com/ethereum/go-ethereum/core"
33+ "github.com/ethereum/go-ethereum/core/bloombits"
3334 "github.com/ethereum/go-ethereum/core/state"
3435 "github.com/ethereum/go-ethereum/core/types"
3536 "github.com/ethereum/go-ethereum/core/vm"
37+ "github.com/ethereum/go-ethereum/eth/filters"
3638 "github.com/ethereum/go-ethereum/ethdb"
39+ "github.com/ethereum/go-ethereum/event"
3740 "github.com/ethereum/go-ethereum/params"
41+ "github.com/ethereum/go-ethereum/rpc"
3842)
3943
4044// This nil assignment ensures compile time that SimulatedBackend implements bind.ContractBackend.
@@ -53,6 +57,8 @@ type SimulatedBackend struct {
5357 pendingBlock * types.Block // Currently pending block that will be imported on request
5458 pendingState * state.StateDB // Currently pending state that will be the active on on request
5559
60+ events * filters.EventSystem // Event system for filtering log events live
61+
5662 config * params.ChainConfig
5763}
5864
@@ -63,7 +69,13 @@ func NewSimulatedBackend(alloc core.GenesisAlloc) *SimulatedBackend {
6369 genesis := core.Genesis {Config : params .AllEthashProtocolChanges , Alloc : alloc }
6470 genesis .MustCommit (database )
6571 blockchain , _ := core .NewBlockChain (database , genesis .Config , ethash .NewFaker (), vm.Config {})
66- backend := & SimulatedBackend {database : database , blockchain : blockchain , config : genesis .Config }
72+
73+ backend := & SimulatedBackend {
74+ database : database ,
75+ blockchain : blockchain ,
76+ config : genesis .Config ,
77+ events : filters .NewEventSystem (new (event.TypeMux ), & filterBackend {database , blockchain }, false ),
78+ }
6779 backend .rollback ()
6880 return backend
6981}
@@ -248,7 +260,7 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
248260 return hi , nil
249261}
250262
251- // callContract implemens common code between normal and pending contract calls.
263+ // callContract implements common code between normal and pending contract calls.
252264// state is modified during execution, make sure to copy it if necessary.
253265func (b * SimulatedBackend ) callContract (ctx context.Context , call ethereum.CallMsg , block * types.Block , statedb * state.StateDB ) ([]byte , uint64 , bool , error ) {
254266 // Ensure message is initialized properly.
@@ -302,7 +314,69 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
302314 return nil
303315}
304316
305- // JumpTimeInSeconds adds skip seconds to the clock
317+ // FilterLogs executes a log filter operation, blocking during execution and
318+ // returning all the results in one batch.
319+ //
320+ // TODO(karalabe): Deprecate when the subscription one can return past data too.
321+ func (b * SimulatedBackend ) FilterLogs (ctx context.Context , query ethereum.FilterQuery ) ([]types.Log , error ) {
322+ // Initialize unset filter boundaried to run from genesis to chain head
323+ from := int64 (0 )
324+ if query .FromBlock != nil {
325+ from = query .FromBlock .Int64 ()
326+ }
327+ to := int64 (- 1 )
328+ if query .ToBlock != nil {
329+ to = query .ToBlock .Int64 ()
330+ }
331+ // Construct and execute the filter
332+ filter := filters .New (& filterBackend {b .database , b .blockchain }, from , to , query .Addresses , query .Topics )
333+
334+ logs , err := filter .Logs (ctx )
335+ if err != nil {
336+ return nil , err
337+ }
338+ res := make ([]types.Log , len (logs ))
339+ for i , log := range logs {
340+ res [i ] = * log
341+ }
342+ return res , nil
343+ }
344+
345+ // SubscribeFilterLogs creates a background log filtering operation, returning a
346+ // subscription immediately, which can be used to stream the found events.
347+ func (b * SimulatedBackend ) SubscribeFilterLogs (ctx context.Context , query ethereum.FilterQuery , ch chan <- types.Log ) (ethereum.Subscription , error ) {
348+ // Subscribe to contract events
349+ sink := make (chan []* types.Log )
350+
351+ sub , err := b .events .SubscribeLogs (query , sink )
352+ if err != nil {
353+ return nil , err
354+ }
355+ // Since we're getting logs in batches, we need to flatten them into a plain stream
356+ return event .NewSubscription (func (quit <- chan struct {}) error {
357+ defer sub .Unsubscribe ()
358+ for {
359+ select {
360+ case logs := <- sink :
361+ for _ , log := range logs {
362+ select {
363+ case ch <- * log :
364+ case err := <- sub .Err ():
365+ return err
366+ case <- quit :
367+ return nil
368+ }
369+ }
370+ case err := <- sub .Err ():
371+ return err
372+ case <- quit :
373+ return nil
374+ }
375+ }
376+ }), nil
377+ }
378+
379+ // AdjustTime adds a time shift to the simulated clock.
306380func (b * SimulatedBackend ) AdjustTime (adjustment time.Duration ) error {
307381 b .mu .Lock ()
308382 defer b .mu .Unlock ()
@@ -331,3 +405,44 @@ func (m callmsg) GasPrice() *big.Int { return m.CallMsg.GasPrice }
331405func (m callmsg ) Gas () uint64 { return m .CallMsg .Gas }
332406func (m callmsg ) Value () * big.Int { return m .CallMsg .Value }
333407func (m callmsg ) Data () []byte { return m .CallMsg .Data }
408+
409+ // filterBackend implements filters.Backend to support filtering for logs without
410+ // taking bloom-bits acceleration structures into account.
411+ type filterBackend struct {
412+ db ethdb.Database
413+ bc * core.BlockChain
414+ }
415+
416+ func (fb * filterBackend ) ChainDb () ethdb.Database { return fb .db }
417+ func (fb * filterBackend ) EventMux () * event.TypeMux { panic ("not supported" ) }
418+
419+ func (fb * filterBackend ) HeaderByNumber (ctx context.Context , block rpc.BlockNumber ) (* types.Header , error ) {
420+ if block == rpc .LatestBlockNumber {
421+ return fb .bc .CurrentHeader (), nil
422+ }
423+ return fb .bc .GetHeaderByNumber (uint64 (block .Int64 ())), nil
424+ }
425+ func (fb * filterBackend ) GetReceipts (ctx context.Context , hash common.Hash ) (types.Receipts , error ) {
426+ return core .GetBlockReceipts (fb .db , hash , core .GetBlockNumber (fb .db , hash )), nil
427+ }
428+
429+ func (fb * filterBackend ) SubscribeTxPreEvent (ch chan <- core.TxPreEvent ) event.Subscription {
430+ return event .NewSubscription (func (quit <- chan struct {}) error {
431+ <- quit
432+ return nil
433+ })
434+ }
435+ func (fb * filterBackend ) SubscribeChainEvent (ch chan <- core.ChainEvent ) event.Subscription {
436+ return fb .bc .SubscribeChainEvent (ch )
437+ }
438+ func (fb * filterBackend ) SubscribeRemovedLogsEvent (ch chan <- core.RemovedLogsEvent ) event.Subscription {
439+ return fb .bc .SubscribeRemovedLogsEvent (ch )
440+ }
441+ func (fb * filterBackend ) SubscribeLogsEvent (ch chan <- []* types.Log ) event.Subscription {
442+ return fb .bc .SubscribeLogsEvent (ch )
443+ }
444+
445+ func (fb * filterBackend ) BloomStatus () (uint64 , uint64 ) { return 4096 , 0 }
446+ func (fb * filterBackend ) ServiceFilter (ctx context.Context , ms * bloombits.MatcherSession ) {
447+ panic ("not supported" )
448+ }
0 commit comments