OSDN Git Service

update GetAccountByAccountID
authorChengcheng Zhang <943420582@qq.com>
Thu, 27 Jun 2019 15:06:01 +0000 (23:06 +0800)
committerChengcheng Zhang <943420582@qq.com>
Thu, 27 Jun 2019 15:06:01 +0000 (23:06 +0800)
account/accounts.go
account/image.go
account/store.go
database/account_store.go

index f173388..a267b49 100644 (file)
@@ -353,13 +353,8 @@ func (m *Manager) FindByID(id string) (*Account, error) {
                return cachedAccount.(*Account), nil
        }
 
-       rawAccount := m.store.GetAccountByAccountID(id)
-       if rawAccount == nil {
-               return nil, ErrFindAccount
-       }
-
-       account := &Account{}
-       if err := json.Unmarshal(rawAccount, account); err != nil {
+       account, err := m.store.GetAccountByAccountID(id)
+       if err != nil {
                return nil, err
        }
 
@@ -371,13 +366,12 @@ func (m *Manager) FindByID(id string) (*Account, error) {
 
 // GetAccountByProgram return Account by given CtrlProgram
 func (m *Manager) GetAccountByProgram(program *CtrlProgram) (*Account, error) {
-       rawAccount := m.store.GetAccountByAccountID(program.AccountID)
-       if rawAccount == nil {
-               return nil, ErrFindAccount
+       account, err := m.store.GetAccountByAccountID(program.AccountID)
+       if err != nil {
+               return nil, err
        }
 
-       account := &Account{}
-       return account, json.Unmarshal(rawAccount, account)
+       return account, nil
 }
 
 // GetAccountByXPubsIndex get account by xPubs and index
@@ -397,16 +391,11 @@ func (m *Manager) GetAccountByXPubsIndex(xPubs []chainkd.XPub, index uint64) (*A
 
 // GetAliasByID return the account alias by given ID
 func (m *Manager) GetAliasByID(id string) string {
-       rawAccount := m.store.GetAccountByAccountID(id)
-       if rawAccount == nil {
+       account, err := m.store.GetAccountByAccountID(id)
+       if err != nil {
                log.Warn("GetAliasByID fail to find account")
                return ""
        }
-
-       account := &Account{}
-       if err := json.Unmarshal(rawAccount, account); err != nil {
-               log.Warn(err)
-       }
        return account.Alias
 }
 
index b3094e1..4bf9bb7 100644 (file)
@@ -52,7 +52,8 @@ func (m *Manager) Restore(image *Image) error {
        defer m.store.CommitBatch()
 
        for _, slice := range image.Slice {
-               if existed := m.store.GetAccountByAccountID(slice.Account.ID); existed != nil {
+               existed, err := m.store.GetAccountByAccountID(slice.Account.ID)
+               if err != nil || existed != nil {
                        log.WithFields(log.Fields{
                                "module": logModule,
                                "alias":  slice.Account.Alias,
index 8774636..3c7f9dc 100644 (file)
@@ -12,7 +12,7 @@ type AccountStorer interface {
        CommitBatch()
        SetAccount(*Account, bool) error
        GetAccountIDByAccountAlias(string) string
-       GetAccountByAccountID(string) []byte
+       GetAccountByAccountID(string) (*Account, error)
        GetAccountIndex([]chainkd.XPub) []byte
        DeleteAccountByAccountAlias(string)
        DeleteAccountByAccountID(string)
index 0b92527..62f756a 100644 (file)
@@ -84,8 +84,16 @@ func (store *AccountStore) GetAccountIDByAccountAlias(accountAlias string) strin
 }
 
 // GetAccountByAccountID get account by accountID
-func (store *AccountStore) GetAccountByAccountID(accountID string) []byte {
-       return store.accountDB.Get(AccountIDKey(accountID))
+func (store *AccountStore) GetAccountByAccountID(accountID string) (*acc.Account, error) {
+       account := new(acc.Account)
+       rawAccount := store.accountDB.Get(AccountIDKey(accountID))
+       if rawAccount == nil {
+               return nil, acc.ErrFindAccount
+       }
+       if err := json.Unmarshal(rawAccount, account); err != nil {
+               return nil, err
+       }
+       return account, nil
 }
 
 // GetAccountIndex get account index by account xpubs