-
Notifications
You must be signed in to change notification settings - Fork 12
/
slot_diff.go
196 lines (149 loc) · 5.1 KB
/
slot_diff.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
package utxoledger
import (
"crypto/sha256"
"sort"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/hive.go/serializer/v2/stream"
iotago "github.com/iotaledger/iota.go/v4"
)
// SlotDiff represents the generated and spent outputs by a slot's confirmation.
type SlotDiff struct {
// The index of the slot.
Slot iotago.SlotIndex
// The outputs newly generated with this diff.
Outputs Outputs
// The outputs spent with this diff.
Spents Spents
}
func slotDiffKeyForIndex(slot iotago.SlotIndex) []byte {
byteBuffer := stream.NewByteBuffer(serializer.OneByte + iotago.SlotIndexLength)
// There can't be any errors.
_ = stream.Write(byteBuffer, StoreKeyPrefixSlotDiffs)
_ = stream.Write(byteBuffer, slot)
return lo.PanicOnErr(byteBuffer.Bytes())
}
func (sd *SlotDiff) KVStorableKey() []byte {
return slotDiffKeyForIndex(sd.Slot)
}
func (sd *SlotDiff) KVStorableValue() []byte {
byteBuffer := stream.NewByteBuffer()
// There can't be any errors.
_ = stream.WriteCollection(byteBuffer, serializer.SeriLengthPrefixTypeAsUint32, func() (elementsCount int, err error) {
for _, output := range sd.sortedOutputs() {
_ = stream.Write(byteBuffer, output.outputID)
}
return len(sd.Outputs), nil
})
_ = stream.WriteCollection(byteBuffer, serializer.SeriLengthPrefixTypeAsUint32, func() (elementsCount int, err error) {
for _, spent := range sd.sortedSpents() {
_ = stream.Write(byteBuffer, spent.output.outputID)
}
return len(sd.Spents), nil
})
return lo.PanicOnErr(byteBuffer.Bytes())
}
// note that this method relies on the data being available within other "tables".
func (sd *SlotDiff) kvStorableLoad(manager *Manager, key []byte, value []byte) error {
var err error
if sd.Slot, _, err = iotago.SlotIndexFromBytes(key[1:]); err != nil {
return err
}
byteReader := stream.NewByteReader(value)
outputsCount, err := stream.PeekSize(byteReader, serializer.SeriLengthPrefixTypeAsUint32)
if err != nil {
return ierrors.Wrap(err, "unable to peek outputs count")
}
outputs := make(Outputs, outputsCount)
if err = stream.ReadCollection(byteReader, serializer.SeriLengthPrefixTypeAsUint32, func(i int) error {
outputID, err := stream.Read[iotago.OutputID](byteReader)
if err != nil {
return ierrors.Wrap(err, "unable to read outputID")
}
output, err := manager.ReadOutputByOutputIDWithoutLocking(outputID)
if err != nil {
return err
}
outputs[i] = output
return nil
}); err != nil {
return ierrors.Wrapf(err, "unable to read slot diff outputs")
}
spentsCount, err := stream.PeekSize(byteReader, serializer.SeriLengthPrefixTypeAsUint32)
if err != nil {
return ierrors.Wrap(err, "unable to peek spents count")
}
spents := make(Spents, spentsCount)
if err = stream.ReadCollection(byteReader, serializer.SeriLengthPrefixTypeAsUint32, func(i int) error {
outputID, err := stream.Read[iotago.OutputID](byteReader)
if err != nil {
return ierrors.Wrap(err, "unable to read outputID")
}
spent, err := manager.ReadSpentForOutputIDWithoutLocking(outputID)
if err != nil {
return err
}
spents[i] = spent
return nil
}); err != nil {
return ierrors.Wrapf(err, "unable to read slot diff spents")
}
sd.Outputs = outputs
sd.Spents = spents
return nil
}
func (sd *SlotDiff) sortedOutputs() LexicalOrderedOutputs {
// do not sort in place
sortedOutputs := make(LexicalOrderedOutputs, len(sd.Outputs))
copy(sortedOutputs, sd.Outputs)
sort.Sort(sortedOutputs)
return sortedOutputs
}
func (sd *SlotDiff) sortedSpents() LexicalOrderedSpents {
// do not sort in place
sortedSpents := make(LexicalOrderedSpents, len(sd.Spents))
copy(sortedSpents, sd.Spents)
sort.Sort(sortedSpents)
return sortedSpents
}
// SHA256Sum computes the sha256 of the slot diff byte representation.
func (sd *SlotDiff) SHA256Sum() ([]byte, error) {
sdDiffHash := sha256.New()
if err := stream.WriteBytes(sdDiffHash, sd.KVStorableKey()); err != nil {
return nil, ierrors.Wrap(err, "unable to serialize slot diff")
}
if err := stream.WriteBytes(sdDiffHash, sd.KVStorableValue()); err != nil {
return nil, ierrors.Wrap(err, "unable to serialize slot diff")
}
// calculate sha256 hash
return sdDiffHash.Sum(nil), nil
}
// DB helper functions.
func storeDiff(diff *SlotDiff, mutations kvstore.BatchedMutations) error {
return mutations.Set(diff.KVStorableKey(), diff.KVStorableValue())
}
func deleteDiff(index iotago.SlotIndex, mutations kvstore.BatchedMutations) error {
return mutations.Delete(slotDiffKeyForIndex(index))
}
// Manager functions.
func (m *Manager) SlotDiffWithoutLocking(index iotago.SlotIndex) (*SlotDiff, error) {
key := slotDiffKeyForIndex(index)
value, err := m.store.Get(key)
if err != nil {
return nil, err
}
diff := &SlotDiff{}
if err := diff.kvStorableLoad(m, key, value); err != nil {
return nil, err
}
return diff, nil
}
func (m *Manager) SlotDiff(index iotago.SlotIndex) (*SlotDiff, error) {
m.ReadLockLedger()
defer m.ReadUnlockLedger()
return m.SlotDiffWithoutLocking(index)
}
// code guards.
var _ kvStorable = &SlotDiff{}