@@ -10,7 +10,9 @@ import (
10
10
errs "github.com/onflow/flow-evm-gateway/models/errors"
11
11
"github.com/onflow/flow-evm-gateway/services/requester"
12
12
"github.com/onflow/flow-go-sdk"
13
+ "github.com/onflow/flow-go/fvm/evm/events"
13
14
flowGo "github.com/onflow/flow-go/model/flow"
15
+ "github.com/onflow/go-ethereum/core/types"
14
16
"github.com/rs/zerolog"
15
17
)
16
18
@@ -151,40 +153,10 @@ func (r *RPCBlockTrackingSubscriber) subscribe(ctx context.Context, height uint6
151
153
return
152
154
}
153
155
154
- var blockEvents flow.BlockEvents
155
- for _ , eventType := range blocksFilter (r .chain ).EventTypes {
156
- evts , err := r .client .GetEventsForHeightRange (
157
- ctx ,
158
- eventType ,
159
- blockHeader .Height ,
160
- blockHeader .Height ,
161
- )
162
- if err != nil {
163
- eventsChan <- models .NewBlockEventsError (
164
- fmt .Errorf (
165
- "failed to fetch EVM events for height: %d, with: %w" ,
166
- blockHeader .Height ,
167
- err ,
168
- ),
169
- )
170
- return
171
- }
172
-
173
- if len (evts ) != 1 {
174
- eventsChan <- models .NewBlockEventsError (
175
- fmt .Errorf (
176
- "received unexpected number of EVM events for height: %d, got: %d, expected: 1" ,
177
- blockHeader .Height ,
178
- len (evts ),
179
- ),
180
- )
181
- return
182
- }
183
- blockEvent := evts [0 ]
184
- blockEvents .BlockID = blockEvent .BlockID
185
- blockEvents .BlockTimestamp = blockEvent .BlockTimestamp
186
- blockEvents .Height = blockEvent .Height
187
- blockEvents .Events = append (blockEvents .Events , blockEvent .Events ... )
156
+ blockEvents , err := r .evmEventsForHeight (ctx , blockHeader .Height )
157
+ if err != nil {
158
+ eventsChan <- models .NewBlockEventsError (err )
159
+ return
188
160
}
189
161
190
162
evmEvents := models .NewSingleBlockEvents (blockEvents )
@@ -243,3 +215,63 @@ func (r *RPCBlockTrackingSubscriber) subscribe(ctx context.Context, height uint6
243
215
244
216
return eventsChan
245
217
}
218
+
219
+ func (r * RPCBlockTrackingSubscriber ) evmEventsForHeight (
220
+ ctx context.Context ,
221
+ height uint64 ,
222
+ ) (flow.BlockEvents , error ) {
223
+ eventTypes := blocksFilter (r .chain ).EventTypes
224
+ evmBlockEvent := eventTypes [0 ]
225
+
226
+ evts , err := r .client .GetEventsForHeightRange (
227
+ ctx ,
228
+ evmBlockEvent ,
229
+ height ,
230
+ height ,
231
+ )
232
+ if err != nil {
233
+ return flow.BlockEvents {}, err
234
+ }
235
+
236
+ if len (evts ) != 1 && len (evts [0 ].Events ) != 1 {
237
+ return flow.BlockEvents {}, fmt .Errorf (
238
+ "received unexpected number of EVM events for height: %d, got: %d, expected: 1" ,
239
+ height ,
240
+ len (evts ),
241
+ )
242
+ }
243
+
244
+ blockEvents := evts [0 ]
245
+ payload , err := events .DecodeBlockEventPayload (blockEvents .Events [0 ].Value )
246
+ if err != nil {
247
+ return flow.BlockEvents {}, err
248
+ }
249
+
250
+ if payload .TransactionHashRoot == types .EmptyTxsHash {
251
+ return blockEvents , nil
252
+ }
253
+
254
+ evmTxEvent := eventTypes [1 ]
255
+ evts , err = r .client .GetEventsForHeightRange (
256
+ ctx ,
257
+ evmTxEvent ,
258
+ height ,
259
+ height ,
260
+ )
261
+ if err != nil {
262
+ return flow.BlockEvents {}, err
263
+ }
264
+
265
+ if len (evts ) != 1 && len (evts [0 ].Events ) != 1 {
266
+ return flow.BlockEvents {}, fmt .Errorf (
267
+ "received unexpected number of EVM events for height: %d, got: %d, expected: 1" ,
268
+ height ,
269
+ len (evts ),
270
+ )
271
+ }
272
+ txEvents := evts [0 ]
273
+
274
+ blockEvents .Events = append (blockEvents .Events , txEvents .Events ... )
275
+
276
+ return blockEvents , nil
277
+ }
0 commit comments