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

Method validateAndPrepareTransaction

transaction.go:1200–1228  ·  view source on GitHub ↗

validateAndPrepareTransaction validates the transaction and prepares it by retrieving the source and destination balances. It starts a tracing span, validates the transaction, retrieves the balances, and updates the transaction with the balance IDs. Parameters: - ctx context.Context: The context fo

(ctx context.Context, transaction *model.Transaction)

Source from the content-addressed store, hash-verified

1198// - *model.Balance: A pointer to the destination Balance model.
1199// - error: An error if the transaction validation or balance retrieval fails.
1200func (l *LedgerForge) validateAndPrepareTransaction(ctx context.Context, transaction *model.Transaction) (*model.Transaction, *model.Balance, *model.Balance, error) {
1201 ctx, span := tracer.Start(ctx, "ValidateAndPrepareTransaction")
1202 defer span.End()
1203
1204 // Validate the transaction
1205 if err := l.validateTxn(ctx, transaction); err != nil {
1206 span.RecordError(err)
1207 return nil, nil, nil, l.logAndRecordError(span, "transaction validation failed", err)
1208 }
1209
1210 // Retrieve the source and destination balances
1211 sourceBalance, destinationBalance, err := l.getSourceAndDestination(ctx, transaction)
1212 if err != nil {
1213 span.RecordError(err)
1214 return nil, nil, nil, l.logAndRecordError(span, "failed to get source and destination balances", err)
1215 }
1216
1217 // Create a copy of the transaction and update it (immutable)
1218 newTransaction := *transaction // Copy the original transaction
1219 newTransaction.Source = sourceBalance.BalanceID
1220 newTransaction.Destination = destinationBalance.BalanceID
1221
1222 span.AddEvent("Transaction validated and prepared", trace.WithAttributes(
1223 attribute.String("source.balance_id", sourceBalance.BalanceID),
1224 attribute.String("destination.balance_id", destinationBalance.BalanceID)))
1225
1226 // Return the new transaction, source, and destination balances
1227 return &newTransaction, sourceBalance, destinationBalance, nil
1228}
1229
1230// processBalances processes the source and destination balances by applying the transaction in-memory.
1231// It starts a tracing span, applies the transaction to the balances, and records relevant events and errors.

Callers 1

Calls 4

validateTxnMethod · 0.95
logAndRecordErrorMethod · 0.95
StartMethod · 0.45

Tested by

no test coverage detected