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

     1  package tessera
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/kisexp/xdchain/log"
     9  	"github.com/kisexp/xdchain/private/engine"
    10  )
    11  
    12  const apiVersion1 = "1.0"
    13  
    14  // this method will be removed once quorum will implement a versioned tessera client (in line with tessera API versioning)
    15  func RetrieveTesseraAPIVersion(client *engine.Client) string {
    16  	res, err := client.Get("/version/api")
    17  	if err != nil {
    18  		log.Error("Error invoking the tessera /version/api API:", "err", err)
    19  		return apiVersion1
    20  	}
    21  	defer res.Body.Close()
    22  	if res.StatusCode != http.StatusOK {
    23  		log.Error(fmt.Sprintf("Invalid status code returned by the tessera /version/api API: %d.", res.StatusCode))
    24  		return apiVersion1
    25  	}
    26  	var versions []string
    27  	if err := json.NewDecoder(res.Body).Decode(&versions); err != nil {
    28  		log.Error("Unable to deserialize the tessera response for /version/api API:", "err", err)
    29  		return apiVersion1
    30  	}
    31  	if len(versions) == 0 {
    32  		log.Error("Expecting at least one API version to be returned by the tessera /version/api API.")
    33  		return apiVersion1
    34  	}
    35  	// pick the latest version from the versions array
    36  	latestVersion := apiVersion1
    37  	latestParsedVersion, _ := parseVersion([]byte(latestVersion))
    38  	for _, ver := range versions {
    39  		if len(ver) == 0 {
    40  			log.Error("Invalid (empty) version returned by the tessera /version/api API. Skipping value.")
    41  			continue
    42  		}
    43  		parsedVer, err := parseVersion([]byte(ver))
    44  		if err != nil {
    45  			log.Error(fmt.Sprintf("Unable to parse version returned by the tessera /version/api API: %s. Skipping value.", ver))
    46  			continue
    47  		}
    48  		if compareVersions(parsedVer, latestParsedVersion) > 0 {
    49  			latestVersion = ver
    50  			latestParsedVersion = parsedVer
    51  		}
    52  	}
    53  	log.Info(fmt.Sprintf("Tessera API version: %s", latestVersion))
    54  	return latestVersion
    55  }