OSDN Git Service

update
authorChengcheng Zhang <943420582@qq.com>
Wed, 3 Jul 2019 15:36:52 +0000 (23:36 +0800)
committerChengcheng Zhang <943420582@qq.com>
Wed, 3 Jul 2019 15:36:52 +0000 (23:36 +0800)
database/account_store.go
database/leveldb/db.go
database/wallet_store.go
wallet/utxo.go
wallet/wallet_test.go

index 57f2a63..055246d 100644 (file)
@@ -92,7 +92,7 @@ func (store *AccountStore) deleteAccountUTXOs(accountID string) error {
                batch = store.batch
        }
 
-       accountUtxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
+       accountUtxoIter := store.accountDB.IteratorPrefix([]byte(dbm.UTXOPrefix))
        defer accountUtxoIter.Release()
 
        for accountUtxoIter.Next() {
@@ -162,7 +162,7 @@ func (store *AccountStore) GetBip44ContractIndex(accountID string, change bool)
 
 // GetCoinbaseArbitrary get coinbase arbitrary
 func (store *AccountStore) GetCoinbaseArbitrary() []byte {
-       return store.accountDB.Get([]byte(CoinbaseAbKey))
+       return store.accountDB.Get([]byte(dbm.CoinbaseAbKey))
 }
 
 // GetContractIndex get contract index
@@ -189,7 +189,7 @@ func (store *AccountStore) GetControlProgram(hash bc.Hash) (*acc.CtrlProgram, er
 
 // GetMiningAddress get mining address
 func (store *AccountStore) GetMiningAddress() (*acc.CtrlProgram, error) {
-       rawCP := store.accountDB.Get([]byte(MiningAddressKey))
+       rawCP := store.accountDB.Get([]byte(dbm.MiningAddressKey))
        if rawCP == nil {
                return nil, acc.ErrFindMiningAddress
        }
@@ -231,8 +231,7 @@ func (store *AccountStore) ListAccounts(id string) ([]*acc.Account, error) {
 // ListControlPrograms get all local control programs
 func (store *AccountStore) ListControlPrograms() ([]*acc.CtrlProgram, error) {
        cps := []*acc.CtrlProgram{}
-       cpIter := store.accountDB.IteratorPrefix([]byte(ContractPrefix))
-       // cpIter := store.accountDB.IteratorPrefix([]byte{0x02, 0x3a})
+       cpIter := store.accountDB.IteratorPrefix([]byte(dbm.ContractPrefix))
        defer cpIter.Release()
 
        for cpIter.Next() {
@@ -247,7 +246,7 @@ func (store *AccountStore) ListControlPrograms() ([]*acc.CtrlProgram, error) {
 
 // ListUTXOs get utxos by accountID
 func (store *AccountStore) ListUTXOs() ([]*acc.UTXO, error) {
-       utxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
+       utxoIter := store.accountDB.IteratorPrefix([]byte(dbm.UTXOPrefix))
        defer utxoIter.Release()
 
        utxos := []*acc.UTXO{}
@@ -306,9 +305,9 @@ func (store *AccountStore) SetBip44ContractIndex(accountID string, change bool,
 // SetCoinbaseArbitrary set coinbase arbitrary
 func (store *AccountStore) SetCoinbaseArbitrary(arbitrary []byte) {
        if store.batch == nil {
-               store.accountDB.Set([]byte(CoinbaseAbKey), arbitrary)
+               store.accountDB.Set([]byte(dbm.CoinbaseAbKey), arbitrary)
        } else {
-               store.batch.Set([]byte(CoinbaseAbKey), arbitrary)
+               store.batch.Set([]byte(dbm.CoinbaseAbKey), arbitrary)
        }
 }
 
@@ -343,9 +342,9 @@ func (store *AccountStore) SetMiningAddress(program *acc.CtrlProgram) error {
        }
 
        if store.batch == nil {
-               store.accountDB.Set([]byte(MiningAddressKey), rawProgram)
+               store.accountDB.Set([]byte(dbm.MiningAddressKey), rawProgram)
        } else {
-               store.batch.Set([]byte(MiningAddressKey), rawProgram)
+               store.batch.Set([]byte(dbm.MiningAddressKey), rawProgram)
        }
        return nil
 }
index 80eb08d..201c7a7 100644 (file)
@@ -39,6 +39,44 @@ type Iterator interface {
 //-----------------------------------------------------------------------------
 
 const (
+       utxoPrefix  byte = iota //UTXOPrefix is StandardUTXOKey prefix
+       sutxoPrefix             //SUTXOPrefix is ContractUTXOKey prefix
+       contractPrefix
+       contractIndexPrefix
+       accountPrefix // AccountPrefix is account ID prefix
+       accountAliasPrefix
+       accountIndexPrefix
+       txPrefix            //TxPrefix is wallet database transactions prefix
+       txIndexPrefix       //TxIndexPrefix is wallet database tx index prefix
+       unconfirmedTxPrefix //UnconfirmedTxPrefix is txpool unconfirmed transactions prefix
+       globalTxIndexPrefix //GlobalTxIndexPrefix is wallet database global tx index prefix
+       walletKey
+       miningAddressKey
+       coinbaseAbKey
+       recoveryKey
+)
+
+// leveldb key prefix
+var (
+       colon               byte = 0x3a
+       UTXOPrefix               = []byte{utxoPrefix, colon}
+       SUTXOPrefix              = []byte{sutxoPrefix, colon}
+       ContractPrefix           = []byte{contractPrefix, contractPrefix, colon}
+       ContractIndexPrefix      = []byte{contractIndexPrefix, colon}
+       AccountPrefix            = []byte{accountPrefix, colon} // AccountPrefix is account ID prefix
+       AccountAliasPrefix       = []byte{accountAliasPrefix, colon}
+       AccountIndexPrefix       = []byte{accountIndexPrefix, colon}
+       TxPrefix                 = []byte{txPrefix, colon}            //TxPrefix is wallet database transactions prefix
+       TxIndexPrefix            = []byte{txIndexPrefix, colon}       //TxIndexPrefix is wallet database tx index prefix
+       UnconfirmedTxPrefix      = []byte{unconfirmedTxPrefix, colon} //UnconfirmedTxPrefix is txpool unconfirmed transactions prefix
+       GlobalTxIndexPrefix      = []byte{globalTxIndexPrefix, colon} //GlobalTxIndexPrefix is wallet database global tx index prefix
+       WalletKey                = []byte{walletKey}
+       MiningAddressKey         = []byte{miningAddressKey}
+       CoinbaseAbKey            = []byte{coinbaseAbKey}
+       RecoveryKey              = []byte{recoveryKey}
+)
+
+const (
        LevelDBBackendStr   = "leveldb" // legacy, defaults to goleveldb.
        CLevelDBBackendStr  = "cleveldb"
        GoLevelDBBackendStr = "goleveldb"
index a8171d7..5f8971f 100644 (file)
@@ -17,42 +17,42 @@ import (
        "github.com/vapor/protocol/bc"
 )
 
-const (
-       utxoPrefix  byte = iota //UTXOPrefix is StandardUTXOKey prefix
-       sutxoPrefix             //SUTXOPrefix is ContractUTXOKey prefix
-       contractPrefix
-       contractIndexPrefix
-       accountPrefix // AccountPrefix is account ID prefix
-       accountAliasPrefix
-       accountIndexPrefix
-       txPrefix            //TxPrefix is wallet database transactions prefix
-       txIndexPrefix       //TxIndexPrefix is wallet database tx index prefix
-       unconfirmedTxPrefix //UnconfirmedTxPrefix is txpool unconfirmed transactions prefix
-       globalTxIndexPrefix //GlobalTxIndexPrefix is wallet database global tx index prefix
-       walletKey
-       miningAddressKey
-       coinbaseAbKey
-       recoveryKey
-)
-
-// leveldb key prefix
-var (
-       UTXOPrefix          = []byte{utxoPrefix, colon}
-       SUTXOPrefix         = []byte{sutxoPrefix, colon}
-       ContractPrefix      = []byte{contractPrefix, contractPrefix, colon}
-       ContractIndexPrefix = []byte{contractIndexPrefix, colon}
-       AccountPrefix       = []byte{accountPrefix, colon} // AccountPrefix is account ID prefix
-       AccountAliasPrefix  = []byte{accountAliasPrefix, colon}
-       AccountIndexPrefix  = []byte{accountIndexPrefix, colon}
-       TxPrefix            = []byte{txPrefix, colon}            //TxPrefix is wallet database transactions prefix
-       TxIndexPrefix       = []byte{txIndexPrefix, colon}       //TxIndexPrefix is wallet database tx index prefix
-       UnconfirmedTxPrefix = []byte{unconfirmedTxPrefix, colon} //UnconfirmedTxPrefix is txpool unconfirmed transactions prefix
-       GlobalTxIndexPrefix = []byte{globalTxIndexPrefix, colon} //GlobalTxIndexPrefix is wallet database global tx index prefix
-       WalletKey           = []byte{walletKey}
-       MiningAddressKey    = []byte{miningAddressKey}
-       CoinbaseAbKey       = []byte{coinbaseAbKey}
-       RecoveryKey         = []byte{recoveryKey}
-)
+// const (
+//     utxoPrefix  byte = iota //UTXOPrefix is StandardUTXOKey prefix
+//     sutxoPrefix             //SUTXOPrefix is ContractUTXOKey prefix
+//     contractPrefix
+//     contractIndexPrefix
+//     accountPrefix // AccountPrefix is account ID prefix
+//     accountAliasPrefix
+//     accountIndexPrefix
+//     txPrefix            //TxPrefix is wallet database transactions prefix
+//     txIndexPrefix       //TxIndexPrefix is wallet database tx index prefix
+//     unconfirmedTxPrefix //UnconfirmedTxPrefix is txpool unconfirmed transactions prefix
+//     globalTxIndexPrefix //GlobalTxIndexPrefix is wallet database global tx index prefix
+//     walletKey
+//     miningAddressKey
+//     coinbaseAbKey
+//     recoveryKey
+// )
+
+// // leveldb key prefix
+// var (
+//     UTXOPrefix          = []byte{utxoPrefix, colon}
+//     SUTXOPrefix         = []byte{sutxoPrefix, colon}
+//     ContractPrefix      = []byte{contractPrefix, contractPrefix, colon}
+//     ContractIndexPrefix = []byte{contractIndexPrefix, colon}
+//     AccountPrefix       = []byte{accountPrefix, colon} // AccountPrefix is account ID prefix
+//     AccountAliasPrefix  = []byte{accountAliasPrefix, colon}
+//     AccountIndexPrefix  = []byte{accountIndexPrefix, colon}
+//     TxPrefix            = []byte{txPrefix, colon}            //TxPrefix is wallet database transactions prefix
+//     TxIndexPrefix       = []byte{txIndexPrefix, colon}       //TxIndexPrefix is wallet database tx index prefix
+//     UnconfirmedTxPrefix = []byte{unconfirmedTxPrefix, colon} //UnconfirmedTxPrefix is txpool unconfirmed transactions prefix
+//     GlobalTxIndexPrefix = []byte{globalTxIndexPrefix, colon} //GlobalTxIndexPrefix is wallet database global tx index prefix
+//     WalletKey           = []byte{walletKey}
+//     MiningAddressKey    = []byte{miningAddressKey}
+//     CoinbaseAbKey       = []byte{coinbaseAbKey}
+//     RecoveryKey         = []byte{recoveryKey}
+// )
 
 // errors
 var (
@@ -70,11 +70,11 @@ func accountIndexKey(xpubs []chainkd.XPub) []byte {
                xPubs = append(xPubs, xpub[:]...)
        }
        sha3pool.Sum256(hash[:], xPubs)
-       return append([]byte(AccountIndexPrefix), hash[:]...)
+       return append([]byte(dbm.AccountIndexPrefix), hash[:]...)
 }
 
 func Bip44ContractIndexKey(accountID string, change bool) []byte {
-       key := append([]byte(ContractIndexPrefix), accountID...)
+       key := append([]byte(dbm.ContractIndexPrefix), accountID...)
        if change {
                return append(key, []byte{1}...)
        }
@@ -83,42 +83,42 @@ func Bip44ContractIndexKey(accountID string, change bool) []byte {
 
 // ContractKey account control promgram store prefix
 func ContractKey(hash bc.Hash) []byte {
-       return append([]byte(ContractPrefix), hash.Bytes()...)
+       return append([]byte(dbm.ContractPrefix), hash.Bytes()...)
 }
 
 // AccountIDKey account id store prefix
 func AccountIDKey(accountID string) []byte {
-       return append([]byte(AccountPrefix), []byte(accountID)...)
+       return append([]byte(dbm.AccountPrefix), []byte(accountID)...)
 }
 
 // StandardUTXOKey makes an account unspent outputs key to store
 func StandardUTXOKey(id bc.Hash) []byte {
-       return append(UTXOPrefix, id.Bytes()...)
+       return append(dbm.UTXOPrefix, id.Bytes()...)
 }
 
 // ContractUTXOKey makes a smart contract unspent outputs key to store
 func ContractUTXOKey(id bc.Hash) []byte {
-       return append(SUTXOPrefix, id.Bytes()...)
+       return append(dbm.SUTXOPrefix, id.Bytes()...)
 }
 
 func calcDeleteKey(blockHeight uint64) []byte {
-       return []byte(fmt.Sprintf("%s%016x", TxPrefix, blockHeight))
+       return []byte(fmt.Sprintf("%s%016x", dbm.TxPrefix, blockHeight))
 }
 
 func calcTxIndexKey(txID string) []byte {
-       return append(TxIndexPrefix, []byte(txID)...)
+       return append(dbm.TxIndexPrefix, []byte(txID)...)
 }
 
 func calcAnnotatedKey(formatKey string) []byte {
-       return append(TxPrefix, []byte(formatKey)...)
+       return append(dbm.TxPrefix, []byte(formatKey)...)
 }
 
 func calcUnconfirmedTxKey(formatKey string) []byte {
-       return append(UnconfirmedTxPrefix, []byte(formatKey)...)
+       return append(dbm.UnconfirmedTxPrefix, []byte(formatKey)...)
 }
 
 func calcGlobalTxIndexKey(txID string) []byte {
-       return append(GlobalTxIndexPrefix, []byte(txID)...)
+       return append(dbm.GlobalTxIndexPrefix, []byte(txID)...)
 }
 
 func CalcGlobalTxIndex(blockHash *bc.Hash, position uint64) []byte {
@@ -133,11 +133,11 @@ func formatKey(blockHeight uint64, position uint32) string {
 }
 
 func contractIndexKey(accountID string) []byte {
-       return append([]byte(ContractIndexPrefix), []byte(accountID)...)
+       return append([]byte(dbm.ContractIndexPrefix), []byte(accountID)...)
 }
 
 func accountAliasKey(name string) []byte {
-       return append([]byte(AccountAliasPrefix), []byte(name)...)
+       return append([]byte(dbm.AccountAliasPrefix), []byte(name)...)
 }
 
 // WalletStore store wallet using leveldb
@@ -185,9 +185,9 @@ func (store *WalletStore) DeleteContractUTXO(outputID bc.Hash) {
 // DeleteRecoveryStatus delete recovery status
 func (store *WalletStore) DeleteRecoveryStatus() {
        if store.batch == nil {
-               store.walletDB.Delete(RecoveryKey)
+               store.walletDB.Delete(dbm.RecoveryKey)
        } else {
-               store.batch.Delete(RecoveryKey)
+               store.batch.Delete(dbm.RecoveryKey)
        }
 }
 
@@ -227,14 +227,14 @@ func (store *WalletStore) DeleteWalletTransactions() {
        if store.batch != nil {
                batch = store.batch
        }
-       txIter := store.walletDB.IteratorPrefix([]byte(TxPrefix))
+       txIter := store.walletDB.IteratorPrefix([]byte(dbm.TxPrefix))
        defer txIter.Release()
 
        for txIter.Next() {
                batch.Delete(txIter.Key())
        }
 
-       txIndexIter := store.walletDB.IteratorPrefix([]byte(TxIndexPrefix))
+       txIndexIter := store.walletDB.IteratorPrefix([]byte(dbm.TxIndexPrefix))
        defer txIndexIter.Release()
 
        for txIndexIter.Next() {
@@ -251,13 +251,13 @@ func (store *WalletStore) DeleteWalletUTXOs() {
        if store.batch != nil {
                batch = store.batch
        }
-       ruIter := store.walletDB.IteratorPrefix([]byte(UTXOPrefix))
+       ruIter := store.walletDB.IteratorPrefix([]byte(dbm.UTXOPrefix))
        defer ruIter.Release()
        for ruIter.Next() {
                batch.Delete(ruIter.Key())
        }
 
-       suIter := store.walletDB.IteratorPrefix([]byte(SUTXOPrefix))
+       suIter := store.walletDB.IteratorPrefix([]byte(dbm.SUTXOPrefix))
        defer suIter.Release()
        for suIter.Next() {
                batch.Delete(suIter.Key())
@@ -352,7 +352,7 @@ func (store *WalletStore) GetRecoveryStatus(recoveryKey []byte) []byte {
 
 // GetWalletInfo get wallet information
 func (store *WalletStore) GetWalletInfo() []byte {
-       return store.walletDB.Get([]byte(WalletKey))
+       return store.walletDB.Get([]byte(dbm.WalletKey))
 }
 
 // ListAccountUTXOs get all account unspent outputs
@@ -374,7 +374,7 @@ func (store *WalletStore) ListAccountUTXOs(key string) ([]*acc.UTXO, error) {
 func (store *WalletStore) ListTransactions(accountID string, StartTxID string, count uint, unconfirmed bool) ([]*query.AnnotatedTx, error) {
        annotatedTxs := []*query.AnnotatedTx{}
        var startKey []byte
-       preFix := TxPrefix
+       preFix := dbm.TxPrefix
 
        if StartTxID != "" {
                if unconfirmed {
@@ -389,7 +389,7 @@ func (store *WalletStore) ListTransactions(accountID string, StartTxID string, c
        }
 
        if unconfirmed {
-               preFix = UnconfirmedTxPrefix
+               preFix = dbm.UnconfirmedTxPrefix
        }
 
        itr := store.walletDB.IteratorPrefixWithStart([]byte(preFix), startKey, true)
@@ -409,7 +409,7 @@ func (store *WalletStore) ListTransactions(accountID string, StartTxID string, c
 // ListUnconfirmedTransactions get all unconfirmed txs
 func (store *WalletStore) ListUnconfirmedTransactions() ([]*query.AnnotatedTx, error) {
        annotatedTxs := []*query.AnnotatedTx{}
-       txIter := store.walletDB.IteratorPrefix([]byte(UnconfirmedTxPrefix))
+       txIter := store.walletDB.IteratorPrefix([]byte(dbm.UnconfirmedTxPrefix))
        defer txIter.Release()
 
        for txIter.Next() {
@@ -500,8 +500,8 @@ func (store *WalletStore) SetUnconfirmedTransaction(txID string, tx *query.Annot
 // SetWalletInfo get wallet information
 func (store *WalletStore) SetWalletInfo(rawWallet []byte) {
        if store.batch == nil {
-               store.walletDB.Set([]byte(WalletKey), rawWallet)
+               store.walletDB.Set([]byte(dbm.WalletKey), rawWallet)
        } else {
-               store.batch.Set([]byte(WalletKey), rawWallet)
+               store.batch.Set([]byte(dbm.WalletKey), rawWallet)
        }
 }
index 2209e64..8a6cbcb 100644 (file)
@@ -9,20 +9,16 @@ import (
        "github.com/vapor/consensus"
        "github.com/vapor/consensus/segwit"
        "github.com/vapor/crypto/sha3pool"
+       dbm "github.com/vapor/database/leveldb"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
 )
 
-var (
-       UTXOPrefix  = []byte{0x00, 0x3a}
-       SUTXOPrefix = []byte{0x01, 0x3a}
-)
-
 // GetAccountUtxos return all account unspent outputs
 func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSmartContract bool, vote bool) []*account.UTXO {
-       prefix := UTXOPrefix
+       prefix := dbm.UTXOPrefix
        if isSmartContract {
-               prefix = SUTXOPrefix
+               prefix = dbm.SUTXOPrefix
        }
 
        accountUtxos := []*account.UTXO{}
index 08d4b2c..790e0b9 100644 (file)
@@ -288,11 +288,6 @@ func TestMemPoolTxQueryLoop(t *testing.T) {
        if err != nil {
                t.Fatal(err)
        }
-       // c := &protocol.Chain{
-       //      store: &mStore{
-       //              blockHeaders: make(map[bc.Hash]*types.BlockHeader),
-       //      },
-       // }
 
        accountStore := mock.NewMockAccountStore(testDB)
        accountManager := account.NewManager(accountStore, chain)