OSDN Git Service

sort account store functions
authorChengcheng Zhang <943420582@qq.com>
Sat, 29 Jun 2019 06:50:14 +0000 (14:50 +0800)
committerChengcheng Zhang <943420582@qq.com>
Sat, 29 Jun 2019 06:50:14 +0000 (14:50 +0800)
database/account_store.go

index c43c94a..d04a36f 100644 (file)
@@ -46,15 +46,6 @@ func (store *AccountStore) CommitBatch() error {
        return nil
 }
 
-// DeleteAccountByAlias delete account by account alias
-func (store *AccountStore) DeleteAccountByAlias(accountAlias string) {
-       if store.batch == nil {
-               store.accountDB.Delete(accountAliasKey(accountAlias))
-       } else {
-               store.batch.Delete(accountAliasKey(accountAlias))
-       }
-}
-
 // DeleteAccount set account account ID, account alias and raw account.
 func (store *AccountStore) DeleteAccount(account *acc.Account) {
        batch := store.accountDB.NewBatch()
@@ -68,86 +59,41 @@ func (store *AccountStore) DeleteAccount(account *acc.Account) {
        }
 }
 
-// DeleteControlProgram delete raw control program by hash
-func (store *AccountStore) DeleteControlProgram(hash common.Hash) {
+// DeleteAccountByAlias delete account by account alias
+func (store *AccountStore) DeleteAccountByAlias(accountAlias string) {
        if store.batch == nil {
-               store.accountDB.Delete(ContractKey(hash))
+               store.accountDB.Delete(accountAliasKey(accountAlias))
        } else {
-               store.batch.Delete(ContractKey(hash))
+               store.batch.Delete(accountAliasKey(accountAlias))
        }
 }
 
-// SetAccount set account account ID, account alias and raw account.
-func (store *AccountStore) SetAccount(account *acc.Account) error {
-       rawAccount, err := json.Marshal(account)
-       if err != nil {
-               return acc.ErrMarshalAccount
-       }
-
+// DeleteAccountUTXOs delete account utxos by accountID
+func (store *AccountStore) DeleteAccountUTXOs(accountID string) error {
        batch := store.accountDB.NewBatch()
        if store.batch != nil {
                batch = store.batch
        }
 
-       batch.Set(AccountIDKey(account.ID), rawAccount)
-       batch.Set(accountAliasKey(account.Alias), []byte(account.ID))
-
-       if store.batch == nil {
-               batch.Write()
-       }
-       return nil
-}
-
-// SetAccountIndex set account account ID, account alias and raw account.
-func (store *AccountStore) SetAccountIndex(account *acc.Account) error {
-       rawAccount, err := json.Marshal(account)
-       if err != nil {
-               return acc.ErrMarshalAccount
-       }
+       accountUtxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
+       defer accountUtxoIter.Release()
 
-       batch := store.accountDB.NewBatch()
-       if store.batch != nil {
-               batch = store.batch
+       for accountUtxoIter.Next() {
+               accountUtxo := &acc.UTXO{}
+               if err := json.Unmarshal(accountUtxoIter.Value(), accountUtxo); err != nil {
+                       return err
+               }
+               if accountID == accountUtxo.AccountID {
+                       batch.Delete(StandardUTXOKey(accountUtxo.OutputID))
+               }
        }
 
-       batch.Set(AccountIDKey(account.ID), rawAccount)
-       batch.Set(accountAliasKey(account.Alias), []byte(account.ID))
-       batch.Set(accountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
-
        if store.batch == nil {
                batch.Write()
        }
        return nil
 }
 
-// GetAccountIDByAlias get account ID by account alias
-func (store *AccountStore) GetAccountIDByAlias(accountAlias string) string {
-       accountID := store.accountDB.Get(accountAliasKey(accountAlias))
-       return string(accountID)
-}
-
-// GetAccountByID get account by accountID
-func (store *AccountStore) GetAccountByID(accountID string) (*acc.Account, error) {
-       rawAccount := store.accountDB.Get(AccountIDKey(accountID))
-       if rawAccount == nil {
-               return nil, acc.ErrFindAccount
-       }
-       account := new(acc.Account)
-       if err := json.Unmarshal(rawAccount, account); err != nil {
-               return nil, err
-       }
-       return account, nil
-}
-
-// GetAccountIndex get account index by account xpubs
-func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) uint64 {
-       currentIndex := uint64(0)
-       if rawIndexBytes := store.accountDB.Get(accountIndexKey(xpubs)); rawIndexBytes != nil {
-               currentIndex = common.BytesToUnit64(rawIndexBytes)
-       }
-       return currentIndex
-}
-
 // DeleteBip44ContractIndex delete bip44 contract index by accountID
 func (store *AccountStore) DeleteBip44ContractIndex(accountID string) {
        batch := store.accountDB.NewBatch()
@@ -170,13 +116,13 @@ func (store *AccountStore) DeleteContractIndex(accountID string) {
        }
 }
 
-// GetContractIndex get contract index
-func (store *AccountStore) GetContractIndex(accountID string) uint64 {
-       index := uint64(0)
-       if rawIndexBytes := store.accountDB.Get(contractIndexKey(accountID)); rawIndexBytes != nil {
-               index = common.BytesToUnit64(rawIndexBytes)
+// DeleteControlProgram delete raw control program by hash
+func (store *AccountStore) DeleteControlProgram(hash common.Hash) {
+       if store.batch == nil {
+               store.accountDB.Delete(ContractKey(hash))
+       } else {
+               store.batch.Delete(ContractKey(hash))
        }
-       return index
 }
 
 // DeleteStandardUTXO delete utxo by outpu id
@@ -188,78 +134,52 @@ func (store *AccountStore) DeleteStandardUTXO(outputID bc.Hash) {
        }
 }
 
-// DeleteAccountUTXOs delete account utxos by accountID
-func (store *AccountStore) DeleteAccountUTXOs(accountID string) error {
-       batch := store.accountDB.NewBatch()
-       if store.batch != nil {
-               batch = store.batch
-       }
-
-       accountUtxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
-       defer accountUtxoIter.Release()
-
-       for accountUtxoIter.Next() {
-               accountUtxo := &acc.UTXO{}
-               if err := json.Unmarshal(accountUtxoIter.Value(), accountUtxo); err != nil {
-                       return err
-               }
-               if accountID == accountUtxo.AccountID {
-                       batch.Delete(StandardUTXOKey(accountUtxo.OutputID))
-               }
+// GetAccountByID get account by accountID
+func (store *AccountStore) GetAccountByID(accountID string) (*acc.Account, error) {
+       rawAccount := store.accountDB.Get(AccountIDKey(accountID))
+       if rawAccount == nil {
+               return nil, acc.ErrFindAccount
        }
-
-       if store.batch == nil {
-               batch.Write()
+       account := new(acc.Account)
+       if err := json.Unmarshal(rawAccount, account); err != nil {
+               return nil, err
        }
-       return nil
+       return account, nil
 }
 
-// GetCoinbaseArbitrary get coinbase arbitrary
-func (store *AccountStore) GetCoinbaseArbitrary() []byte {
-       return store.accountDB.Get([]byte(CoinbaseAbKey))
+// GetAccountIDByAlias get account ID by account alias
+func (store *AccountStore) GetAccountIDByAlias(accountAlias string) string {
+       accountID := store.accountDB.Get(accountAliasKey(accountAlias))
+       return string(accountID)
 }
 
-// SetCoinbaseArbitrary set coinbase arbitrary
-func (store *AccountStore) SetCoinbaseArbitrary(arbitrary []byte) {
-       if store.batch == nil {
-               store.accountDB.Set([]byte(CoinbaseAbKey), arbitrary)
-       } else {
-               store.batch.Set([]byte(CoinbaseAbKey), arbitrary)
+// GetAccountIndex get account index by account xpubs
+func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) uint64 {
+       currentIndex := uint64(0)
+       if rawIndexBytes := store.accountDB.Get(accountIndexKey(xpubs)); rawIndexBytes != nil {
+               currentIndex = common.BytesToUnit64(rawIndexBytes)
        }
+       return currentIndex
 }
 
-// GetMiningAddress get mining address
-func (store *AccountStore) GetMiningAddress() (*acc.CtrlProgram, error) {
-       rawCP := store.accountDB.Get([]byte(MiningAddressKey))
-       if rawCP == nil {
-               return nil, acc.ErrFindMiningAddress
-       }
-       cp := new(acc.CtrlProgram)
-       if err := json.Unmarshal(rawCP, cp); err != nil {
-               return nil, err
+// GetBip44ContractIndex get bip44 contract index
+func (store *AccountStore) GetBip44ContractIndex(accountID string, change bool) uint64 {
+       index := uint64(0)
+       if rawIndexBytes := store.accountDB.Get(Bip44ContractIndexKey(accountID, change)); rawIndexBytes != nil {
+               index = common.BytesToUnit64(rawIndexBytes)
        }
-       return cp, nil
+       return index
 }
 
-// SetMiningAddress set mining address
-func (store *AccountStore) SetMiningAddress(program *acc.CtrlProgram) error {
-       rawProgram, err := json.Marshal(program)
-       if err != nil {
-               return err
-       }
-
-       if store.batch == nil {
-               store.accountDB.Set([]byte(MiningAddressKey), rawProgram)
-       } else {
-               store.batch.Set([]byte(MiningAddressKey), rawProgram)
-       }
-       return nil
+// GetCoinbaseArbitrary get coinbase arbitrary
+func (store *AccountStore) GetCoinbaseArbitrary() []byte {
+       return store.accountDB.Get([]byte(CoinbaseAbKey))
 }
 
-// GetBip44ContractIndex get bip44 contract index
-func (store *AccountStore) GetBip44ContractIndex(accountID string, change bool) uint64 {
+// GetContractIndex get contract index
+func (store *AccountStore) GetContractIndex(accountID string) uint64 {
        index := uint64(0)
-       if rawIndexBytes := store.accountDB.Get(Bip44ContractIndexKey(accountID, change)); rawIndexBytes != nil {
+       if rawIndexBytes := store.accountDB.Get(contractIndexKey(accountID)); rawIndexBytes != nil {
                index = common.BytesToUnit64(rawIndexBytes)
        }
        return index
@@ -278,6 +198,31 @@ func (store *AccountStore) GetControlProgram(hash common.Hash) (*acc.CtrlProgram
        return cp, nil
 }
 
+// GetMiningAddress get mining address
+func (store *AccountStore) GetMiningAddress() (*acc.CtrlProgram, error) {
+       rawCP := store.accountDB.Get([]byte(MiningAddressKey))
+       if rawCP == nil {
+               return nil, acc.ErrFindMiningAddress
+       }
+       cp := new(acc.CtrlProgram)
+       if err := json.Unmarshal(rawCP, cp); err != nil {
+               return nil, err
+       }
+       return cp, nil
+}
+
+// GetUTXO get standard utxo by id
+func (store *AccountStore) GetUTXO(outid bc.Hash) (*acc.UTXO, error) {
+       u := new(acc.UTXO)
+       if data := store.accountDB.Get(StandardUTXOKey(outid)); data != nil {
+               return u, json.Unmarshal(data, u)
+       }
+       if data := store.accountDB.Get(ContractUTXOKey(outid)); data != nil {
+               return u, json.Unmarshal(data, u)
+       }
+       return nil, acc.ErrMatchUTXO
+}
+
 // ListAccounts get all accounts which name prfix is id.
 func (store *AccountStore) ListAccounts(id string) ([]*acc.Account, error) {
        accounts := []*acc.Account{}
@@ -310,27 +255,64 @@ func (store *AccountStore) ListControlPrograms() ([]*acc.CtrlProgram, error) {
        return cps, nil
 }
 
-// SetControlProgram set raw program
-func (store *AccountStore) SetControlProgram(hash common.Hash, program *acc.CtrlProgram) error {
-       accountCP, err := json.Marshal(program)
+// ListUTXOs get utxos by accountID
+func (store *AccountStore) ListUTXOs() []*acc.UTXO {
+       utxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
+       defer utxoIter.Release()
+
+       utxos := []*acc.UTXO{}
+       for utxoIter.Next() {
+               utxo := new(acc.UTXO)
+               if err := json.Unmarshal(utxoIter.Value(), utxo); err != nil {
+                       log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo")
+                       continue
+               }
+               utxos = append(utxos, utxo)
+       }
+       return utxos
+}
+
+// SetAccount set account account ID, account alias and raw account.
+func (store *AccountStore) SetAccount(account *acc.Account) error {
+       rawAccount, err := json.Marshal(account)
        if err != nil {
-               return err
+               return acc.ErrMarshalAccount
+       }
+
+       batch := store.accountDB.NewBatch()
+       if store.batch != nil {
+               batch = store.batch
        }
+
+       batch.Set(AccountIDKey(account.ID), rawAccount)
+       batch.Set(accountAliasKey(account.Alias), []byte(account.ID))
+
        if store.batch == nil {
-               store.accountDB.Set(ContractKey(hash), accountCP)
-       } else {
-               store.batch.Set(ContractKey(hash), accountCP)
+               batch.Write()
        }
        return nil
 }
 
-// SetContractIndex set contract index
-func (store *AccountStore) SetContractIndex(accountID string, index uint64) {
+// SetAccountIndex set account account ID, account alias and raw account.
+func (store *AccountStore) SetAccountIndex(account *acc.Account) error {
+       rawAccount, err := json.Marshal(account)
+       if err != nil {
+               return acc.ErrMarshalAccount
+       }
+
+       batch := store.accountDB.NewBatch()
+       if store.batch != nil {
+               batch = store.batch
+       }
+
+       batch.Set(AccountIDKey(account.ID), rawAccount)
+       batch.Set(accountAliasKey(account.Alias), []byte(account.ID))
+       batch.Set(accountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
+
        if store.batch == nil {
-               store.accountDB.Set(contractIndexKey(accountID), common.Unit64ToBytes(index))
-       } else {
-               store.batch.Set(contractIndexKey(accountID), common.Unit64ToBytes(index))
+               batch.Write()
        }
+       return nil
 }
 
 // SetBip44ContractIndex set contract index
@@ -342,33 +324,51 @@ func (store *AccountStore) SetBip44ContractIndex(accountID string, change bool,
        }
 }
 
-// ListUTXOs get utxos by accountID
-func (store *AccountStore) ListUTXOs() []*acc.UTXO {
-       utxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
-       defer utxoIter.Release()
+// SetCoinbaseArbitrary set coinbase arbitrary
+func (store *AccountStore) SetCoinbaseArbitrary(arbitrary []byte) {
+       if store.batch == nil {
+               store.accountDB.Set([]byte(CoinbaseAbKey), arbitrary)
+       } else {
+               store.batch.Set([]byte(CoinbaseAbKey), arbitrary)
+       }
+}
 
-       utxos := []*acc.UTXO{}
-       for utxoIter.Next() {
-               utxo := new(acc.UTXO)
-               if err := json.Unmarshal(utxoIter.Value(), utxo); err != nil {
-                       log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo")
-                       continue
-               }
-               utxos = append(utxos, utxo)
+// SetContractIndex set contract index
+func (store *AccountStore) SetContractIndex(accountID string, index uint64) {
+       if store.batch == nil {
+               store.accountDB.Set(contractIndexKey(accountID), common.Unit64ToBytes(index))
+       } else {
+               store.batch.Set(contractIndexKey(accountID), common.Unit64ToBytes(index))
        }
-       return utxos
 }
 
-// GetUTXO get standard utxo by id
-func (store *AccountStore) GetUTXO(outid bc.Hash) (*acc.UTXO, error) {
-       u := new(acc.UTXO)
-       if data := store.accountDB.Get(StandardUTXOKey(outid)); data != nil {
-               return u, json.Unmarshal(data, u)
+// SetControlProgram set raw program
+func (store *AccountStore) SetControlProgram(hash common.Hash, program *acc.CtrlProgram) error {
+       accountCP, err := json.Marshal(program)
+       if err != nil {
+               return err
        }
-       if data := store.accountDB.Get(ContractUTXOKey(outid)); data != nil {
-               return u, json.Unmarshal(data, u)
+       if store.batch == nil {
+               store.accountDB.Set(ContractKey(hash), accountCP)
+       } else {
+               store.batch.Set(ContractKey(hash), accountCP)
        }
-       return nil, acc.ErrMatchUTXO
+       return nil
+}
+
+// SetMiningAddress set mining address
+func (store *AccountStore) SetMiningAddress(program *acc.CtrlProgram) error {
+       rawProgram, err := json.Marshal(program)
+       if err != nil {
+               return err
+       }
+
+       if store.batch == nil {
+               store.accountDB.Set([]byte(MiningAddressKey), rawProgram)
+       } else {
+               store.batch.Set([]byte(MiningAddressKey), rawProgram)
+       }
+       return nil
 }
 
 // SetStandardUTXO set standard utxo