From 29be1e36082ff2147259f8a58475a9f7fc94609a Mon Sep 17 00:00:00 2001 From: Chengcheng Zhang <943420582@qq.com> Date: Fri, 28 Jun 2019 14:29:10 +0800 Subject: [PATCH] update GetAccountByAccountID --- database/wallet_store.go | 12 ++++++++++-- wallet/annotated.go | 15 ++++++--------- wallet/store.go | 4 ++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/database/wallet_store.go b/database/wallet_store.go index 16f4fccc..60d3992d 100644 --- a/database/wallet_store.go +++ b/database/wallet_store.go @@ -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 diff --git a/wallet/annotated.go b/wallet/annotated.go index b146e4ab..c5d87178 100644 --- a/wallet/annotated.go +++ b/wallet/annotated.go @@ -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(`{}`) diff --git a/wallet/store.go b/wallet/store.go index e600c136..5feaa44b 100644 --- a/wallet/store.go +++ b/wallet/store.go @@ -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) -- 2.11.0