MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / prepareQueuedBatchTransaction

Method prepareQueuedBatchTransaction

transaction.go:1061–1089  ·  view source on GitHub ↗

prepareQueuedBatchTransaction performs the per-transaction work inside a coalesced batch: hooks, reference validation, in-memory balance application, and persistence shaping.

(ctx context.Context, span trace.Span, txn *model.Transaction, balancesByID map[string]*model.Balance, preHooks []*ledgerforgehooks.Hook, prefetchedReferences, existingReferences, batchReferences map[string]struct{})

Source from the content-addressed store, hash-verified

1059// prepareQueuedBatchTransaction performs the per-transaction work inside a coalesced batch:
1060// hooks, reference validation, in-memory balance application, and persistence shaping.
1061func (l *LedgerForge) prepareQueuedBatchTransaction(ctx context.Context, span trace.Span, txn *model.Transaction, balancesByID map[string]*model.Balance, preHooks []*ledgerforgehooks.Hook, prefetchedReferences, existingReferences, batchReferences map[string]struct{}) (queuedBatchPostCommitWork, error) {
1062 sourceBalance, destinationBalance, err := coalescingBalancesForTransaction(balancesByID, txn)
1063 if err != nil {
1064 return queuedBatchPostCommitWork{}, err
1065 }
1066
1067 if l.Hooks != nil {
1068 if err := l.Hooks.ExecuteHooks(ctx, preHooks, ledgerforgehooks.PreTransaction, txn.TransactionID, txn); err != nil {
1069 span.RecordError(err)
1070 return queuedBatchPostCommitWork{}, fmt.Errorf("batch pre-transaction hook failed: %w", err)
1071 }
1072 }
1073
1074 if err := l.validateQueuedBatchTransactionReference(ctx, txn, prefetchedReferences, existingReferences, batchReferences); err != nil {
1075 return queuedBatchPostCommitWork{}, fmt.Errorf("batch transaction validation failed: %w", err)
1076 }
1077
1078 finalizedTxn := l.updateTransactionDetails(ctx, txn, sourceBalance, destinationBalance)
1079 if err := l.processBalances(ctx, finalizedTxn, sourceBalance, destinationBalance); err != nil {
1080 return queuedBatchPostCommitWork{}, err
1081 }
1082
1083 work, skipPersist := l.buildTransactionExecutionWork(ctx, finalizedTxn, sourceBalance, destinationBalance)
1084 if skipPersist {
1085 return queuedBatchPostCommitWork{}, fmt.Errorf("batch coalescing does not support zero-amount transactions")
1086 }
1087
1088 return work, nil
1089}
1090
1091// runQueuedBatchPostCommitWork dispatches the post-commit work for a coalesced batch after
1092// the balance-set lock has been released.

Tested by

no test coverage detected