@@ -1342,10 +1342,15 @@ where
13421342 let index = state. next_internal_index ;
13431343 let parent_key = & state. keychains [ keychain as usize ] ;
13441344
1345+ // `preview` is negated because it turns into `persist_db`
13451346 let ( address, next_index) =
13461347 self . derive_and_persist_address ( label, parent_key, account, keychain, index, !preview) ?;
13471348
1348- state. next_internal_index = next_index;
1349+ // Don't advance the internal index if we are simply previewing
1350+ if !preview {
1351+ state. next_internal_index = next_index;
1352+ log:: debug!( "Internal keychain advanced to index #{next_index}" ) ;
1353+ }
13491354
13501355 Ok ( address)
13511356 }
@@ -1461,6 +1466,18 @@ where
14611466 // - Cannot borrow `state` as mutable because it is also borrowed as immutable
14621467 let state = & mut * state;
14631468
1469+ // Move internal keychain forward if we used a new change address
1470+ let next = self . _gen_internal_address ( state, None , true ) ?;
1471+ if match & txn. transaction {
1472+ Transaction :: ValueTransfer ( vtt) => {
1473+ vtt. body . outputs . iter ( ) . any ( |vto| vto. pkh == next. pkh )
1474+ }
1475+ Transaction :: DataRequest ( dr) => dr. body . outputs . iter ( ) . any ( |vto| vto. pkh == next. pkh ) ,
1476+ _ => false ,
1477+ } {
1478+ let _ = self . _gen_internal_address ( state, None , false ) ;
1479+ }
1480+
14641481 // Mark UTXOs as used so we don't double spend
14651482 // Save the timestamp to after which the UTXO can be spent again
14661483 let tx_pending_timeout = self . params . pending_transactions_timeout_seconds ;
0 commit comments