github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/operation/transaction_results.go (about)

     1  package operation
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/dgraph-io/badger/v2"
     7  
     8  	"github.com/onflow/flow-go/model/flow"
     9  )
    10  
    11  func InsertTransactionResult(blockID flow.Identifier, transactionResult *flow.TransactionResult) func(*badger.Txn) error {
    12  	return insert(makePrefix(codeTransactionResult, blockID, transactionResult.TransactionID), transactionResult)
    13  }
    14  
    15  func BatchInsertTransactionResult(blockID flow.Identifier, transactionResult *flow.TransactionResult) func(batch *badger.WriteBatch) error {
    16  	return batchWrite(makePrefix(codeTransactionResult, blockID, transactionResult.TransactionID), transactionResult)
    17  }
    18  
    19  func BatchIndexTransactionResult(blockID flow.Identifier, txIndex uint32, transactionResult *flow.TransactionResult) func(batch *badger.WriteBatch) error {
    20  	return batchWrite(makePrefix(codeTransactionResultIndex, blockID, txIndex), transactionResult)
    21  }
    22  
    23  func RetrieveTransactionResult(blockID flow.Identifier, transactionID flow.Identifier, transactionResult *flow.TransactionResult) func(*badger.Txn) error {
    24  	return retrieve(makePrefix(codeTransactionResult, blockID, transactionID), transactionResult)
    25  }
    26  func RetrieveTransactionResultByIndex(blockID flow.Identifier, txIndex uint32, transactionResult *flow.TransactionResult) func(*badger.Txn) error {
    27  	return retrieve(makePrefix(codeTransactionResultIndex, blockID, txIndex), transactionResult)
    28  }
    29  
    30  // LookupTransactionResultsByBlockIDUsingIndex retrieves all tx results for a block, by using
    31  // tx_index index. This correctly handles cases of duplicate transactions within block.
    32  func LookupTransactionResultsByBlockIDUsingIndex(blockID flow.Identifier, txResults *[]flow.TransactionResult) func(*badger.Txn) error {
    33  
    34  	txErrIterFunc := func() (checkFunc, createFunc, handleFunc) {
    35  		check := func(_ []byte) bool {
    36  			return true
    37  		}
    38  		var val flow.TransactionResult
    39  		create := func() interface{} {
    40  			return &val
    41  		}
    42  		handle := func() error {
    43  			*txResults = append(*txResults, val)
    44  			return nil
    45  		}
    46  		return check, create, handle
    47  	}
    48  
    49  	return traverse(makePrefix(codeTransactionResultIndex, blockID), txErrIterFunc)
    50  }
    51  
    52  // RemoveTransactionResultsByBlockID removes the transaction results for the given blockID
    53  func RemoveTransactionResultsByBlockID(blockID flow.Identifier) func(*badger.Txn) error {
    54  	return func(txn *badger.Txn) error {
    55  
    56  		prefix := makePrefix(codeTransactionResult, blockID)
    57  		err := removeByPrefix(prefix)(txn)
    58  		if err != nil {
    59  			return fmt.Errorf("could not remove transaction results for block %v: %w", blockID, err)
    60  		}
    61  
    62  		return nil
    63  	}
    64  }
    65  
    66  // BatchRemoveTransactionResultsByBlockID removes transaction results for the given blockID in a provided batch.
    67  // No errors are expected during normal operation, but it may return generic error
    68  // if badger fails to process request
    69  func BatchRemoveTransactionResultsByBlockID(blockID flow.Identifier, batch *badger.WriteBatch) func(*badger.Txn) error {
    70  	return func(txn *badger.Txn) error {
    71  
    72  		prefix := makePrefix(codeTransactionResult, blockID)
    73  		err := batchRemoveByPrefix(prefix)(txn, batch)
    74  		if err != nil {
    75  			return fmt.Errorf("could not remove transaction results for block %v: %w", blockID, err)
    76  		}
    77  
    78  		return nil
    79  	}
    80  }
    81  
    82  func InsertLightTransactionResult(blockID flow.Identifier, transactionResult *flow.LightTransactionResult) func(*badger.Txn) error {
    83  	return insert(makePrefix(codeLightTransactionResult, blockID, transactionResult.TransactionID), transactionResult)
    84  }
    85  
    86  func BatchInsertLightTransactionResult(blockID flow.Identifier, transactionResult *flow.LightTransactionResult) func(batch *badger.WriteBatch) error {
    87  	return batchWrite(makePrefix(codeLightTransactionResult, blockID, transactionResult.TransactionID), transactionResult)
    88  }
    89  
    90  func BatchIndexLightTransactionResult(blockID flow.Identifier, txIndex uint32, transactionResult *flow.LightTransactionResult) func(batch *badger.WriteBatch) error {
    91  	return batchWrite(makePrefix(codeLightTransactionResultIndex, blockID, txIndex), transactionResult)
    92  }
    93  
    94  func RetrieveLightTransactionResult(blockID flow.Identifier, transactionID flow.Identifier, transactionResult *flow.LightTransactionResult) func(*badger.Txn) error {
    95  	return retrieve(makePrefix(codeLightTransactionResult, blockID, transactionID), transactionResult)
    96  }
    97  
    98  func RetrieveLightTransactionResultByIndex(blockID flow.Identifier, txIndex uint32, transactionResult *flow.LightTransactionResult) func(*badger.Txn) error {
    99  	return retrieve(makePrefix(codeLightTransactionResultIndex, blockID, txIndex), transactionResult)
   100  }
   101  
   102  // LookupLightTransactionResultsByBlockIDUsingIndex retrieves all tx results for a block, but using
   103  // tx_index index. This correctly handles cases of duplicate transactions within block.
   104  func LookupLightTransactionResultsByBlockIDUsingIndex(blockID flow.Identifier, txResults *[]flow.LightTransactionResult) func(*badger.Txn) error {
   105  
   106  	txErrIterFunc := func() (checkFunc, createFunc, handleFunc) {
   107  		check := func(_ []byte) bool {
   108  			return true
   109  		}
   110  		var val flow.LightTransactionResult
   111  		create := func() interface{} {
   112  			return &val
   113  		}
   114  		handle := func() error {
   115  			*txResults = append(*txResults, val)
   116  			return nil
   117  		}
   118  		return check, create, handle
   119  	}
   120  
   121  	return traverse(makePrefix(codeLightTransactionResultIndex, blockID), txErrIterFunc)
   122  }