Skip to content

Commit

Permalink
Merge pull request #111 from SiaFoundation/nate/delete-wallet-addresses
Browse files Browse the repository at this point in the history
Fix foreign key failure when deleting a wallet
  • Loading branch information
n8maninger authored May 2, 2024
2 parents 2aaf4b2 + bc72ac3 commit 0560d67
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
7 changes: 6 additions & 1 deletion persist/sqlite/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,13 @@ func (s *Store) UpdateWallet(w wallet.Wallet) (wallet.Wallet, error) {
// addresses that were previously associated with the wallet.
func (s *Store) DeleteWallet(id wallet.ID) error {
return s.transaction(func(tx *txn) error {
_, err := tx.Exec(`DELETE FROM wallet_addresses WHERE wallet_id=$1`, id)
if err != nil {
return fmt.Errorf("failed to delete wallet addresses: %w", err)
}

var dummyID int64
err := tx.QueryRow(`DELETE FROM wallets WHERE id=$1 RETURNING id`, id).Scan(&dummyID)
err = tx.QueryRow(`DELETE FROM wallets WHERE id=$1 RETURNING id`, id).Scan(&dummyID)
if errors.Is(err, sql.ErrNoRows) {
return wallet.ErrNotFound
}
Expand Down
44 changes: 44 additions & 0 deletions wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1173,3 +1173,47 @@ func TestOrphans(t *testing.T) {
t.Fatalf("expected 0 output, got %v", len(utxos))
}
}

func TestDeleteWallet(t *testing.T) {
pk := types.GeneratePrivateKey()
addr := types.StandardUnlockHash(pk.PublicKey())

log := zaptest.NewLogger(t)
dir := t.TempDir()
db, err := sqlite.OpenDatabase(filepath.Join(dir, "walletd.sqlite3"), log.Named("sqlite3"))
if err != nil {
t.Fatal(err)
}
defer db.Close()

bdb, err := coreutils.OpenBoltChainDB(filepath.Join(dir, "consensus.db"))
if err != nil {
t.Fatal(err)
}
defer bdb.Close()

network, genesisBlock := testV1Network(types.VoidAddress) // don't care about siafunds

store, genesisState, err := chain.NewDBStore(bdb, network, genesisBlock)
if err != nil {
t.Fatal(err)
}
cm := chain.NewManager(store, genesisState)

wm, err := wallet.NewManager(cm, db, log.Named("wallet"))
if err != nil {
t.Fatal(err)
}
defer wm.Close()

w, err := wm.AddWallet(wallet.Wallet{Name: "test"})
if err != nil {
t.Fatal(err)
} else if err := wm.AddAddress(w.ID, wallet.Address{Address: addr}); err != nil {
t.Fatal(err)
}

if err := wm.DeleteWallet(w.ID); err != nil {
t.Fatal(err)
}
}

0 comments on commit 0560d67

Please sign in to comment.