/
markersmanager.models.go
138 lines (105 loc) · 5.08 KB
/
markersmanager.models.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
package tangle
import (
"fmt"
"github.com/iotaledger/hive.go/generics/model"
"github.com/iotaledger/hive.go/generics/objectstorage"
"github.com/iotaledger/hive.go/generics/thresholdmap"
"github.com/iotaledger/goshimmer/packages/ledger/utxo"
"github.com/iotaledger/goshimmer/packages/markers"
)
// region markerIndexConflictIDMap /////////////////////////////////////////////////////////////////////////////////////////
type markerIndexConflictIDMap struct {
T *thresholdmap.ThresholdMap[markers.Index, utxo.TransactionIDs] `serix:"0"`
}
func newMarkerIndexConflictIDMap() *markerIndexConflictIDMap {
return &markerIndexConflictIDMap{thresholdmap.New[markers.Index, utxo.TransactionIDs](thresholdmap.LowerThresholdMode)}
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region MarkerIndexConflictIDMapping ///////////////////////////////////////////////////////////////////////////////////
// MarkerIndexConflictIDMapping is a data structure that allows to map marker Indexes to a ConflictID.
type MarkerIndexConflictIDMapping struct {
model.Storable[markers.SequenceID, MarkerIndexConflictIDMapping, *MarkerIndexConflictIDMapping, markerIndexConflictIDMap] `serix:"0"`
}
// NewMarkerIndexConflictIDMapping creates a new MarkerIndexConflictIDMapping for the given SequenceID.
func NewMarkerIndexConflictIDMapping(sequenceID markers.SequenceID) (markerConflictMapping *MarkerIndexConflictIDMapping) {
markerConflictMapping = model.NewStorable[markers.SequenceID, MarkerIndexConflictIDMapping](
newMarkerIndexConflictIDMap(),
)
markerConflictMapping.SetID(sequenceID)
return
}
// SequenceID returns the SequenceID that this MarkerIndexConflictIDMapping represents.
func (m *MarkerIndexConflictIDMapping) SequenceID() markers.SequenceID {
return m.ID()
}
// ConflictIDs returns the ConflictID that is associated to the given marker Index.
func (m *MarkerIndexConflictIDMapping) ConflictIDs(markerIndex markers.Index) (conflictIDs utxo.TransactionIDs) {
m.RLock()
defer m.RUnlock()
value, exists := m.M.T.Get(markerIndex)
if !exists {
panic(fmt.Sprintf("tried to retrieve the ConflictID of unknown marker.%s", markerIndex))
}
return value.Clone()
}
// SetConflictIDs creates a mapping between the given marker Index and the given ConflictID.
func (m *MarkerIndexConflictIDMapping) SetConflictIDs(index markers.Index, conflictIDs utxo.TransactionIDs) {
m.Lock()
defer m.Unlock()
m.M.T.Set(index, conflictIDs)
m.SetModified()
}
// DeleteConflictID deletes a mapping between the given marker Index and the stored ConflictID.
func (m *MarkerIndexConflictIDMapping) DeleteConflictID(index markers.Index) {
m.Lock()
defer m.Unlock()
m.M.T.Delete(index)
m.SetModified()
}
// Floor returns the largest Index that is <= the given Index which has a mapped ConflictID (and a boolean value
// indicating if it exists).
func (m *MarkerIndexConflictIDMapping) Floor(index markers.Index) (marker markers.Index, conflictIDs utxo.TransactionIDs, exists bool) {
m.RLock()
defer m.RUnlock()
if untypedIndex, untypedConflictIDs, exists := m.M.T.Floor(index); exists {
return untypedIndex, untypedConflictIDs, true
}
return 0, utxo.NewTransactionIDs(), false
}
// Ceiling returns the smallest Index that is >= the given Index which has a mapped ConflictID (and a boolean value
// indicating if it exists).
func (m *MarkerIndexConflictIDMapping) Ceiling(index markers.Index) (marker markers.Index, conflictIDs utxo.TransactionIDs, exists bool) {
m.RLock()
defer m.RUnlock()
if untypedIndex, untypedConflictIDs, exists := m.M.T.Ceiling(index); exists {
return untypedIndex, untypedConflictIDs, true
}
return 0, utxo.NewTransactionIDs(), false
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region MarkerBlockMapping /////////////////////////////////////////////////////////////////////////////////////////
// MarkerBlockMappingPartitionKeys defines the "layout" of the key. This enables prefix iterations in the object
// storage.
var MarkerBlockMappingPartitionKeys = objectstorage.PartitionKey(markers.SequenceID(0).Length(), markers.Index(0).Length())
// MarkerBlockMapping is a data structure that denotes a mapping from a Marker to a Block.
type MarkerBlockMapping struct {
model.Storable[markers.Marker, MarkerBlockMapping, *MarkerBlockMapping, BlockID] `serix:"0"`
}
// NewMarkerBlockMapping is the constructor for the MarkerBlockMapping.
func NewMarkerBlockMapping(marker markers.Marker, blockID BlockID) *MarkerBlockMapping {
markerBlockMapping := model.NewStorable[markers.Marker, MarkerBlockMapping](&blockID)
markerBlockMapping.SetID(marker)
return markerBlockMapping
}
// Marker returns the Marker that is mapped to a BlockID.
func (m *MarkerBlockMapping) Marker() *markers.Marker {
marker := m.ID()
return &marker
}
// BlockID returns the BlockID of the Marker.
func (m *MarkerBlockMapping) BlockID() BlockID {
m.RLock()
defer m.RUnlock()
return m.M
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////