-
Notifications
You must be signed in to change notification settings - Fork 12
/
state_metadata.go
177 lines (140 loc) · 4.72 KB
/
state_metadata.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
package mempoolv1
import (
"sync/atomic"
"github.com/iotaledger/hive.go/ds/reactive"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/event"
"github.com/iotaledger/hive.go/runtime/promise"
"github.com/iotaledger/iota-core/pkg/protocol/engine/mempool"
iotago "github.com/iotaledger/iota.go/v4"
)
type StateMetadata struct {
state mempool.State
// lifecycle
spenderCount uint64
spent *promise.Event
doubleSpent *promise.Event
spendAccepted reactive.Variable[*TransactionMetadata]
spendCommitted reactive.Variable[*TransactionMetadata]
inclusionSlot reactive.Variable[*iotago.SlotIndex]
allSpendersRemoved *event.Event
spenderIDs reactive.DerivedSet[iotago.TransactionID]
*inclusionFlags
}
func NewStateMetadata(state mempool.State, optSource ...*TransactionMetadata) *StateMetadata {
return (&StateMetadata{
state: state,
spent: promise.NewEvent(),
doubleSpent: promise.NewEvent(),
spendAccepted: reactive.NewVariable[*TransactionMetadata](),
spendCommitted: reactive.NewVariable[*TransactionMetadata](),
inclusionSlot: reactive.NewVariable[*iotago.SlotIndex](),
allSpendersRemoved: event.New(),
spenderIDs: reactive.NewDerivedSet[iotago.TransactionID](),
inclusionFlags: newInclusionFlags(),
}).setup(optSource...)
}
func (s *StateMetadata) setup(optSource ...*TransactionMetadata) *StateMetadata {
if len(optSource) == 0 {
return s
}
source := optSource[0]
s.spenderIDs.InheritFrom(source.spenderIDs)
source.earliestIncludedValidAttachment.OnUpdate(func(_, newValue iotago.BlockID) {
s.inclusionSlot.Compute(func(currentValue *iotago.SlotIndex) *iotago.SlotIndex {
if newSlot := newValue.Slot(); currentValue == nil || newSlot < *currentValue {
return &newSlot
}
return currentValue
})
})
source.OnAccepted(func() { s.accepted.Set(true) })
source.OnRejected(func() { s.rejected.Trigger() })
source.OnCommittedSlotUpdated(lo.Void(s.committedSlot.Set))
source.OnOrphanedSlotUpdated(lo.Void(s.orphanedSlot.Set))
return s
}
func (s *StateMetadata) State() mempool.State {
return s.state
}
func (s *StateMetadata) SpenderIDs() reactive.Set[iotago.TransactionID] {
return s.spenderIDs
}
func (s *StateMetadata) IsDoubleSpent() bool {
return s.doubleSpent.WasTriggered()
}
func (s *StateMetadata) OnDoubleSpent(callback func()) {
s.doubleSpent.OnTrigger(callback)
}
func (s *StateMetadata) AcceptedSpender() (mempool.TransactionMetadata, bool) {
acceptedSpender := s.spendAccepted.Get()
return acceptedSpender, acceptedSpender != nil
}
func (s *StateMetadata) OnAcceptedSpenderUpdated(callback func(spender mempool.TransactionMetadata)) {
s.spendAccepted.OnUpdate(func(prevValue *TransactionMetadata, newValue *TransactionMetadata) {
if prevValue != newValue {
callback(newValue)
}
})
}
func (s *StateMetadata) OnSpendCommitted(callback func(spender mempool.TransactionMetadata)) {
s.spendCommitted.OnUpdate(func(prevValue *TransactionMetadata, newValue *TransactionMetadata) {
if prevValue != newValue {
callback(newValue)
}
})
}
func (s *StateMetadata) AllSpendersRemoved() bool {
return s.allSpendersRemoved.WasTriggered()
}
func (s *StateMetadata) onAllSpendersRemoved(callback func()) {
s.allSpendersRemoved.Hook(callback)
}
func (s *StateMetadata) PendingSpenderCount() int {
return int(atomic.LoadUint64(&s.spenderCount))
}
func (s *StateMetadata) HasNoSpenders() bool {
return atomic.LoadUint64(&s.spenderCount) == 0
}
func (s *StateMetadata) InclusionSlot() iotago.SlotIndex {
return *s.inclusionSlot.Get()
}
func (s *StateMetadata) OnInclusionSlotUpdated(callback func(prevID iotago.SlotIndex, newID iotago.SlotIndex)) {
s.inclusionSlot.OnUpdate(func(oldValue *iotago.SlotIndex, newValue *iotago.SlotIndex) {
switch {
case oldValue == nil:
callback(iotago.SlotIndex(0), *newValue)
case newValue == nil:
callback(*oldValue, iotago.SlotIndex(0))
default:
callback(*oldValue, *newValue)
}
})
}
func (s *StateMetadata) increaseSpenderCount() {
if spenderCount := atomic.AddUint64(&s.spenderCount, 1); spenderCount == 1 {
s.spent.Trigger()
} else if spenderCount == 2 {
s.doubleSpent.Trigger()
}
}
func (s *StateMetadata) decreaseSpenderCount() {
if atomic.AddUint64(&s.spenderCount, ^uint64(0)) == 0 {
s.allSpendersRemoved.Trigger()
}
}
func (s *StateMetadata) setupSpender(spender *TransactionMetadata) {
s.increaseSpenderCount()
spender.OnAccepted(func() {
if !s.state.IsReadOnly() {
s.spendAccepted.Set(spender)
}
})
spender.OnCommittedSlotUpdated(func(_ iotago.SlotIndex) {
if !s.state.IsReadOnly() {
s.spendCommitted.Set(spender)
}
s.decreaseSpenderCount()
})
spender.OnOrphanedSlotUpdated(func(_ iotago.SlotIndex) { s.decreaseSpenderCount() })
}