github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/internal/pkg/txflags/validation_flags.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package txflags
     8  
     9  import (
    10  	"github.com/hyperledger/fabric-protos-go/peer"
    11  )
    12  
    13  // ValidationFlags is array of transaction validation codes. It is used when committer validates block.
    14  type ValidationFlags []uint8
    15  
    16  // New create new object-array of validation codes with target size.
    17  // Default values: TxValidationCode_NOT_VALIDATED
    18  func New(size int) ValidationFlags {
    19  	return newWithValues(size, peer.TxValidationCode_NOT_VALIDATED)
    20  }
    21  
    22  // NewWithValues creates new object-array of validation codes with target size
    23  // and the supplied value
    24  func NewWithValues(size int, value peer.TxValidationCode) ValidationFlags {
    25  	return newWithValues(size, value)
    26  }
    27  
    28  func newWithValues(size int, value peer.TxValidationCode) ValidationFlags {
    29  	inst := make(ValidationFlags, 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 ValidationFlags) SetFlag(txIndex int, flag peer.TxValidationCode) {
    39  	obj[txIndex] = uint8(flag)
    40  }
    41  
    42  // Flag returns validation code at specified transaction
    43  func (obj ValidationFlags) Flag(txIndex int) peer.TxValidationCode {
    44  	return peer.TxValidationCode(obj[txIndex])
    45  }
    46  
    47  // IsValid checks if specified transaction is valid
    48  func (obj ValidationFlags) 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 ValidationFlags) 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 ValidationFlags) IsSetTo(txIndex int, flag peer.TxValidationCode) bool {
    59  	return obj.Flag(txIndex) == flag
    60  }