-
Notifications
You must be signed in to change notification settings - Fork 151
/
spent.go
194 lines (150 loc) · 4.55 KB
/
spent.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
package utxo
import (
"bytes"
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/serializer/v2/marshalutil"
iotago "github.com/iotaledger/iota.go/v3"
)
// SpentConsumer is a function that consumes a spent output.
// Returning false from this function indicates to abort the iteration.
type SpentConsumer func(spent *Spent) bool
// LexicalOrderedSpents are spents ordered in lexical order by their outputID.
type LexicalOrderedSpents []*Spent
func (l LexicalOrderedSpents) Len() int {
return len(l)
}
func (l LexicalOrderedSpents) Less(i, j int) bool {
return bytes.Compare(l[i].outputID[:], l[j].outputID[:]) < 0
}
func (l LexicalOrderedSpents) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
// Spent are already spent TXOs (transaction outputs).
type Spent struct {
outputID iotago.OutputID
// the ID of the transaction that spent the output
transactionIDSpent iotago.TransactionID
// the index of the milestone that spent the output
msIndexSpent iotago.MilestoneIndex
// the timestamp of the milestone that spent the output
msTimestampSpent uint32
output *Output
}
func (s *Spent) Output() *Output {
return s.output
}
func (s *Spent) OutputID() iotago.OutputID {
return s.outputID
}
func (s *Spent) MapKey() string {
return string(s.outputID[:])
}
func (s *Spent) BlockID() iotago.BlockID {
return s.output.BlockID()
}
func (s *Spent) OutputType() iotago.OutputType {
return s.output.OutputType()
}
func (s *Spent) Deposit() uint64 {
return s.output.Deposit()
}
// TransactionIDSpent returns the ID of the transaction that spent the output.
func (s *Spent) TransactionIDSpent() iotago.TransactionID {
return s.transactionIDSpent
}
// MilestoneIndexSpent returns the index of the milestone that spent the output.
func (s *Spent) MilestoneIndexSpent() iotago.MilestoneIndex {
return s.msIndexSpent
}
// MilestoneTimestampSpent returns the timestamp of the milestone that spent the output.
func (s *Spent) MilestoneTimestampSpent() uint32 {
return s.msTimestampSpent
}
type Spents []*Spent
func NewSpent(output *Output, transactionIDSpent iotago.TransactionID, msIndexSpent iotago.MilestoneIndex, msTimestampSpent uint32) *Spent {
return &Spent{
outputID: output.outputID,
output: output,
transactionIDSpent: transactionIDSpent,
msIndexSpent: msIndexSpent,
msTimestampSpent: msTimestampSpent,
}
}
func spentStorageKeyForOutputID(outputID iotago.OutputID) []byte {
ms := marshalutil.New(35)
ms.WriteByte(UTXOStoreKeyPrefixOutputSpent) // 1 byte
ms.WriteBytes(outputID[:]) // 34 bytes
return ms.Bytes()
}
func (s *Spent) KVStorableKey() (key []byte) {
return spentStorageKeyForOutputID(s.outputID)
}
func (s *Spent) KVStorableValue() (value []byte) {
ms := marshalutil.New(40)
ms.WriteBytes(s.transactionIDSpent[:]) // 32 bytes
ms.WriteUint32(s.msIndexSpent) // 4 bytes
ms.WriteUint32(s.msTimestampSpent) // 4 bytes
return ms.Bytes()
}
func (s *Spent) kvStorableLoad(_ *Manager, key []byte, value []byte) error {
// Parse key
keyUtil := marshalutil.New(key)
// Read prefix output
_, err := keyUtil.ReadByte()
if err != nil {
return err
}
// Read OutputID
if s.outputID, err = ParseOutputID(keyUtil); err != nil {
return err
}
// Parse value
valueUtil := marshalutil.New(value)
// Read transaction ID
if s.transactionIDSpent, err = parseTransactionID(valueUtil); err != nil {
return err
}
// Read milestone index
s.msIndexSpent, err = valueUtil.ReadUint32()
if err != nil {
return err
}
// Read milestone timestamp
if s.msTimestampSpent, err = valueUtil.ReadUint32(); err != nil {
return err
}
return nil
}
func (u *Manager) loadOutputOfSpent(s *Spent) error {
output, err := u.ReadOutputByOutputIDWithoutLocking(s.outputID)
if err != nil {
return err
}
s.output = output
return nil
}
func (u *Manager) ReadSpentForOutputIDWithoutLocking(outputID iotago.OutputID) (*Spent, error) {
output, err := u.ReadOutputByOutputIDWithoutLocking(outputID)
if err != nil {
return nil, err
}
key := spentStorageKeyForOutputID(outputID)
value, err := u.utxoStorage.Get(key)
if err != nil {
return nil, err
}
spent := &Spent{}
if err := spent.kvStorableLoad(u, key, value); err != nil {
return nil, err
}
spent.output = output
return spent, nil
}
func storeSpent(spent *Spent, mutations kvstore.BatchedMutations) error {
return mutations.Set(spent.KVStorableKey(), spent.KVStorableValue())
}
func deleteSpent(spent *Spent, mutations kvstore.BatchedMutations) error {
return mutations.Delete(spent.KVStorableKey())
}
// code guards.
var _ kvStorable = &Spent{}