-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle_transaction_storage.go
362 lines (288 loc) · 11.8 KB
/
bundle_transaction_storage.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package tangle
import (
"bytes"
"fmt"
"time"
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/objectstorage"
"github.com/gohornet/hornet/pkg/model/hornet"
"github.com/gohornet/hornet/pkg/profile"
)
const (
BundleTxIsTail = 1
)
var (
bundleTransactionsStorage *objectstorage.ObjectStorage
)
func databaseKeyPrefixForBundleHash(bundleHash hornet.Hash) []byte {
return bundleHash
}
func databaseKeyForBundleTransaction(bundleHash hornet.Hash, txHash hornet.Hash, isTail bool) []byte {
var isTailByte byte
if isTail {
isTailByte = BundleTxIsTail
}
result := append(databaseKeyPrefixForBundleHash(bundleHash), isTailByte)
return append(result, txHash...)
}
func bundleTransactionFactory(key []byte) (objectstorage.StorableObject, int, error) {
bundleTx := &BundleTransaction{
BundleHash: make([]byte, 49),
IsTail: key[49] == BundleTxIsTail,
TxHash: make([]byte, 49),
}
copy(bundleTx.BundleHash, key[:49])
copy(bundleTx.TxHash, key[50:])
return bundleTx, 99, nil
}
func GetBundleTransactionsStorageSize() int {
return bundleTransactionsStorage.GetSize()
}
func configureBundleTransactionsStorage(store kvstore.KVStore, opts profile.CacheOpts) {
bundleTransactionsStorage = objectstorage.New(
store.WithRealm([]byte{StorePrefixBundleTransactions}),
bundleTransactionFactory,
objectstorage.CacheTime(time.Duration(opts.CacheTimeMs)*time.Millisecond),
objectstorage.PersistenceEnabled(true),
objectstorage.PartitionKey(49, 1, 49), // BundleHash, IsTail, TxHash
objectstorage.KeysOnly(true),
objectstorage.StoreOnCreation(true),
objectstorage.LeakDetectionEnabled(opts.LeakDetectionOptions.Enabled,
objectstorage.LeakDetectionOptions{
MaxConsumersPerObject: opts.LeakDetectionOptions.MaxConsumersPerObject,
MaxConsumerHoldTime: time.Duration(opts.LeakDetectionOptions.MaxConsumerHoldTimeSec) * time.Second,
}),
)
}
// Storable Object
type BundleTransaction struct {
objectstorage.StorableObjectFlags
// Key
BundleHash hornet.Hash
IsTail bool
TxHash hornet.Hash
}
func (bt *BundleTransaction) GetTxHash() hornet.Hash {
return bt.TxHash
}
func (bt *BundleTransaction) GetBundleHash() hornet.Hash {
return bt.BundleHash
}
// ObjectStorage interface
func (bt *BundleTransaction) Update(_ objectstorage.StorableObject) {
panic(fmt.Sprintf("BundleTransaction should never be updated: %v, TxHash: %v", bt.BundleHash.Trytes(), bt.TxHash.Trytes()))
}
func (bt *BundleTransaction) ObjectStorageKey() []byte {
var isTailByte byte
if bt.IsTail {
isTailByte = BundleTxIsTail
}
result := append(bt.BundleHash, isTailByte)
return append(result, bt.TxHash...)
}
func (bt *BundleTransaction) ObjectStorageValue() (_ []byte) {
return nil
}
func (bt *BundleTransaction) UnmarshalObjectStorageValue(_ []byte) (consumedBytes int, err error) {
return 0, nil
}
// Cached Object
type CachedBundleTransaction struct {
objectstorage.CachedObject
}
type CachedBundleTransactions []*CachedBundleTransaction
func (cachedBundleTransactions CachedBundleTransactions) Retain() CachedBundleTransactions {
cachedResult := CachedBundleTransactions{}
for _, cachedBundleTransaction := range cachedBundleTransactions {
cachedResult = append(cachedResult, cachedBundleTransaction.Retain().(*CachedBundleTransaction))
}
return cachedResult
}
func (cachedBundleTransactions CachedBundleTransactions) Release(force ...bool) {
for _, cachedBundleTransaction := range cachedBundleTransactions {
cachedBundleTransaction.Release(force...)
}
}
func (c *CachedBundleTransaction) GetBundleTransaction() *BundleTransaction {
return c.Get().(*BundleTransaction)
}
// bundleTx +1
func GetCachedBundleTransactionOrNil(bundleHash hornet.Hash, txHash hornet.Hash, isTail bool) *CachedBundleTransaction {
cachedBundleTx := bundleTransactionsStorage.Load(databaseKeyForBundleTransaction(bundleHash, txHash, isTail)) // bundleTx +1
if !cachedBundleTx.Exists() {
cachedBundleTx.Release(true) // bundleTx -1
return nil
}
return &CachedBundleTransaction{CachedObject: cachedBundleTx}
}
// bundleTx +-0
func GetBundleTransactionHashes(bundleHash hornet.Hash, forceRelease bool, maxFind ...int) hornet.Hashes {
var bundleTransactionHashes hornet.Hashes
i := 0
bundleTransactionsStorage.ForEachKeyOnly(func(key []byte) bool {
i++
if (len(maxFind) > 0) && (i > maxFind[0]) {
return false
}
bundleTransactionHashes = append(bundleTransactionHashes, key[50:99])
return true
}, false, databaseKeyPrefixForBundleHash(bundleHash))
return bundleTransactionHashes
}
// bundleTx +1
func GetAllBundleTransactionHashes(maxFind ...int) hornet.Hashes {
var bundleTransactionHashes hornet.Hashes
i := 0
bundleTransactionsStorage.ForEachKeyOnly(func(key []byte) bool {
i++
if (len(maxFind) > 0) && (i > maxFind[0]) {
return false
}
bundleTransactionHashes = append(bundleTransactionHashes, key[50:99])
return true
}, false)
return bundleTransactionHashes
}
// bundleTx +1
func GetBundleTailTransactionHashes(bundleHash hornet.Hash, forceRelease bool, maxFind ...int) hornet.Hashes {
var bundleTransactionHashes hornet.Hashes
i := 0
bundleTransactionsStorage.ForEachKeyOnly(func(key []byte) bool {
i++
if (len(maxFind) > 0) && (i > maxFind[0]) {
return false
}
bundleTransactionHashes = append(bundleTransactionHashes, key[50:99])
return true
}, false, append(databaseKeyPrefixForBundleHash(bundleHash), BundleTxIsTail))
return bundleTransactionHashes
}
// BundleTransactionConsumer consumes the given bundle transaction during looping through all bundle transactions in the persistence layer.
type BundleTransactionConsumer func(bundleHash hornet.Hash, txHash hornet.Hash, isTail bool) bool
// ForEachBundleTransaction loops over all bundle transactions.
func ForEachBundleTransaction(consumer BundleTransactionConsumer, skipCache bool) {
bundleTransactionsStorage.ForEachKeyOnly(func(key []byte) bool {
return consumer(key[:49], key[50:99], key[49] == BundleTxIsTail)
}, skipCache)
}
// bundleTx +-0
func ContainsBundleTransaction(bundleHash hornet.Hash, txHash hornet.Hash, isTail bool) bool {
return bundleTransactionsStorage.Contains(databaseKeyForBundleTransaction(bundleHash, txHash, isTail))
}
// bundleTx +1
func StoreBundleTransaction(bundleHash hornet.Hash, txHash hornet.Hash, isTail bool) *CachedBundleTransaction {
bundleTx := &BundleTransaction{
BundleHash: bundleHash,
IsTail: isTail,
TxHash: txHash,
}
return &CachedBundleTransaction{CachedObject: bundleTransactionsStorage.Store(bundleTx)}
}
// bundleTx +-0
func DeleteBundleTransaction(bundleHash hornet.Hash, txHash hornet.Hash, isTail bool) {
bundleTransactionsStorage.Delete(databaseKeyForBundleTransaction(bundleHash, txHash, isTail))
}
func ShutdownBundleTransactionsStorage() {
bundleTransactionsStorage.Shutdown()
}
func FlushBundleTransactionsStorage() {
bundleTransactionsStorage.Flush()
}
////////////////////////////////////////////////////////////////////////////////
// getTailApproversOfSameBundle returns all tailTx hashes of the same bundle that approve this transaction
func getTailApproversOfSameBundle(bundleHash hornet.Hash, txHash hornet.Hash, forceRelease bool) hornet.Hashes {
var tailTxHashes hornet.Hashes
txsToCheck := make(map[string]struct{})
txsToCheck[string(txHash)] = struct{}{}
// Loop as long as new transactions are added in every loop cycle
for len(txsToCheck) != 0 {
for txHashToCheck := range txsToCheck {
delete(txsToCheck, txHashToCheck)
for _, approverHash := range GetApproverHashes(hornet.Hash(txHashToCheck)) {
cachedApproverTxMeta := GetCachedTxMetadataOrNil(approverHash) // meta +1
if cachedApproverTxMeta == nil {
continue
}
approverTxMeta := cachedApproverTxMeta.GetMetadata()
if !bytes.Equal(approverTxMeta.GetBundleHash(), bundleHash) {
// Not the same bundle => skip
cachedApproverTxMeta.Release(forceRelease) // meta -1
continue
}
if approverTxMeta.IsTail() {
// TailTx of the bundle
tailTxHashes = append(tailTxHashes, approverHash)
} else {
// Not the tail, but in the same bundle => walk the future cone
txsToCheck[string(approverHash)] = struct{}{}
}
cachedApproverTxMeta.Release(forceRelease) // meta -1
}
}
}
return tailTxHashes
}
// approversFromSameBundleExist returns whether there are other transactions in the same bundle, that approve this transaction
func approversFromSameBundleExist(bundleHash hornet.Hash, txHash hornet.Hash, forceRelease bool) bool {
for _, approverHash := range GetApproverHashes(txHash) {
if ContainsBundleTransaction(bundleHash, approverHash, true) || ContainsBundleTransaction(bundleHash, approverHash, false) {
// Tx is used in another bundle instance => do not delete
return true
}
}
return false
}
// RemoveTransactionFromBundle removes the transaction if non-tail and not associated to a bundle instance or
// if tail, it removes all the transactions of the bundle from the storage that are not used in another bundle instance.
func RemoveTransactionFromBundle(txMeta *hornet.TransactionMetadata) map[string]struct{} {
txsToRemove := make(map[string]struct{})
// check whether this transaction is a tail or respectively stored as a bundle tail
isTail := ContainsBundleTransaction(txMeta.GetBundleHash(), txMeta.GetTxHash(), true)
if !isTail {
// Tx is not a tail => check if the tx is part of another bundle instance, otherwise remove the tx from the storage
if approversFromSameBundleExist(txMeta.GetBundleHash(), txMeta.GetTxHash(), true) {
return txsToRemove
}
DeleteBundleTransaction(txMeta.GetBundleHash(), txMeta.GetTxHash(), false)
txsToRemove[string(txMeta.GetTxHash())] = struct{}{}
return txsToRemove
}
// Tx is a tail => remove all txs of this bundle that are not used in another bundle instance
// Tails can't be in another bundle instance => remove it
DeleteBundle(txMeta.GetTxHash())
DeleteBundleTransaction(txMeta.GetBundleHash(), txMeta.GetTxHash(), true)
txsToRemove[string(txMeta.GetTxHash())] = struct{}{}
cachedCurrentTxMeta := loadBundleTxMetaIfExistsOrPanic(txMeta.GetTxHash(), txMeta.GetBundleHash()) // meta +1
// iterate as long as the bundle isn't complete and prevent cyclic transactions (such as the genesis)
for !cachedCurrentTxMeta.GetMetadata().IsHead() && !bytes.Equal(cachedCurrentTxMeta.GetMetadata().GetTxHash(), cachedCurrentTxMeta.GetMetadata().GetTrunkHash()) {
// check whether the trunk transaction is known to the bundle storage.
// this also ensures that the transaction has to be in the database
if !ContainsBundleTransaction(txMeta.GetBundleHash(), cachedCurrentTxMeta.GetMetadata().GetTrunkHash(), false) {
// Tx may not exist if the bundle was not received completly
// Do not force release, since it is loaded again for pruning
cachedCurrentTxMeta.Release() // meta -1
return txsToRemove
}
// Tx is not a tail => check if the tx is part of another bundle instance, otherwise remove the tx from the bucket
if approversFromSameBundleExist(txMeta.GetBundleHash(), cachedCurrentTxMeta.GetMetadata().GetTrunkHash(), true) {
// Do not force release, since it is loaded again for pruning
cachedCurrentTxMeta.Release() // meta -1
return txsToRemove
}
DeleteBundleTransaction(txMeta.GetBundleHash(), cachedCurrentTxMeta.GetMetadata().GetTrunkHash(), false)
txsToRemove[string(cachedCurrentTxMeta.GetMetadata().GetTrunkHash())] = struct{}{}
cachedTxMeta := GetCachedTxMetadataOrNil(cachedCurrentTxMeta.GetMetadata().GetTrunkHash()) // meta +1
if cachedTxMeta == nil {
// Tx may not exist if the bundle was not received completly
// Do not force release, since it is loaded again for pruning
cachedCurrentTxMeta.Release() // meta -1
return txsToRemove
}
// Do not force release, since it is loaded again for pruning
cachedCurrentTxMeta.Release() // meta -1
cachedCurrentTxMeta = cachedTxMeta
}
// Do not force release, since it is loaded again for pruning
cachedCurrentTxMeta.Release() // meta -1
return txsToRemove
}