diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 6372b42b..9a4bf835 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -267,7 +267,7 @@ impl DaemonControl { .increment() .expect("Must not get into hardened territory"); db_conn.set_change_index(next_index, &self.secp); - } else if !is_change && db_conn.receive_index() < *index { + } else if !is_change && db_conn.receive_index() <= *index { let next_index = index .increment() .expect("Must not get into hardened territory"); diff --git a/tests/test_spend.py b/tests/test_spend.py index 65105116..0cf4ab12 100644 --- a/tests/test_spend.py +++ b/tests/test_spend.py @@ -246,6 +246,20 @@ def test_send_to_self(lianad, bitcoind): ) wait_for(lambda: len(list(unspent_coins())) == 1) + # We've used 3 receive addresses and so the DB receive index must be 3. + assert len(lianad.rpc.listaddresses()["addresses"]) == 3 + # Create a new spend to the receive address with index 3. + recv_addr = lianad.rpc.listaddresses(3, 1)["addresses"][0]["receive"] + res = lianad.rpc.createspend({recv_addr: 11_955_000}, [], 1) + assert "psbt" in res + # Max(receive_index, change_index) is now 4: + assert len(lianad.rpc.listaddresses()["addresses"]) == 4 + # But the spend has no change: + psbt = PSBT.from_base64(res["psbt"]) + assert len(psbt.o) == 1 + # As the spend has no change, only the receive index was incremented. + # Therefore, the DB receive index is now 4. + def test_coin_selection(lianad, bitcoind): """We can create a spend using coin selection."""