github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/events/transaction_update.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package events
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/kaleido-io/firefly/internal/i18n"
    23  	"github.com/kaleido-io/firefly/internal/log"
    24  	"github.com/kaleido-io/firefly/pkg/blockchain"
    25  	"github.com/kaleido-io/firefly/pkg/database"
    26  	"github.com/kaleido-io/firefly/pkg/fftypes"
    27  )
    28  
    29  func (em *eventManager) TxSubmissionUpdate(bi blockchain.Plugin, txTrackingID string, txState fftypes.OpStatus, protocolTxID, errorMessage string, additionalInfo fftypes.JSONObject) error {
    30  
    31  	// Find a matching operation, for this plugin, with the specified ID.
    32  	// We retry a few times, as there's an outside possibility of the event arriving before we're finished persisting the operation itself
    33  	var operations []*fftypes.Operation
    34  	fb := database.OperationQueryFactory.NewFilter(em.ctx)
    35  	filter := fb.And(
    36  		fb.Eq("backendid", txTrackingID),
    37  		fb.Eq("plugin", bi.Name()),
    38  	)
    39  	err := em.retry.Do(em.ctx, fmt.Sprintf("correlate tx %s", txTrackingID), func(attempt int) (retry bool, err error) {
    40  		operations, err = em.database.GetOperations(em.ctx, filter)
    41  		if err == nil && len(operations) == 0 {
    42  			err = i18n.NewError(em.ctx, i18n.Msg404NotFound)
    43  		}
    44  		return (err != nil && attempt <= em.opCorrelationRetries), err
    45  	})
    46  	if err != nil {
    47  		log.L(em.ctx).Warnf("Failed to correlate tracking ID '%s' with a submitted operation", txTrackingID)
    48  		return nil
    49  	}
    50  
    51  	update := database.OperationQueryFactory.NewUpdate(em.ctx).
    52  		Set("status", txState).
    53  		Set("error", errorMessage).
    54  		Set("info", additionalInfo)
    55  	for _, op := range operations {
    56  		if err := em.database.UpdateOperation(em.ctx, op.ID, update); err != nil {
    57  			return err
    58  		}
    59  	}
    60  
    61  	return nil
    62  }