-
Notifications
You must be signed in to change notification settings - Fork 151
/
receipt.go
198 lines (161 loc) · 5.72 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
package utxo
import (
"encoding/binary"
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/marshalutil"
"github.com/iotaledger/hive.go/serializer"
"github.com/iotaledger/hornet/pkg/model/hornet"
"github.com/iotaledger/hornet/pkg/model/milestone"
iotago "github.com/iotaledger/iota.go/v2"
)
// ReceiptTuple contains a receipt and the index of the milestone
// which contained the receipt.
type ReceiptTuple struct {
// The actual receipt.
Receipt *iotago.Receipt `json:"receipt"`
// The index of the milestone which included the receipt.
MilestoneIndex milestone.Index `json:"milestoneIndex"`
}
func (rt *ReceiptTuple) kvStorableKey() (key []byte) {
return marshalutil.New(9).
WriteByte(UTXOStoreKeyPrefixReceipts).
WriteUint32(rt.Receipt.MigratedAt).
WriteUint32(uint32(rt.MilestoneIndex)).
Bytes()
}
func (rt *ReceiptTuple) kvStorableValue() (value []byte) {
receiptBytes, err := rt.Receipt.Serialize(serializer.DeSeriModeNoValidation)
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.Receipt{}
if _, err := r.Deserialize(value, serializer.DeSeriModeNoValidation); err != nil {
return err
}
rt.Receipt = r
rt.MilestoneIndex = milestone.Index(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 ...UTXOIterateOption) (uint32, error) {
var highestMigratedAtIndex uint32
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.
type ReceiptTupleConsumer func(rt *ReceiptTuple) bool
// ForEachReceiptTuple iterates over all stored receipt tuples.
func (u *Manager) ForEachReceiptTuple(consumer ReceiptTupleConsumer, options ...UTXOIterateOption) 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 milestone.Index, consumer ReceiptTupleConsumer, options ...UTXOIterateOption) error {
opt := iterateOptions(options)
if opt.readLockLedger {
u.ReadLockLedger()
defer u.ReadUnlockLedger()
}
prefix := make([]byte, 5)
prefix[0] = UTXOStoreKeyPrefixReceipts
binary.LittleEndian.PutUint32(prefix[1:], uint32(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.Receipt, msgID hornet.MessageID, msID *iotago.MilestoneID) ([]*Output, error) {
outputs := make([]*Output, len(r.Funds))
for outputIndex, migFundsEntry := range r.Funds {
entry := migFundsEntry.(*iotago.MigratedFundsEntry)
utxoID := OutputIDForMigratedFunds(*msID, uint16(outputIndex))
// we use the milestone hash as the "origin message"
outputs[outputIndex] = CreateOutput(&utxoID, msgID, iotago.OutputSigLockedSingleOutput, entry.Address.(iotago.Address), entry.Deposit)
}
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.UTXOInputID {
var utxoID iotago.UTXOInputID
copy(utxoID[:], milestoneHash[:])
binary.LittleEndian.PutUint16(utxoID[len(utxoID)-2:], outputIndex)
return utxoID
}
// ReceiptToTreasuryMutation converts a receipt to a treasury mutation tuple.
func ReceiptToTreasuryMutation(r *iotago.Receipt, unspentTreasuryOutput *TreasuryOutput, newMsID *iotago.MilestoneID) (*TreasuryMutationTuple, error) {
newOutput := &TreasuryOutput{
Amount: r.Transaction.(*iotago.TreasuryTransaction).Output.(*iotago.TreasuryOutput).Amount,
Spent: false,
}
copy(newOutput.MilestoneID[:], newMsID[:])
return &TreasuryMutationTuple{
NewOutput: newOutput,
SpentOutput: unspentTreasuryOutput,
}, nil
}