From: Chengcheng Zhang <943420582@qq.com> Date: Thu, 27 Jun 2019 14:19:35 +0000 (+0800) Subject: add SetAccount X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=commitdiff_plain;h=a9a3f2dcd1b8e331194bc562aba613a3d10bcb50 add SetAccount --- diff --git a/account/accounts.go b/account/accounts.go index 86086a90..488cf120 100644 --- a/account/accounts.go +++ b/account/accounts.go @@ -113,17 +113,11 @@ func CreateAccount(xpubs []chainkd.XPub, quorum int, alias string, acctIndex uin } func (m *Manager) saveAccount(account *Account, updateIndex bool) error { - rawAccount, err := json.Marshal(account) - if err != nil { - return ErrMarshalAccount - } - m.store.InitBatch() defer m.store.CommitBatch() - m.store.SetAccount(account.ID, account.Alias, rawAccount) - if updateIndex { - m.store.SetAccountIndex(account.XPubs, account.KeyIndex) + if err := m.store.SetAccount(account, updateIndex); err != nil { + return err } return nil @@ -179,7 +173,7 @@ func (m *Manager) Create(xpubs []chainkd.XPub, quorum int, alias string, deriveR return account, nil } -func (m *Manager) UpdateAccountAlias(accountID string, newAlias string) (err error) { +func (m *Manager) UpdateAccountAlias(accountID string, newAlias string) error { m.accountMu.Lock() defer m.accountMu.Unlock() @@ -199,16 +193,14 @@ func (m *Manager) UpdateAccountAlias(accountID string, newAlias string) (err err m.cacheMu.Unlock() account.Alias = normalizedAlias - rawAccount, err := json.Marshal(account) - if err != nil { - return ErrMarshalAccount - } m.store.InitBatch() defer m.store.CommitBatch() m.store.DeleteAccountByAccountAlias(oldAlias) - m.store.SetAccount(accountID, normalizedAlias, rawAccount) + if err := m.store.SetAccount(account, false); err != nil { + return err + } return nil } diff --git a/account/image.go b/account/image.go index f3fd4d6a..ed8d9265 100644 --- a/account/image.go +++ b/account/image.go @@ -64,12 +64,9 @@ func (m *Manager) Restore(image *Image) error { return ErrDuplicateAlias } - rawAccount, err := json.Marshal(slice.Account) - if err != nil { - return ErrMarshalAccount + if err := m.store.SetAccount(slice.Account, false); err != nil { + return err } - - m.store.SetAccount(slice.Account.ID, slice.Account.Alias, rawAccount) } return nil diff --git a/account/store.go b/account/store.go index 8a6537f1..60d164c2 100644 --- a/account/store.go +++ b/account/store.go @@ -10,8 +10,7 @@ import ( type AccountStorer interface { InitBatch() CommitBatch() - SetAccount(string, string, []byte) - SetAccountIndex([]chainkd.XPub, uint64) + SetAccount(account *Account, updateIndex bool) error GetAccountByAccountAlias(string) []byte GetAccountByAccountID(string) []byte GetAccountIndex([]chainkd.XPub) []byte diff --git a/database/account_store.go b/database/account_store.go index 3b5e281d..e7f502bb 100644 --- a/database/account_store.go +++ b/database/account_store.go @@ -1,8 +1,10 @@ package database import ( + "encoding/json" "strings" + acc "github.com/vapor/account" "github.com/vapor/common" "github.com/vapor/crypto/ed25519/chainkd" dbm "github.com/vapor/database/leveldb" @@ -39,16 +41,26 @@ func (store *AccountStore) CommitBatch() { } // SetAccount set account account ID, account alias and raw account. -func (store *AccountStore) SetAccount(accountID, accountAlias string, rawAccount []byte) { +func (store *AccountStore) SetAccount(account *acc.Account, updateIndex bool) 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(accountID), rawAccount) - batch.Set(accountAliasKey(accountAlias), []byte(accountID)) + batch.Set(AccountIDKey(account.ID), rawAccount) + batch.Set(accountAliasKey(account.Alias), []byte(account.ID)) + if updateIndex { + batch.Set(accountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex)) + } + if store.batch == nil { batch.Write() } + return nil } // DeleteAccount set account account ID, account alias and raw account. @@ -64,15 +76,6 @@ func (store *AccountStore) DeleteAccount(accountID, accountAlias string) { } } -// SetAccountIndex set account index -func (store *AccountStore) SetAccountIndex(xpubs []chainkd.XPub, keyIndex uint64) { - if store.batch == nil { - store.accountDB.Set(accountIndexKey(xpubs), common.Unit64ToBytes(keyIndex)) - } else { - store.batch.Set(accountIndexKey(xpubs), common.Unit64ToBytes(keyIndex)) - } -} - // GetAccountByAccountAlias get account by account alias func (store *AccountStore) GetAccountByAccountAlias(accountAlias string) []byte { return store.accountDB.Get(accountAliasKey(accountAlias))