const logModule = "leveldb"
var (
- // CheckpointPrefix represent the namespace of checkpoints in db
- CheckpointPrefix = []byte("CP:")
// BlockStoreKey block store key
BlockStoreKey = []byte("blockStore")
)
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()...)
}
// 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 {
// 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)
"duration": time.Since(startTime),
}).Info("checkpoint saved on disk")
}
+
+ batch.Write()
return nil
}
const (
colon = byte(0x3a)
- blockStore byte = iota
- blockHashes
+ blockHashes byte = iota + 1
blockHeader
blockTransactions
mainChainIndex
+ checkpoint
+ utxo
)
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 {
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 {
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() {