Repository: bitcoin/bitcoin
File: test/functional/wallet_anchor.py (Python)

Code before:
                {"desc": descsum_create(f"addr({ANCHOR_ADDRESS})"), "timestamp": "now"},
                {"desc": descsum_create(f"raw({PAY_TO_ANCHOR.hex()})"), "timestamp": "now"}
            ])
            assert_equal(import_res[0]["success"], disable_privkeys)
            assert_equal(import_res[1]["success"], disable_privkeys)

        anchor_txid = self.default_wallet.sendtoaddress(ANCHOR_ADDRESS, 1)
        self.generate(self.nodes[0], 1)

        wallet = self.nodes[0].get_wallet_rpc("anchor_spend_True")
        utxos = wallet.listunspent()
        assert_equal(len(utxos), 1)
        assert_equal(utxos[0]["txid"], anchor_txid)
        assert_equal(utxos[0]["address"], ANCHOR_ADDRESS)
        assert_equal(utxos[0]["amount"], 1)

        assert_raises_rpc_error(-4, "Missing solving data for estimating transaction size", wallet.send, [{self.default_wallet.getnewaddress(): 0.9999}])
        assert_raises_rpc_error(-4, "Error: Private keys are disabled for this wallet", wallet.sendtoaddress, self.default_wallet.getnewaddress(), 0.9999)
        assert_raises_rpc_error(-4, "Unable to determine the size of the transaction, the wallet contains unsolvable descriptors", wallet.sendall, recipients=[self.default_wallet.getnewaddress()], inputs=utxos)
        assert_raises_rpc_error(-4, "Unable to determine the size of the transaction, the wallet contains unsolvable descriptors", wallet.sendall, recipients=[self.default_wallet.getnewaddress()])

    def run_test(self):
        self.default_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)
        self.test_0_value_anchor_listunspent()
        if self.options.descriptors:
            self.test_cannot_sign_anchors()

if __name__ == '__main__':
    WalletAnchorTest(__file__).main()


Review comment (suggestion):
In b605cc8c73a10162f47e0aeb361144e4913a2a54 "test: Add a test for anchor outputs in the wallet"

The following diff allows this test to work for both descriptors and legacy wallets

```diff
diff --git a/test/functional/wallet_anchor.py b/test/functional/wallet_anchor.py
index 49ec75928c0..0b08986d00d 100755
--- a/test/functional/wallet_anchor.py
+++ b/test/functional/wallet_anchor.py
@@ -92,12 +92,15 @@ class WalletAnchorTest(BitcoinTestFramework):
         for disable_privkeys in [False, True]:
             self.nodes[0].createwallet(wallet_name=f"anchor_spend_{disable_privkeys}", disable_private_keys=disable_privkeys)
             wallet = self.nodes[0].get_wallet_rpc(f"anchor_spend_{disable_privkeys}")
-            import_res = wallet.importdescriptors([
-                {"desc": descsum_create(f"addr({ANCHOR_ADDRESS})"), "timestamp": "now"},
-                {"desc": descsum_create(f"raw({PAY_TO_ANCHOR.hex()})"), "timestamp": "now"}
-            ])
-            assert_equal(import_res[0]["success"], disable_privkeys)
-            assert_equal(import_res[1]["success"], disable_privkeys)
+            if self.options.descriptors:
+                import_res = wallet.importdescriptors([
+                    {"desc": descsum_create(f"addr({ANCHOR_ADDRESS})"), "timestamp": "now"},
+                    {"desc": descsum_create(f"raw({PAY_TO_ANCHOR.hex()})"), "timestamp": "now"}
+                ])
+                assert_equal(import_res[0]["success"], disable_privkeys)
+                assert_equal(import_res[1]["success"], disable_privkeys)
+            else:
+                wallet.importaddress(ANCHOR_ADDRESS)
 
         anchor_txid = self.default_wallet.sendtoaddress(ANCHOR_ADDRESS, 1)
         self.generate(self.nodes[0], 1)
@@ -109,16 +112,19 @@ class WalletAnchorTest(BitcoinTestFramework):
         assert_equal(utxos[0]["address"], ANCHOR_ADDRESS)
         assert_equal(utxos[0]["amount"], 1)
 
-        assert_raises_rpc_error(-4, "Missing solving data for estimating transaction size", wallet.send, [{self.default_wallet.getnewaddress(): 0.9999}])
+        if self.options.descriptors:
+            assert_raises_rpc_error(-4, "Missing solving data for estimating transaction size", wallet.send, [{self.default_wallet.getnewaddress(): 0.9999}])
+            assert_raises_rpc_error(-4, "Unable to determine the size of the transaction, the wallet contains unsolvable descriptors", wallet.sendall, recipients=[self.default_wallet.getnewaddress()])
+        else:
+            assert_raises_rpc_error(-4, "Insufficient funds", wallet.send, [{self.default_wallet.getnewaddress(): 0.9999}])
+            assert_raises_rpc_error(-6, "Total value of UTXO pool too low to pay for transaction. Try using lower feerate or excluding uneconomic UTXOs with 'send_max' option.", wallet.sendall, recipients=[self.default_wallet.getnewaddress()])
         assert_raises_rpc_error(-4, "Error: Private keys are disabled for this wallet", wallet.sendtoaddress, self.default_wallet.getnewaddress(), 0.9999)
         assert_raises_rpc_error(-4, "Unable to determine the size of the transaction, the wallet contains unsolvable descriptors", wallet.sendall, recipients=[self.default_wallet.getnewaddress()], inputs=utxos)
-        assert_raises_rpc_error(-4, "Unable to determine the size of the transaction, the wallet contains unsolvable descriptors", wallet.sendall, recipients=[self.default_wallet.getnewaddress()])
 
     def run_test(self):
         self.default_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)
         self.test_0_value_anchor_listunspent()
-        if self.options.descriptors:
-            self.test_cannot_sign_anchors()
+        self.test_cannot_sign_anchors()
 
 if __name__ == '__main__':
     WalletAnchorTest(__file__).main()
```

Code after:
                    {"desc": descsum_create(f"raw({PAY_TO_ANCHOR.hex()})"), "timestamp": "now"}
                ])
                assert_equal(import_res[0]["success"], disable_privkeys)
                assert_equal(import_res[1]["success"], disable_privkeys)
            else:
                wallet.importaddress(ANCHOR_ADDRESS)

        anchor_txid = self.default_wallet.sendtoaddress(ANCHOR_ADDRESS, 1)
        self.generate(self.nodes[0], 1)

        wallet = self.nodes[0].get_wallet_rpc("anchor_spend_True")
        utxos = wallet.listunspent()
        assert_equal(len(utxos), 1)
        assert_equal(utxos[0]["txid"], anchor_txid)
        assert_equal(utxos[0]["address"], ANCHOR_ADDRESS)
        assert_equal(utxos[0]["amount"], 1)

        if self.options.descriptors:
            assert_raises_rpc_error(-4, "Missing solving data for estimating transaction size", wallet.send, [{self.default_wallet.getnewaddress(): 0.9999}])
            assert_raises_rpc_error(-4, "Unable to determine the size of the transaction, the wallet contains unsolvable descriptors", wallet.sendall, recipients=[self.default_wallet.getnewaddress()])
        else:
            assert_raises_rpc_error(-4, "Insufficient funds", wallet.send, [{self.default_wallet.getnewaddress(): 0.9999}])
            assert_raises_rpc_error(-6, "Total value of UTXO pool too low to pay for transaction. Try using lower feerate or excluding uneconomic UTXOs with 'send_max' option.", wallet.sendall, recipients=[self.default_wallet.getnewaddress()])
        assert_raises_rpc_error(-4, "Error: Private keys are disabled for this wallet", wallet.sendtoaddress, self.default_wallet.getnewaddress(), 0.9999)
        assert_raises_rpc_error(-4, "Unable to determine the size of the transaction, the wallet contains unsolvable descriptors", wallet.sendall, recipients=[self.default_wallet.getnewaddress()], inputs=utxos)

    def run_test(self):
        self.default_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)
        self.test_0_value_anchor_listunspent()
        self.test_cannot_sign_anchors()

if __name__ == '__main__':
    WalletAnchorTest(__file__).main()