github.com/Night-mk/quorum@v21.1.0+incompatible/private/engine/tessera/tessera_version_checker.go (about)

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