-
Notifications
You must be signed in to change notification settings - Fork 95
/
api_protocol_epoch_versions.go
91 lines (71 loc) · 2.4 KB
/
api_protocol_epoch_versions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package iotago
import (
"fmt"
"sort"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/stringify"
)
type ProtocolEpochVersions struct {
versionsPerEpoch []ProtocolEpochVersion
knownVersions map[Version]EpochIndex
}
func NewProtocolEpochVersions() *ProtocolEpochVersions {
return &ProtocolEpochVersions{
versionsPerEpoch: make([]ProtocolEpochVersion, 0),
knownVersions: make(map[Version]EpochIndex),
}
}
func (p *ProtocolEpochVersions) VersionForEpoch(epoch EpochIndex) Version {
for i := len(p.versionsPerEpoch) - 1; i >= 0; i-- {
if p.versionsPerEpoch[i].StartEpoch <= epoch {
return p.versionsPerEpoch[i].Version
}
}
// This means that the protocol versions are not properly configured.
panic(ierrors.Errorf("could not find a protocol version for epoch %d", epoch))
}
func (p *ProtocolEpochVersions) Add(version Version, epoch EpochIndex) {
if _, exists := p.knownVersions[version]; exists {
return
}
p.versionsPerEpoch = append(p.versionsPerEpoch, ProtocolEpochVersion{
Version: version,
StartEpoch: epoch,
})
p.knownVersions[version] = epoch
sort.Slice(p.versionsPerEpoch, func(i, j int) bool {
return p.versionsPerEpoch[i].Version < p.versionsPerEpoch[j].Version
})
}
func (p *ProtocolEpochVersions) Slice() []ProtocolEpochVersion {
return lo.CopySlice(p.versionsPerEpoch)
}
func (p *ProtocolEpochVersions) EpochForVersion(version Version) (EpochIndex, bool) {
startEpoch, exists := p.knownVersions[version]
return startEpoch, exists
}
func (p *ProtocolEpochVersions) Bytes() []byte {
versionsBytes := make([]byte, 0)
for _, protocolEpochVersion := range p.versionsPerEpoch {
versionsBytes = append(versionsBytes, protocolEpochVersion.Bytes()...)
}
return versionsBytes
}
func (p *ProtocolEpochVersions) String() string {
builder := stringify.NewStructBuilder("ProtocolEpochVersions")
for i, protocolEpochVersion := range p.versionsPerEpoch {
builder.AddField(stringify.NewStructField(fmt.Sprintf("entry%d", i), protocolEpochVersion.String()))
}
return builder.String()
}
type ProtocolEpochVersion struct {
Version Version
StartEpoch EpochIndex
}
func (p *ProtocolEpochVersion) Bytes() []byte {
return append(lo.PanicOnErr(p.Version.Bytes()), lo.PanicOnErr(p.StartEpoch.Bytes())...)
}
func (p *ProtocolEpochVersion) String() string {
return fmt.Sprintf("Version %d: Epoch %d", p.Version, p.StartEpoch)
}