}
}
-// InitBatch initial batch
-func (store *WalletStore) InitBatch() error {
- if store.batch != nil {
- return errors.New("WalletStore initail fail, store batch is not nil.")
- }
-
- store.batch = store.db.NewBatch()
- return nil
+// InitStore initial new wallet store
+func (store *WalletStore) InitStore() wallet.WalletStore {
+ newStore := NewWalletStore(store.db)
+ newStore.batch = newStore.db.NewBatch()
+ return newStore
}
// CommitBatch commit batch
// when query ,query local first and if have no then query external
// details see getAliasDefinition
func saveExternalAssetDefinition(b *types.Block, store WalletStore) error {
- if err := store.InitBatch(); err != nil {
- return err
- }
+ newStore := store.InitStore()
for _, tx := range b.Transactions {
for _, orig := range tx.Inputs {
if cci, ok := orig.TypedInput.(*types.CrossChainInput); ok {
assetID := cci.AssetId
- assetExist, err := store.GetAsset(assetID)
+ assetExist, err := newStore.GetAsset(assetID)
if err != nil {
return err
}
if assetExist == nil {
- store.SetAssetDefinition(assetID, cci.AssetDefinition)
+ newStore.SetAssetDefinition(assetID, cci.AssetDefinition)
}
}
}
}
- if err := store.CommitBatch(); err != nil {
+ if err := newStore.CommitBatch(); err != nil {
return err
}
}
// indexTransactions saves all annotated transactions to the database.
-func (w *Wallet) indexTransactions(b *types.Block, txStatus *bc.TransactionStatus, annotatedTxs []*query.AnnotatedTx) error {
+func (w *Wallet) indexTransactions(b *types.Block, txStatus *bc.TransactionStatus, annotatedTxs []*query.AnnotatedTx, store WalletStore) error {
for _, tx := range annotatedTxs {
if err := w.store.SetTransaction(b.Height, tx); err != nil {
return err
}
- w.store.DeleteUnconfirmedTransaction(tx.ID.String())
+ store.DeleteUnconfirmedTransaction(tx.ID.String())
}
if !w.TxIndexFlag {
for position, globalTx := range b.Transactions {
blockHash := b.BlockHeader.Hash()
- w.store.SetGlobalTransactionIndex(globalTx.ID.String(), &blockHash, uint64(position))
+ store.SetGlobalTransactionIndex(globalTx.ID.String(), &blockHash, uint64(position))
}
return nil
// WalletStore interface contains wallet storage functions.
type WalletStore interface {
- InitBatch() error
+ InitStore() WalletStore
CommitBatch() error
DeleteContractUTXO(bc.Hash)
DeleteRecoveryStatus()
return newAccountUtxos
}
-func (w *Wallet) attachUtxos(b *types.Block, txStatus *bc.TransactionStatus) {
+func (w *Wallet) attachUtxos(b *types.Block, txStatus *bc.TransactionStatus, store WalletStore) {
for txIndex, tx := range b.Transactions {
statusFail, err := txStatus.GetStatus(txIndex)
if err != nil {
if segwit.IsP2WScript(inputUtxo.ControlProgram) {
w.AccountMgr.DeleteStandardUTXO(inputUtxo.OutputID)
} else {
- w.store.DeleteContractUTXO(inputUtxo.OutputID)
+ store.DeleteContractUTXO(inputUtxo.OutputID)
}
}
//hand update the transaction output utxos
outputUtxos := txOutToUtxos(tx, statusFail, b.Height)
utxos := w.filterAccountUtxo(outputUtxos)
- if err := w.saveUtxos(utxos); err != nil {
+ if err := w.saveUtxos(utxos, store); err != nil {
log.WithFields(log.Fields{"module": logModule, "err": err}).Error("attachUtxos fail on saveUtxos")
}
}
}
-func (w *Wallet) detachUtxos(b *types.Block, txStatus *bc.TransactionStatus) {
+func (w *Wallet) detachUtxos(b *types.Block, txStatus *bc.TransactionStatus, store WalletStore) {
for txIndex := len(b.Transactions) - 1; txIndex >= 0; txIndex-- {
tx := b.Transactions[txIndex]
for j := range tx.Outputs {
if segwit.IsP2WScript(code) {
w.AccountMgr.DeleteStandardUTXO(*tx.ResultIds[j])
} else {
- w.store.DeleteContractUTXO(*tx.ResultIds[j])
+ store.DeleteContractUTXO(*tx.ResultIds[j])
}
}
inputUtxos := txInToUtxos(tx, statusFail)
utxos := w.filterAccountUtxo(inputUtxos)
- if err := w.saveUtxos(utxos); err != nil {
+ if err := w.saveUtxos(utxos, store); err != nil {
log.WithFields(log.Fields{"module": logModule, "err": err}).Error("detachUtxos fail on batchSaveUtxos")
return
}
return result
}
-func (w *Wallet) saveUtxos(utxos []*account.UTXO) error {
+func (w *Wallet) saveUtxos(utxos []*account.UTXO, store WalletStore) error {
for _, utxo := range utxos {
if segwit.IsP2WScript(utxo.ControlProgram) {
if err := w.AccountMgr.SetStandardUTXO(utxo.OutputID, utxo); err != nil {
return err
}
} else {
- if err := w.store.SetContractUTXO(utxo.OutputID, utxo); err != nil {
+ if err := store.SetContractUTXO(utxo.OutputID, utxo); err != nil {
return err
}
}
return w.AttachBlock(block)
}
-func (w *Wallet) commitWalletInfo() error {
- if err := w.store.SetWalletInfo(&w.Status); err != nil {
+func (w *Wallet) commitWalletInfo(store WalletStore) error {
+ if err := store.SetWalletInfo(&w.Status); err != nil {
log.WithFields(log.Fields{"module": logModule, "err": err}).Error("save wallet info")
return err
}
w.annotateTxsAccount(annotatedTxs)
- if err := w.store.InitBatch(); err != nil {
- return err
- }
+ newStore := w.store.InitStore()
- if err := w.indexTransactions(block, txStatus, annotatedTxs); err != nil {
+ if err := w.indexTransactions(block, txStatus, annotatedTxs, newStore); err != nil {
return err
}
- w.attachUtxos(block, txStatus)
+ w.attachUtxos(block, txStatus, newStore)
w.Status.WorkHeight = block.Height
w.Status.WorkHash = block.Hash()
if w.Status.WorkHeight >= w.Status.BestHeight {
w.Status.BestHash = w.Status.WorkHash
}
- if err := w.commitWalletInfo(); err != nil {
+ if err := w.commitWalletInfo(newStore); err != nil {
return err
}
- if err := w.store.CommitBatch(); err != nil {
+ if err := newStore.CommitBatch(); err != nil {
return err
}
return err
}
- if err := w.store.InitBatch(); err != nil {
- return err
- }
+ newStore := w.store.InitStore()
- w.detachUtxos(block, txStatus)
- w.store.DeleteTransactions(w.Status.BestHeight)
+ w.detachUtxos(block, txStatus, newStore)
+ newStore.DeleteTransactions(w.Status.BestHeight)
w.Status.BestHeight = block.Height - 1
w.Status.BestHash = block.PreviousBlockHash
w.Status.WorkHeight = w.Status.BestHeight
w.Status.WorkHash = w.Status.BestHash
}
- if err := w.commitWalletInfo(); err != nil {
+ if err := w.commitWalletInfo(newStore); err != nil {
return err
}
- if err := w.store.CommitBatch(); err != nil {
+ if err := newStore.CommitBatch(); err != nil {
return err
}
}
}
-// InitBatch initial batch
-func (store *MockWalletStore) InitBatch() error {
- if store.batch != nil {
- return errors.New("MockWalletStore initail fail, store batch is not nil.")
- }
- store.batch = store.db.NewBatch()
- return nil
+// InitStore initial new wallet store
+func (store *MockWalletStore) InitStore() WalletStore {
+ newStore := NewMockWalletStore(store.db)
+ newStore.batch = newStore.db.NewBatch()
+ return newStore
}
// CommitBatch commit batch