Coverage for ledger/version.py: 87%
15 statements
« prev ^ index » next coverage.py v7.5.3, created at 2025-07-10 13:43 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2025-07-10 13:43 +0000
1# The MIT License (MIT)
2#
3# Copyright (c) 2021 RSK Labs Ltd
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy of
6# this software and associated documentation files (the "Software"), to deal in
7# the Software without restriction, including without limitation the rights to
8# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9# of the Software, and to permit persons to whom the Software is furnished to do
10# so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
23# Instances of this represent firmware versions
24# of firmware installed on a powHSM.
25# It is also used to represent implemented versions
26# of the powHSM protocol on the middleware manager.
27# A version consists of a major, minor and patch
28class HSM2FirmwareVersion:
29 def __init__(self, major, minor, patch):
30 self.major = major
31 self.minor = minor
32 self.patch = patch
34 def __str__(self):
35 return "v%d.%d.%d" % (self.major, self.minor, self.patch)
37 def __repr__(self):
38 return f"HSM2FirmwareVersion<{str(self)[1:]}>"
40 # This returns true iff this version's
41 # middleware protocol is compatible
42 # with the given firmware running version.
43 #
44 # Declaratively, middleware and firmware major versions must be always equal,
45 # but middleware's <minor, patch> version must be greater or equal than
46 # that of the firmware's.
47 def supports(self, running_version):
48 return (
49 self.major == running_version.major
50 and self.minor >= running_version.minor
51 and (
52 self.minor > running_version.minor or self.patch >= running_version.patch
53 )
54 )
56 # This returns true iff this version is
57 # greater or equal than the given version
58 # (the implementation is the same as that
59 # of the 'supports' method, but the semantics
60 # are different)
61 def __ge__(self, other):
62 return self.supports(other)
64 # Self explanatory
65 def __eq__(self, other):
66 return (
67 self.major == other.major
68 and self.minor == other.minor
69 and self.patch == other.patch
70 )