-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger_mint.go
144 lines (126 loc) · 3.75 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
package storage
import (
"encoding/binary"
"fmt"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
"github.com/dgraph-io/badger"
)
func (s *BadgerStore) ReadMintDistributions(group string, offset, count uint64) ([]*common.MintDistribution, []*common.VersionedTransaction, error) {
mints := make([]*common.MintDistribution, 0)
transactions := make([]*common.VersionedTransaction, 0)
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
prefix := []byte(graphPrefixMint + group)
it.Seek(graphMintKey(group, offset))
for ; it.ValidForPrefix(prefix) && uint64(len(mints)) < count; it.Next() {
item := it.Item()
ival, err := item.ValueCopy(nil)
if err != nil {
return nil, nil, err
}
var data common.MintDistribution
err = common.MsgpackUnmarshal(ival, &data)
if err != nil {
return nil, nil, err
}
if data.Batch != graphMintBatch(item.Key(), group) {
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(group string) (*common.MintDistribution, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
opts := badger.DefaultIteratorOptions
opts.Reverse = true
it := txn.NewIterator(opts)
defer it.Close()
dist := &common.MintDistribution{Group: group}
it.Seek(graphMintKey(group, ^uint64(0)))
if it.ValidForPrefix([]byte(graphPrefixMint + group)) {
item := it.Item()
ival, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
var data common.MintDistribution
err = common.MsgpackUnmarshal(ival, &data)
if err != nil {
return nil, err
}
dist.Batch = graphMintBatch(item.Key(), group)
dist.Transaction = data.Transaction
dist.Amount = data.Amount
}
return dist, nil
}
func (s *BadgerStore) LockMintInput(mint *common.MintData, tx crypto.Hash, fork bool) error {
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.Group, mint.Batch)
item, err := txn.Get(key)
if err != nil {
return nil, err
}
ival, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
var dist common.MintDistribution
err = common.MsgpackUnmarshal(ival, &dist)
return &dist, err
}
func writeMintDistribution(txn *badger.Txn, mint *common.MintData, tx crypto.Hash) error {
key := graphMintKey(mint.Group, mint.Batch)
val := common.MsgpackMarshalPanic(mint.Distribute(tx))
return txn.Set(key, val)
}
func graphMintKey(group string, batch uint64) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, batch)
key := append([]byte(group), buf...)
return append([]byte(graphPrefixMint), key...)
}
func graphMintBatch(key []byte, group string) uint64 {
batch := key[len(graphPrefixMint)+len(group):]
return binary.BigEndian.Uint64(batch)
}