How does a light node know which block its transaction got included in?
Most SPV wallets use BIP 37. The protocol defined in BIP 37 is roughly as follows:
- Wallet sends to a full node a bloom filter which, when applied to transactions, indicates whether a transaction applies to that wallet.
- Full nodes, for every block and transaction, applies the filter to see if there is a match.
- If there is, relay the transactions to the wallet.
- If there is a match for a transaction in a block, generate a merkle proof and send the proof with the transaction to the wallet.
- Update the filter on matches.
- When blocks are received, send the block header to the wallet.
This is generally how all SPV wallets work too, even ones that don't use BIP 37. They may not use bloom filters, but they use some kind of filter and rely on a full node to check things against the filter and then send along the things that match.
For privacy reasons, the filters do not match exactly. They have a false positive rate (but not a false negative rate) so some things are sent to the wallet that do not apply to it. The wallet just ignores those things.
Also, say a wallet program was loaded after a month of inactivity. Now the wallet needs to know the balance of its address? How does it go about reconstructing its balance after downloading the latest block headers?
It connects to a full node, sends it the filter to use, and then asks the node to send all of the transactions that match that filter that are in all blocks since block X where X is the latest block header the wallet has.