github.com/MikyChow/arbitrum-go-ethereum@v0.0.0-20230306102812-078da49636de/arbitrum/backend.go (about) 1 package arbitrum 2 3 import ( 4 "context" 5 6 "github.com/MikyChow/arbitrum-go-ethereum/core" 7 "github.com/MikyChow/arbitrum-go-ethereum/core/bloombits" 8 "github.com/MikyChow/arbitrum-go-ethereum/core/types" 9 "github.com/MikyChow/arbitrum-go-ethereum/eth/filters" 10 "github.com/MikyChow/arbitrum-go-ethereum/ethdb" 11 "github.com/MikyChow/arbitrum-go-ethereum/event" 12 "github.com/MikyChow/arbitrum-go-ethereum/internal/shutdowncheck" 13 "github.com/MikyChow/arbitrum-go-ethereum/node" 14 ) 15 16 type Backend struct { 17 arb ArbInterface 18 stack *node.Node 19 apiBackend *APIBackend 20 config *Config 21 chainDb ethdb.Database 22 23 txFeed event.Feed 24 scope event.SubscriptionScope 25 26 bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests 27 bloomIndexer *core.ChainIndexer // Bloom indexer operating during block imports 28 29 shutdownTracker *shutdowncheck.ShutdownTracker 30 31 chanTxs chan *types.Transaction 32 chanClose chan struct{} //close coroutine 33 chanNewBlock chan struct{} //create new L2 block unless empty 34 } 35 36 func NewBackend(stack *node.Node, config *Config, chainDb ethdb.Database, publisher ArbInterface, sync SyncProgressBackend, filterConfig filters.Config) (*Backend, *filters.FilterSystem, error) { 37 backend := &Backend{ 38 arb: publisher, 39 stack: stack, 40 config: config, 41 chainDb: chainDb, 42 43 bloomRequests: make(chan chan *bloombits.Retrieval), 44 bloomIndexer: core.NewBloomIndexer(chainDb, config.BloomBitsBlocks, config.BloomConfirms), 45 46 shutdownTracker: shutdowncheck.NewShutdownTracker(chainDb), 47 48 chanTxs: make(chan *types.Transaction, 100), 49 chanClose: make(chan struct{}), 50 chanNewBlock: make(chan struct{}, 1), 51 } 52 53 backend.bloomIndexer.Start(backend.arb.BlockChain()) 54 filterSystem, err := createRegisterAPIBackend(backend, sync, filterConfig, config.ClassicRedirect, config.ClassicRedirectTimeout) 55 if err != nil { 56 return nil, nil, err 57 } 58 return backend, filterSystem, nil 59 } 60 61 func (b *Backend) APIBackend() *APIBackend { 62 return b.apiBackend 63 } 64 65 func (b *Backend) ChainDb() ethdb.Database { 66 return b.chainDb 67 } 68 69 func (b *Backend) EnqueueL2Message(ctx context.Context, tx *types.Transaction) error { 70 return b.arb.PublishTransaction(ctx, tx) 71 } 72 73 func (b *Backend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 74 return b.scope.Track(b.txFeed.Subscribe(ch)) 75 } 76 77 func (b *Backend) Stack() *node.Node { 78 return b.stack 79 } 80 81 func (b *Backend) ArbInterface() ArbInterface { 82 return b.arb 83 } 84 85 // TODO: this is used when registering backend as lifecycle in stack 86 func (b *Backend) Start() error { 87 b.startBloomHandlers(b.config.BloomBitsBlocks) 88 b.shutdownTracker.MarkStartup() 89 b.shutdownTracker.Start() 90 91 return nil 92 } 93 94 func (b *Backend) Stop() error { 95 b.scope.Close() 96 b.bloomIndexer.Close() 97 b.shutdownTracker.Stop() 98 b.chainDb.Close() 99 close(b.chanClose) 100 return nil 101 }