From b2321be4adc593d54f86451a0dc3fd878a31afd0 Mon Sep 17 00:00:00 2001 From: Aditya Vyas Date: Thu, 14 Sep 2023 15:28:54 -0400 Subject: [PATCH] Use defer for sys.finish --- services/horizon/internal/txsub/system.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/services/horizon/internal/txsub/system.go b/services/horizon/internal/txsub/system.go index b729dec172..f624089452 100644 --- a/services/horizon/internal/txsub/system.go +++ b/services/horizon/internal/txsub/system.go @@ -99,6 +99,11 @@ func (sys *System) Submit( sys.Init() resultCh := make(chan Result, 1) resultReadCh = resultCh + var result Result + + defer func() { + sys.finish(ctx, hash, resultCh, result) + }() db := sys.DB(ctx) // The database doesn't (yet) store muxed accounts, so we query @@ -116,19 +121,19 @@ func (sys *System) Submit( minSeqNum := envelope.MinSeqNum() // Ensure sequence numbers make sense if seqNum < 0 || (minSeqNum != nil && (*minSeqNum < 0 || *minSeqNum >= seqNum)) { - sys.finish(ctx, hash, resultCh, Result{Err: ErrBadSequence}) + result = Result{Err: ErrBadSequence} return } tx, err := txResultByHash(ctx, db, hash) if err == nil { sys.Log.Ctx(ctx).WithField("hash", hash).Info("Found submission result in a DB") - sys.finish(ctx, hash, resultCh, Result{Transaction: tx}) + result = Result{Transaction: tx} return } if err != ErrNoResults { sys.Log.Ctx(ctx).WithField("hash", hash).Info("Error getting submission result from a DB") - sys.finish(ctx, hash, resultCh, Result{Transaction: tx, Err: err}) + result = Result{Transaction: tx, Err: err} return } @@ -139,11 +144,11 @@ func (sys *System) Submit( // any error other than "txBAD_SEQ" is a failure isBad, err := sr.IsBadSeq() if err != nil { - sys.finish(ctx, hash, resultCh, Result{Err: err}) + result = Result{Err: err} return } if !isBad { - sys.finish(ctx, hash, resultCh, Result{Err: sr.Err}) + result = Result{Err: sr.Err} return } @@ -151,7 +156,7 @@ func (sys *System) Submit( // be lagging behind leading to txBAD_SEQ. This function will block a txsub request // until the request times out or account sequence is bumped to txn sequence. if err = sys.waitUntilAccountSequence(ctx, db, sourceAddress, uint64(envelope.SeqNum())); err != nil { - sys.finish(ctx, hash, resultCh, Result{Err: err}) + result = Result{Err: err} return } @@ -159,11 +164,11 @@ func (sys *System) Submit( tx, err = txResultByHash(ctx, db, hash) if err != nil { // finally, return the bad_seq error if no result was found on 2nd attempt - sys.finish(ctx, hash, resultCh, Result{Err: sr.Err}) + result = Result{Err: sr.Err} return } // If we found the result, use it as the result - sys.finish(ctx, hash, resultCh, Result{Transaction: tx}) + result = Result{Transaction: tx} return }