-
Notifications
You must be signed in to change notification settings - Fork 151
/
receipt.go
216 lines (169 loc) · 6 KB
/
receipt.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
package utxo
import (
"encoding/binary"
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/hive.go/serializer/v2/marshalutil"
iotago "github.com/iotaledger/iota.go/v3"
)
// ReceiptTuple contains a receipt and the index of the milestone
// which contained the receipt.
type ReceiptTuple struct {
// The actual receipt.
Receipt *iotago.ReceiptMilestoneOpt `json:"receipt"`
// The index of the milestone which included the receipt.
MilestoneIndex iotago.MilestoneIndex `json:"milestoneIndex"`
}
func (rt *ReceiptTuple) KVStorableKey() (key []byte) {
return marshalutil.New(9).
WriteByte(UTXOStoreKeyPrefixReceipts).
WriteUint32(rt.Receipt.MigratedAt).
WriteUint32(rt.MilestoneIndex).
Bytes()
}
func (rt *ReceiptTuple) KVStorableValue() (value []byte) {
receiptBytes, err := rt.Receipt.Serialize(serializer.DeSeriModeNoValidation, nil)
if err != nil {
panic(err)
}
return receiptBytes
}
func (rt *ReceiptTuple) kvStorableLoad(_ *Manager, key []byte, value []byte) error {
keyExt := marshalutil.New(key)
// skip prefix and migrated at index
if _, err := keyExt.ReadByte(); err != nil {
return err
}
if _, err := keyExt.ReadUint32(); err != nil {
return err
}
// read out index of the milestone which contained this receipt
msIndex, err := keyExt.ReadUint32()
if err != nil {
return err
}
r := &iotago.ReceiptMilestoneOpt{}
if _, err := r.Deserialize(value, serializer.DeSeriModeNoValidation, nil); err != nil {
return err
}
rt.Receipt = r
rt.MilestoneIndex = msIndex
return nil
}
// adds a receipt store instruction to the given mutations.
func storeReceipt(rt *ReceiptTuple, mutations kvstore.BatchedMutations) error {
return mutations.Set(rt.KVStorableKey(), rt.KVStorableValue())
}
// adds a receipt delete instruction to the given mutations.
func deleteReceipt(rt *ReceiptTuple, mutations kvstore.BatchedMutations) error {
return mutations.Delete(rt.KVStorableKey())
}
// SearchHighestReceiptMigratedAtIndex searches the highest migratedAt of all stored receipts.
func (u *Manager) SearchHighestReceiptMigratedAtIndex(options ...IterateOption) (iotago.MilestoneIndex, error) {
var highestMigratedAtIndex iotago.MilestoneIndex
if err := u.ForEachReceiptTuple(func(rt *ReceiptTuple) bool {
if rt.Receipt.MigratedAt > highestMigratedAtIndex {
highestMigratedAtIndex = rt.Receipt.MigratedAt
}
return true
}, options...); err != nil {
return 0, err
}
return highestMigratedAtIndex, nil
}
// ReceiptTupleConsumer is a function that consumes a receipt tuple.
// Returning false from this function indicates to abort the iteration.
type ReceiptTupleConsumer func(rt *ReceiptTuple) bool
// ForEachReceiptTuple iterates over all stored receipt tuples.
func (u *Manager) ForEachReceiptTuple(consumer ReceiptTupleConsumer, options ...IterateOption) error {
opt := iterateOptions(options)
if opt.readLockLedger {
u.ReadLockLedger()
defer u.ReadUnlockLedger()
}
var innerErr error
var i int
if err := u.utxoStorage.Iterate([]byte{UTXOStoreKeyPrefixReceipts}, func(key kvstore.Key, value kvstore.Value) bool {
if (opt.maxResultCount > 0) && (i >= opt.maxResultCount) {
return false
}
i++
rt := &ReceiptTuple{}
if err := rt.kvStorableLoad(u, key, value); err != nil {
innerErr = err
return false
}
return consumer(rt)
}); err != nil {
return err
}
return innerErr
}
// ForEachReceiptTupleMigratedAt iterates over all stored receipt tuples for a given migrated at index.
func (u *Manager) ForEachReceiptTupleMigratedAt(migratedAtIndex iotago.MilestoneIndex, consumer ReceiptTupleConsumer, options ...IterateOption) error {
opt := iterateOptions(options)
if opt.readLockLedger {
u.ReadLockLedger()
defer u.ReadUnlockLedger()
}
prefix := make([]byte, 5)
prefix[0] = UTXOStoreKeyPrefixReceipts
binary.LittleEndian.PutUint32(prefix[1:], migratedAtIndex)
var innerErr error
var i int
if err := u.utxoStorage.Iterate(prefix, func(key kvstore.Key, value kvstore.Value) bool {
if (opt.maxResultCount > 0) && (i >= opt.maxResultCount) {
return false
}
i++
rt := &ReceiptTuple{}
if err := rt.kvStorableLoad(u, key, value); err != nil {
innerErr = err
return false
}
return consumer(rt)
}); err != nil {
return err
}
return innerErr
}
// ReceiptToOutputs extracts the migrated funds to outputs.
func ReceiptToOutputs(r *iotago.ReceiptMilestoneOpt, milestoneID iotago.MilestoneID, msIndex iotago.MilestoneIndex, msTimestamp uint32) ([]*Output, error) {
outputs := make([]*Output, len(r.Funds))
for outputIndex, migFundsEntry := range r.Funds {
entry := migFundsEntry
outputID := OutputIDForMigratedFunds(milestoneID, uint16(outputIndex))
// we use the milestone ID as the "origin block"
output := &iotago.BasicOutput{
Amount: entry.Deposit,
Conditions: iotago.UnlockConditions{
&iotago.AddressUnlockCondition{Address: entry.Address},
},
}
// outputs created by milestone receipts are identified by EmptyBlockID
outputs[outputIndex] = CreateOutput(outputID, iotago.EmptyBlockID(), msIndex, msTimestamp, output)
}
return outputs, nil
}
// OutputIDForMigratedFunds returns the UTXO ID for a migrated funds entry given the milestone containing the receipt
// and the index of the entry.
func OutputIDForMigratedFunds(milestoneHash iotago.MilestoneID, outputIndex uint16) iotago.OutputID {
var outputID iotago.OutputID
copy(outputID[:], milestoneHash[:])
binary.LittleEndian.PutUint16(outputID[len(outputID)-2:], outputIndex)
return outputID
}
// ReceiptToTreasuryMutation converts a receipt to a treasury mutation tuple.
func ReceiptToTreasuryMutation(r *iotago.ReceiptMilestoneOpt, unspentTreasuryOutput *TreasuryOutput, newMilestoneID iotago.MilestoneID) (*TreasuryMutationTuple, error) {
newOutput := &TreasuryOutput{
Amount: r.Transaction.Output.Amount,
Spent: false,
}
copy(newOutput.MilestoneID[:], newMilestoneID[:])
return &TreasuryMutationTuple{
NewOutput: newOutput,
SpentOutput: unspentTreasuryOutput,
}, nil
}
// code guards.
var _ kvStorable = &ReceiptTuple{}