github.com/koko1123/flow-go-1@v0.29.6/storage/badger/operation/transaction_results.go (about)

     1  // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
     2  
     3  package operation
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"github.com/dgraph-io/badger/v3"
     9  
    10  	"github.com/koko1123/flow-go-1/model/flow"
    11  )
    12  
    13  func InsertTransactionResult(blockID flow.Identifier, transactionResult *flow.TransactionResult) func(*badger.Txn) error {
    14  	return insert(makePrefix(codeTransactionResult, blockID, transactionResult.TransactionID), transactionResult)
    15  }
    16  
    17  func BatchInsertTransactionResult(blockID flow.Identifier, transactionResult *flow.TransactionResult) func(batch *badger.WriteBatch) error {
    18  	return batchWrite(makePrefix(codeTransactionResult, blockID, transactionResult.TransactionID), transactionResult)
    19  }
    20  
    21  func BatchIndexTransactionResult(blockID flow.Identifier, txIndex uint32, transactionResult *flow.TransactionResult) func(batch *badger.WriteBatch) error {
    22  	return batchWrite(makePrefix(codeTransactionResultIndex, blockID, txIndex), transactionResult)
    23  }
    24  
    25  func RetrieveTransactionResult(blockID flow.Identifier, transactionID flow.Identifier, transactionResult *flow.TransactionResult) func(*badger.Txn) error {
    26  	return retrieve(makePrefix(codeTransactionResult, blockID, transactionID), transactionResult)
    27  }
    28  func RetrieveTransactionResultByIndex(blockID flow.Identifier, txIndex uint32, transactionResult *flow.TransactionResult) func(*badger.Txn) error {
    29  	return retrieve(makePrefix(codeTransactionResultIndex, blockID, txIndex), transactionResult)
    30  }
    31  
    32  func LookupTransactionResultsByBlockID(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(codeTransactionResult, blockID), txErrIterFunc)
    50  }
    51  
    52  // LookupTransactionResultsByBlockIDUsingIndex retrieves all tx results for a block, but using
    53  // tx_index index. This correctly handles cases of duplicate transactions within block, and should
    54  // eventually replace uses of LookupTransactionResultsByBlockID
    55  func LookupTransactionResultsByBlockIDUsingIndex(blockID flow.Identifier, txResults *[]flow.TransactionResult) func(*badger.Txn) error {
    56  
    57  	txErrIterFunc := func() (checkFunc, createFunc, handleFunc) {
    58  		check := func(_ []byte) bool {
    59  			return true
    60  		}
    61  		var val flow.TransactionResult
    62  		create := func() interface{} {
    63  			return &val
    64  		}
    65  		handle := func() error {
    66  			*txResults = append(*txResults, val)
    67  			return nil
    68  		}
    69  		return check, create, handle
    70  	}
    71  
    72  	return traverse(makePrefix(codeTransactionResultIndex, blockID), txErrIterFunc)
    73  }
    74  
    75  // RemoveTransactionResultsByBlockID removes the transaction results for the given blockID
    76  func RemoveTransactionResultsByBlockID(blockID flow.Identifier) func(*badger.Txn) error {
    77  	return func(txn *badger.Txn) error {
    78  
    79  		prefix := makePrefix(codeTransactionResult, blockID)
    80  		err := removeByPrefix(prefix)(txn)
    81  		if err != nil {
    82  			return fmt.Errorf("could not remove transaction results for block %v: %w", blockID, err)
    83  		}
    84  
    85  		return nil
    86  	}
    87  }
    88  
    89  // BatchRemoveTransactionResultsByBlockID removes transaction results for the given blockID in a provided batch.
    90  // No errors are expected during normal operation, but it may return generic error
    91  // if badger fails to process request
    92  func BatchRemoveTransactionResultsByBlockID(blockID flow.Identifier, batch *badger.WriteBatch) func(*badger.Txn) error {
    93  	return func(txn *badger.Txn) error {
    94  
    95  		prefix := makePrefix(codeTransactionResult, blockID)
    96  		err := batchRemoveByPrefix(prefix)(txn, batch)
    97  		if err != nil {
    98  			return fmt.Errorf("could not remove transaction results for block %v: %w", blockID, err)
    99  		}
   100  
   101  		return nil
   102  	}
   103  }