}
func (m *Manager) saveAccount(account *Account) error {
- if err := m.store.InitBatch(); err != nil {
- return err
- }
+ newStore := m.store.InitStore()
// update account index
- m.store.SetAccountIndex(account)
- if err := m.store.SetAccount(account); err != nil {
+ newStore.SetAccountIndex(account)
+ if err := newStore.SetAccount(account); err != nil {
return err
}
- if err := m.store.CommitBatch(); err != nil {
+ if err := newStore.CommitBatch(); err != nil {
return err
}
account.Alias = normalizedAlias
- if err := m.store.InitBatch(); err != nil {
- return err
- }
+ newStore := m.store.InitStore()
- if err := m.store.DeleteAccount(&oldAccount); err != nil {
+ if err := newStore.DeleteAccount(&oldAccount); err != nil {
return err
}
- if err := m.store.SetAccount(account); err != nil {
+ if err := newStore.SetAccount(account); err != nil {
return err
}
- if err := m.store.CommitBatch(); err != nil {
+ if err := newStore.CommitBatch(); err != nil {
return err
}
return err
}
- if err := m.store.InitBatch(); err != nil {
- return err
- }
+ newStore := m.store.InitStore()
- if err := m.store.SetControlProgram(bc.NewHash(hash), prog); err != nil {
+ if err := newStore.SetControlProgram(bc.NewHash(hash), prog); err != nil {
return nil
}
if updateIndex {
switch acct.DeriveRule {
case signers.BIP0032:
- m.store.SetContractIndex(acct.ID, prog.KeyIndex)
+ newStore.SetContractIndex(acct.ID, prog.KeyIndex)
case signers.BIP0044:
- m.store.SetBip44ContractIndex(acct.ID, prog.Change, prog.KeyIndex)
+ newStore.SetBip44ContractIndex(acct.ID, prog.Change, prog.KeyIndex)
}
}
- return m.store.CommitBatch()
+ return newStore.CommitBatch()
}
// SaveControlPrograms save account control programs
m.accountMu.Lock()
defer m.accountMu.Unlock()
- if err := m.store.InitBatch(); err != nil {
- return err
- }
+ newStore := m.store.InitStore()
for _, slice := range image.Slice {
- if _, err := m.store.GetAccountByID(slice.Account.ID); err != nil {
+ if _, err := newStore.GetAccountByID(slice.Account.ID); err != nil {
log.WithFields(log.Fields{
"module": logModule,
"alias": slice.Account.Alias,
continue
}
- if _, err := m.store.GetAccountByAlias(slice.Account.Alias); err != nil {
+ if _, err := newStore.GetAccountByAlias(slice.Account.Alias); err != nil {
return err
}
- if err := m.store.SetAccount(slice.Account); err != nil {
+ if err := newStore.SetAccount(slice.Account); err != nil {
return err
}
}
- return m.store.CommitBatch()
+ return newStore.CommitBatch()
}
// AccountStore interface contains account storage functions.
type AccountStore interface {
- InitBatch() error
+ InitStore() AccountStore
CommitBatch() error
DeleteAccount(*Account) error
DeleteStandardUTXO(bc.Hash)
return append(SUTXOPrefix, id.Bytes()...)
}
-func (store *mockAccountStore) InitBatch() error { return nil }
+func (store *mockAccountStore) InitStore() AccountStore { return nil }
func (store *mockAccountStore) CommitBatch() error { return nil }
func (store *mockAccountStore) DeleteAccount(*Account) error { return nil }
func (store *mockAccountStore) GetAccountByAlias(string) (*Account, error) { return nil, nil }
}
}
-// InitBatch initial batch
-func (store *AccountStore) InitBatch() error {
- if store.batch != nil {
- return errors.New("AccountStore initail fail, store batch is not nil.")
- }
- store.batch = store.db.NewBatch()
- return nil
+// InitStore initial batch
+func (store *AccountStore) InitStore() acc.AccountStore {
+ newStore := NewAccountStore(store.db)
+ newStore.batch = newStore.db.NewBatch()
+ return newStore
}
// CommitBatch commit batch
}
}
-// InitBatch initial batch
-func (store *MockAccountStore) InitBatch() error {
- if store.batch != nil {
- return errors.New("MockAccountStore initail fail, store batch is not nil.")
- }
- store.batch = store.db.NewBatch()
- return nil
+// InitStore initial batch
+func (store *MockAccountStore) InitStore() acc.AccountStore {
+ newStore := NewMockAccountStore(store.db)
+ newStore.batch = newStore.db.NewBatch()
+ return newStore
}
// CommitBatch commit batch