github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/arbitrum/backend.go (about)

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