OSDN Git Service

add DeleteAccountUTXOs
authorChengcheng Zhang <943420582@qq.com>
Thu, 27 Jun 2019 16:05:12 +0000 (00:05 +0800)
committerChengcheng Zhang <943420582@qq.com>
Thu, 27 Jun 2019 16:05:12 +0000 (00:05 +0800)
account/accounts.go
account/store.go
database/account_store.go

index f054863..632db13 100644 (file)
@@ -277,18 +277,9 @@ func (m *Manager) deleteAccountControlPrograms(accountID string) error {
 
 // deleteAccountUtxos deletes utxos matching accountID
 func (m *Manager) deleteAccountUtxos(accountID string) error {
-       rawUTXOs := m.store.GetAccountUTXOs(accountID)
-
-       for _, rawUTXO := range rawUTXOs {
-               utxo := new(UTXO)
-               if err := json.Unmarshal(rawUTXO, utxo); err != nil {
-                       return err
-               }
-               if accountID == utxo.AccountID {
-                       m.store.DeleteStandardUTXO(utxo.OutputID)
-               }
+       if err := m.store.DeleteAccountUTXOs(accountID); err != nil {
+               return err
        }
-
        return nil
 }
 
index c965731..2df813a 100644 (file)
@@ -20,7 +20,7 @@ type AccountStorer interface {
        DeleteBip44ContractIndex(string)
        DeleteContractIndex(string)
        GetContractIndex(string) uint64
-       GetAccountUTXOs(string) [][]byte
+       DeleteAccountUTXOs(string) error
        DeleteStandardUTXO(bc.Hash)
        GetCoinbaseArbitrary() []byte
        SetCoinbaseArbitrary([]byte)
index 78c85d4..6331f7d 100644 (file)
@@ -162,25 +162,39 @@ func (store *AccountStore) GetContractIndex(accountID string) uint64 {
        return index
 }
 
-// GetAccountUTXOs get account utxos by account id
-func (store *AccountStore) GetAccountUTXOs(accountID string) [][]byte {
+// DeleteStandardUTXO delete utxo by outpu id
+func (store *AccountStore) DeleteStandardUTXO(outputID bc.Hash) {
+       if store.batch == nil {
+               store.accountDB.Delete(StandardUTXOKey(outputID))
+       } else {
+               store.batch.Delete(StandardUTXOKey(outputID))
+       }
+}
+
+// DeleteAccountUTXOs delete account utxos by accountID
+func (store *AccountStore) DeleteAccountUTXOs(accountID string) error {
+       batch := store.accountDB.NewBatch()
+       if store.batch != nil {
+               batch = store.batch
+       }
+
        accountUtxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
        defer accountUtxoIter.Release()
 
-       utxos := make([][]byte, 0)
        for accountUtxoIter.Next() {
-               utxos = append(utxos, accountUtxoIter.Value())
+               accountUtxo := &acc.UTXO{}
+               if err := json.Unmarshal(accountUtxoIter.Value(), accountUtxo); err != nil {
+                       return err
+               }
+               if accountID == accountUtxo.AccountID {
+                       batch.Delete(StandardUTXOKey(accountUtxo.OutputID))
+               }
        }
-       return utxos
-}
 
-// DeleteStandardUTXO delete utxo by outpu id
-func (store *AccountStore) DeleteStandardUTXO(outputID bc.Hash) {
        if store.batch == nil {
-               store.accountDB.Delete(StandardUTXOKey(outputID))
-       } else {
-               store.batch.Delete(StandardUTXOKey(outputID))
+               batch.Write()
        }
+       return nil
 }
 
 // GetCoinbaseArbitrary get coinbase arbitrary