OSDN Git Service

edit the db (#2044)
authorPaladz <yzhu101@uottawa.ca>
Mon, 19 Jul 2021 09:06:34 +0000 (17:06 +0800)
committerGitHub <noreply@github.com>
Mon, 19 Jul 2021 09:06:34 +0000 (17:06 +0800)
Co-authored-by: paladz <colt@ColtdeMacBook-Pro.local>
database/store.go
database/store_geter.go
database/utxo_view.go
test/utxo_view/utxo_view_test.go
test/utxo_view/utxo_view_test_util.go

index ba5d903..8076516 100644 (file)
@@ -19,8 +19,6 @@ import (
 const logModule = "leveldb"
 
 var (
-       // CheckpointPrefix represent the namespace of checkpoints in db
-       CheckpointPrefix = []byte("CP:")
        // BlockStoreKey block store key
        BlockStoreKey = []byte("blockStore")
 )
@@ -244,7 +242,7 @@ func (s *Store) SaveChainStatus(blockHeader *types.BlockHeader, mainBlockHeaders
 func calcCheckpointKey(height uint64, hash *bc.Hash) []byte {
        buf := make([]byte, 8)
        binary.BigEndian.PutUint64(buf, height)
-       key := append(CheckpointPrefix, buf...)
+       key := append(checkpointKeyPrefix, buf...)
        if hash != nil {
                key = append(key, hash.Bytes()...)
        }
@@ -277,7 +275,7 @@ func (s *Store) GetCheckpointsByHeight(height uint64) ([]*state.Checkpoint, erro
 // CheckpointsFromNode return all checkpoints from specified block height and hash
 func (s *Store) CheckpointsFromNode(height uint64, hash *bc.Hash) ([]*state.Checkpoint, error) {
        startKey := calcCheckpointKey(height, hash)
-       iter := s.db.IteratorPrefixWithStart(CheckpointPrefix, startKey, false)
+       iter := s.db.IteratorPrefixWithStart(checkpointKeyPrefix, startKey, false)
 
        firstCheckpoint := &state.Checkpoint{}
        if err := json.Unmarshal(iter.Value(), firstCheckpoint); err != nil {
@@ -317,16 +315,6 @@ func (s *Store) loadCheckpointsFromIter(iter dbm.Iterator) ([]*state.Checkpoint,
 // SaveCheckpoints bulk save multiple checkpoint
 func (s *Store) SaveCheckpoints(checkpoints []*state.Checkpoint) error {
        batch := s.db.NewBatch()
-
-       if err := s.saveCheckpoints(batch, checkpoints); err != nil {
-               return err
-       }
-
-       batch.Write()
-       return nil
-}
-
-func (s *Store) saveCheckpoints(batch dbm.Batch, checkpoints []*state.Checkpoint) error {
        for _, checkpoint := range checkpoints {
                startTime := time.Now()
                data, err := json.Marshal(checkpoint)
@@ -343,5 +331,7 @@ func (s *Store) saveCheckpoints(batch dbm.Batch, checkpoints []*state.Checkpoint
                        "duration": time.Since(startTime),
                }).Info("checkpoint saved on disk")
        }
+
+       batch.Write()
        return nil
 }
index dcf0baa..2761ee6 100644 (file)
@@ -13,11 +13,12 @@ import (
 const (
        colon = byte(0x3a)
 
-       blockStore byte = iota
-       blockHashes
+       blockHashes byte = iota + 1
        blockHeader
        blockTransactions
        mainChainIndex
+       checkpoint
+       utxo
 )
 
 var (
@@ -26,6 +27,8 @@ var (
        blockHeaderKeyPrefix    = []byte{blockHeader, colon}
        blockTransactionsKey    = []byte{blockTransactions, colon}
        mainChainIndexKeyPrefix = []byte{mainChainIndex, colon}
+       checkpointKeyPrefix     = []byte{checkpoint, colon}
+       UtxoKeyPrefix           = []byte{utxo, colon}
 )
 
 func calcMainChainIndexPrefix(height uint64) []byte {
index a165199..df30c67 100644 (file)
@@ -1,18 +1,16 @@
 package database
 
 import (
+       dbm "github.com/bytom/bytom/database/leveldb"
        "github.com/bytom/bytom/database/storage"
        "github.com/bytom/bytom/errors"
        "github.com/bytom/bytom/protocol/bc"
        "github.com/bytom/bytom/protocol/state"
        "github.com/golang/protobuf/proto"
-       dbm "github.com/bytom/bytom/database/leveldb"
 )
 
-const UtxoPreFix = "UT:"
-
 func CalcUtxoKey(hash *bc.Hash) []byte {
-       return []byte(UtxoPreFix + hash.String())
+       return append(UtxoKeyPrefix, hash.Bytes()...)
 }
 
 func getTransactionsUtxo(db dbm.DB, view *state.UtxoViewpoint, txs []*bc.Tx) error {
index 4294407..1c29c4e 100644 (file)
@@ -218,10 +218,10 @@ func TestAttachOrDetachBlocks(t *testing.T) {
                result := make(map[string]*storage.UtxoEntry)
 
                for k, v := range c.want {
-                       want[string(calcUtxoKey(&k))] = v
+                       want[string(database.CalcUtxoKey(&k))] = v
                }
 
-               iter := testDB.IteratorPrefix([]byte(utxoPreFix))
+               iter := testDB.IteratorPrefix([]byte(database.UtxoKeyPrefix))
                defer iter.Release()
 
                for iter.Next() {
index 49b3462..52e988f 100644 (file)
@@ -9,12 +9,6 @@ import (
        "github.com/bytom/bytom/testutil"
 )
 
-const utxoPreFix = "UT:"
-
-func calcUtxoKey(hash *bc.Hash) []byte {
-       return []byte(utxoPreFix + hash.String())
-}
-
 type tx struct {
        Tx *types.Tx
 }