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