"time"
"github.com/vapor/crypto/sha3pool"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
"github.com/vapor/errors"
)
// CredentialStore store user access credential.
type CredentialStore struct {
- DB dbm.DB
+ DB dbutils.DB
}
// NewStore creates and returns a new Store object.
-func NewStore(db dbm.DB) *CredentialStore {
+func NewStore(db dbutils.DB) *CredentialStore {
return &CredentialStore{
DB: db,
}
"github.com/vapor/crypto"
"github.com/vapor/crypto/ed25519/chainkd"
"github.com/vapor/crypto/sha3pool"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
"github.com/vapor/errors"
"github.com/vapor/protocol"
"github.com/vapor/protocol/bc"
// Manager stores accounts and their associated control programs.
type Manager struct {
- db dbm.DB
+ db dbutils.DB
chain *protocol.Chain
utxoKeeper *utxoKeeper
}
// NewManager creates a new account manager
-func NewManager(walletDB dbm.DB, chain *protocol.Chain) *Manager {
+func NewManager(walletDB dbutils.DB, chain *protocol.Chain) *Manager {
return &Manager{
db: walletDB,
chain: chain,
log "github.com/sirupsen/logrus"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
"github.com/vapor/errors"
"github.com/vapor/protocol/bc"
)
// `sync/atomic` expects the first word in an allocated struct to be 64-bit
// aligned on both ARM and x86-32. See https://goo.gl/zW7dgq for more details.
nextIndex uint64
- db dbm.DB
+ db dbutils.DB
mtx sync.RWMutex
currentHeight func() uint64
reservations map[uint64]*reservation
}
-func newUtxoKeeper(f func() uint64, walletdb dbm.DB) *utxoKeeper {
+func newUtxoKeeper(f func() uint64, walletdb dbutils.DB) *utxoKeeper {
uk := &utxoKeeper{
db: walletdb,
currentHeight: f,
"github.com/golang/groupcache/lru"
"github.com/vapor/consensus"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
chainjson "github.com/vapor/encoding/json"
"github.com/vapor/errors"
"github.com/vapor/protocol"
)
//NewRegistry create new registry
-func NewRegistry(db dbm.DB, chain *protocol.Chain) *Registry {
+func NewRegistry(db dbutils.DB, chain *protocol.Chain) *Registry {
initNativeAsset()
return &Registry{
db: db,
// Registry tracks and stores all known assets on a blockchain.
type Registry struct {
- db dbm.DB
+ db dbutils.DB
chain *protocol.Chain
cacheMu sync.Mutex
--- /dev/null
+package dbutils
+
+type DB interface {
+ Get([]byte) []byte
+ Set([]byte, []byte)
+ SetSync([]byte, []byte)
+ Delete([]byte)
+ DeleteSync([]byte)
+ Close()
+ NewBatch() Batch
+ Iterator() Iterator
+ IteratorPrefix([]byte) Iterator
+
+ // For debugging
+ Print()
+ Stats() map[string]string
+}
+
+type Batch interface {
+ Set(key, value []byte)
+ Delete(key []byte)
+ Write()
+}
+
+type Iterator interface {
+ Next() bool
+
+ Key() []byte
+ Value() []byte
+ Seek([]byte) bool
+
+ Release()
+ Error() error
+}
package db
-import . "github.com/tendermint/tmlibs/common"
-
-type DB interface {
- Get([]byte) []byte
- Set([]byte, []byte)
- SetSync([]byte, []byte)
- Delete([]byte)
- DeleteSync([]byte)
- Close()
- NewBatch() Batch
- Iterator() Iterator
- IteratorPrefix([]byte) Iterator
-
- // For debugging
- Print()
- Stats() map[string]string
-}
-
-type Batch interface {
- Set(key, value []byte)
- Delete(key []byte)
- Write()
-}
-
-type Iterator interface {
- Next() bool
-
- Key() []byte
- Value() []byte
- Seek([]byte) bool
-
- Release()
- Error() error
-}
-
-//-----------------------------------------------------------------------------
+import (
+ . "github.com/tendermint/tmlibs/common"
+ "github.com/vapor/database/dbutils"
+)
const (
LevelDBBackendStr = "leveldb" // legacy, defaults to goleveldb.
MemDBBackendStr = "memdb"
)
-type dbCreator func(name string, dir string) (DB, error)
+type dbCreator func(name string, dir string) (dbutils.DB, error)
var backends = map[string]dbCreator{}
backends[backend] = creator
}
-func NewDB(name string, backend string, dir string) DB {
+func NewDB(name string, backend string, dir string) dbutils.DB {
db, err := backends[backend](name, dir)
if err != nil {
PanicSanity(Fmt("Error initializing DB: %v", err))
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
+ "github.com/vapor/database/dbutils"
. "github.com/tendermint/tmlibs/common"
)
func init() {
- dbCreator := func(name string, dir string) (DB, error) {
+ dbCreator := func(name string, dir string) (dbutils.DB, error) {
return NewGoLevelDB(name, dir)
}
registerDBCreator(LevelDBBackendStr, dbCreator, false)
it.source.Release()
}
-func (db *GoLevelDB) Iterator() Iterator {
+func (db *GoLevelDB) Iterator() dbutils.Iterator {
return &goLevelDBIterator{db.db.NewIterator(nil, nil)}
}
-func (db *GoLevelDB) IteratorPrefix(prefix []byte) Iterator {
+func (db *GoLevelDB) IteratorPrefix(prefix []byte) dbutils.Iterator {
return &goLevelDBIterator{db.db.NewIterator(util.BytesPrefix(prefix), nil)}
}
-func (db *GoLevelDB) NewBatch() Batch {
+func (db *GoLevelDB) NewBatch() dbutils.Batch {
batch := new(leveldb.Batch)
return &goLevelDBBatch{db, batch}
}
"sort"
"strings"
"sync"
+
+ "github.com/vapor/database/dbutils"
)
func init() {
- registerDBCreator(MemDBBackendStr, func(name string, dir string) (DB, error) {
+ registerDBCreator(MemDBBackendStr, func(name string, dir string) (dbutils.DB, error) {
return NewMemDB(), nil
}, false)
}
return nil
}
-func (db *MemDB) Iterator() Iterator {
+func (db *MemDB) Iterator() dbutils.Iterator {
return db.IteratorPrefix([]byte{})
}
-func (db *MemDB) IteratorPrefix(prefix []byte) Iterator {
+func (db *MemDB) IteratorPrefix(prefix []byte) dbutils.Iterator {
it := newMemDBIterator()
it.db = db
it.last = -1
return it
}
-func (db *MemDB) NewBatch() Batch {
+func (db *MemDB) NewBatch() dbutils.Batch {
return &memDBBatch{db, nil}
}
"github.com/golang/protobuf/proto"
log "github.com/sirupsen/logrus"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
"github.com/vapor/database/storage"
"github.com/vapor/errors"
"github.com/vapor/protocol"
voteResultPrefix = []byte("VR:")
)
-func loadBlockStoreStateJSON(db dbm.DB) *protocol.BlockStoreState {
+func loadBlockStoreStateJSON(db dbutils.DB) *protocol.BlockStoreState {
bytes := db.Get(blockStoreKey)
if bytes == nil {
return nil
// It satisfies the interface protocol.Store, and provides additional
// methods for querying current data.
type Store struct {
- db dbm.DB
+ db dbutils.DB
cache cache
}
}
// GetBlockHeader return the block header by given hash and height
-func GetBlockHeader(db dbm.DB, hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
+func GetBlockHeader(db dbutils.DB, hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
binaryBlockHeader := db.Get(calcBlockHeaderKey(height, hash))
if binaryBlockHeader == nil {
return nil, fmt.Errorf("There are no blockHeader with given hash %s", hash.String())
}
// GetBlockTransactions return the block transactions by given hash
-func GetBlockTransactions(db dbm.DB, hash *bc.Hash) ([]*types.Tx, error) {
+func GetBlockTransactions(db dbutils.DB, hash *bc.Hash) ([]*types.Tx, error) {
binaryBlockTxs := db.Get(calcBlockTransactionsKey(hash))
if binaryBlockTxs == nil {
return nil, fmt.Errorf("There are no block transactions with given hash %s", hash.String())
}
// GetVoteResult return the vote result by given sequence
-func GetVoteResult(db dbm.DB, seq uint64) (*state.VoteResult, error) {
+func GetVoteResult(db dbutils.DB, seq uint64) (*state.VoteResult, error) {
data := db.Get(calcVoteResultKey(seq))
if data == nil {
return nil, protocol.ErrNotFoundVoteResult
}
// NewStore creates and returns a new Store object.
-func NewStore(db dbm.DB) *Store {
+func NewStore(db dbutils.DB) *Store {
fillBlockHeaderFn := func(hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
return GetBlockHeader(db, hash, height)
}
import (
"github.com/golang/protobuf/proto"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
"github.com/vapor/database/storage"
"github.com/vapor/errors"
"github.com/vapor/protocol/bc"
return []byte(utxoPreFix + hash.String())
}
-func getTransactionsUtxo(db dbm.DB, view *state.UtxoViewpoint, txs []*bc.Tx) error {
+func getTransactionsUtxo(db dbutils.DB, view *state.UtxoViewpoint, txs []*bc.Tx) error {
for _, tx := range txs {
for _, prevout := range tx.SpentOutputIDs {
if view.HasUtxo(&prevout) {
return nil
}
-func getUtxo(db dbm.DB, hash *bc.Hash) (*storage.UtxoEntry, error) {
+func getUtxo(db dbutils.DB, hash *bc.Hash) (*storage.UtxoEntry, error) {
var utxo storage.UtxoEntry
data := db.Get(calcUtxoKey(hash))
if data == nil {
return &utxo, nil
}
-func saveUtxoView(batch dbm.Batch, view *state.UtxoViewpoint) error {
+func saveUtxoView(batch dbutils.Batch, view *state.UtxoViewpoint) error {
for key, entry := range view.Entries {
if (entry.Type == storage.CrosschainUTXOType) && (!entry.Spent) {
batch.Delete(calcUtxoKey(&key))
return nil
}
-func SaveUtxoView(batch dbm.Batch, view *state.UtxoViewpoint) error {
+func SaveUtxoView(batch dbutils.Batch, view *state.UtxoViewpoint) error {
return saveUtxoView(batch, view)
}
wire "github.com/tendermint/go-wire"
"github.com/vapor/crypto"
+ "github.com/vapor/database/dbutils"
dbm "github.com/vapor/database/leveldb"
"github.com/vapor/errors"
)
// nodeDB stores all nodes we know about.
type nodeDB struct {
- lvl dbm.DB // Interface to the database itself
+ lvl dbutils.DB // Interface to the database itself
self NodeID // Own node id to prevent adding it into the database
runner sync.Once // Ensures we can start at most one expirer
quit chan struct{} // Channel to signal the expiring thread to stop
// reads the next node record from the iterator, skipping over other
// database entries.
-func nextNode(it dbm.Iterator) *Node {
+func nextNode(it dbutils.Iterator) *Node {
var (
n = int(0)
err = error(nil)
"time"
cfg "github.com/vapor/config"
+ "github.com/vapor/database/dbutils"
dbm "github.com/vapor/database/leveldb"
)
type Blacklist struct {
peers map[string]time.Time
- db dbm.DB
+ db dbutils.DB
mtx sync.Mutex
}
cmn "github.com/tendermint/tmlibs/common"
cfg "github.com/vapor/config"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
"github.com/vapor/p2p/connection"
"github.com/vapor/p2p/discover/dht"
"github.com/vapor/p2p/signlib"
return 0
}
-func MakeSwitch(cfg *cfg.Config, testdb dbm.DB, privKey signlib.PrivKey, initSwitch func(*Switch) *Switch) *Switch {
+func MakeSwitch(cfg *cfg.Config, testdb dbutils.DB, privKey signlib.PrivKey, initSwitch func(*Switch) *Switch) *Switch {
// new switch, add reactors
l, listenAddr := GetListener(cfg.P2P)
cfg.P2P.LANDiscover = false
"github.com/vapor/consensus"
"github.com/vapor/crypto/ed25519/chainkd"
"github.com/vapor/database"
+ "github.com/vapor/database/dbutils"
dbm "github.com/vapor/database/leveldb"
"github.com/vapor/database/storage"
"github.com/vapor/event"
}
}
-func GenerateChainData(dirPath string, testDB dbm.DB, txNumber, otherAssetNum int, txType string) (*protocol.Chain, []*types.Tx, *protocol.TxPool, error) {
+func GenerateChainData(dirPath string, testDB dbutils.DB, txNumber, otherAssetNum int, txType string) (*protocol.Chain, []*types.Tx, *protocol.TxPool, error) {
var err error
// generate transactions
}
}
- block, err := proposal.NewBlockTemplate(chain, txPool, nil, uint64(time.Now().UnixNano() / 1e6))
+ block, err := proposal.NewBlockTemplate(chain, txPool, nil, uint64(time.Now().UnixNano()/1e6))
if err != nil {
return err
}
return txs, nil
}
-func SetUtxoView(db dbm.DB, view *state.UtxoViewpoint) error {
+func SetUtxoView(db dbutils.DB, view *state.UtxoViewpoint) error {
batch := db.NewBatch()
if err := database.SaveUtxoView(batch, view); err != nil {
return err
}
//-------------------------Mock actual transaction----------------------------------
-func MockTxsP2PKH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
+func MockTxsP2PKH(keyDirPath string, testDB dbutils.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
accountManager := account.NewManager(testDB, nil)
hsm, err := pseudohsm.New(keyDirPath)
if err != nil {
return txs, nil
}
-func MockTxsP2SH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
+func MockTxsP2SH(keyDirPath string, testDB dbutils.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
accountManager := account.NewManager(testDB, nil)
hsm, err := pseudohsm.New(keyDirPath)
if err != nil {
return txs, nil
}
-func MockTxsMultiSign(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
+func MockTxsMultiSign(keyDirPath string, testDB dbutils.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
accountManager := account.NewManager(testDB, nil)
hsm, err := pseudohsm.New(keyDirPath)
if err != nil {
"github.com/vapor/blockchain/txbuilder"
"github.com/vapor/consensus"
"github.com/vapor/database"
+ "github.com/vapor/database/dbutils"
dbm "github.com/vapor/database/leveldb"
"github.com/vapor/database/storage"
"github.com/vapor/protocol"
type chainTestContext struct {
Chain *protocol.Chain
- DB dbm.DB
+ DB dbutils.DB
Store *database.Store
}
"github.com/vapor/consensus"
"github.com/vapor/crypto/ed25519/chainkd"
"github.com/vapor/crypto/sha3pool"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
"github.com/vapor/errors"
"github.com/vapor/protocol/bc"
"github.com/vapor/protocol/bc/types"
}
// SignInstructionFor read CtrlProgram from db, construct SignInstruction for SpendInput
-func SignInstructionFor(input *types.SpendInput, db dbm.DB, signer *signers.Signer) (*txbuilder.SigningInstruction, error) {
+func SignInstructionFor(input *types.SpendInput, db dbutils.DB, signer *signers.Signer) (*txbuilder.SigningInstruction, error) {
cp := account.CtrlProgram{}
var hash [32]byte
sha3pool.Sum256(hash[:], input.ControlProgram)
"github.com/vapor/consensus"
"github.com/vapor/crypto/ed25519/chainkd"
"github.com/vapor/database"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
"github.com/vapor/event"
"github.com/vapor/protocol"
"github.com/vapor/protocol/bc"
)
// MockChain mock chain with genesis block
-func MockChain(testDB dbm.DB) (*protocol.Chain, *database.Store, *protocol.TxPool, error) {
+func MockChain(testDB dbutils.DB) (*protocol.Chain, *database.Store, *protocol.TxPool, error) {
config.CommonConfig = config.DefaultConfig()
store := database.NewStore(testDB)
dispatcher := event.NewDispatcher()
"github.com/vapor/consensus"
"github.com/vapor/consensus/segwit"
"github.com/vapor/crypto/sha3pool"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
"github.com/vapor/protocol/bc"
"github.com/vapor/protocol/bc/types"
)
}
// annotateTxs adds account data to transactions
-func annotateTxsAccount(txs []*query.AnnotatedTx, walletDB dbm.DB) {
+func annotateTxsAccount(txs []*query.AnnotatedTx, walletDB dbutils.DB) {
for i, tx := range txs {
for j, input := range tx.Inputs {
//issue asset tx input SpentOutputID is nil
}
}
-func getAccountFromACP(program []byte, walletDB dbm.DB) (*account.Account, error) {
+func getAccountFromACP(program []byte, walletDB dbutils.DB) (*account.Account, error) {
var hash common.Hash
accountCP := account.CtrlProgram{}
localAccount := account.Account{}
"github.com/vapor/blockchain/query"
"github.com/vapor/consensus"
"github.com/vapor/crypto/sha3pool"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
chainjson "github.com/vapor/encoding/json"
"github.com/vapor/errors"
"github.com/vapor/protocol/bc"
}
// deleteTransaction delete transactions when orphan block rollback
-func (w *Wallet) deleteTransactions(batch dbm.Batch, height uint64) {
+func (w *Wallet) deleteTransactions(batch dbutils.Batch, height uint64) {
tmpTx := query.AnnotatedTx{}
txIter := w.DB.IteratorPrefix(calcDeleteKey(height))
defer txIter.Release()
// saveExternalAssetDefinition save external and local assets definition,
// when query ,query local first and if have no then query external
// details see getAliasDefinition
-func saveExternalAssetDefinition(b *types.Block, walletDB dbm.DB) {
+func saveExternalAssetDefinition(b *types.Block, walletDB dbutils.DB) {
storeBatch := walletDB.NewBatch()
defer storeBatch.Write()
}
// indexTransactions saves all annotated transactions to the database.
-func (w *Wallet) indexTransactions(batch dbm.Batch, b *types.Block, txStatus *bc.TransactionStatus) error {
+func (w *Wallet) indexTransactions(batch dbutils.Batch, b *types.Block, txStatus *bc.TransactionStatus) error {
annotatedTxs := w.filterAccountTxs(b, txStatus)
saveExternalAssetDefinition(b, w.DB)
annotateTxsAccount(annotatedTxs, w.DB)
"github.com/vapor/blockchain/signers"
"github.com/vapor/crypto/ed25519/chainkd"
"github.com/vapor/crypto/sha3pool"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
"github.com/vapor/errors"
"github.com/vapor/protocol/bc"
"github.com/vapor/protocol/bc/types"
type recoveryManager struct {
mu sync.Mutex
- db dbm.DB
+ db dbutils.DB
accountMgr *account.Manager
locked int32
}
// newRecoveryManager create recovery manger.
-func newRecoveryManager(db dbm.DB, accountMgr *account.Manager) *recoveryManager {
+func newRecoveryManager(db dbutils.DB, accountMgr *account.Manager) *recoveryManager {
return &recoveryManager{
db: db,
accountMgr: accountMgr,
"github.com/vapor/consensus"
"github.com/vapor/consensus/segwit"
"github.com/vapor/crypto/sha3pool"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
"github.com/vapor/errors"
"github.com/vapor/protocol/bc"
"github.com/vapor/protocol/bc/types"
return accountUtxos
}
-func (w *Wallet) attachUtxos(batch dbm.Batch, b *types.Block, txStatus *bc.TransactionStatus) {
+func (w *Wallet) attachUtxos(batch dbutils.Batch, b *types.Block, txStatus *bc.TransactionStatus) {
for txIndex, tx := range b.Transactions {
statusFail, err := txStatus.GetStatus(txIndex)
if err != nil {
}
}
-func (w *Wallet) detachUtxos(batch dbm.Batch, b *types.Block, txStatus *bc.TransactionStatus) {
+func (w *Wallet) detachUtxos(batch dbutils.Batch, b *types.Block, txStatus *bc.TransactionStatus) {
for txIndex := len(b.Transactions) - 1; txIndex >= 0; txIndex-- {
tx := b.Transactions[txIndex]
for j := range tx.Outputs {
return result
}
-func batchSaveUtxos(utxos []*account.UTXO, batch dbm.Batch) error {
+func batchSaveUtxos(utxos []*account.UTXO, batch dbutils.Batch) error {
for _, utxo := range utxos {
data, err := json.Marshal(utxo)
if err != nil {
"github.com/vapor/account"
"github.com/vapor/asset"
"github.com/vapor/blockchain/pseudohsm"
- dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/database/dbutils"
"github.com/vapor/errors"
"github.com/vapor/event"
"github.com/vapor/protocol"
//Wallet is related to storing account unspent outputs
type Wallet struct {
- DB dbm.DB
+ DB dbutils.DB
rw sync.RWMutex
status StatusInfo
TxIndexFlag bool
}
//NewWallet return a new wallet instance
-func NewWallet(walletDB dbm.DB, account *account.Manager, asset *asset.Registry, hsm *pseudohsm.HSM, chain *protocol.Chain, dispatcher *event.Dispatcher, txIndexFlag bool) (*Wallet, error) {
+func NewWallet(walletDB dbutils.DB, account *account.Manager, asset *asset.Registry, hsm *pseudohsm.HSM, chain *protocol.Chain, dispatcher *event.Dispatcher, txIndexFlag bool) (*Wallet, error) {
w := &Wallet{
DB: walletDB,
AccountMgr: account,
return w.AttachBlock(block)
}
-func (w *Wallet) commitWalletInfo(batch dbm.Batch) error {
+func (w *Wallet) commitWalletInfo(batch dbutils.Batch) error {
rawWallet, err := json.Marshal(w.status)
if err != nil {
log.WithFields(log.Fields{"module": logModule, "err": err}).Error("save wallet info")
"github.com/vapor/consensus"
"github.com/vapor/crypto/ed25519/chainkd"
"github.com/vapor/database"
+ "github.com/vapor/database/dbutils"
dbm "github.com/vapor/database/leveldb"
"github.com/vapor/event"
"github.com/vapor/protocol"
return tplBuilder.Build()
}
-func mockWallet(walletDB dbm.DB, account *account.Manager, asset *asset.Registry, chain *protocol.Chain, dispatcher *event.Dispatcher, txIndexFlag bool) *Wallet {
+func mockWallet(walletDB dbutils.DB, account *account.Manager, asset *asset.Registry, chain *protocol.Chain, dispatcher *event.Dispatcher, txIndexFlag bool) *Wallet {
wallet := &Wallet{
DB: walletDB,
AccountMgr: account,