-
Notifications
You must be signed in to change notification settings - Fork 151
/
spent_status.go
76 lines (57 loc) · 2.19 KB
/
spent_status.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
package utxo
import (
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/serializer/v2/marshalutil"
iotago "github.com/iotaledger/iota.go/v3"
)
// OutputIDConsumer is a function that consumes an output ID.
// Returning false from this function indicates to abort the iteration.
type OutputIDConsumer func(outputID iotago.OutputID) bool
// OutputConsumer is a function that consumes an output.
// Returning false from this function indicates to abort the iteration.
type OutputConsumer func(output *Output) bool
type LookupKey []byte
func lookupKeyUnspentOutput(outputID iotago.OutputID) LookupKey {
ms := marshalutil.New(35)
ms.WriteByte(UTXOStoreKeyPrefixOutputUnspent) // 1 byte
ms.WriteBytes(outputID[:]) // 34 bytes
return ms.Bytes()
}
func (o *Output) UnspentLookupKey() LookupKey {
return lookupKeyUnspentOutput(o.outputID)
}
func outputIDFromDatabaseKey(key LookupKey) (iotago.OutputID, error) {
ms := marshalutil.New([]byte(key))
// prefix
if _, err := ms.ReadByte(); err != nil {
return iotago.OutputID{}, err
}
return ParseOutputID(ms)
}
func markAsUnspent(output *Output, mutations kvstore.BatchedMutations) error {
return mutations.Set(output.UnspentLookupKey(), []byte{})
}
func markAsSpent(output *Output, mutations kvstore.BatchedMutations) error {
return deleteOutputLookups(output, mutations)
}
func deleteOutputLookups(output *Output, mutations kvstore.BatchedMutations) error {
return mutations.Delete(output.UnspentLookupKey())
}
func (u *Manager) IsOutputIDUnspentWithoutLocking(outputID iotago.OutputID) (bool, error) {
return u.utxoStorage.Has(lookupKeyUnspentOutput(outputID))
}
func (u *Manager) IsOutputUnspentWithoutLocking(output *Output) (bool, error) {
return u.utxoStorage.Has(output.UnspentLookupKey())
}
func storeSpentAndMarkOutputAsSpent(spent *Spent, mutations kvstore.BatchedMutations) error {
if err := storeSpent(spent, mutations); err != nil {
return err
}
return markAsSpent(spent.output, mutations)
}
func deleteSpentAndMarkOutputAsUnspent(spent *Spent, mutations kvstore.BatchedMutations) error {
if err := deleteSpent(spent, mutations); err != nil {
return err
}
return markAsUnspent(spent.output, mutations)
}