OSDN Git Service

update SetAccountIndex
authorChengcheng Zhang <943420582@qq.com>
Tue, 2 Jul 2019 06:37:05 +0000 (14:37 +0800)
committerChengcheng Zhang <943420582@qq.com>
Tue, 2 Jul 2019 06:37:05 +0000 (14:37 +0800)
account/accounts.go
account/store.go
account/utxo_keeper_test.go
database/account_store.go

index 203c9a4..4af6fd5 100644 (file)
@@ -119,13 +119,10 @@ func (m *Manager) saveAccount(account *Account, updateIndex bool) error {
        }
 
        if updateIndex {
-               if err := m.store.SetAccountIndex(account); err != nil {
-                       return err
-               }
-       } else {
-               if err := m.store.SetAccount(account); err != nil {
-                       return err
-               }
+               m.store.SetAccountIndex(account)
+       }
+       if err := m.store.SetAccount(account); err != nil {
+               return err
        }
 
        if err := m.store.CommitBatch(); err != nil {
index 9ce4565..f724fe0 100644 (file)
@@ -24,7 +24,7 @@ type AccountStore interface {
        ListControlPrograms() ([]*CtrlProgram, error)
        ListUTXOs() []*UTXO
        SetAccount(*Account) error
-       SetAccountIndex(*Account) error
+       SetAccountIndex(*Account)
        SetBip44ContractIndex(string, bool, uint64)
        SetCoinbaseArbitrary([]byte)
        SetContractIndex(string, uint64)
index 91acee1..850f1ce 100644 (file)
@@ -1356,7 +1356,7 @@ func (store *mockAccountStore) GetMiningAddress() (*CtrlProgram, error)
 func (store *mockAccountStore) ListAccounts(string) ([]*Account, error)         { return nil, nil }
 func (store *mockAccountStore) ListControlPrograms() ([]*CtrlProgram, error)    { return nil, nil }
 func (store *mockAccountStore) SetAccount(*Account) error                       { return nil }
-func (store *mockAccountStore) SetAccountIndex(*Account) error                  { return nil }
+func (store *mockAccountStore) SetAccountIndex(*Account)                        { return }
 func (store *mockAccountStore) SetBip44ContractIndex(string, bool, uint64)      { return }
 func (store *mockAccountStore) SetCoinbaseArbitrary([]byte)                     { return }
 func (store *mockAccountStore) SetContractIndex(string, uint64)                 { return }
index b0e6295..573a282 100644 (file)
@@ -288,25 +288,12 @@ func (store *AccountStore) SetAccount(account *acc.Account) error {
 }
 
 // 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))
-
+func (store *AccountStore) SetAccountIndex(account *acc.Account) {
        if store.batch == nil {
-               batch.Write()
+               store.accountDB.Set(accountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
+       } else {
+               store.batch.Set(accountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
        }
-       return nil
 }
 
 // SetBip44ContractIndex set contract index