github.com/amazechain/amc@v0.1.3/params/version.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package params
    18  
    19  import (
    20  	"fmt"
    21  	"github.com/amazechain/amc/modules"
    22  	"github.com/ledgerwatch/erigon-lib/kv"
    23  )
    24  
    25  var (
    26  	// Following vars are injected through the build flags (see Makefile)
    27  	GitCommit string
    28  	GitBranch string
    29  	GitTag    string
    30  )
    31  
    32  // see https://calver.org
    33  const (
    34  	VersionMajor       = 0  // Major version component of the current release
    35  	VersionMinor       = 1  // Minor version component of the current release
    36  	VersionMicro       = 1  // Patch version component of the current release
    37  	VersionModifier    = "" // Modifier component of the current release
    38  	VersionKeyCreated  = "AmcVersionCreated"
    39  	VersionKeyFinished = "AmcVersionFinished"
    40  )
    41  
    42  func withModifier(vsn string) string {
    43  	if !isStable() {
    44  		vsn += "-" + VersionModifier
    45  	}
    46  	return vsn
    47  }
    48  
    49  func isStable() bool {
    50  	return VersionModifier == "stable"
    51  }
    52  
    53  func isRelease() bool {
    54  	return isStable() || VersionModifier == "alpha" || VersionModifier == "beta"
    55  }
    56  
    57  // Version holds the textual version string.
    58  var Version = func() string {
    59  	return fmt.Sprintf("%d.%02d.%d", VersionMajor, VersionMinor, VersionMicro)
    60  }()
    61  
    62  // VersionWithMeta holds the textual version string including the metadata.
    63  var VersionWithMeta = func() string {
    64  	v := Version
    65  	if VersionModifier != "" {
    66  		v += "-" + VersionModifier
    67  	}
    68  	return v
    69  }()
    70  
    71  // ArchiveVersion holds the textual version string used for Geth archives.
    72  // e.g. "1.8.11-dea1ce05" for stable releases, or
    73  //
    74  //	"1.8.13-unstable-21c059b6" for unstable releases
    75  func ArchiveVersion(gitCommit string) string {
    76  	vsn := withModifier(Version)
    77  	if len(gitCommit) >= 8 {
    78  		vsn += "-" + gitCommit[:8]
    79  	}
    80  	return vsn
    81  }
    82  
    83  func VersionWithCommit(gitCommit, gitDate string) string {
    84  	vsn := VersionWithMeta
    85  	if len(gitCommit) >= 8 {
    86  		vsn += "-" + gitCommit[:8]
    87  	}
    88  	return vsn
    89  }
    90  
    91  func SetAmcVersion(tx kv.RwTx, versionKey string) error {
    92  	versionKeyByte := []byte(versionKey)
    93  	hasVersion, err := tx.Has(modules.DatabaseInfo, versionKeyByte)
    94  	if err != nil {
    95  		return err
    96  	}
    97  	if hasVersion {
    98  		return nil
    99  	}
   100  	// Save version if it does not exist
   101  	if err := tx.Put(modules.DatabaseInfo, versionKeyByte, []byte(Version)); err != nil {
   102  		return err
   103  	}
   104  	return nil
   105  }