-
Notifications
You must be signed in to change notification settings - Fork 12
/
sorted_spender.go
197 lines (149 loc) · 6.81 KB
/
sorted_spender.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package spenddagv1
import (
"bytes"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/event"
"github.com/iotaledger/hive.go/runtime/syncutils"
"github.com/iotaledger/iota-core/pkg/core/acceptance"
"github.com/iotaledger/iota-core/pkg/core/weight"
"github.com/iotaledger/iota-core/pkg/protocol/engine/mempool/spenddag"
)
// sortedSpend is a wrapped Spender that contains additional information for the SortedSpenders.
type sortedSpender[SpenderID, ResourceID spenddag.IDType, VoteRank spenddag.VoteRankType[VoteRank]] struct {
// sortedSet is the SortedSpenders that contains this sortedSpender.
sortedSet *SortedSpenders[SpenderID, ResourceID, VoteRank]
// lighterMember is the sortedSpender that is lighter than this one.
lighterMember *sortedSpender[SpenderID, ResourceID, VoteRank]
// heavierMember is the sortedSpender that is heavierMember than this one.
heavierMember *sortedSpender[SpenderID, ResourceID, VoteRank]
// currentWeight is the current weight of the Spender.
currentWeight weight.Value
// queuedWeight is the weight that is queued to be applied to the Spender.
queuedWeight *weight.Value
// weightMutex is used to protect the currentWeight and queuedWeight.
weightMutex syncutils.RWMutex
// currentPreferredInstead is the current PreferredInstead value of the Spender.
currentPreferredInstead *Spender[SpenderID, ResourceID, VoteRank]
// queuedPreferredInstead is the PreferredInstead value that is queued to be applied to the Spender.
queuedPreferredInstead *Spender[SpenderID, ResourceID, VoteRank]
// preferredMutex is used to protect the currentPreferredInstead and queuedPreferredInstead.
preferredInsteadMutex syncutils.RWMutex
onAcceptanceStateUpdatedHook *event.Hook[func(acceptance.State, acceptance.State)]
// onWeightUpdatedHook is the hook that is triggered when the weight of the Spender is updated.
onWeightUpdatedHook *event.Hook[func(weight.Value)]
// onPreferredUpdatedHook is the hook that is triggered when the PreferredInstead value of the Spender is updated.
onPreferredUpdatedHook *event.Hook[func(*Spender[SpenderID, ResourceID, VoteRank])]
// Spender is the wrapped Spender.
*Spender[SpenderID, ResourceID, VoteRank]
}
// newSortedSpender creates a new sortedSpender.
func newSortedSpender[SpenderID, ResourceID spenddag.IDType, VoteRank spenddag.VoteRankType[VoteRank]](set *SortedSpenders[SpenderID, ResourceID, VoteRank], spender *Spender[SpenderID, ResourceID, VoteRank]) *sortedSpender[SpenderID, ResourceID, VoteRank] {
s := &sortedSpender[SpenderID, ResourceID, VoteRank]{
sortedSet: set,
currentWeight: spender.Weight.Value(),
currentPreferredInstead: spender.PreferredInstead(),
Spender: spender,
}
if set.owner != nil {
s.onAcceptanceStateUpdatedHook = spender.AcceptanceStateUpdated.Hook(s.onAcceptanceStateUpdated)
}
s.onWeightUpdatedHook = spender.Weight.OnUpdate.Hook(s.queueWeightUpdate)
s.onPreferredUpdatedHook = spender.PreferredInsteadUpdated.Hook(s.queuePreferredInsteadUpdate)
return s
}
// Weight returns the current weight of the sortedSpender.
func (s *sortedSpender[SpenderID, ResourceID, VoteRank]) Weight() weight.Value {
s.weightMutex.RLock()
defer s.weightMutex.RUnlock()
return s.currentWeight
}
// Compare compares the sortedSpend to another sortedSpender.
func (s *sortedSpender[SpenderID, ResourceID, VoteRank]) Compare(other *sortedSpender[SpenderID, ResourceID, VoteRank]) int {
if result := s.Weight().Compare(other.Weight()); result != weight.Equal {
return result
}
return bytes.Compare(lo.PanicOnErr(s.ID.Bytes()), lo.PanicOnErr(other.ID.Bytes()))
}
// PreferredInstead returns the current preferred instead value of the sortedSpender.
func (s *sortedSpender[SpenderID, ResourceID, VoteRank]) PreferredInstead() *Spender[SpenderID, ResourceID, VoteRank] {
s.preferredInsteadMutex.RLock()
defer s.preferredInsteadMutex.RUnlock()
return s.currentPreferredInstead
}
// IsPreferred returns true if the sortedSpender is preferred instead of its Spenders.
func (s *sortedSpender[SpenderID, ResourceID, VoteRank]) IsPreferred() bool {
return s.PreferredInstead() == s.Spender
}
// Unhook cleans up the sortedSpender.
func (s *sortedSpender[SpenderID, ResourceID, VoteRank]) Unhook() {
if s.onAcceptanceStateUpdatedHook != nil {
s.onAcceptanceStateUpdatedHook.Unhook()
s.onAcceptanceStateUpdatedHook = nil
}
if s.onWeightUpdatedHook != nil {
s.onWeightUpdatedHook.Unhook()
s.onWeightUpdatedHook = nil
}
if s.onPreferredUpdatedHook != nil {
s.onPreferredUpdatedHook.Unhook()
s.onPreferredUpdatedHook = nil
}
}
func (s *sortedSpender[SpenderID, ResourceID, VoteRank]) onAcceptanceStateUpdated(_ acceptance.State, newState acceptance.State) {
if newState.IsAccepted() {
s.sortedSet.owner.setAcceptanceState(acceptance.Rejected)
}
}
// queueWeightUpdate queues a weight update for the sortedSpender.
func (s *sortedSpender[SpenderID, ResourceID, VoteRank]) queueWeightUpdate(newWeight weight.Value) {
s.weightMutex.Lock()
defer s.weightMutex.Unlock()
if (s.queuedWeight == nil && s.currentWeight == newWeight) || (s.queuedWeight != nil && *s.queuedWeight == newWeight) {
return
}
s.queuedWeight = &newWeight
s.sortedSet.notifyPendingWeightUpdate(s)
}
// weightUpdateApplied tries to apply a queued weight update to the sortedSpend and returns true if successful.
func (s *sortedSpender[SpenderID, ResourceID, VoteRank]) weightUpdateApplied() bool {
s.weightMutex.Lock()
defer s.weightMutex.Unlock()
if s.queuedWeight == nil {
return false
}
if *s.queuedWeight == s.currentWeight {
s.queuedWeight = nil
return false
}
s.currentWeight = *s.queuedWeight
s.queuedWeight = nil
return true
}
// queuePreferredInsteadUpdate notifies the sortedSet that the preferred instead flag of the Spend was updated.
func (s *sortedSpender[SpenderID, ResourceID, VoteRank]) queuePreferredInsteadUpdate(spender *Spender[SpenderID, ResourceID, VoteRank]) {
s.preferredInsteadMutex.Lock()
defer s.preferredInsteadMutex.Unlock()
if (s.queuedPreferredInstead == nil && s.currentPreferredInstead == spender) ||
(s.queuedPreferredInstead != nil && s.queuedPreferredInstead == spender) ||
s.sortedSet.owner.Spender == spender {
return
}
s.queuedPreferredInstead = spender
s.sortedSet.notifyPendingPreferredInsteadUpdate(s)
}
// preferredInsteadUpdateApplied tries to apply a queued preferred instead update to the sortedSpend and returns
// true if successful.
func (s *sortedSpender[SpenderID, ResourceID, VoteRank]) preferredInsteadUpdateApplied() bool {
s.preferredInsteadMutex.Lock()
defer s.preferredInsteadMutex.Unlock()
if s.queuedPreferredInstead == nil {
return false
}
if s.queuedPreferredInstead == s.currentPreferredInstead {
s.queuedPreferredInstead = nil
return false
}
s.currentPreferredInstead = s.queuedPreferredInstead
s.queuedPreferredInstead = nil
return true
}