OSDN Git Service

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

index a267b49..43e340e 100644 (file)
@@ -141,10 +141,7 @@ func (m *Manager) SaveAccount(account *Account) error {
                return ErrDuplicateIndex
        }
 
-       currentIndex := uint64(0)
-       if rawIndexBytes := m.store.GetAccountIndex(account.XPubs); rawIndexBytes != nil {
-               currentIndex = common.BytesToUnit64(rawIndexBytes)
-       }
+       currentIndex := m.store.GetAccountIndex(account.XPubs)
        return m.saveAccount(account, account.KeyIndex > currentIndex)
 }
 
@@ -158,8 +155,8 @@ func (m *Manager) Create(xpubs []chainkd.XPub, quorum int, alias string, deriveR
        }
 
        acctIndex := uint64(1)
-       if rawIndexBytes := m.store.GetAccountIndex(xpubs); rawIndexBytes != nil {
-               acctIndex = common.BytesToUnit64(rawIndexBytes) + 1
+       if currentIndex := m.store.GetAccountIndex(xpubs); currentIndex != 0 {
+               acctIndex = currentIndex + 1
        }
        account, err := CreateAccount(xpubs, quorum, alias, acctIndex, deriveRule)
        if err != nil {
index 3c7f9dc..28e2b54 100644 (file)
@@ -13,7 +13,7 @@ type AccountStorer interface {
        SetAccount(*Account, bool) error
        GetAccountIDByAccountAlias(string) string
        GetAccountByAccountID(string) (*Account, error)
-       GetAccountIndex([]chainkd.XPub) []byte
+       GetAccountIndex([]chainkd.XPub) uint64
        DeleteAccountByAccountAlias(string)
        DeleteAccountByAccountID(string)
        DeleteRawProgram(common.Hash)
index 62f756a..9b1e78a 100644 (file)
@@ -79,7 +79,6 @@ func (store *AccountStore) DeleteAccount(accountID, accountAlias string) {
 // GetAccountIDByAccountAlias get account ID by account alias
 func (store *AccountStore) GetAccountIDByAccountAlias(accountAlias string) string {
        accountID := store.accountDB.Get(accountAliasKey(accountAlias))
-
        return string(accountID)
 }
 
@@ -97,8 +96,12 @@ func (store *AccountStore) GetAccountByAccountID(accountID string) (*acc.Account
 }
 
 // GetAccountIndex get account index by account xpubs
-func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) []byte {
-       return store.accountDB.Get(accountIndexKey(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
 }
 
 // DeleteAccountByAccountAlias delete account by account alias