-
Notifications
You must be signed in to change notification settings - Fork 0
/
unconfirmed_tx_storage.go
119 lines (90 loc) · 3.65 KB
/
unconfirmed_tx_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
package tangle
import (
"encoding/binary"
"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/model/milestone"
"github.com/gohornet/hornet/pkg/profile"
)
var unconfirmedTxStorage *objectstorage.ObjectStorage
type CachedUnconfirmedTx struct {
objectstorage.CachedObject
}
type CachedUnconfirmedTxs []*CachedUnconfirmedTx
func (cachedUnconfirmedTxs CachedUnconfirmedTxs) Release(force ...bool) {
for _, cachedUnconfirmedTx := range cachedUnconfirmedTxs {
cachedUnconfirmedTx.Release(force...)
}
}
func (c *CachedUnconfirmedTx) GetUnconfirmedTx() *hornet.UnconfirmedTx {
return c.Get().(*hornet.UnconfirmedTx)
}
func unconfirmedTxFactory(key []byte) (objectstorage.StorableObject, int, error) {
unconfirmedTx := hornet.NewUnconfirmedTx(milestone.Index(binary.LittleEndian.Uint32(key[:4])), key[4:53])
return unconfirmedTx, 53, nil
}
func GetUnconfirmedTxStorageSize() int {
return unconfirmedTxStorage.GetSize()
}
func configureUnconfirmedTxStorage(store kvstore.KVStore, opts profile.CacheOpts) {
unconfirmedTxStorage = objectstorage.New(
store.WithRealm([]byte{StorePrefixUnconfirmedTransactions}),
unconfirmedTxFactory,
objectstorage.CacheTime(time.Duration(opts.CacheTimeMs)*time.Millisecond),
objectstorage.PersistenceEnabled(true),
objectstorage.PartitionKey(4, 49),
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,
}),
)
}
// GetUnconfirmedTxHashes returns all hashes of unconfirmed transactions for that milestone.
func GetUnconfirmedTxHashes(msIndex milestone.Index, forceRelease bool) hornet.Hashes {
var unconfirmedTxHashes hornet.Hashes
key := make([]byte, 4)
binary.LittleEndian.PutUint32(key, uint32(msIndex))
unconfirmedTxStorage.ForEachKeyOnly(func(key []byte) bool {
unconfirmedTxHashes = append(unconfirmedTxHashes, hornet.Hash(key[4:53]))
return true
}, false, key)
return unconfirmedTxHashes
}
// UnconfirmedTxConsumer consumes the given unconfirmed transaction during looping through all unconfirmed transactions in the persistence layer.
type UnconfirmedTxConsumer func(msIndex milestone.Index, txHash hornet.Hash) bool
// ForEachUnconfirmedTx loops over all unconfirmed transactions.
func ForEachUnconfirmedTx(consumer UnconfirmedTxConsumer, skipCache bool) {
unconfirmedTxStorage.ForEachKeyOnly(func(key []byte) bool {
return consumer(milestone.Index(binary.LittleEndian.Uint32(key[:4])), key[4:53])
}, skipCache)
}
// unconfirmedTx +1
func StoreUnconfirmedTx(msIndex milestone.Index, txHash hornet.Hash) *CachedUnconfirmedTx {
unconfirmedTx := hornet.NewUnconfirmedTx(msIndex, txHash)
return &CachedUnconfirmedTx{CachedObject: unconfirmedTxStorage.Store(unconfirmedTx)}
}
// DeleteUnconfirmedTxs deletes unconfirmed transaction entries.
func DeleteUnconfirmedTxs(msIndex milestone.Index) int {
msIndexBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(msIndexBytes, uint32(msIndex))
var keysToDelete [][]byte
unconfirmedTxStorage.ForEachKeyOnly(func(key []byte) bool {
keysToDelete = append(keysToDelete, key)
return true
}, false, msIndexBytes)
for _, key := range keysToDelete {
unconfirmedTxStorage.Delete(key)
}
return len(keysToDelete)
}
func ShutdownUnconfirmedTxsStorage() {
unconfirmedTxStorage.Shutdown()
}
func FlushUnconfirmedTxsStorage() {
unconfirmedTxStorage.Flush()
}