github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/private/engine/tessera/tessera_version_checker.go (about)

     1  package tessera
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strconv"
     7  
     8  	"github.com/kisexp/xdchain/private/engine"
     9  )
    10  
    11  const versionLength = 3
    12  
    13  type Version [versionLength]uint64
    14  
    15  var (
    16  	zero                         = Version{0, 0, 0}
    17  	privacyEnhancementsVersion   = Version{2, 0, 0}
    18  	multitenancyVersion          = Version{2, 1, 0}
    19  	multiplePrivateStatesVersion = Version{3, 0, 0}
    20  	mandatoryRecipientsVersion   = Version{4, 0, 0}
    21  
    22  	featureVersions = map[engine.PrivateTransactionManagerFeature]Version{
    23  		engine.PrivacyEnhancements:   privacyEnhancementsVersion,
    24  		engine.MultiTenancy:          multitenancyVersion,
    25  		engine.MultiplePrivateStates: multiplePrivateStatesVersion,
    26  		engine.MandatoryRecipients:   mandatoryRecipientsVersion,
    27  	}
    28  )
    29  
    30  func tesseraVersionFeatures(version Version) []engine.PrivateTransactionManagerFeature {
    31  	result := make([]engine.PrivateTransactionManagerFeature, 0)
    32  	for feature, featureVersion := range featureVersions {
    33  		if compareVersions(version, featureVersion) >= 0 {
    34  			result = append(result, feature)
    35  		}
    36  	}
    37  	return result
    38  }
    39  
    40  // compare two versions
    41  // if v1 > v2 - returns 1
    42  // if v1 < v2 - returns -1
    43  // if v1 = v2 - returns 0
    44  func compareVersions(v1, v2 Version) int {
    45  	for i := 0; i < versionLength; i++ {
    46  		if v1[i] > v2[i] {
    47  			return 1
    48  		} else if v1[i] < v2[i] {
    49  			return -1
    50  		}
    51  	}
    52  	return 0
    53  }
    54  
    55  // The tessera release versions have 3 components: major.mid.minor.
    56  // Snapshot tessera builds may have versions made of 2 components: major.mid-SNAPSHOT.
    57  // parseVersion will assume the minor version to be 0 for versions with only 2 components.
    58  func parseVersion(version []byte) (res Version, err error) {
    59  	versionMajMidRegExp, _ := regexp.Compile(`([0-9]+)\.([0-9]+)([^0-9].*)?`)
    60  	versionMajMidMinRegExp, _ := regexp.Compile(`([0-9]+)\.([0-9]+)\.([0-9]+)([^0-9].*)?`)
    61  
    62  	var submatch [][]byte
    63  	if versionMajMidMinRegExp.Match(version) {
    64  		submatch = versionMajMidMinRegExp.FindSubmatch(version)[1:4]
    65  	} else if versionMajMidRegExp.Match(version) {
    66  		submatch = versionMajMidRegExp.FindSubmatch(version)[1:3]
    67  	} else {
    68  		return zero, fmt.Errorf("input does not match the expected version pattern")
    69  	}
    70  
    71  	// res should be initialized with {0,0,0} - thus it is ok for submatch to have a variable length of 2 or 3
    72  	for idx, val := range submatch {
    73  		intVal, err := strconv.ParseUint(string(val), 10, 64)
    74  		if err != nil {
    75  			return zero, err
    76  		}
    77  		res[idx] = intVal
    78  	}
    79  	return res, nil
    80  }