Skip to content

Commit

Permalink
chain,wallet: handle no transactions and move locking to methods
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Aug 28, 2024
1 parent 6670189 commit 7754053
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
2 changes: 2 additions & 0 deletions chain/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
26 changes: 13 additions & 13 deletions wallet/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

0 comments on commit 7754053

Please sign in to comment.