From 77540538c6ba17973f2ae964f582362f0c1c790d Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Wed, 28 Aug 2024 07:50:34 -0700 Subject: [PATCH] chain,wallet: handle no transactions and move locking to methods --- chain/manager.go | 2 ++ wallet/update.go | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/chain/manager.go b/chain/manager.go index 6d2b543..fe55a7f 100644 --- a/chain/manager.go +++ b/chain/manager.go @@ -941,6 +941,8 @@ func (m *Manager) V2TransactionSet(basis types.ChainIndex, txn types.V2Transacti _, txns, err := m.updateV2TransactionSet(basis, []types.V2Transaction{txn}) if err != nil { return types.ChainIndex{}, nil, fmt.Errorf("failed to update transaction set basis: %w", err) + } else if len(txns) == 0 { + return types.ChainIndex{}, nil, errors.New("no transactions to broadcast") } txn = txns[0] diff --git a/wallet/update.go b/wallet/update.go index 8b479b7..f19b07b 100644 --- a/wallet/update.go +++ b/wallet/update.go @@ -285,8 +285,11 @@ func appliedEvents(cau chain.ApplyUpdate, walletAddress types.Address) (events [ return } -// applyChainState atomically applies a chain update -func applyChainState(tx UpdateTx, address types.Address, cau chain.ApplyUpdate) error { +// applyChainUpdate atomically applies a chain update +func (sw *SingleAddressWallet) applyChainUpdate(tx UpdateTx, address types.Address, cau chain.ApplyUpdate) error { + sw.mu.Lock() + defer sw.mu.Unlock() + // update current state elements stateElements, err := tx.WalletStateElements() if err != nil { @@ -321,11 +324,15 @@ func applyChainState(tx UpdateTx, address types.Address, cau chain.ApplyUpdate) if err := tx.WalletApplyIndex(cau.State.Index, createdUTXOs, spentUTXOs, appliedEvents(cau, address), cau.Block.Timestamp); err != nil { return fmt.Errorf("failed to apply index: %w", err) } + sw.tip = cau.State.Index return nil } // revertChainUpdate atomically reverts a chain update from a wallet -func revertChainUpdate(tx UpdateTx, revertedIndex types.ChainIndex, address types.Address, cru chain.RevertUpdate) error { +func (sw *SingleAddressWallet) revertChainUpdate(tx UpdateTx, revertedIndex types.ChainIndex, address types.Address, cru chain.RevertUpdate) error { + sw.mu.Lock() + defer sw.mu.Unlock() + var removedUTXOs, unspentUTXOs []types.SiacoinElement cru.ForEachSiacoinElement(func(se types.SiacoinElement, created, spent bool) { switch { @@ -360,6 +367,7 @@ func revertChainUpdate(tx UpdateTx, revertedIndex types.ChainIndex, address type if err := tx.UpdateWalletStateElements(stateElements); err != nil { return fmt.Errorf("failed to update state elements: %w", err) } + sw.tip = revertedIndex return nil } @@ -371,25 +379,17 @@ func (sw *SingleAddressWallet) UpdateChainState(tx UpdateTx, reverted []chain.Re ID: cru.Block.ID(), Height: cru.State.Index.Height + 1, } - sw.mu.Lock() - err := revertChainUpdate(tx, revertedIndex, sw.addr, cru) + err := sw.revertChainUpdate(tx, revertedIndex, sw.addr, cru) if err != nil { - sw.mu.Unlock() return fmt.Errorf("failed to revert chain update %q: %w", cru.State.Index, err) } - sw.tip = cru.State.Index - sw.mu.Unlock() } for _, cau := range applied { - sw.mu.Lock() - err := applyChainState(tx, sw.addr, cau) + err := sw.applyChainUpdate(tx, sw.addr, cau) if err != nil { - sw.mu.Unlock() return fmt.Errorf("failed to apply chain update %q: %w", cau.State.Index, err) } - sw.tip = cau.State.Index - sw.mu.Unlock() } return nil }