From a072d52d767a3eaa08f0409c75e0a9ca68a839ad Mon Sep 17 00:00:00 2001 From: Chengcheng Zhang <943420582@qq.com> Date: Wed, 19 Jun 2019 19:29:14 +0800 Subject: [PATCH] move DB interface to dbutils --- accesstoken/accesstoken.go | 6 +++--- account/accounts.go | 6 +++--- account/utxo_keeper.go | 6 +++--- asset/asset.go | 6 +++--- database/dbutils/db_interface.go | 34 +++++++++++++++++++++++++++++++ database/leveldb/db.go | 44 ++++++---------------------------------- database/leveldb/go_level_db.go | 9 ++++---- database/leveldb/mem_db.go | 10 +++++---- database/store.go | 14 ++++++------- database/utxo_view.go | 10 ++++----- p2p/discover/dht/database.go | 5 +++-- p2p/security/blacklist.go | 3 ++- p2p/test_util.go | 4 ++-- test/bench_blockchain_test.go | 13 ++++++------ test/chain_test_util.go | 3 ++- test/tx_test_util.go | 4 ++-- test/util.go | 4 ++-- wallet/annotated.go | 6 +++--- wallet/indexer.go | 8 ++++---- wallet/recovery.go | 6 +++--- wallet/utxo.go | 8 ++++---- wallet/wallet.go | 8 ++++---- wallet/wallet_test.go | 3 ++- 23 files changed, 115 insertions(+), 105 deletions(-) create mode 100644 database/dbutils/db_interface.go diff --git a/accesstoken/accesstoken.go b/accesstoken/accesstoken.go index a2b9d858..3e4e85dc 100644 --- a/accesstoken/accesstoken.go +++ b/accesstoken/accesstoken.go @@ -11,7 +11,7 @@ import ( "time" "github.com/vapor/crypto/sha3pool" - dbm "github.com/vapor/database/leveldb" + "github.com/vapor/database/dbutils" "github.com/vapor/errors" ) @@ -44,11 +44,11 @@ type Token struct { // 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, } diff --git a/account/accounts.go b/account/accounts.go index 0526efe7..94549f6a 100644 --- a/account/accounts.go +++ b/account/accounts.go @@ -19,7 +19,7 @@ import ( "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" @@ -103,7 +103,7 @@ type CtrlProgram struct { // Manager stores accounts and their associated control programs. type Manager struct { - db dbm.DB + db dbutils.DB chain *protocol.Chain utxoKeeper *utxoKeeper @@ -119,7 +119,7 @@ type Manager struct { } // 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, diff --git a/account/utxo_keeper.go b/account/utxo_keeper.go index cd9beea6..f331585c 100644 --- a/account/utxo_keeper.go +++ b/account/utxo_keeper.go @@ -11,7 +11,7 @@ import ( 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" ) @@ -55,7 +55,7 @@ type utxoKeeper struct { // `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 @@ -64,7 +64,7 @@ type utxoKeeper struct { 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, diff --git a/asset/asset.go b/asset/asset.go index 12ae60b3..2571664a 100644 --- a/asset/asset.go +++ b/asset/asset.go @@ -9,7 +9,7 @@ import ( "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" @@ -70,7 +70,7 @@ var ( ) //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, @@ -82,7 +82,7 @@ func NewRegistry(db dbm.DB, chain *protocol.Chain) *Registry { // 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 diff --git a/database/dbutils/db_interface.go b/database/dbutils/db_interface.go new file mode 100644 index 00000000..40ef2958 --- /dev/null +++ b/database/dbutils/db_interface.go @@ -0,0 +1,34 @@ +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 +} diff --git a/database/leveldb/db.go b/database/leveldb/db.go index 38cab5bd..cb4dc682 100644 --- a/database/leveldb/db.go +++ b/database/leveldb/db.go @@ -1,41 +1,9 @@ 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. @@ -44,7 +12,7 @@ const ( 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{} @@ -56,7 +24,7 @@ func registerDBCreator(backend string, creator dbCreator, force bool) { 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)) diff --git a/database/leveldb/go_level_db.go b/database/leveldb/go_level_db.go index e9e8d3dd..2d9ecef1 100644 --- a/database/leveldb/go_level_db.go +++ b/database/leveldb/go_level_db.go @@ -9,12 +9,13 @@ import ( "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) @@ -155,15 +156,15 @@ func (it *goLevelDBIterator) Release() { 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} } diff --git a/database/leveldb/mem_db.go b/database/leveldb/mem_db.go index 62f40fc6..d110bbd7 100644 --- a/database/leveldb/mem_db.go +++ b/database/leveldb/mem_db.go @@ -5,10 +5,12 @@ import ( "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) } @@ -120,11 +122,11 @@ func (it *memDBIterator) Error() error { 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 @@ -143,7 +145,7 @@ func (db *MemDB) IteratorPrefix(prefix []byte) Iterator { return it } -func (db *MemDB) NewBatch() Batch { +func (db *MemDB) NewBatch() dbutils.Batch { return &memDBBatch{db, nil} } diff --git a/database/store.go b/database/store.go index 6de274be..5c4f18ff 100644 --- a/database/store.go +++ b/database/store.go @@ -9,7 +9,7 @@ import ( "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" @@ -28,7 +28,7 @@ var ( 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 @@ -45,7 +45,7 @@ func loadBlockStoreStateJSON(db dbm.DB) *protocol.BlockStoreState { // 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 } @@ -71,7 +71,7 @@ func calcVoteResultKey(seq uint64) []byte { } // 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()) @@ -85,7 +85,7 @@ func GetBlockHeader(db dbm.DB, hash *bc.Hash, height uint64) (*types.BlockHeader } // 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()) @@ -99,7 +99,7 @@ func GetBlockTransactions(db dbm.DB, hash *bc.Hash) ([]*types.Tx, error) { } // 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 @@ -113,7 +113,7 @@ func GetVoteResult(db dbm.DB, seq uint64) (*state.VoteResult, error) { } // 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) } diff --git a/database/utxo_view.go b/database/utxo_view.go index f05dafda..0708e3cb 100644 --- a/database/utxo_view.go +++ b/database/utxo_view.go @@ -2,7 +2,7 @@ package database 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" @@ -15,7 +15,7 @@ func calcUtxoKey(hash *bc.Hash) []byte { 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) { @@ -58,7 +58,7 @@ func getTransactionsUtxo(db dbm.DB, view *state.UtxoViewpoint, txs []*bc.Tx) err 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 { @@ -70,7 +70,7 @@ func getUtxo(db dbm.DB, hash *bc.Hash) (*storage.UtxoEntry, error) { 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)) @@ -91,6 +91,6 @@ func saveUtxoView(batch dbm.Batch, view *state.UtxoViewpoint) error { return nil } -func SaveUtxoView(batch dbm.Batch, view *state.UtxoViewpoint) error { +func SaveUtxoView(batch dbutils.Batch, view *state.UtxoViewpoint) error { return saveUtxoView(batch, view) } diff --git a/p2p/discover/dht/database.go b/p2p/discover/dht/database.go index 13dc923f..9f416ef3 100644 --- a/p2p/discover/dht/database.go +++ b/p2p/discover/dht/database.go @@ -16,6 +16,7 @@ import ( 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" ) @@ -28,7 +29,7 @@ var ( // 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 @@ -345,7 +346,7 @@ func (db *nodeDB) updateTopicRegTickets(id NodeID, issued, used uint32) { // 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) diff --git a/p2p/security/blacklist.go b/p2p/security/blacklist.go index f8ca05e0..5badcbc3 100644 --- a/p2p/security/blacklist.go +++ b/p2p/security/blacklist.go @@ -7,6 +7,7 @@ import ( "time" cfg "github.com/vapor/config" + "github.com/vapor/database/dbutils" dbm "github.com/vapor/database/leveldb" ) @@ -21,7 +22,7 @@ var ( type Blacklist struct { peers map[string]time.Time - db dbm.DB + db dbutils.DB mtx sync.Mutex } diff --git a/p2p/test_util.go b/p2p/test_util.go index fa5d631f..05fdc311 100644 --- a/p2p/test_util.go +++ b/p2p/test_util.go @@ -7,7 +7,7 @@ import ( 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" @@ -88,7 +88,7 @@ func (m *mockDiscv) ReadRandomNodes(buf []*dht.Node) (n int) { 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 diff --git a/test/bench_blockchain_test.go b/test/bench_blockchain_test.go index 0d548feb..c6d9898a 100644 --- a/test/bench_blockchain_test.go +++ b/test/bench_blockchain_test.go @@ -15,6 +15,7 @@ import ( "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" @@ -97,7 +98,7 @@ func benchInsertChain(b *testing.B, blockTxNumber int, otherAssetNum int, txType } } -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 @@ -159,7 +160,7 @@ func InsertChain(chain *protocol.Chain, txPool *protocol.TxPool, txs []*types.Tx } } - 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 } @@ -357,7 +358,7 @@ func CreateTxbyNum(txNumber, otherAssetNum int) ([]*types.Tx, error) { 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 @@ -367,7 +368,7 @@ func SetUtxoView(db dbm.DB, view *state.UtxoViewpoint) error { } //-------------------------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 { @@ -409,7 +410,7 @@ func MockTxsP2PKH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) 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 { @@ -456,7 +457,7 @@ func MockTxsP2SH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) 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 { diff --git a/test/chain_test_util.go b/test/chain_test_util.go index 9181053f..d797adac 100644 --- a/test/chain_test_util.go +++ b/test/chain_test_util.go @@ -9,6 +9,7 @@ import ( "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" @@ -21,7 +22,7 @@ const utxoPrefix = "UT:" type chainTestContext struct { Chain *protocol.Chain - DB dbm.DB + DB dbutils.DB Store *database.Store } diff --git a/test/tx_test_util.go b/test/tx_test_util.go index 11c63136..c80f280e 100644 --- a/test/tx_test_util.go +++ b/test/tx_test_util.go @@ -14,7 +14,7 @@ import ( "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" @@ -263,7 +263,7 @@ func CreateSpendInput(tx *types.Tx, outputIndex uint64) (*types.SpendInput, erro } // 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) diff --git a/test/util.go b/test/util.go index 54b9b05e..b9063358 100644 --- a/test/util.go +++ b/test/util.go @@ -11,7 +11,7 @@ import ( "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" @@ -25,7 +25,7 @@ const ( ) // 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() diff --git a/wallet/annotated.go b/wallet/annotated.go index 66048769..9e402868 100644 --- a/wallet/annotated.go +++ b/wallet/annotated.go @@ -13,7 +13,7 @@ import ( "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" ) @@ -82,7 +82,7 @@ func (w *Wallet) getAliasDefinition(assetID bc.AssetID) (string, json.RawMessage } // 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 @@ -107,7 +107,7 @@ func annotateTxsAccount(txs []*query.AnnotatedTx, walletDB dbm.DB) { } } -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{} diff --git a/wallet/indexer.go b/wallet/indexer.go index bc61dc33..d4388e96 100644 --- a/wallet/indexer.go +++ b/wallet/indexer.go @@ -14,7 +14,7 @@ import ( "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" @@ -68,7 +68,7 @@ func parseGlobalTxIdx(globalTxIdx []byte) (*bc.Hash, uint64) { } // 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() @@ -84,7 +84,7 @@ func (w *Wallet) deleteTransactions(batch dbm.Batch, height uint64) { // 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() @@ -120,7 +120,7 @@ type TxSummary struct { } // 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) diff --git a/wallet/recovery.go b/wallet/recovery.go index 2933ad6f..cdc8990c 100644 --- a/wallet/recovery.go +++ b/wallet/recovery.go @@ -12,7 +12,7 @@ import ( "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" @@ -170,7 +170,7 @@ func (rs *recoveryState) stateForScope(account *account.Account) { type recoveryManager struct { mu sync.Mutex - db dbm.DB + db dbutils.DB accountMgr *account.Manager locked int32 @@ -187,7 +187,7 @@ type recoveryManager struct { } // 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, diff --git a/wallet/utxo.go b/wallet/utxo.go index 1e60a34e..a70017f8 100644 --- a/wallet/utxo.go +++ b/wallet/utxo.go @@ -9,7 +9,7 @@ import ( "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" @@ -48,7 +48,7 @@ func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSma 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 { @@ -75,7 +75,7 @@ func (w *Wallet) attachUtxos(batch dbm.Batch, b *types.Block, txStatus *bc.Trans } } -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 { @@ -151,7 +151,7 @@ func (w *Wallet) filterAccountUtxo(utxos []*account.UTXO) []*account.UTXO { 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 { diff --git a/wallet/wallet.go b/wallet/wallet.go index 92a24b96..515316c2 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -9,7 +9,7 @@ import ( "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" @@ -42,7 +42,7 @@ type StatusInfo struct { //Wallet is related to storing account unspent outputs type Wallet struct { - DB dbm.DB + DB dbutils.DB rw sync.RWMutex status StatusInfo TxIndexFlag bool @@ -58,7 +58,7 @@ type Wallet struct { } //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, @@ -156,7 +156,7 @@ func (w *Wallet) loadWalletInfo() error { 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") diff --git a/wallet/wallet_test.go b/wallet/wallet_test.go index 823eab9e..cca69718 100644 --- a/wallet/wallet_test.go +++ b/wallet/wallet_test.go @@ -17,6 +17,7 @@ import ( "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" @@ -378,7 +379,7 @@ func mockTxData(utxos []*account.UTXO, testAccount *account.Account) (*txbuilder 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, -- 2.11.0