-
Notifications
You must be signed in to change notification settings - Fork 12
/
validator_performance.go
77 lines (63 loc) · 2.58 KB
/
validator_performance.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
package model
import (
"io"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/serializer/v2/stream"
"github.com/iotaledger/hive.go/stringify"
)
type ValidatorPerformance struct {
// works if ValidationBlocksPerSlot is less than 32 because we use it as bit vector
SlotActivityVector uint32
// can be uint8 because max count per slot is maximally ValidationBlocksPerSlot + 1
BlocksIssuedCount uint8
HighestSupportedVersionAndHash VersionAndHash
}
func NewValidatorPerformance() *ValidatorPerformance {
return &ValidatorPerformance{
SlotActivityVector: 0,
BlocksIssuedCount: 0,
HighestSupportedVersionAndHash: VersionAndHash{},
}
}
func ValidatorPerformanceFromBytes(bytes []byte) (*ValidatorPerformance, int, error) {
byteReader := stream.NewByteReader(bytes)
v, err := ValidatorPerformanceFromReader(byteReader)
if err != nil {
return nil, 0, ierrors.Wrap(err, "failed to parse ValidatorPerformance")
}
return v, byteReader.BytesRead(), nil
}
func ValidatorPerformanceFromReader(reader io.ReadSeeker) (*ValidatorPerformance, error) {
var err error
v := NewValidatorPerformance()
if v.SlotActivityVector, err = stream.Read[uint32](reader); err != nil {
return nil, ierrors.Wrap(err, "failed to read SlotActivityVector")
}
if v.BlocksIssuedCount, err = stream.Read[uint8](reader); err != nil {
return nil, ierrors.Wrap(err, "failed to read BlocksIssuedCount")
}
if v.HighestSupportedVersionAndHash, err = stream.ReadObject(reader, VersionAndHashSize, VersionAndHashFromBytes); err != nil {
return nil, ierrors.Wrap(err, "failed to read HighestSupportedVersionAndHash")
}
return v, nil
}
func (p *ValidatorPerformance) Bytes() ([]byte, error) {
byteBuffer := stream.NewByteBuffer()
if err := stream.Write(byteBuffer, p.SlotActivityVector); err != nil {
return nil, ierrors.Wrap(err, "failed to write SlotActivityVector")
}
if err := stream.Write(byteBuffer, p.BlocksIssuedCount); err != nil {
return nil, ierrors.Wrap(err, "failed to write BlocksIssuedCount")
}
if err := stream.WriteObject(byteBuffer, p.HighestSupportedVersionAndHash, VersionAndHash.Bytes); err != nil {
return nil, ierrors.Wrap(err, "failed to write HighestSupportedVersionAndHash")
}
return byteBuffer.Bytes()
}
func (p *ValidatorPerformance) String() string {
return stringify.Struct("ValidatorPerformance",
stringify.NewStructField("SlotActivityVector", p.SlotActivityVector),
stringify.NewStructField("BlocksIssuedCount", p.BlocksIssuedCount),
stringify.NewStructField("HighestSupportedVersionAndHash", p.HighestSupportedVersionAndHash),
)
}