-
Notifications
You must be signed in to change notification settings - Fork 12
/
tx_retainer_cache.go
217 lines (174 loc) · 8.39 KB
/
tx_retainer_cache.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package txretainer
import (
"sync"
"github.com/iotaledger/hive.go/ds/shrinkingmap"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/options"
iotago "github.com/iotaledger/iota.go/v4"
)
// transactionRetainerCache is a cache for transaction metadata changes that are not yet committed to the database.
type transactionRetainerCache struct {
accessMutex sync.RWMutex
txMetadataUpdateFunc txMetadataUpdateFunc
uncommittedTxMetadataChangesByID *shrinkingmap.ShrinkingMap[iotago.TransactionID, *TransactionMetadata]
uncommittedTxIDsBySlot *shrinkingmap.ShrinkingMap[iotago.SlotIndex, map[iotago.TransactionID]struct{}]
}
// defaultTxMetadataUpdateFunc is the default update function for transaction metadata in the transaction retainer cache.
// HINT: do not modify the contents of the old transaction metadata in the update function, as it is used as a reference
// to the earliest attachment slot in the cache.
func defaultTxMetadataUpdateFunc(oldTxMeta *TransactionMetadata, newTxMeta *TransactionMetadata) (*TransactionMetadata, bool, error) {
if oldTxMeta == nil {
// no former transaction metadata exists, return the new one
return newTxMeta, true, nil
}
if oldTxMeta.Equal(newTxMeta) {
// the transaction metadata has not changed, return the old one
return oldTxMeta, false, nil
}
if oldTxMeta.ValidSignature && !newTxMeta.ValidSignature {
// do not update the entry in the cache if the signature was valid before and is invalid now.
// this can for example happen if there is a new attachment with an invalid signature.
return oldTxMeta, false, nil
}
if !oldTxMeta.ValidSignature && newTxMeta.ValidSignature {
// if the signature was invalid before and is valid now, update the entry in the cache.
// all former fields are overwritten by the new transaction metadata, including the earliest attachment slot.
return newTxMeta, true, nil
}
// update the new earliest attachment slot if it is not set in the new transaction metadata, but was set in the old one or it was smaller in the old one.
if newTxMeta.EarliestAttachmentSlot == 0 || (oldTxMeta.EarliestAttachmentSlot != 0 && oldTxMeta.EarliestAttachmentSlot < newTxMeta.EarliestAttachmentSlot) {
newTxMeta.EarliestAttachmentSlot = oldTxMeta.EarliestAttachmentSlot
}
return newTxMeta, true, nil
}
// WithTxRetainerCacheTxMetadataUpdateFunc is an option for the transaction retainer cache that allows to set a custom update function for transaction metadata.
func WithTxRetainerCacheTxMetadataUpdateFunc(updateFunc txMetadataUpdateFunc) options.Option[transactionRetainerCache] {
return func(c *transactionRetainerCache) {
c.txMetadataUpdateFunc = updateFunc
}
}
// NewTransactionRetainerCache creates a new transaction retainer cache.
//
//nolint:revive // only used in the tests
func NewTransactionRetainerCache(opts ...options.Option[transactionRetainerCache]) *transactionRetainerCache {
return options.Apply(&transactionRetainerCache{
uncommittedTxMetadataChangesByID: shrinkingmap.New[iotago.TransactionID, *TransactionMetadata](),
uncommittedTxIDsBySlot: shrinkingmap.New[iotago.SlotIndex, map[iotago.TransactionID]struct{}](),
}, opts, func(c *transactionRetainerCache) {
if c.txMetadataUpdateFunc == nil {
c.txMetadataUpdateFunc = defaultTxMetadataUpdateFunc
}
})
}
// Reset resets the cache.
func (c *transactionRetainerCache) Reset() {
c.accessMutex.Lock()
defer c.accessMutex.Unlock()
c.uncommittedTxMetadataChangesByID.Clear()
c.uncommittedTxIDsBySlot.Clear()
}
// UpdateTxMetadata updates the metadata of a transaction or adds a new one if it didn't exist in the cache before.
func (c *transactionRetainerCache) UpdateTxMetadata(newTxMeta *TransactionMetadata) error {
c.accessMutex.Lock()
defer c.accessMutex.Unlock()
txID := iotago.TransactionID(newTxMeta.TransactionID)
var innerErr error
// update the transaction metadata
c.uncommittedTxMetadataChangesByID.Compute(txID, func(oldTxMeta *TransactionMetadata, exists bool) *TransactionMetadata {
// update the transaction metadata using the update function
updatedTxMeta, updated, err := c.txMetadataUpdateFunc(oldTxMeta, newTxMeta)
if err != nil {
innerErr = err
return oldTxMeta
}
// if the transaction metadata has not changed, return
if !updated {
return updatedTxMeta
}
// we need to keep the list of earliest attachment slots in sync with the transaction metadata changes
if exists {
// update the earliest attachment slot of the transaction if it has changed
if oldTxMeta.EarliestAttachmentSlot != updatedTxMeta.EarliestAttachmentSlot {
c.updateEarliestAttachmentSlotWithoutLocking(txID, oldTxMeta.EarliestAttachmentSlot, updatedTxMeta.EarliestAttachmentSlot)
}
} else {
// add the earliest attachment slot of the new transaction
c.addEarliestAttachmentSlotWithoutLocking(txID, updatedTxMeta.EarliestAttachmentSlot)
}
return updatedTxMeta
})
return innerErr
}
func (c *transactionRetainerCache) addEarliestAttachmentSlotWithoutLocking(txID iotago.TransactionID, slot iotago.SlotIndex) {
uncommittedSlot, _ := c.uncommittedTxIDsBySlot.GetOrCreate(slot, func() map[iotago.TransactionID]struct{} {
return make(map[iotago.TransactionID]struct{})
})
// add the transaction to the slot
uncommittedSlot[txID] = struct{}{}
}
// updateEarliestAttachmentSlotWithoutLocking updates the earliest attachment slot of a transaction without locking the cache.
// the caller needs to ensure that old entry exists in the uncommittedTxIDsBySlot map.
func (c *transactionRetainerCache) updateEarliestAttachmentSlotWithoutLocking(txID iotago.TransactionID, oldSlot iotago.SlotIndex, newSlot iotago.SlotIndex) {
// remove the transaction from the old slot
delete(lo.Return1(c.uncommittedTxIDsBySlot.Get(oldSlot)), txID)
// add the transaction to the new slot
c.addEarliestAttachmentSlotWithoutLocking(txID, newSlot)
}
// UpdateEarliestAttachmentSlot updates the earliest attachment slot of a transaction.
func (c *transactionRetainerCache) UpdateEarliestAttachmentSlot(txID iotago.TransactionID, earliestAttachmentSlot iotago.SlotIndex) {
c.accessMutex.Lock()
defer c.accessMutex.Unlock()
txMeta, exists := c.uncommittedTxMetadataChangesByID.Get(txID)
if !exists {
// the transaction metadata does not exist, no need to update the earliest attachment slot
return
}
// update the earliest attachment slot of the transaction
c.updateEarliestAttachmentSlotWithoutLocking(txID, txMeta.EarliestAttachmentSlot, earliestAttachmentSlot)
// update the earliest attachment slot in the transaction metadata
txMeta.EarliestAttachmentSlot = earliestAttachmentSlot
}
// TransactionMetadataByID returns the transaction metadata of a transaction by its ID.
func (c *transactionRetainerCache) TransactionMetadataByID(txID iotago.TransactionID) (*TransactionMetadata, bool) {
c.accessMutex.RLock()
defer c.accessMutex.RUnlock()
return c.uncommittedTxMetadataChangesByID.Get(txID)
}
// DeleteAndReturnTxMetadataChangesBySlot deletes all the transaction metadata changes of a certain slot and returns them.
func (c *transactionRetainerCache) DeleteAndReturnTxMetadataChangesBySlot(targetSlot iotago.SlotIndex) map[iotago.TransactionID]*TransactionMetadata {
c.accessMutex.Lock()
defer c.accessMutex.Unlock()
var slots []iotago.SlotIndex
// iterate over all slots, to ensure that all slots below the given slot are deleted from the cache as well
c.uncommittedTxIDsBySlot.ForEachKey(func(slot iotago.SlotIndex) bool {
if slot <= targetSlot {
slots = append(slots, slot)
}
return true
})
txIDs := make(map[iotago.TransactionID]struct{})
for _, slot := range slots {
// get the affected transaction IDs for the slot and delete the slot from the cache
slotTxIDs, exists := c.uncommittedTxIDsBySlot.DeleteAndReturn(slot)
if !exists {
// no transaction metadata changes for the slot found
continue
}
// add the affected transaction IDs to the list
for txID := range slotTxIDs {
txIDs[txID] = struct{}{}
}
}
// get all affected transaction metadata changes and delete them from the cache
txMetadataChanges := make(map[iotago.TransactionID]*TransactionMetadata, len(txIDs))
for txID := range txIDs {
txMeta, exists := c.uncommittedTxMetadataChangesByID.DeleteAndReturn(txID)
if !exists {
// transaction metadata not found
panic(ierrors.Errorf("transaction metadata not found for transaction ID %s", txID.ToHex()))
}
txMetadataChanges[txID] = txMeta
}
return txMetadataChanges
}