-
Notifications
You must be signed in to change notification settings - Fork 2
/
engine.go
210 lines (173 loc) · 4.59 KB
/
engine.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package indexer
import (
"context"
"errors"
"math/big"
"sync"
"time"
"github.com/cartridge-gg/starknet-indexer/ent"
"github.com/cartridge-gg/starknet-indexer/processor"
"github.com/dontpanicdao/caigo/jsonrpc"
"github.com/dontpanicdao/caigo/types"
"github.com/rs/zerolog/log"
concurrently "github.com/tejzpr/ordered-concurrently/v3"
)
const concurrency = 5
type Contract struct {
Address string
StartBlock uint64
Handler func(types.Transaction) error
}
type Config struct {
Head uint64
Interval time.Duration
Contracts []Contract
}
type Engine struct {
sync.Mutex
client *ent.Client
latest uint64
provider *jsonrpc.Client
ticker *time.Ticker
blockProcessors []processor.BlockProcessor
transactionProcessors []processor.TransactionProcessor
eventProcessors []processor.EventProcessor
}
func NewEngine(ctx context.Context, client *ent.Client, provider *jsonrpc.Client, config Config) (*Engine, error) {
e := &Engine{
client: client,
provider: provider,
ticker: time.NewTicker(config.Interval),
latest: config.Head,
}
return e, nil
}
func (e *Engine) Start(ctx context.Context) {
defer e.ticker.Stop()
log.Info().Msg("Starting indexer.")
for {
select {
case <-e.ticker.C:
if err := e.process(ctx); err != nil {
log.Err(err).Msg("Processing block.")
return
}
case <-ctx.Done():
return
}
}
}
func (e *Engine) Register(ctx context.Context, h interface{}) error {
switch h := h.(type) {
case processor.BlockProcessor:
e.Lock()
e.blockProcessors = append(e.blockProcessors, h)
e.Unlock()
return nil
case processor.TransactionProcessor:
e.Lock()
e.transactionProcessors = append(e.transactionProcessors, h)
e.Unlock()
return nil
case processor.EventProcessor:
e.Lock()
e.eventProcessors = append(e.eventProcessors, h)
e.Unlock()
return nil
}
return errors.New("unsupported processor")
}
func (e *Engine) Subscribe(ctx context.Context) {
}
func (e *Engine) process(ctx context.Context) error {
e.Lock()
defer e.Unlock()
worker := make(chan concurrently.WorkFunction, concurrency)
outputs := concurrently.Process(ctx, worker, &concurrently.Options{PoolSize: concurrency, OutChannelBuffer: concurrency})
block, err := e.provider.BlockByNumber(ctx, nil, "FULL_TXN_AND_RECEIPTS")
if err != nil {
log.Err(err).Msg("Getting latest block number.")
return err
}
head := uint64(block.BlockNumber)
go func() {
for i := e.latest; i < head; i++ {
worker <- fetcher{e.provider, i, e.blockProcessors, e.transactionProcessors, e.eventProcessors}
}
close(worker)
}()
for output := range outputs {
v, ok := output.Value.(response)
if !ok {
continue
}
if v.err != nil {
log.Err(v.err).Msg("Fetching block.")
return v.err
}
if err := ent.WithTx(ctx, e.client, func(tx *ent.Tx) error {
for _, cb := range v.callbacks {
if cb == nil {
continue
}
if err := cb(tx); err != nil {
log.Err(err).Uint64("block", uint64(v.block.BlockNumber)).Msg("Writing block.")
return err
}
}
return nil
}); err != nil {
return err
}
e.latest = uint64(v.block.BlockNumber)
}
return nil
}
// Create a type based on your input to the work function
type fetcher struct {
provider *jsonrpc.Client
blockNumber uint64
blockProcessors []processor.BlockProcessor
transactionProcessors []processor.TransactionProcessor
eventProcessors []processor.EventProcessor
}
type response struct {
block *types.Block
callbacks []func(*ent.Tx) error
err error
}
// The work that needs to be performed
// The input type should implement the WorkFunction interface
func (f fetcher) Run(ctx context.Context) interface{} {
block, err := f.provider.BlockByNumber(ctx, big.NewInt(int64(f.blockNumber)), "FULL_TXN_AND_RECEIPTS")
if err != nil {
return response{block, nil, err}
}
var cbs []func(*ent.Tx) error
for _, p := range f.blockProcessors {
cb, err := p.Process(ctx, f.provider, block)
if err != nil {
return response{block, nil, err}
}
cbs = append(cbs, cb)
}
for _, t := range block.Transactions {
for _, p := range f.transactionProcessors {
cb, err := p.Process(ctx, f.provider, block, t)
if err != nil {
return response{block, nil, err}
}
cbs = append(cbs, cb)
}
for i, evt := range t.Events {
for _, p := range f.eventProcessors {
cb, err := p.Process(ctx, f.provider, block, t, &processor.Event{evt, uint64(i)})
if err != nil {
return response{block, nil, err}
}
cbs = append(cbs, cb)
}
}
}
return response{block, cbs, nil}
}