-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger_utxo.go
77 lines (67 loc) · 1.66 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
package storage
import (
"fmt"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
"github.com/dgraph-io/badger/v2"
)
func (s *BadgerStore) ReadUTXO(hash crypto.Hash, index int) (*common.UTXOWithLock, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
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
}
var out common.UTXOWithLock
err = common.DecompressMsgpackUnmarshal(ival, &out)
return &out, err
}
func (s *BadgerStore) LockUTXO(hash crypto.Hash, index int, tx crypto.Hash, fork bool) error {
return s.snapshotsDB.Update(func(txn *badger.Txn) 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
}
var out common.UTXOWithLock
err = common.DecompressMsgpackUnmarshal(ival, &out)
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, common.CompressMsgpackMarshalPanic(out))
})
}
func (s *BadgerStore) CheckGhost(key crypto.Key) (bool, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
_, err := txn.Get(graphGhostKey(key))
if err == badger.ErrKeyNotFound {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}