-
Notifications
You must be signed in to change notification settings - Fork 95
/
allotment.go
92 lines (73 loc) · 2.28 KB
/
allotment.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
92
package iotago
import (
"bytes"
"math"
"sort"
"github.com/iotaledger/hive.go/core/safemath"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/serializer/v2"
)
// BlockIssuanceCredits defines the type of block issuance credits.
type BlockIssuanceCredits int64
const MaxBlockIssuanceCredits = BlockIssuanceCredits(math.MaxInt64)
// Allotment is a struct that represents a list of account IDs and an allotted value.
type Allotment struct {
AccountID AccountID `serix:""`
Mana Mana `serix:""`
}
func (a *Allotment) Clone() *Allotment {
return &Allotment{
AccountID: a.AccountID,
Mana: a.Mana,
}
}
func (a *Allotment) Compare(other *Allotment) int {
return bytes.Compare(a.AccountID[:], other.AccountID[:])
}
// Allotments is a slice of Allotment.
type Allotments []*Allotment
func (a Allotments) Clone() Allotments {
return lo.CloneSlice(a)
}
// Sort sorts the allotments in lexical order.
func (a Allotments) Sort() {
sort.Slice(a, func(i, j int) bool {
return a[i].Compare(a[j]) < 0
})
}
func (a Allotments) Size() int {
// LengthPrefixType
return serializer.UInt16ByteSize + len(a)*(AccountIDLength+ManaSize)
}
func (a Allotments) WorkScore(workScoreParameters *WorkScoreParameters) (WorkScore, error) {
// Allotments requires invocation of account managers, so requires extra work.
workScoreAllotments, err := workScoreParameters.Allotment.Multiply(len(a))
if err != nil {
return 0, err
}
return workScoreAllotments, nil
}
func (a Allotments) Get(id AccountID) Mana {
for _, allotment := range a {
if allotment.AccountID == id {
return allotment.Mana
}
}
return 0
}
// allotmentMaxManaValidator checks that the sum of all allotted mana does not exceed 2^(Mana Bits Count) - 1.
func allotmentMaxManaValidator(maxManaValue Mana) ElementValidationFunc[*Allotment] {
var sum Mana
return func(index int, next *Allotment) error {
var err error
sum, err = safemath.SafeAdd(sum, next.Mana)
if err != nil {
return ierrors.Join(ErrMaxManaExceeded, ierrors.Wrapf(err, "allotment mana sum calculation failed at allotment %d", index))
}
if sum > maxManaValue {
return ierrors.WithMessagef(ErrMaxManaExceeded, "sum of allotted mana exceeds max value with allotment %d", index)
}
return nil
}
}