-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger_mint_fix.go
63 lines (52 loc) · 1.37 KB
/
badger_mint_fix.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
package storage
import (
"encoding/binary"
"github.com/MixinNetwork/mixin/common"
"github.com/dgraph-io/badger/v4"
)
// FIXME remove this in the future
func (s *BadgerStore) OneTimeFixMintPrefix() error {
txn := s.snapshotsDB.NewTransaction(true)
defer txn.Discard()
mints, err := s.readOldMintDistributions(txn)
if err != nil || len(mints) == 0 {
return err
}
for _, m := range mints {
err = writeMintDistribution(txn, &m.MintData, m.Transaction)
if err != nil {
return err
}
key := binary.BigEndian.AppendUint64([]byte("MINTKERNELNODE"), m.Batch)
err = txn.Delete(key)
if err != nil {
return err
}
}
return txn.Commit()
}
func (s *BadgerStore) readOldMintDistributions(txn *badger.Txn) ([]*common.MintDistribution, error) {
mints := make([]*common.MintDistribution, 0)
opts := badger.DefaultIteratorOptions
opts.Prefix = []byte("MINTKERNELNODE")
it := txn.NewIterator(opts)
defer it.Close()
it.Seek(opts.Prefix)
for ; it.ValidForPrefix(opts.Prefix); it.Next() {
item := it.Item()
key := item.KeyCopy(nil)
ival, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
data, err := common.DecompressUnmarshalMintDistribution(ival)
if err != nil {
return nil, err
}
if data.Batch != binary.BigEndian.Uint64(key[len(opts.Prefix):]) {
panic("malformed mint data")
}
mints = append(mints, data)
}
return mints, nil
}