code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/transaction_results.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package sqlsubscribers
    17  
    18  import (
    19  	"context"
    20  
    21  	"code.vegaprotocol.io/vega/core/events"
    22  	"code.vegaprotocol.io/vega/datanode/utils"
    23  	"code.vegaprotocol.io/vega/libs/slice"
    24  	"code.vegaprotocol.io/vega/logging"
    25  )
    26  
    27  type TransactionResultEvent interface {
    28  	events.Event
    29  	TransactionResult() events.TransactionResult
    30  }
    31  
    32  type TransactionResults struct {
    33  	subscriber
    34  	observer utils.Observer[events.TransactionResult]
    35  }
    36  
    37  func NewTransactionResults(log *logging.Logger) *TransactionResults {
    38  	return &TransactionResults{
    39  		observer: utils.NewObserver[events.TransactionResult]("transaction_result", log, 5, 5),
    40  	}
    41  }
    42  
    43  func (tr *TransactionResults) Push(ctx context.Context, evt events.Event) error {
    44  	switch e := evt.(type) {
    45  	case TransactionResultEvent:
    46  		tr.observer.Notify([]events.TransactionResult{e.TransactionResult()})
    47  		return nil
    48  	default:
    49  		return nil
    50  	}
    51  }
    52  
    53  func (tr *TransactionResults) Types() []events.Type {
    54  	return []events.Type{events.TransactionResultEvent}
    55  }
    56  
    57  func (tr *TransactionResults) Observe(ctx context.Context, retries int,
    58  	partyIDs []string, hashes []string, status *bool,
    59  ) (transactions <-chan []events.TransactionResult, ref uint64) {
    60  	ch, ref := tr.observer.Observe(ctx,
    61  		retries,
    62  		func(tre events.TransactionResult) bool {
    63  			partiesOk := len(partyIDs) == 0 || slice.Contains(partyIDs, tre.PartyID())
    64  			hashesOk := len(hashes) == 0 || slice.Contains(hashes, tre.Hash())
    65  			statusOK := status == nil || *status == tre.Status()
    66  
    67  			return partiesOk && hashesOk && statusOK
    68  		})
    69  	return ch, ref
    70  }
    71  
    72  func (tr *TransactionResults) Name() string {
    73  	return "TransactionResults"
    74  }