frostfs-contract/common/version.go
Evgenii Stratonikov ba7329c3a7 [#103] common: Disallow downgrading contracts
`PrevVersion` marks suitable version that we can upgrade from.
However, we can have multiple minor versions, so, currently an upgrade
from v0.19.3 to v0.19.1 is possible. Prevent this with an additional
check.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2024-08-19 13:57:03 +03:00

46 lines
1.3 KiB
Go

package common
import "github.com/nspcc-dev/neo-go/pkg/interop/native/std"
const (
major = 0
minor = 19
patch = 1
// Versions from which an update should be performed.
// These should be used in a group (so prevMinor can be equal to minor if there are
// any migration routines.
prevMajor = 0
prevMinor = 18
prevPatch = 0
Version = major*1_000_000 + minor*1_000 + patch
PrevVersion = prevMajor*1_000_000 + prevMinor*1_000 + prevPatch
// ErrVersionMismatch is thrown by CheckVersion in case of error.
ErrVersionMismatch = "previous version mismatch"
// ErrAlreadyUpdated is thrown by CheckVersion if current version equals to version contract
// is being updated from.
ErrAlreadyUpdated = "contract is already of the latest version"
)
// CheckVersion checks that previous version is more than PrevVersion to ensure migrating contract data
// was done successfully.
func CheckVersion(from int) {
if from < PrevVersion {
panic(ErrVersionMismatch + ": expected >=" + std.Itoa(PrevVersion, 10))
}
if Version <= from {
panic(ErrAlreadyUpdated + ": " + std.Itoa(Version, 10))
}
}
// AppendVersion appends current contract version to the list of deploy arguments.
func AppendVersion(data any) []interface{} {
if data == nil {
return []any{Version}
}
return append(data.([]any), Version)
}