validateTxn validates a transaction by checking if its reference has already been used. It starts a tracing span, checks the existence of the transaction reference, and records relevant events and errors. Parameters: - ctx context.Context: The context for the operation. - transaction *model.Transac
(ctx context.Context, transaction *model.Transaction)
| 379 | // Returns: |
| 380 | // - error: An error if the transaction reference has already been used or if there was an issue checking the reference. |
| 381 | func (l *LedgerForge) validateTxn(ctx context.Context, transaction *model.Transaction) error { |
| 382 | ctx, span := tracer.Start(ctx, "Validating Transaction Reference") |
| 383 | defer span.End() |
| 384 | |
| 385 | // Check if a transaction with the same reference already exists |
| 386 | txn, err := l.datasource.TransactionExistsByRef(ctx, transaction.Reference) |
| 387 | if err != nil { |
| 388 | return err |
| 389 | } |
| 390 | |
| 391 | // If the transaction reference already exists, return an error |
| 392 | if txn { |
| 393 | err := fmt.Errorf("reference %s has already been used", transaction.Reference) |
| 394 | span.RecordError(err) |
| 395 | return err |
| 396 | } |
| 397 | |
| 398 | span.AddEvent("Transaction validated") |
| 399 | return nil |
| 400 | } |
| 401 | |
| 402 | // applyTransactionToBalances applies a transaction to the provided balances. |
| 403 | // It starts a tracing span, calculates new balances, and updates the balances based on the transaction status. |