-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger_mint.go
160 lines (137 loc) · 3.8 KB
/
badger_mint.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
package storage
import (
"encoding/binary"
"fmt"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
"github.com/dgraph-io/badger/v4"
)
func (s *BadgerStore) ReadMintDistributions(offset, count uint64) ([]*common.MintDistribution, []*common.VersionedTransaction, error) {
if count > 500 {
return nil, nil, fmt.Errorf("count %d too large, the maximum is 500", count)
}
mints := make([]*common.MintDistribution, 0)
transactions := make([]*common.VersionedTransaction, 0)
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
opts := badger.DefaultIteratorOptions
opts.Prefix = []byte(graphPrefixMint)
it := txn.NewIterator(opts)
defer it.Close()
it.Seek(graphMintKey(offset))
for ; it.ValidForPrefix(opts.Prefix) && uint64(len(mints)) < count; it.Next() {
item := it.Item()
key := item.KeyCopy(nil)
ival, err := item.ValueCopy(nil)
if err != nil {
return nil, nil, err
}
data, err := common.UnmarshalMintDistribution(ival)
if err != nil {
return nil, nil, err
}
if data.Batch != graphMintBatch(key) {
panic("malformed mint data")
}
tx, err := readTransaction(txn, data.Transaction)
if err != nil {
return nil, nil, err
}
if tx == nil {
continue
}
_, err = txn.Get(graphFinalizationKey(data.Transaction))
if err == badger.ErrKeyNotFound {
continue
} else if err != nil {
return nil, nil, err
}
transactions = append(transactions, tx)
mints = append(mints, data)
}
return mints, transactions, nil
}
func (s *BadgerStore) ReadLastMintDistribution(batch uint64) (*common.MintDistribution, error) {
s.mutex.RLock()
defer s.mutex.RUnlock()
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
opts := badger.DefaultIteratorOptions
opts.Reverse = true
opts.Prefix = []byte(graphPrefixMint)
it := txn.NewIterator(opts)
defer it.Close()
it.Seek(graphMintKey(batch))
for ; it.Valid(); it.Next() {
item := it.Item()
key := item.KeyCopy(nil)
ival, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
data, err := common.UnmarshalMintDistribution(ival)
if err != nil {
return nil, err
}
if data.Batch != graphMintBatch(key) {
panic("malformed mint data")
}
_, err = txn.Get(graphFinalizationKey(data.Transaction))
if err == badger.ErrKeyNotFound {
continue
} else if err != nil {
return nil, err
}
return data, nil
}
return nil, nil
}
func (s *BadgerStore) LockMintInput(mint *common.MintData, tx crypto.Hash, fork bool) error {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.snapshotsDB.Update(func(txn *badger.Txn) error {
dist, err := readMintInput(txn, mint)
if err == badger.ErrKeyNotFound {
return writeMintDistribution(txn, mint, tx)
}
if err != nil {
return err
}
if dist.Transaction == tx && dist.Amount.Cmp(mint.Amount) == 0 {
return nil
}
if !fork {
return fmt.Errorf("mint locked for transaction %s amount %s", dist.Transaction.String(), dist.Amount.String())
}
err = pruneTransaction(txn, dist.Transaction)
if err != nil {
return err
}
return writeMintDistribution(txn, mint, tx)
})
}
func readMintInput(txn *badger.Txn, mint *common.MintData) (*common.MintDistribution, error) {
key := graphMintKey(mint.Batch)
item, err := txn.Get(key)
if err != nil {
return nil, err
}
ival, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
return common.UnmarshalMintDistribution(ival)
}
func writeMintDistribution(txn *badger.Txn, mint *common.MintData, tx crypto.Hash) error {
key := graphMintKey(mint.Batch)
val := mint.Distribute(tx).Marshal()
return txn.Set(key, val)
}
func graphMintKey(batch uint64) []byte {
key := []byte(graphPrefixMint)
return binary.BigEndian.AppendUint64(key, batch)
}
func graphMintBatch(key []byte) uint64 {
batch := key[len(graphPrefixMint):]
return binary.BigEndian.Uint64(batch)
}