github.com/lzy4123/fabric@v2.1.1+incompatible/core/ledger/util/txvalidationflags.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package util
     8  
     9  import (
    10  	"github.com/hyperledger/fabric-protos-go/peer"
    11  )
    12  
    13  // TxValidationFlags is array of transaction validation codes. It is used when committer validates block.
    14  type TxValidationFlags []uint8
    15  
    16  // NewTxValidationFlags Create new object-array of validation codes with target size.
    17  // Default values: TxValidationCode_NOT_VALIDATED
    18  func NewTxValidationFlags(size int) TxValidationFlags {
    19  	return newTxValidationFlagsSetValue(size, peer.TxValidationCode_NOT_VALIDATED)
    20  }
    21  
    22  // NewTxValidationFlagsSetValue Creates new object-array of validation codes with target size
    23  // and the supplied value
    24  func NewTxValidationFlagsSetValue(size int, value peer.TxValidationCode) TxValidationFlags {
    25  	return newTxValidationFlagsSetValue(size, value)
    26  }
    27  
    28  func newTxValidationFlagsSetValue(size int, value peer.TxValidationCode) TxValidationFlags {
    29  	inst := make(TxValidationFlags, size)
    30  	for i := range inst {
    31  		inst[i] = uint8(value)
    32  	}
    33  
    34  	return inst
    35  }
    36  
    37  // SetFlag assigns validation code to specified transaction
    38  func (obj TxValidationFlags) SetFlag(txIndex int, flag peer.TxValidationCode) {
    39  	obj[txIndex] = uint8(flag)
    40  }
    41  
    42  // Flag returns validation code at specified transaction
    43  func (obj TxValidationFlags) Flag(txIndex int) peer.TxValidationCode {
    44  	return peer.TxValidationCode(obj[txIndex])
    45  }
    46  
    47  // IsValid checks if specified transaction is valid
    48  func (obj TxValidationFlags) IsValid(txIndex int) bool {
    49  	return obj.IsSetTo(txIndex, peer.TxValidationCode_VALID)
    50  }
    51  
    52  // IsInvalid checks if specified transaction is invalid
    53  func (obj TxValidationFlags) IsInvalid(txIndex int) bool {
    54  	return !obj.IsValid(txIndex)
    55  }
    56  
    57  // IsSetTo returns true if the specified transaction equals flag; false otherwise.
    58  func (obj TxValidationFlags) IsSetTo(txIndex int, flag peer.TxValidationCode) bool {
    59  	return obj.Flag(txIndex) == flag
    60  }