OSDN Git Service

update GetAccountByAccountID
authorChengcheng Zhang <943420582@qq.com>
Fri, 28 Jun 2019 06:29:10 +0000 (14:29 +0800)
committerChengcheng Zhang <943420582@qq.com>
Fri, 28 Jun 2019 06:29:10 +0000 (14:29 +0800)
database/wallet_store.go
wallet/annotated.go
wallet/store.go

index 16f4fcc..60d3992 100644 (file)
@@ -234,8 +234,16 @@ func (store *WalletStore) GetControlProgram(hash common.Hash) (*acc.CtrlProgram,
 }
 
 // GetAccountByAccountID get account value by account ID
-func (store *WalletStore) GetAccountByAccountID(accountID string) []byte {
-       return store.walletDB.Get(AccountIDKey(accountID))
+func (store *WalletStore) GetAccountByAccountID(accountID string) (*acc.Account, error) {
+       rawAccount := store.walletDB.Get(AccountIDKey(accountID))
+       if rawAccount == nil {
+               return nil, fmt.Errorf("failed get account, accountID: %s ", accountID)
+       }
+       account := new(acc.Account)
+       if err := json.Unmarshal(rawAccount, account); err != nil {
+               return nil, err
+       }
+       return account, nil
 }
 
 // DeleteTransactions delete transactions when orphan block rollback
index b146e4a..c5d8717 100644 (file)
@@ -98,7 +98,6 @@ func annotateTxsAccount(txs []*query.AnnotatedTx, store WalletStorer) {
 
 func getAccountFromACP(program []byte, store WalletStorer) (*account.Account, error) {
        var hash common.Hash
-       localAccount := account.Account{}
 
        sha3pool.Sum256(hash[:], program)
        accountCP, err := store.GetControlProgram(hash)
@@ -109,16 +108,14 @@ func getAccountFromACP(program []byte, store WalletStorer) (*account.Account, er
                return nil, fmt.Errorf("failed get account control program:%x ", hash)
        }
 
-       accountValue := store.GetAccountByAccountID(accountCP.AccountID)
-       if accountValue == nil {
-               return nil, fmt.Errorf("failed get account:%s ", accountCP.AccountID)
-       }
-
-       if err := json.Unmarshal(accountValue, &localAccount); err != nil {
+       account, err := store.GetAccountByAccountID(accountCP.AccountID)
+       if err != nil {
                return nil, err
        }
-
-       return &localAccount, nil
+       if account == nil {
+               return nil, fmt.Errorf("failed get account:%s ", accountCP.AccountID)
+       }
+       return account, nil
 }
 
 var emptyJSONObject = json.RawMessage(`{}`)
index e600c13..5feaa44 100644 (file)
@@ -14,8 +14,8 @@ type WalletStorer interface {
        CommitBatch()
        GetAssetDefinition(*bc.AssetID) (*asset.Asset, error)
        SetAssetDefinition(*bc.AssetID, []byte)
-       GetControlProgram(hash common.Hash) (*acc.CtrlProgram, error)
-       GetAccountByAccountID(string) []byte
+       GetControlProgram(common.Hash) (*acc.CtrlProgram, error)
+       GetAccountByAccountID(string) (*acc.Account, error)
        DeleteTransactions(uint64)
        SetTransaction(uint64, uint32, string, []byte)
        DeleteUnconfirmedTransaction(string)