From 967269df0a1d7f4facc19be1dd4661726d989c14 Mon Sep 17 00:00:00 2001 From: Vanessa Violet Date: Fri, 7 Jul 2023 20:08:08 +0000 Subject: [PATCH] fix old mint data store --- main.go | 5 +++ storage/badger_graph.go | 2 +- storage/badger_mint.go | 2 +- storage/badger_mint_fix.go | 62 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 storage/badger_mint_fix.go diff --git a/main.go b/main.go index 26e27bf77..58d797cfe 100644 --- a/main.go +++ b/main.go @@ -563,6 +563,11 @@ func kernelCmd(c *cli.Context) error { } defer store.Close() + err = store.OneTimeFixMintPrefix() + if err != nil { + return err + } + addr := fmt.Sprintf(":%d", c.Int("port")) node, err := kernel.SetupNode(custom, store, cache, addr, c.String("dir")) if err != nil { diff --git a/storage/badger_graph.go b/storage/badger_graph.go index cc9bdcc0e..a265104e7 100644 --- a/storage/badger_graph.go +++ b/storage/badger_graph.go @@ -16,7 +16,7 @@ const ( graphPrefixGhost = "GHOST" // each output key should only be used once graphPrefixUTXO = "UTXO" // unspent outputs, including first consumed transaction hash graphPrefixDeposit = "DEPOSIT" - graphPrefixMint = "MINT" + graphPrefixMint = "MINTUNIVERSAL" graphPrefixTransaction = "TRANSACTION" // raw transaction, may not be finalized yet, if finalized with first finalized snapshot hash graphPrefixFinalization = "FINALIZATION" // transaction finalization hack graphPrefixUnique = "UNIQUE" // unique transaction in one node diff --git a/storage/badger_mint.go b/storage/badger_mint.go index b1d3a9b2d..818a3f0e1 100644 --- a/storage/badger_mint.go +++ b/storage/badger_mint.go @@ -26,7 +26,7 @@ func (s *BadgerStore) ReadMintDistributions(offset, count uint64) ([]*common.Min defer it.Close() it.Seek(graphMintKey(offset)) - for ; it.Valid() && uint64(len(mints)) < count; it.Next() { + for ; it.ValidForPrefix(opts.Prefix) && uint64(len(mints)) < count; it.Next() { item := it.Item() key := item.KeyCopy(nil) ival, err := item.ValueCopy(nil) diff --git a/storage/badger_mint_fix.go b/storage/badger_mint_fix.go new file mode 100644 index 000000000..9d9ddd4f6 --- /dev/null +++ b/storage/badger_mint_fix.go @@ -0,0 +1,62 @@ +package storage + +import ( + "encoding/binary" + + "github.com/MixinNetwork/mixin/common" + "github.com/dgraph-io/badger/v4" +) + +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 +}