What is atomicity in IT?
An all-or-nothing operation
Why is this relevant for payment routing?
It prevents theft!
Funds can only be spent after a given time or block number
Funds can only be spent if you know the secret s, such that:
hash(s) == x
x is written to the unlocking script, s needs to be provided by the spender such that x can be reproduced
signature + hash lock + time lock
Funds can be spent by you, at block 800,000 (or later), if you know a secret
signature, hash lock, time lock
Funds can be spent by Alice at block 800,000 (or later)
ORby Bob if he knows a secret
Alice wants to send Eric 1 BTC
1) Eric creates a secret R and communicates its hash to Alice
2) Alice creates a commitment TX: I will pay Bob 1.003 BTC if he presents the secret (R) that leads to hash H, during the next 10 blocks
Bob now also knows H, so he can craft a similar TX
3) Bob creates a commitment TX: I will pay Carol 1.002 BTC if she presents the secret (R) that leads to hash H, during the next 9 blocks
Remember, the commitment TX spends the 2-of-2 multiSig (funding TX) and is not being published to the Bitcoin network!
4) Carol now also knows H, she crafts a similar TX for Diana
5) Same story for Diana
Diminishing BTC amount --> a routing fee being paid
Diminishing time lock --> Security measure to ensure atomicity of the transaction
Eric is the only one who actually knows R! Diana constructed an HTLC in which she promised 1 BTC to Eric if he reveals R!
Eric is happy! But what about Diana? Will she ever receive her "refund" from Carol?
Eric publishes the HTLC on-chain (closes the channel)
Now he has 7 blocks to spend the commitment TX, or else Diana can take the money back.
He spends that commitment TX and sends it to himself to ensure the money belongs to him forever. This means, he needs to reveal R on-chain!
Bob, Carol and Diana are watching the blockchain and learn R from there! They can now also claim their funds.
Happy end!
Well, not really.
Even though the payment went through, channels have also been closed, which is unfortunate.
So let's go back to the moment Diana sent Eric her HTLC
As we have just witnessed, Eric could claim his funds now, and Diana knows that as well.
So Eric asks Diana to move to a new channel state, where Eric owns +1 BTC, and Diana -1 BTC
Diana tells Eric that she is only willing to do that if he tells her R
HTLC consolidation
They collaboratively move to a new channel state and remove the HTLC
Diana has given 1 BTC to Bob. She is now in the same situation as Bob before.
The scheme repeats through 7, 8, 9 until the transaction went through.
What if step 1 does not happen?
Payment cannot be constructed.
What if step 2 does not happen?
Alice searches for a new payment route
What if one of steps 2, 3, 4, 5, 6 does not happen?
Funds are locked in HTLC until time lock runs out
What if one of steps 7, 8, 9 does not happen?
Bob, Carol or Diana may lose 1 BTC
They are protected by the time-lock to have enough time to react. But if they leave the system, they can only harm themselves, not anyone else.
The transaction is atomic!
(as long as you run your system responsibly)
Any questions so far?
For the sake of completeness
Extract from HTLC output script in (Bolt 3)
# To remote node with preimage.
OP_HASH160 <RIPEMD160(payment_hash)> OP_EQUALVERIFY
OP_CHECKSIG
Extract from HTLC output script in (Bolt 3)
# To remote node after timeout.
OP_DROP <cltv_expiry> OP_CHECKLOCKTIMEVERIFY OP_DROP
OP_CHECKSIG
Any questions regarding HTLCs?