Skip to content

Commit 7b05488

Browse files
committed
tidy & fix event indexing
1 parent 3223fc9 commit 7b05488

2 files changed

Lines changed: 25 additions & 25 deletions

File tree

internal/transactions/profile.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,16 @@ func profileTransactionWithFVM(
312312
)...)
313313

314314
// Execute prior transactions to recreate state
315+
txIndex := 0
315316
if len(priorUserTxs) > 0 {
316-
if err := executeTransactions(vm, userCtx, execState, priorUserTxs, logger); err != nil {
317+
if err := executeTransactions(vm, userCtx, execState, priorUserTxs, txIndex, logger); err != nil {
317318
return nil, 0, fmt.Errorf("failed to execute prior user transactions: %w", err)
318319
}
320+
txIndex += len(priorUserTxs)
319321
}
320322

321323
if len(priorSystemTxs) > 0 {
322-
if err := executeTransactions(vm, systemCtx, execState, priorSystemTxs, logger); err != nil {
324+
if err := executeTransactions(vm, systemCtx, execState, priorSystemTxs, txIndex, logger); err != nil {
323325
return nil, 0, fmt.Errorf("failed to execute prior system transactions: %w", err)
324326
}
325327
}
@@ -339,8 +341,8 @@ func profileTransactionWithFVM(
339341
return nil, 0, fmt.Errorf("failed to create transaction context: %w", err)
340342
}
341343

342-
txIndex := uint32(len(priorUserTxs) + len(priorSystemTxs))
343-
txProc := fvm.Transaction(targetFlowTx, txIndex)
344+
targetTxIndex := uint32(len(priorUserTxs) + len(priorSystemTxs))
345+
txProc := fvm.Transaction(targetFlowTx, targetTxIndex)
344346
_, output, err := vm.Run(targetCtx, txProc, txn)
345347
if err != nil {
346348
return nil, 0, fmt.Errorf("failed to execute target transaction: %w", err)
@@ -371,7 +373,7 @@ func isSystemTransaction(tx *flowsdk.Transaction) bool {
371373
// separateTransactionsByType separates transactions into user and system transactions
372374
func separateTransactionsByType(txs []*flowsdk.Transaction) (user, system []*flowsdk.Transaction) {
373375
user = make([]*flowsdk.Transaction, 0, len(txs))
374-
system = make([]*flowsdk.Transaction, 0)
376+
system = make([]*flowsdk.Transaction, 0, len(txs))
375377

376378
for _, tx := range txs {
377379
if isSystemTransaction(tx) {
@@ -389,6 +391,7 @@ func executeTransactions(
389391
ctx fvm.Context,
390392
execState *fvmState.ExecutionState,
391393
txs []*flowsdk.Transaction,
394+
startIndex int,
392395
logger output.Logger,
393396
) error {
394397
for i, tx := range txs {
@@ -397,17 +400,17 @@ func executeTransactions(
397400
blockDB := fvmStorage.NewBlockDatabase(execState, 0, nil)
398401
txn, err := blockDB.NewTransaction(0, fvmState.DefaultParameters())
399402
if err != nil {
400-
return fmt.Errorf("failed to create transaction context for tx %d: %w", i, err)
403+
return fmt.Errorf("failed to create transaction context for tx %d: %w", startIndex+i, err)
401404
}
402405

403-
txProc := fvm.Transaction(flowTx, uint32(i))
406+
txProc := fvm.Transaction(flowTx, uint32(startIndex+i))
404407
executionSnapshot, _, err := vm.Run(ctx, txProc, txn)
405408
if err != nil {
406-
return fmt.Errorf("failed to execute transaction %d (%s): %w", i, tx.ID().String()[:txIDDisplayLength], err)
409+
return fmt.Errorf("failed to execute transaction %d (%s): %w", startIndex+i, tx.ID().String()[:txIDDisplayLength], err)
407410
}
408411

409412
if err := execState.Merge(executionSnapshot); err != nil {
410-
return fmt.Errorf("failed to merge execution snapshot for tx %d: %w", i, err)
413+
return fmt.Errorf("failed to merge execution snapshot for tx %d: %w", startIndex+i, err)
411414
}
412415
}
413416

internal/transactions/profile_test.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,12 @@ func Test_Profile_Integration_LocalEmulator(t *testing.T) {
211211

212212
func getFreePort(t *testing.T) int {
213213
listener, err := net.Listen("tcp", "127.0.0.1:0")
214-
if err != nil {
215-
if t != nil {
216-
require.NoError(t, err)
217-
} else {
218-
panic(fmt.Sprintf("failed to get free port: %v", err))
219-
}
220-
}
214+
require.NoError(t, err)
221215
defer listener.Close()
222-
return listener.Addr().(*net.TCPAddr).Port
216+
217+
tcpAddr, ok := listener.Addr().(*net.TCPAddr)
218+
require.True(t, ok, "expected TCP address")
219+
return tcpAddr.Port
223220
}
224221

225222
func runProfileTest(t *testing.T, emulatorHost string, testTxID flow.Identifier, testBlockHeight uint64) {
@@ -270,12 +267,12 @@ func runProfileTest(t *testing.T, emulatorHost string, testTxID flow.Identifier,
270267
assert.Equal(t, testBlockHeight, jsonMap["block_height"])
271268
}
272269

273-
func createEmulatorServer(port int) *server.EmulatorServer {
270+
func createEmulatorServer(t *testing.T, port int) *server.EmulatorServer {
274271
zlog := zerolog.New(zerolog.ConsoleWriter{Out: io.Discard})
275272

276-
restPort := getFreePort(nil)
277-
adminPort := getFreePort(nil)
278-
debuggerPort := getFreePort(nil)
273+
restPort := getFreePort(t)
274+
adminPort := getFreePort(t)
275+
debuggerPort := getFreePort(t)
279276

280277
serverConf := &server.Config{
281278
GRPCPort: port,
@@ -346,7 +343,7 @@ func submitAndCommitTransaction(t *testing.T, tx *flow.Transaction, blockchain e
346343
}
347344

348345
func startEmulatorWithTestTransaction(t *testing.T, host string, port int) (*server.EmulatorServer, flow.Identifier, uint64) {
349-
emulatorServer := createEmulatorServer(port)
346+
emulatorServer := createEmulatorServer(t, port)
350347
blockchain := emulatorServer.Emulator()
351348

352349
createInitialBlocks(t, blockchain)
@@ -374,7 +371,7 @@ func startEmulatorWithTestTransaction(t *testing.T, host string, port int) (*ser
374371
}
375372

376373
func startEmulatorWithFailedTransaction(t *testing.T, host string, port int) (*server.EmulatorServer, flow.Identifier, uint64) {
377-
emulatorServer := createEmulatorServer(port)
374+
emulatorServer := createEmulatorServer(t, port)
378375
blockchain := emulatorServer.Emulator()
379376

380377
createInitialBlocks(t, blockchain)
@@ -399,7 +396,7 @@ func startEmulatorWithFailedTransaction(t *testing.T, host string, port int) (*s
399396
}
400397

401398
func startEmulatorWithMultipleTransactions(t *testing.T, host string, port int, count int) (*server.EmulatorServer, flow.Identifier, uint64) {
402-
emulatorServer := createEmulatorServer(port)
399+
emulatorServer := createEmulatorServer(t, port)
403400
blockchain := emulatorServer.Emulator()
404401

405402
createInitialBlocks(t, blockchain)
@@ -437,7 +434,7 @@ func startEmulatorWithMultipleTransactions(t *testing.T, host string, port int,
437434
}
438435

439436
func startEmulatorWithScheduledTransaction(t *testing.T, host string, port int) (*server.EmulatorServer, flow.Identifier, uint64) {
440-
emulatorServer := createEmulatorServer(port)
437+
emulatorServer := createEmulatorServer(t, port)
441438

442439
blockchain := emulatorServer.Emulator()
443440
serviceAddress := blockchain.ServiceKey().Address

0 commit comments

Comments
 (0)