-
Notifications
You must be signed in to change notification settings - Fork 107
/
backend.go
91 lines (72 loc) · 2.33 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
package arbitrum
import (
"context"
"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/ethdb"
"github.com/ethereum/go-ethereum/event"
"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
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) (*Backend, 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),
chanTxs: make(chan *types.Transaction, 100),
chanClose: make(chan struct{}),
chanNewBlock: make(chan struct{}, 1),
}
backend.bloomIndexer.Start(backend.arb.BlockChain())
err := createRegisterAPIBackend(backend, sync, config.ClassicRedirect, config.ClassicRedirectTimeout)
if err != nil {
return nil, err
}
return backend, 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) error {
return b.arb.PublishTransaction(ctx, tx)
}
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)
return nil
}
func (b *Backend) Stop() error {
b.scope.Close()
b.bloomIndexer.Close()
close(b.chanClose)
return nil
}