-
Notifications
You must be signed in to change notification settings - Fork 126
/
badger_utxo.go
161 lines (142 loc) · 3.67 KB
/
badger_utxo.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
package storage
import (
"fmt"
"slices"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
"github.com/dgraph-io/badger/v4"
)
func (s *BadgerStore) ReadUTXOKeys(hash crypto.Hash, index int) (*common.UTXOKeys, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
utxo, err := s.readUTXOLock(txn, hash, index)
if err != nil {
return nil, err
}
return &common.UTXOKeys{
Mask: utxo.Mask,
Keys: utxo.Keys,
}, nil
}
func (s *BadgerStore) ReadUTXOLock(hash crypto.Hash, index int) (*common.UTXOWithLock, error) {
s.mutex.RLock()
defer s.mutex.RUnlock()
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
return s.readUTXOLock(txn, hash, index)
}
func (s *BadgerStore) readUTXOLock(txn *badger.Txn, hash crypto.Hash, index int) (*common.UTXOWithLock, error) {
key := graphUtxoKey(hash, index)
item, err := txn.Get(key)
if err == badger.ErrKeyNotFound {
return nil, nil
}
if err != nil {
return nil, err
}
ival, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
return common.UnmarshalUTXO(ival)
}
func (s *BadgerStore) LockUTXOs(inputs []*common.Input, tx crypto.Hash, fork bool) error {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.snapshotsDB.Update(func(txn *badger.Txn) error {
for _, in := range inputs {
err := lockUTXO(txn, in.Hash, in.Index, tx, fork)
if err != nil {
return err
}
}
return nil
})
}
func lockUTXO(txn *badger.Txn, hash crypto.Hash, index int, tx crypto.Hash, fork bool) error {
key := graphUtxoKey(hash, index)
item, err := txn.Get(key)
if err != nil {
return err
}
ival, err := item.ValueCopy(nil)
if err != nil {
return err
}
out, err := common.UnmarshalUTXO(ival)
if err != nil {
return err
}
if out.LockHash.HasValue() && out.LockHash != tx {
if !fork {
return fmt.Errorf("utxo locked for transaction %s", out.LockHash)
}
err := pruneTransaction(txn, out.LockHash)
if err != nil {
return err
}
}
out.LockHash = tx
return txn.Set(key, out.Marshal())
}
func (s *BadgerStore) ReadGhostKeyLock(key crypto.Key) (*crypto.Hash, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
item, err := txn.Get(graphGhostKey(key))
if err == badger.ErrKeyNotFound {
return nil, nil
}
if err != nil {
return nil, err
}
var by crypto.Hash
_, err = item.ValueCopy(by[:])
return &by, err
}
func (s *BadgerStore) LockGhostKeys(keys []*crypto.Key, tx crypto.Hash, fork bool) error {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.snapshotsDB.Update(func(txn *badger.Txn) error {
filter := make(map[crypto.Key]bool)
for _, ghost := range keys {
if filter[*ghost] {
return fmt.Errorf("duplicated ghost key %s", ghost.String())
}
filter[*ghost] = true
err := lockGhostKey(txn, ghost, tx, fork)
if err != nil {
return err
}
}
return nil
})
}
func lockGhostKey(txn *badger.Txn, ghost *crypto.Key, tx crypto.Hash, fork bool) error {
key := graphGhostKey(*ghost)
item, err := txn.Get(key)
if err == badger.ErrKeyNotFound {
return txn.Set(key, tx[:])
}
if err != nil {
return err
}
var by crypto.Hash
val, err := item.ValueCopy(by[:])
if err != nil {
return err
}
if len(val) != len(by) || !by.HasValue() {
return fmt.Errorf("ghost key %s malformed lock %x", ghost.String(), val)
}
if fork && slices.Contains([]string{
"c63b6373652def5999c1d951fcb8f064db67b7d18565847b921b21639e15dddd",
"60deaf2471bb0b6481efe9080d8852b020ab2941e7faae21989d2404f34284ee",
"a558b1efbe27eb6a6f902fd97d4b7e2e3099e6edde1fe6e8e41204e0685fe426",
}, tx.String()) {
return nil
}
if by != tx {
return fmt.Errorf("ghost key %s locked for transaction %s", ghost.String(), by.String())
}
return nil
}