Move the balance calculation from lnchannel to lnhtlc.
Maintain a running balance in lnhtlc that is coupled with _maybe_active_htlc_ids
for practicality reasons.
1. In lnhtlc, sent_in_ctn and failed_in_ctn need to look at the
remote ctx, and they need to be called when we receive a revocation,
not when we send one.
2. In lnchannel, we use 3 lnworker callbacks:
- payment sent/payment failed (called when we receive a revocation)
- payment received (called when we send a revocation)
3. Make revoke_current_commitment return a single value.
The second value was only used in tests, there is no need
to bloat the code with that
In recv_rev() previously all unacked_local_updates were deleted
as it was assumed that all of them have been acked at that point by
the revoke_and_ack itself. However this is not necessarily the case:
see new test case.
renamed log['unacked_local_updates'] to log['unacked_local_updates2']
to avoid breaking existing wallet files
Replay un-acked local updates (including commitment_signed) byte-for-byte.
If we have sent them a commitment signature that they "lost" (due to disconnect),
we need to make sure we replay the same local updates, as otherwise they could
end up with two (or more) signed valid commitment transactions at the same ctn.
Multiple valid ctxs at the same ctn is a major headache for pre-signing spending txns,
e.g. for watchtowers, hence we must ensure these ctxs coincide.
This resolves the error formerly manifested as:
Traceback (most recent call last):
File "/home/janus/Skrivebord/lightning-rfc/tools/electrum/packages/jsonrpclib/SimpleJSONRPCServer.py", line 376, in _dispatch
return func(*params)
File "/home/janus/Skrivebord/lightning-rfc/tools/electrum/electrum/daemon.py", line 292, in run_cmdline
result = func(*args, **kwargs)
File "/home/janus/Skrivebord/lightning-rfc/tools/electrum/electrum/commands.py", line 87, in func_wrapper
return func(*args, **kwargs)
File "/home/janus/Skrivebord/lightning-rfc/tools/electrum/electrum/commands.py", line 697, in lnpay
return f.result()
File "/usr/lib/python3.6/concurrent/futures/_base.py", line 432, in result
return self.__get_result()
File "/usr/lib/python3.6/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/home/janus/Skrivebord/lightning-rfc/tools/electrum/electrum/lnbase.py", line 887, in pay
sig_64, htlc_sigs = chan.sign_next_commitment()
File "/home/janus/Skrivebord/lightning-rfc/tools/electrum/electrum/lnhtlc.py", line 281, in sign_next_commitment
htlc_tx = make_htlc_tx_with_open_channel(self, *args)
File "/home/janus/Skrivebord/lightning-rfc/tools/electrum/electrum/lnutil.py", line 262, in make_htlc_tx_with_open_channel
commit.txid(), commit.htlc_output_indices[original_htlc_output_index],
KeyError: 0