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