Skip to content
LearnBitcoin

ResearchBitcoin Dev Archive lightning-dev list

A state machine.

22 messages, Aug 20 to Sep 23, 2015. Started by Rusty Russell.

Rusty Russell (11), Pierre (5), Anthony Towns (4), Jasper Hugunin, CJP

Permalink First message in the Wayback Machine

Rusty Russell permalink original

Hi all,

        I've written a state machine for the wire protocol (though it
covers more than that).  It's a bit of a monster, handling all the
commands as well as bitcoin event notifications.  It's had some
simulation testing, but it's not wired up to anything yet.

You can read it in all its glory in my "state" branch:

https://github.com/ElementsProject/lightning/tree/state

In particular, I've pasted state_types.h below.

I'll do a proper RFC eventually, including all the transactions, but here's a quick protocol summary:

The basic protocol state alternates between high and low priority: this resolves the conflict if they both try to do an update at once. They respond to protocol violations with an error packet and unilateral close.

Simplified (high and low prio merged):
        http://ozlabs.org/~rusty/diagrams/lightning/simplified-states.svg
Normal:
        http://ozlabs.org/~rusty/diagrams/lightning/normal-states.svg
Everything (don't bother opening):
        http://ozlabs.org/~rusty/diagrams/lightning/full-states.svg

PKT_CLOSE is allowed at any time, though currently if you want to close with outstanding HTLCs you need to do so unilaterally.

The protocol should handle re-transmits (for which the response is to retransmit any packet since that packet was received). This means if you save state after every transition, you should be able to recover.

Cheers, Rusty.

#define STATE_CLOSE_STEAL_BIT 1 #define STATE_CLOSE_SPENDTHEM_BIT 2 #define STATE_CLOSE_CLOSE_BIT 4 #define STATE_CLOSE_OURCOMMIT_BIT 8 #define STATE_CLOSE_SPENDOURS_BIT 16

enum state {
	STATE_INIT_NOANCHOR,
	STATE_INIT_WITHANCHOR,
	/*
	 * Opening.
	 */
	STATE_OPEN_WAIT_FOR_OPEN_NOANCHOR,
	STATE_OPEN_WAIT_FOR_OPEN_WITHANCHOR,
	STATE_OPEN_WAIT_FOR_ANCHOR,
	STATE_OPEN_WAIT_FOR_COMMIT_SIG,
	STATE_OPEN_WAITING_OURANCHOR,
	STATE_OPEN_WAITING_THEIRANCHOR,
	STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR,
	STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR,
	/*
	 * Normal update loop.
	 *
	 * NOTE: High and low prios must alternate!
	 */
	STATE_NORMAL_LOWPRIO,
	STATE_NORMAL_HIGHPRIO,
	STATE_WAIT_FOR_HTLC_ACCEPT_LOWPRIO,
	STATE_WAIT_FOR_HTLC_ACCEPT_HIGHPRIO,
	STATE_WAIT_FOR_UPDATE_ACCEPT_LOWPRIO,
	STATE_WAIT_FOR_UPDATE_ACCEPT_HIGHPRIO,
	STATE_WAIT_FOR_UPDATE_COMPLETE_LOWPRIO,
	STATE_WAIT_FOR_UPDATE_COMPLETE_HIGHPRIO,
	STATE_WAIT_FOR_UPDATE_SIG_LOWPRIO,
	STATE_WAIT_FOR_UPDATE_SIG_HIGHPRIO,
	/*
	 * Closing.
	 */
	/* We told them to close, waiting for complete msg. */
	STATE_WAIT_FOR_CLOSE_COMPLETE,
	/* They told us to close, waiting for ack msg. */
	STATE_WAIT_FOR_CLOSE_ACK,
	/*
	 * They can broadcast one or more revoked commit tx, or their latest
	 * commit tx at any time.  We respond to revoked commit txs by stealing
	 * their funds (steal).  We respond to their latest commit tx by
	 * spending (spend_them).  They can also (with our help) broadcast
	 * a mutual close tx (mutual_close).
	 *
	 * We can also broadcast one of the following:
	 * 1) Our latest commit tx (our_commit).
	 * 2) After delay has passed, spend of our tx (spend_ours).
	 * 3) Mutual close tx (mutual_close), already covered above.
	 *
	 * Thus, we could be waiting for the following combinations:
	 * - steal
	 * - spend_them
	 * - steal + spend_them
	 * - mutual_close
	 * - steal + mutual_close
	 * - spend_them + mutual_close
	 * - steal + spend_them + mutual_close
	 *
	 * - our_commit
	 * - steal + our_commit
	 * - spend_them + our_commit
	 * - steal + spend_them + our_commit
	 * - mutual_close + our_commit
	 * - steal + mutual_close + our_commit
	 * - spend_them + mutual_close + our_commit
	 * - steal + spend_them + mutual_close + our_commit
	 *
	 * - spend_ours
	 * - steal + spend_ours
	 * - spend_them + spend_ours
	 * - steal + spend_them + spend_ours
	 * - mutual_close + spend_ours
	 * - steal + mutual_close + spend_ours
	 * - spend_them + mutual_close + spend_ours
	 * - steal + spend_them + mutual_close + spend_ours
	 */
	STATE_CLOSE_WAIT_STEAL,
	STATE_CLOSE_WAIT_SPENDTHEM,
	STATE_CLOSE_WAIT_STEAL_SPENDTHEM,
	STATE_CLOSE_WAIT_CLOSE,
	STATE_CLOSE_WAIT_STEAL_CLOSE,
	STATE_CLOSE_WAIT_SPENDTHEM_CLOSE,
	STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE,
	STATE_CLOSE_WAIT_OURCOMMIT,
	STATE_CLOSE_WAIT_STEAL_OURCOMMIT,
	STATE_CLOSE_WAIT_SPENDTHEM_OURCOMMIT,
	STATE_CLOSE_WAIT_STEAL_SPENDTHEM_OURCOMMIT,
	STATE_CLOSE_WAIT_CLOSE_OURCOMMIT,
	STATE_CLOSE_WAIT_STEAL_CLOSE_OURCOMMIT,
	STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_OURCOMMIT,
	STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_OURCOMMIT,
	STATE_CLOSE_WAIT_SPENDOURS,
	STATE_CLOSE_WAIT_STEAL_SPENDOURS,
	STATE_CLOSE_WAIT_SPENDTHEM_SPENDOURS,
	STATE_CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS,
	STATE_CLOSE_WAIT_CLOSE_SPENDOURS,
	STATE_CLOSE_WAIT_STEAL_CLOSE_SPENDOURS,
	STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_SPENDOURS,
	STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS,
	/* All closed. */
	STATE_CLOSED,
	/*
	 * Where angels fear to tread.
	 */
	/* Their anchor didn't reach blockchain in reasonable time. */
	STATE_ERR_ANCHOR_TIMEOUT,
	/* Anchor was double-spent, after both considered it sufficient depth. */
	STATE_ERR_ANCHOR_LOST,
	/* A commitment tx we didn't recognise spent the anchor (impossible) */
	STATE_ERR_INFORMATION_LEAK,
	/* We ended up in an unexpected state. */
	STATE_ERR_INTERNAL,
	STATE_MAX
};
enum state_input {
	/* Packet inputs. */
	PKT_OPEN = PKT__PKT_OPEN,
	PKT_OPEN_ANCHOR = PKT__PKT_OPEN_ANCHOR,
	PKT_OPEN_COMMIT_SIG = PKT__PKT_OPEN_COMMIT_SIG,
	PKT_OPEN_COMPLETE = PKT__PKT_OPEN_COMPLETE,
	PKT_UPDATE = PKT__PKT_UPDATE,
	PKT_UPDATE_ADD_HTLC = PKT__PKT_UPDATE_ADD_HTLC,
	PKT_UPDATE_ACCEPT = PKT__PKT_UPDATE_ACCEPT,
	PKT_UPDATE_SIGNATURE = PKT__PKT_UPDATE_SIGNATURE,
	PKT_UPDATE_COMPLETE = PKT__PKT_UPDATE_COMPLETE,
	PKT_UPDATE_COMPLETE_HTLC = PKT__PKT_UPDATE_COMPLETE_HTLC,
	PKT_UPDATE_TIMEDOUT_HTLC = PKT__PKT_UPDATE_TIMEDOUT_HTLC,
	PKT_UPDATE_ROUTEFAIL_HTLC = PKT__PKT_UPDATE_ROUTEFAIL_HTLC,
	PKT_UPDATE_DECLINE_HTLC = PKT__PKT_UPDATE_DECLINE_HTLC,
	PKT_CLOSE = PKT__PKT_CLOSE,
	PKT_CLOSE_COMPLETE = PKT__PKT_CLOSE_COMPLETE,
	PKT_CLOSE_ACK = PKT__PKT_CLOSE_ACK,
	PKT_ERROR = PKT__PKT_ERROR,
	/* Non-packet inputs. */
	INPUT_NONE,
	/*
	 * Bitcoin events
	 */
	/* It reached the required depth. */
	BITCOIN_ANCHOR_DEPTHOK,
	/* It didn't reach the required depth in time. */
	BITCOIN_ANCHOR_TIMEOUT,
	/* It reached the required depth, then was forked off. */
	BITCOIN_ANCHOR_UNSPENT,
	/* Anchor was spent by our commit, and we can now spend it. */
	BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED,
	/* Anchor was spent by their commit tx. */
	BITCOIN_ANCHOR_THEIRSPEND,
	/* Anchor was spent by another commit tx (eg. expired). */
	BITCOIN_ANCHOR_OTHERSPEND,
	/* Our spend of their commit tx is completely buried. */
	BITCOIN_SPEND_THEIRS_DONE,
	/* Our spend of our own tx is completely buried. */
	BITCOIN_SPEND_OURS_DONE,
	/* Our spend of their revoked tx is completely buried. */
	BITCOIN_STEAL_DONE,
	/* Bitcoin close transaction considered completely buried. */
	BITCOIN_CLOSE_DONE,
	/*
	 * Timeouts.
	 */
	INPUT_CLOSE_COMPLETE_TIMEOUT,
	/* Commands */
	CMD_SEND_UPDATE,
	CMD_SEND_HTLC_UPDATE,
	CMD_SEND_HTLC_COMPLETE,
	CMD_SEND_HTLC_TIMEDOUT,
	CMD_SEND_HTLC_ROUTEFAIL,
	CMD_CLOSE,
	INPUT_MAX
};
Pierre in reply to Rusty Russell permalink original

Hello all,

First, bravo for the great work on lightning ! @Rusty I'm the guy who recently made two dummy PR on your github project, thanks for merging them ;-)

I've just got one remark : from OPEN_WAITING state, you seem to be assuming that the event BITCOIN_ANCHOR_DEPTHOK will always happen before receiving the other party's PKT_OPEN_COMPLETE ; but that won't necessarily be the case depending on each party's minDepth, right ?

Also, can you please confirm that the following is correct in an Alice->Bob->Carol->Dave scenario if we look at the state of *Bob* ?

1) Bob is in NORMAL state
- Bob receives update_add_htlc from Alice
- Bob sends update_accept to Alice
2) Bob switches to WAIT_FOR_UPDATE_SIG state
- Bob receives update_signature from Alice
- (Bob sends update_add_htlc to Carol on another channel)
- (Bob receives update_complete_htlc on another channel)
3) Bob switches to NORMAL state
- Bob sends update_accept to Alice
4) Bob switches to WAIT_FOR_UPDATE_SIG state
- Bob receives update_signature from Alice
5) Bob switches to NORMAL state

Thanks,

Pierre

2015-08-20 9:52 GMT+02:00 Rusty Russell <rusty at rustcorp.com.au>:

Quoted text (243 lines)
> Hi all,
>
>         I've written a state machine for the wire protocol (though it
> covers more than that).  It's a bit of a monster, handling all the
> commands as well as bitcoin event notifications.  It's had some
> simulation testing, but it's not wired up to anything yet.
>
> You can read it in all its glory in my "state" branch:
>
> https://github.com/ElementsProject/lightning/tree/state
>
> In particular, I've pasted state_types.h below.
>
> I'll do a proper RFC eventually, including all the transactions, but
> here's a quick protocol summary:
>
> The basic protocol state alternates between high and low priority: this
> resolves the conflict if they both try to do an update at once.  They
> respond to protocol violations with an error packet and unilateral
> close.
>
> Simplified (high and low prio merged):
>         http://ozlabs.org/~rusty/diagrams/lightning/simplified-states.svg
> Normal:
>         http://ozlabs.org/~rusty/diagrams/lightning/normal-states.svg
> Everything (don't bother opening):
>         http://ozlabs.org/~rusty/diagrams/lightning/full-states.svg
>
> PKT_CLOSE is allowed at any time, though currently if you want to close
> with outstanding HTLCs you need to do so unilaterally.
>
> The protocol should handle re-transmits (for which the response is to
> retransmit any packet since that packet was received).  This means if
> you save state after every transition, you should be able to recover.
>
> Cheers,
> Rusty.
>
> #define STATE_CLOSE_STEAL_BIT 1
> #define STATE_CLOSE_SPENDTHEM_BIT 2
> #define STATE_CLOSE_CLOSE_BIT 4
> #define STATE_CLOSE_OURCOMMIT_BIT 8
> #define STATE_CLOSE_SPENDOURS_BIT 16
>
> enum state {
>         STATE_INIT_NOANCHOR,
>         STATE_INIT_WITHANCHOR,
>
>         /*
>          * Opening.
>          */
>         STATE_OPEN_WAIT_FOR_OPEN_NOANCHOR,
>         STATE_OPEN_WAIT_FOR_OPEN_WITHANCHOR,
>         STATE_OPEN_WAIT_FOR_ANCHOR,
>         STATE_OPEN_WAIT_FOR_COMMIT_SIG,
>         STATE_OPEN_WAITING_OURANCHOR,
>         STATE_OPEN_WAITING_THEIRANCHOR,
>         STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR,
>         STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR,
>
>         /*
>          * Normal update loop.
>          *
>          * NOTE: High and low prios must alternate!
>          */
>         STATE_NORMAL_LOWPRIO,
>         STATE_NORMAL_HIGHPRIO,
>
>         STATE_WAIT_FOR_HTLC_ACCEPT_LOWPRIO,
>         STATE_WAIT_FOR_HTLC_ACCEPT_HIGHPRIO,
>
>         STATE_WAIT_FOR_UPDATE_ACCEPT_LOWPRIO,
>         STATE_WAIT_FOR_UPDATE_ACCEPT_HIGHPRIO,
>
>         STATE_WAIT_FOR_UPDATE_COMPLETE_LOWPRIO,
>         STATE_WAIT_FOR_UPDATE_COMPLETE_HIGHPRIO,
>
>         STATE_WAIT_FOR_UPDATE_SIG_LOWPRIO,
>         STATE_WAIT_FOR_UPDATE_SIG_HIGHPRIO,
>
>         /*
>          * Closing.
>          */
>         /* We told them to close, waiting for complete msg. */
>         STATE_WAIT_FOR_CLOSE_COMPLETE,
>         /* They told us to close, waiting for ack msg. */
>         STATE_WAIT_FOR_CLOSE_ACK,
>
>         /*
>          * They can broadcast one or more revoked commit tx, or their
> latest
>          * commit tx at any time.  We respond to revoked commit txs by
> stealing
>          * their funds (steal).  We respond to their latest commit tx by
>          * spending (spend_them).  They can also (with our help) broadcast
>          * a mutual close tx (mutual_close).
>          *
>          * We can also broadcast one of the following:
>          * 1) Our latest commit tx (our_commit).
>          * 2) After delay has passed, spend of our tx (spend_ours).
>          * 3) Mutual close tx (mutual_close), already covered above.
>          *
>          * Thus, we could be waiting for the following combinations:
>          * - steal
>          * - spend_them
>          * - steal + spend_them
>          * - mutual_close
>          * - steal + mutual_close
>          * - spend_them + mutual_close
>          * - steal + spend_them + mutual_close
>          *
>          * - our_commit
>          * - steal + our_commit
>          * - spend_them + our_commit
>          * - steal + spend_them + our_commit
>          * - mutual_close + our_commit
>          * - steal + mutual_close + our_commit
>          * - spend_them + mutual_close + our_commit
>          * - steal + spend_them + mutual_close + our_commit
>          *
>          * - spend_ours
>          * - steal + spend_ours
>          * - spend_them + spend_ours
>          * - steal + spend_them + spend_ours
>          * - mutual_close + spend_ours
>          * - steal + mutual_close + spend_ours
>          * - spend_them + mutual_close + spend_ours
>          * - steal + spend_them + mutual_close + spend_ours
>          */
>         STATE_CLOSE_WAIT_STEAL,
>         STATE_CLOSE_WAIT_SPENDTHEM,
>         STATE_CLOSE_WAIT_STEAL_SPENDTHEM,
>         STATE_CLOSE_WAIT_CLOSE,
>         STATE_CLOSE_WAIT_STEAL_CLOSE,
>         STATE_CLOSE_WAIT_SPENDTHEM_CLOSE,
>         STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE,
>
>         STATE_CLOSE_WAIT_OURCOMMIT,
>         STATE_CLOSE_WAIT_STEAL_OURCOMMIT,
>         STATE_CLOSE_WAIT_SPENDTHEM_OURCOMMIT,
>         STATE_CLOSE_WAIT_STEAL_SPENDTHEM_OURCOMMIT,
>         STATE_CLOSE_WAIT_CLOSE_OURCOMMIT,
>         STATE_CLOSE_WAIT_STEAL_CLOSE_OURCOMMIT,
>         STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_OURCOMMIT,
>         STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_OURCOMMIT,
>
>         STATE_CLOSE_WAIT_SPENDOURS,
>         STATE_CLOSE_WAIT_STEAL_SPENDOURS,
>         STATE_CLOSE_WAIT_SPENDTHEM_SPENDOURS,
>         STATE_CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS,
>         STATE_CLOSE_WAIT_CLOSE_SPENDOURS,
>         STATE_CLOSE_WAIT_STEAL_CLOSE_SPENDOURS,
>         STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_SPENDOURS,
>         STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS,
>
>         /* All closed. */
>         STATE_CLOSED,
>
>         /*
>          * Where angels fear to tread.
>          */
>         /* Their anchor didn't reach blockchain in reasonable time. */
>         STATE_ERR_ANCHOR_TIMEOUT,
>         /* Anchor was double-spent, after both considered it sufficient
> depth. */
>         STATE_ERR_ANCHOR_LOST,
>         /* A commitment tx we didn't recognise spent the anchor
> (impossible) */
>         STATE_ERR_INFORMATION_LEAK,
>         /* We ended up in an unexpected state. */
>         STATE_ERR_INTERNAL,
>
>         STATE_MAX
> };
>
> enum state_input {
>         /* Packet inputs. */
>         PKT_OPEN = PKT__PKT_OPEN,
>         PKT_OPEN_ANCHOR = PKT__PKT_OPEN_ANCHOR,
>         PKT_OPEN_COMMIT_SIG = PKT__PKT_OPEN_COMMIT_SIG,
>         PKT_OPEN_COMPLETE = PKT__PKT_OPEN_COMPLETE,
>         PKT_UPDATE = PKT__PKT_UPDATE,
>         PKT_UPDATE_ADD_HTLC = PKT__PKT_UPDATE_ADD_HTLC,
>         PKT_UPDATE_ACCEPT = PKT__PKT_UPDATE_ACCEPT,
>         PKT_UPDATE_SIGNATURE = PKT__PKT_UPDATE_SIGNATURE,
>         PKT_UPDATE_COMPLETE = PKT__PKT_UPDATE_COMPLETE,
>         PKT_UPDATE_COMPLETE_HTLC = PKT__PKT_UPDATE_COMPLETE_HTLC,
>         PKT_UPDATE_TIMEDOUT_HTLC = PKT__PKT_UPDATE_TIMEDOUT_HTLC,
>         PKT_UPDATE_ROUTEFAIL_HTLC = PKT__PKT_UPDATE_ROUTEFAIL_HTLC,
>         PKT_UPDATE_DECLINE_HTLC = PKT__PKT_UPDATE_DECLINE_HTLC,
>         PKT_CLOSE = PKT__PKT_CLOSE,
>         PKT_CLOSE_COMPLETE = PKT__PKT_CLOSE_COMPLETE,
>         PKT_CLOSE_ACK = PKT__PKT_CLOSE_ACK,
>         PKT_ERROR = PKT__PKT_ERROR,
>
>         /* Non-packet inputs. */
>         INPUT_NONE,
>
>         /*
>          * Bitcoin events
>          */
>         /* It reached the required depth. */
>         BITCOIN_ANCHOR_DEPTHOK,
>         /* It didn't reach the required depth in time. */
>         BITCOIN_ANCHOR_TIMEOUT,
>         /* It reached the required depth, then was forked off. */
>         BITCOIN_ANCHOR_UNSPENT,
>         /* Anchor was spent by our commit, and we can now spend it. */
>         BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED,
>         /* Anchor was spent by their commit tx. */
>         BITCOIN_ANCHOR_THEIRSPEND,
>         /* Anchor was spent by another commit tx (eg. expired). */
>         BITCOIN_ANCHOR_OTHERSPEND,
>
>         /* Our spend of their commit tx is completely buried. */
>         BITCOIN_SPEND_THEIRS_DONE,
>         /* Our spend of our own tx is completely buried. */
>         BITCOIN_SPEND_OURS_DONE,
>         /* Our spend of their revoked tx is completely buried. */
>         BITCOIN_STEAL_DONE,
>         /* Bitcoin close transaction considered completely buried. */
>         BITCOIN_CLOSE_DONE,
>
>         /*
>          * Timeouts.
>          */
>         INPUT_CLOSE_COMPLETE_TIMEOUT,
>
>         /* Commands */
>         CMD_SEND_UPDATE,
>         CMD_SEND_HTLC_UPDATE,
>         CMD_SEND_HTLC_COMPLETE,
>         CMD_SEND_HTLC_TIMEDOUT,
>         CMD_SEND_HTLC_ROUTEFAIL,
>         CMD_CLOSE,
>
>         INPUT_MAX
> };
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
>
Pierre in reply to Pierre permalink original

Sorry I messed things up, let me try again !

1) Bob is in NORMAL state
- Bob receives update_add_htlc from Alice
- Bob sends update_accept to Alice
2) Bob switches to WAIT_FOR_UPDATE_SIG state
- Bob receives update_signature from Alice
- Bob sends update_complete to Alice
3) Bob switches to NORMAL state
- (Bob sends update_add_htlc to Carol on another channel)
- (Bob receives update_complete_htlc from Carol on another channel)
- Bob sends update_complete_htlc to Alice
4) Bob switches to WAIT_FOR_HTLC_ACCEPT state
- Bob receives update_accept from Alice
- Bob sends update_signature to Alice
5) Bob switches to WAIT_FOR_UPDATE_COMPLETE state
- Bob receives update_complete from Alice
6) Bob switches to NORMAL state

Phew, does that make sense ?

Pierre

2015-08-20 20:20 GMT+02:00 Pierre <pm+lists at acinq.fr>:

Quoted text (283 lines)
> Hello all,
>
> First, bravo for the great work on lightning ! @Rusty I'm the guy who
> recently made two dummy PR on your github project, thanks for merging them
> ;-)
>
> I've just got one remark : from OPEN_WAITING state, you seem to be
> assuming that the event BITCOIN_ANCHOR_DEPTHOK will always happen before
> receiving the other party's PKT_OPEN_COMPLETE ; but that won't necessarily
> be the case depending on each party's minDepth, right ?
>
> Also, can you please confirm that the following is correct in an
> Alice->Bob->Carol->Dave scenario if we look at the state of *Bob* ?
>
> 1) Bob is in NORMAL state
> - Bob receives update_add_htlc from Alice
> - Bob sends update_accept to Alice
>
> 2) Bob switches to WAIT_FOR_UPDATE_SIG state
> - Bob receives update_signature from Alice
>
> - (Bob sends update_add_htlc to Carol on another channel)
> - (Bob receives update_complete_htlc on another channel)
>
> 3) Bob switches to NORMAL state
> - Bob sends update_accept to Alice
>
> 4) Bob switches to WAIT_FOR_UPDATE_SIG state
> - Bob receives update_signature from Alice
>
> 5) Bob switches to NORMAL state
>
> Thanks,
>
> Pierre
>
> 2015-08-20 9:52 GMT+02:00 Rusty Russell <rusty at rustcorp.com.au>:
>
>> Hi all,
>>
>>         I've written a state machine for the wire protocol (though it
>> covers more than that).  It's a bit of a monster, handling all the
>> commands as well as bitcoin event notifications.  It's had some
>> simulation testing, but it's not wired up to anything yet.
>>
>> You can read it in all its glory in my "state" branch:
>>
>> https://github.com/ElementsProject/lightning/tree/state
>>
>> In particular, I've pasted state_types.h below.
>>
>> I'll do a proper RFC eventually, including all the transactions, but
>> here's a quick protocol summary:
>>
>> The basic protocol state alternates between high and low priority: this
>> resolves the conflict if they both try to do an update at once.  They
>> respond to protocol violations with an error packet and unilateral
>> close.
>>
>> Simplified (high and low prio merged):
>>         http://ozlabs.org/~rusty/diagrams/lightning/simplified-states.svg
>> Normal:
>>         http://ozlabs.org/~rusty/diagrams/lightning/normal-states.svg
>> Everything (don't bother opening):
>>         http://ozlabs.org/~rusty/diagrams/lightning/full-states.svg
>>
>> PKT_CLOSE is allowed at any time, though currently if you want to close
>> with outstanding HTLCs you need to do so unilaterally.
>>
>> The protocol should handle re-transmits (for which the response is to
>> retransmit any packet since that packet was received).  This means if
>> you save state after every transition, you should be able to recover.
>>
>> Cheers,
>> Rusty.
>>
>> #define STATE_CLOSE_STEAL_BIT 1
>> #define STATE_CLOSE_SPENDTHEM_BIT 2
>> #define STATE_CLOSE_CLOSE_BIT 4
>> #define STATE_CLOSE_OURCOMMIT_BIT 8
>> #define STATE_CLOSE_SPENDOURS_BIT 16
>>
>> enum state {
>>         STATE_INIT_NOANCHOR,
>>         STATE_INIT_WITHANCHOR,
>>
>>         /*
>>          * Opening.
>>          */
>>         STATE_OPEN_WAIT_FOR_OPEN_NOANCHOR,
>>         STATE_OPEN_WAIT_FOR_OPEN_WITHANCHOR,
>>         STATE_OPEN_WAIT_FOR_ANCHOR,
>>         STATE_OPEN_WAIT_FOR_COMMIT_SIG,
>>         STATE_OPEN_WAITING_OURANCHOR,
>>         STATE_OPEN_WAITING_THEIRANCHOR,
>>         STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR,
>>         STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR,
>>
>>         /*
>>          * Normal update loop.
>>          *
>>          * NOTE: High and low prios must alternate!
>>          */
>>         STATE_NORMAL_LOWPRIO,
>>         STATE_NORMAL_HIGHPRIO,
>>
>>         STATE_WAIT_FOR_HTLC_ACCEPT_LOWPRIO,
>>         STATE_WAIT_FOR_HTLC_ACCEPT_HIGHPRIO,
>>
>>         STATE_WAIT_FOR_UPDATE_ACCEPT_LOWPRIO,
>>         STATE_WAIT_FOR_UPDATE_ACCEPT_HIGHPRIO,
>>
>>         STATE_WAIT_FOR_UPDATE_COMPLETE_LOWPRIO,
>>         STATE_WAIT_FOR_UPDATE_COMPLETE_HIGHPRIO,
>>
>>         STATE_WAIT_FOR_UPDATE_SIG_LOWPRIO,
>>         STATE_WAIT_FOR_UPDATE_SIG_HIGHPRIO,
>>
>>         /*
>>          * Closing.
>>          */
>>         /* We told them to close, waiting for complete msg. */
>>         STATE_WAIT_FOR_CLOSE_COMPLETE,
>>         /* They told us to close, waiting for ack msg. */
>>         STATE_WAIT_FOR_CLOSE_ACK,
>>
>>         /*
>>          * They can broadcast one or more revoked commit tx, or their
>> latest
>>          * commit tx at any time.  We respond to revoked commit txs by
>> stealing
>>          * their funds (steal).  We respond to their latest commit tx by
>>          * spending (spend_them).  They can also (with our help) broadcast
>>          * a mutual close tx (mutual_close).
>>          *
>>          * We can also broadcast one of the following:
>>          * 1) Our latest commit tx (our_commit).
>>          * 2) After delay has passed, spend of our tx (spend_ours).
>>          * 3) Mutual close tx (mutual_close), already covered above.
>>          *
>>          * Thus, we could be waiting for the following combinations:
>>          * - steal
>>          * - spend_them
>>          * - steal + spend_them
>>          * - mutual_close
>>          * - steal + mutual_close
>>          * - spend_them + mutual_close
>>          * - steal + spend_them + mutual_close
>>          *
>>          * - our_commit
>>          * - steal + our_commit
>>          * - spend_them + our_commit
>>          * - steal + spend_them + our_commit
>>          * - mutual_close + our_commit
>>          * - steal + mutual_close + our_commit
>>          * - spend_them + mutual_close + our_commit
>>          * - steal + spend_them + mutual_close + our_commit
>>          *
>>          * - spend_ours
>>          * - steal + spend_ours
>>          * - spend_them + spend_ours
>>          * - steal + spend_them + spend_ours
>>          * - mutual_close + spend_ours
>>          * - steal + mutual_close + spend_ours
>>          * - spend_them + mutual_close + spend_ours
>>          * - steal + spend_them + mutual_close + spend_ours
>>          */
>>         STATE_CLOSE_WAIT_STEAL,
>>         STATE_CLOSE_WAIT_SPENDTHEM,
>>         STATE_CLOSE_WAIT_STEAL_SPENDTHEM,
>>         STATE_CLOSE_WAIT_CLOSE,
>>         STATE_CLOSE_WAIT_STEAL_CLOSE,
>>         STATE_CLOSE_WAIT_SPENDTHEM_CLOSE,
>>         STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE,
>>
>>         STATE_CLOSE_WAIT_OURCOMMIT,
>>         STATE_CLOSE_WAIT_STEAL_OURCOMMIT,
>>         STATE_CLOSE_WAIT_SPENDTHEM_OURCOMMIT,
>>         STATE_CLOSE_WAIT_STEAL_SPENDTHEM_OURCOMMIT,
>>         STATE_CLOSE_WAIT_CLOSE_OURCOMMIT,
>>         STATE_CLOSE_WAIT_STEAL_CLOSE_OURCOMMIT,
>>         STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_OURCOMMIT,
>>         STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_OURCOMMIT,
>>
>>         STATE_CLOSE_WAIT_SPENDOURS,
>>         STATE_CLOSE_WAIT_STEAL_SPENDOURS,
>>         STATE_CLOSE_WAIT_SPENDTHEM_SPENDOURS,
>>         STATE_CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS,
>>         STATE_CLOSE_WAIT_CLOSE_SPENDOURS,
>>         STATE_CLOSE_WAIT_STEAL_CLOSE_SPENDOURS,
>>         STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_SPENDOURS,
>>         STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS,
>>
>>         /* All closed. */
>>         STATE_CLOSED,
>>
>>         /*
>>          * Where angels fear to tread.
>>          */
>>         /* Their anchor didn't reach blockchain in reasonable time. */
>>         STATE_ERR_ANCHOR_TIMEOUT,
>>         /* Anchor was double-spent, after both considered it sufficient
>> depth. */
>>         STATE_ERR_ANCHOR_LOST,
>>         /* A commitment tx we didn't recognise spent the anchor
>> (impossible) */
>>         STATE_ERR_INFORMATION_LEAK,
>>         /* We ended up in an unexpected state. */
>>         STATE_ERR_INTERNAL,
>>
>>         STATE_MAX
>> };
>>
>> enum state_input {
>>         /* Packet inputs. */
>>         PKT_OPEN = PKT__PKT_OPEN,
>>         PKT_OPEN_ANCHOR = PKT__PKT_OPEN_ANCHOR,
>>         PKT_OPEN_COMMIT_SIG = PKT__PKT_OPEN_COMMIT_SIG,
>>         PKT_OPEN_COMPLETE = PKT__PKT_OPEN_COMPLETE,
>>         PKT_UPDATE = PKT__PKT_UPDATE,
>>         PKT_UPDATE_ADD_HTLC = PKT__PKT_UPDATE_ADD_HTLC,
>>         PKT_UPDATE_ACCEPT = PKT__PKT_UPDATE_ACCEPT,
>>         PKT_UPDATE_SIGNATURE = PKT__PKT_UPDATE_SIGNATURE,
>>         PKT_UPDATE_COMPLETE = PKT__PKT_UPDATE_COMPLETE,
>>         PKT_UPDATE_COMPLETE_HTLC = PKT__PKT_UPDATE_COMPLETE_HTLC,
>>         PKT_UPDATE_TIMEDOUT_HTLC = PKT__PKT_UPDATE_TIMEDOUT_HTLC,
>>         PKT_UPDATE_ROUTEFAIL_HTLC = PKT__PKT_UPDATE_ROUTEFAIL_HTLC,
>>         PKT_UPDATE_DECLINE_HTLC = PKT__PKT_UPDATE_DECLINE_HTLC,
>>         PKT_CLOSE = PKT__PKT_CLOSE,
>>         PKT_CLOSE_COMPLETE = PKT__PKT_CLOSE_COMPLETE,
>>         PKT_CLOSE_ACK = PKT__PKT_CLOSE_ACK,
>>         PKT_ERROR = PKT__PKT_ERROR,
>>
>>         /* Non-packet inputs. */
>>         INPUT_NONE,
>>
>>         /*
>>          * Bitcoin events
>>          */
>>         /* It reached the required depth. */
>>         BITCOIN_ANCHOR_DEPTHOK,
>>         /* It didn't reach the required depth in time. */
>>         BITCOIN_ANCHOR_TIMEOUT,
>>         /* It reached the required depth, then was forked off. */
>>         BITCOIN_ANCHOR_UNSPENT,
>>         /* Anchor was spent by our commit, and we can now spend it. */
>>         BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED,
>>         /* Anchor was spent by their commit tx. */
>>         BITCOIN_ANCHOR_THEIRSPEND,
>>         /* Anchor was spent by another commit tx (eg. expired). */
>>         BITCOIN_ANCHOR_OTHERSPEND,
>>
>>         /* Our spend of their commit tx is completely buried. */
>>         BITCOIN_SPEND_THEIRS_DONE,
>>         /* Our spend of our own tx is completely buried. */
>>         BITCOIN_SPEND_OURS_DONE,
>>         /* Our spend of their revoked tx is completely buried. */
>>         BITCOIN_STEAL_DONE,
>>         /* Bitcoin close transaction considered completely buried. */
>>         BITCOIN_CLOSE_DONE,
>>
>>         /*
>>          * Timeouts.
>>          */
>>         INPUT_CLOSE_COMPLETE_TIMEOUT,
>>
>>         /* Commands */
>>         CMD_SEND_UPDATE,
>>         CMD_SEND_HTLC_UPDATE,
>>         CMD_SEND_HTLC_COMPLETE,
>>         CMD_SEND_HTLC_TIMEDOUT,
>>         CMD_SEND_HTLC_ROUTEFAIL,
>>         CMD_CLOSE,
>>
>>         INPUT_MAX
>> };
>> _______________________________________________
>> Lightning-dev mailing list
>> Lightning-dev at lists.linuxfoundation.org
>> https://www.learnbitcoin.com/archive/lists/lightning-dev
>>
>
>
Rusty Russell in reply to Pierre permalink original

Pierre <pm+lists at acinq.fr> writes:

Quoted text (10 lines)
> Hello all,
>
> First, bravo for the great work on lightning ! @Rusty I'm the guy who
> recently made two dummy PR on your github project, thanks for merging them
> ;-)
>
> I've just got one remark : from OPEN_WAITING state, you seem to be assuming
> that the event BITCOIN_ANCHOR_DEPTHOK will always happen before receiving
> the other party's PKT_OPEN_COMPLETE ; but that won't necessarily be the
> case depending on each party's minDepth, right ?

It actually ignores ("defers") that packet until after the transition; I suppressed the loops in the simplified diagram, but the code is:

	case STATE_OPEN_WAITING_OURANCHOR:
		if (input_is(input, BITCOIN_ANCHOR_DEPTHOK)) {
			set_effect(effect, send,
				   pkt_open_complete(effect, sdata));
			return STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR;
		} else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) {
			goto anchor_unspent;
		} else if (input_is(input, PKT_OPEN_COMPLETE)) {
			/* Ignore until we've hit depth ourselves. */
			set_effect(effect, defer, input);
			return state;
                ...
Quoted text (2 lines)
> Also, can you please confirm that the following is correct in an
> Alice->Bob->Carol->Dave scenario if we look at the state of *Bob* ?

Bob has two states here; the states are per connection.

So there's Bob->Alice and Bob->Carol.

Quoted text (11 lines)
> 1) Bob is in NORMAL state
> - Bob receives update_add_htlc from Alice
> - Bob sends update_accept to Alice
>
> 2) Bob switches to WAIT_FOR_UPDATE_SIG state
> - Bob receives update_signature from Alice
>
> - (Bob sends update_add_htlc to Carol on another channel)
> - (Bob receives update_complete_htlc on another channel)
>
> 3) Bob switches to NORMAL state

So far so good. Now the HTLC is established, so I don't understand these transitions:

Quoted text (6 lines)
> - Bob sends update_accept to Alice
>
> 4) Bob switches to WAIT_FOR_UPDATE_SIG state
> - Bob receives update_signature from Alice
>
> 5) Bob switches to NORMAL state

Cheers, Ruysty.

Rusty Russell in reply to Pierre permalink original

Pierre <pm+lists at acinq.fr> writes:

Quoted text (11 lines)
> Sorry I messed things up, let me try again !
>
> 1) Bob is in NORMAL state
> - Bob receives update_add_htlc from Alice
> - Bob sends update_accept to Alice
>
> 2) Bob switches to WAIT_FOR_UPDATE_SIG state
> - Bob receives update_signature from Alice
> - Bob sends update_complete to Alice
>
> 3) Bob switches to NORMAL state
Yep, Bob goes NORMAL (send PKT_UPDATE_ACCEPT)
        -> WAIT_FOR_UPDATE_SIG
        -> NORMAL

When Carol completes htlc, Bob sees CMD_SEND_HTLC_COMPLETE, and does:

        NORMAL (send PKT_UPDATE_COMPLETE_HTLC)
        -> STATE_WAIT_FOR_HTLC_ACCEPT (send PKT_UPDATE_SIGNATURE)
        -> STATE_WAIT_FOR_UPDATE_COMPLETE
        -> NORMAL
Quoted text (14 lines)
> - (Bob sends update_add_htlc to Carol on another channel)
> - (Bob receives update_complete_htlc from Carol on another channel)
> - Bob sends update_complete_htlc to Alice
>
> 4) Bob switches to WAIT_FOR_HTLC_ACCEPT state
> - Bob receives update_accept from Alice
> - Bob sends update_signature to Alice
>
> 5) Bob switches to WAIT_FOR_UPDATE_COMPLETE state
> - Bob receives update_complete from Alice
>
> 6) Bob switches to NORMAL state
>
> Phew, does that make sense ?

Yeah. Let me generate a decent text flowchart for the normal cases...

Thanks! Rusty.

Rusty Russell in reply to Rusty Russell permalink original

Rusty Russell <rusty at rustcorp.com.au> writes:

Quoted text (1 line)
> Yeah.  Let me generate a decent text flowchart for the normal cases...

I've taken out some transitions for simplicity (eg. ERR_ANCHOR_LOST and ERR_INFORMATION_LEAK, which shouldn't happen):

Normal opening states:

INIT_NOANCHOR:
	INPUT_NONE -> OPEN_WAIT_FOR_OPEN_NOANCHOR (=>PKT_OPEN)
INIT_WITHANCHOR:
	INPUT_NONE -> OPEN_WAIT_FOR_OPEN_WITHANCHOR (=>PKT_OPEN)
OPEN_WAIT_FOR_OPEN_NOANCHOR:
	PKT_OPEN -> OPEN_WAIT_FOR_ANCHOR
	CMD_CLOSE -> CLOSED
OPEN_WAIT_FOR_OPEN_WITHANCHOR:
	PKT_OPEN -> OPEN_WAIT_FOR_COMMIT_SIG (=>PKT_OPEN_ANCHOR)
	CMD_CLOSE -> CLOSED
OPEN_WAIT_FOR_ANCHOR:
	PKT_OPEN_ANCHOR -> OPEN_WAITING_THEIRANCHOR (=>PKT_OPEN_COMMIT_SIG)
	CMD_CLOSE -> CLOSED
OPEN_WAIT_FOR_COMMIT_SIG:
	PKT_OPEN_COMMIT_SIG -> OPEN_WAITING_OURANCHOR
	CMD_CLOSE -> CLOSED
OPEN_WAITING_OURANCHOR:
	PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
	BITCOIN_ANCHOR_DEPTHOK -> OPEN_WAIT_FOR_COMPLETE_OURANCHOR (=>PKT_OPEN_COMPLETE)
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
	CMD_CLOSE -> WAIT_FOR_CLOSE_COMPLETE (=>PKT_CLOSE)
OPEN_WAITING_THEIRANCHOR:
	PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
	PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
	BITCOIN_ANCHOR_DEPTHOK -> OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR (=>PKT_OPEN_COMPLETE)
	BITCOIN_ANCHOR_TIMEOUT -> ERR_ANCHOR_TIMEOUT (=>PKT_ERROR)
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
	CMD_CLOSE -> WAIT_FOR_CLOSE_COMPLETE (=>PKT_CLOSE)
OPEN_WAIT_FOR_COMPLETE_OURANCHOR:
	PKT_OPEN_COMPLETE -> NORMAL_HIGHPRIO
	PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
	PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
	CMD_CLOSE -> WAIT_FOR_CLOSE_COMPLETE (=>PKT_CLOSE)
OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR:
	PKT_OPEN_COMPLETE -> NORMAL_LOWPRIO
	PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
	PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
	CMD_CLOSE -> WAIT_FOR_CLOSE_COMPLETE (=>PKT_CLOSE)

Operational state loop: ====================== I've folded high and low here, but we alternate.

NORMAL_LOWPRIO:
NORMAL_HIGHPRIO:
	PKT_UPDATE -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_UPDATE_ADD_HTLC -> NORMAL_HIGHPRIO (=>PKT_UPDATE_DECLINE_HTLC)
	PKT_UPDATE_ADD_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_UPDATE_COMPLETE_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_UPDATE_TIMEDOUT_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_UPDATE_ROUTEFAIL_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
	PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL (=>PKT_ERROR)
	CMD_SEND_UPDATE -> WAIT_FOR_UPDATE_ACCEPT_LOWPRIO (=>PKT_UPDATE)
	CMD_SEND_HTLC_UPDATE -> WAIT_FOR_HTLC_ACCEPT_LOWPRIO (=>PKT_UPDATE_ADD_HTLC)
	CMD_SEND_HTLC_COMPLETE -> WAIT_FOR_HTLC_ACCEPT_LOWPRIO (=>PKT_UPDATE_COMPLETE_HTLC)
	CMD_SEND_HTLC_TIMEDOUT -> WAIT_FOR_HTLC_ACCEPT_LOWPRIO (=>PKT_UPDATE_TIMEDOUT_HTLC)
	CMD_SEND_HTLC_ROUTEFAIL -> WAIT_FOR_HTLC_ACCEPT_LOWPRIO (=>PKT_UPDATE_ROUTEFAIL_HTLC)
	CMD_CLOSE -> WAIT_FOR_CLOSE_COMPLETE (=>PKT_CLOSE)
WAIT_FOR_HTLC_ACCEPT_LOWPRIO:
WAIT_FOR_HTLC_ACCEPT_HIGHPRIO:
	PKT_UPDATE -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_UPDATE_ADD_HTLC -> NORMAL_HIGHPRIO (=>PKT_UPDATE_DECLINE_HTLC)
	PKT_UPDATE_ADD_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_UPDATE_ACCEPT -> WAIT_FOR_UPDATE_COMPLETE_LOWPRIO (=>PKT_UPDATE_SIGNATURE)
	PKT_UPDATE_DECLINE_HTLC -> NORMAL_HIGHPRIO
	PKT_UPDATE_COMPLETE_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_UPDATE_TIMEDOUT_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_UPDATE_ROUTEFAIL_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
	PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL (=>PKT_ERROR)
WAIT_FOR_HTLC_ACCEPT_HIGHPRIO:
	PKT_UPDATE_ACCEPT -> WAIT_FOR_UPDATE_COMPLETE_HIGHPRIO (=>PKT_UPDATE_SIGNATURE)
	PKT_UPDATE_DECLINE_HTLC -> NORMAL_LOWPRIO
	PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
	PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL (=>PKT_ERROR)
WAIT_FOR_UPDATE_ACCEPT_LOWPRIO:
WAIT_FOR_UPDATE_ACCEPT_HIGHPRIO:
	PKT_UPDATE -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_UPDATE_ADD_HTLC -> NORMAL_HIGHPRIO (=>PKT_UPDATE_DECLINE_HTLC)
	PKT_UPDATE_ADD_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_UPDATE_ACCEPT -> WAIT_FOR_UPDATE_COMPLETE_LOWPRIO (=>PKT_UPDATE_SIGNATURE)
	PKT_UPDATE_COMPLETE_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_UPDATE_TIMEDOUT_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_UPDATE_ROUTEFAIL_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
	PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
	PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL (=>PKT_ERROR)
WAIT_FOR_UPDATE_COMPLETE_LOWPRIO:
WAIT_FOR_UPDATE_COMPLETE_HIGHPRIO:
	PKT_UPDATE_COMPLETE -> NORMAL_HIGHPRIO
	PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
	PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL (=>PKT_ERROR)
WAIT_FOR_UPDATE_SIG_LOWPRIO:
	PKT_UPDATE_SIGNATURE -> NORMAL_HIGHPRIO (=>PKT_UPDATE_COMPLETE)
        ...
WAIT_FOR_UPDATE_SIG_HIGHPRIO:
	PKT_UPDATE_SIGNATURE -> NORMAL_LOWPRIO (=>PKT_UPDATE_COMPLETE)
	PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL (=>PKT_ERROR)
	CMD_CLOSE -> WAIT_FOR_CLOSE_COMPLETE (=>PKT_CLOSE)

Normal closing: ==============

WAIT_FOR_CLOSE_COMPLETE:
	PKT_CLOSE -> CLOSE_WAIT_CLOSE (=>PKT_CLOSE_ACK)
	PKT_CLOSE_COMPLETE -> CLOSE_WAIT_CLOSE (=>PKT_CLOSE_ACK)
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_CLOSE
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_CLOSE
	BITCOIN_CLOSE_DONE -> CLOSED
	INPUT_CLOSE_COMPLETE_TIMEOUT -> CLOSE_WAIT_CLOSE_OURCOMMIT (=>PKT_ERROR)
WAIT_FOR_CLOSE_ACK:
	PKT_CLOSE_ACK -> CLOSE_WAIT_CLOSE
	PKT_ERROR -> CLOSE_WAIT_CLOSE
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_CLOSE
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_CLOSE
	BITCOIN_CLOSE_DONE -> CLOSED
CLOSE_WAIT_CLOSE:
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_CLOSE
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_CLOSE
	BITCOIN_CLOSE_DONE -> CLOSED

Weird close states ================== Eg. steal transction, unilateral close, combinations thereof:

CLOSE_WAIT_STEAL:
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM
	BITCOIN_STEAL_DONE -> CLOSED
CLOSE_WAIT_SPENDTHEM:
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM
	BITCOIN_SPEND_THEIRS_DONE -> CLOSED
CLOSE_WAIT_STEAL_SPENDTHEM:
	BITCOIN_SPEND_THEIRS_DONE -> CLOSED
	BITCOIN_STEAL_DONE -> CLOSED
CLOSE_WAIT_STEAL_CLOSE:
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE
	BITCOIN_STEAL_DONE -> CLOSED
	BITCOIN_CLOSE_DONE -> CLOSED
CLOSE_WAIT_SPENDTHEM_CLOSE:
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE
	BITCOIN_SPEND_THEIRS_DONE -> CLOSED
	BITCOIN_CLOSE_DONE -> CLOSED
CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE:
	BITCOIN_SPEND_THEIRS_DONE -> CLOSED
	BITCOIN_STEAL_DONE -> CLOSED
	BITCOIN_CLOSE_DONE -> CLOSED
CLOSE_WAIT_OURCOMMIT:
	BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED -> CLOSE_WAIT_SPENDOURS
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_OURCOMMIT
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_OURCOMMIT
CLOSE_WAIT_STEAL_OURCOMMIT:
	BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED -> CLOSE_WAIT_STEAL_SPENDOURS
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_OURCOMMIT
	BITCOIN_STEAL_DONE -> CLOSED
CLOSE_WAIT_SPENDTHEM_OURCOMMIT:
	BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED -> CLOSE_WAIT_SPENDTHEM_SPENDOURS
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_OURCOMMIT
	BITCOIN_SPEND_THEIRS_DONE -> CLOSED
CLOSE_WAIT_STEAL_SPENDTHEM_OURCOMMIT:
	BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED -> CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS
	BITCOIN_SPEND_THEIRS_DONE -> CLOSED
	BITCOIN_STEAL_DONE -> CLOSED
CLOSE_WAIT_CLOSE_OURCOMMIT:
	BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED -> CLOSE_WAIT_CLOSE_SPENDOURS
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_CLOSE_OURCOMMIT
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_CLOSE_OURCOMMIT
	BITCOIN_CLOSE_DONE -> CLOSED
CLOSE_WAIT_STEAL_CLOSE_OURCOMMIT:
	BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED -> CLOSE_WAIT_STEAL_CLOSE_SPENDOURS
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_OURCOMMIT
	BITCOIN_STEAL_DONE -> CLOSED
	BITCOIN_CLOSE_DONE -> CLOSED
CLOSE_WAIT_SPENDTHEM_CLOSE_OURCOMMIT:
	BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED -> CLOSE_WAIT_SPENDTHEM_CLOSE_SPENDOURS
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_OURCOMMIT
	BITCOIN_SPEND_THEIRS_DONE -> CLOSED
	BITCOIN_CLOSE_DONE -> CLOSED
CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_OURCOMMIT:
	BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED -> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS
	BITCOIN_SPEND_THEIRS_DONE -> CLOSED
	BITCOIN_STEAL_DONE -> CLOSED
	BITCOIN_CLOSE_DONE -> CLOSED
CLOSE_WAIT_SPENDOURS:
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_SPENDOURS
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_SPENDOURS
	BITCOIN_SPEND_OURS_DONE -> CLOSED
CLOSE_WAIT_STEAL_SPENDOURS:
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS
	BITCOIN_SPEND_OURS_DONE -> CLOSED
	BITCOIN_STEAL_DONE -> CLOSED
CLOSE_WAIT_SPENDTHEM_SPENDOURS:
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS
	BITCOIN_SPEND_THEIRS_DONE -> CLOSED
	BITCOIN_SPEND_OURS_DONE -> CLOSED
CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS:
	BITCOIN_SPEND_THEIRS_DONE -> CLOSED
	BITCOIN_SPEND_OURS_DONE -> CLOSED
	BITCOIN_STEAL_DONE -> CLOSED
CLOSE_WAIT_CLOSE_SPENDOURS:
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_CLOSE_SPENDOURS
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_CLOSE_SPENDOURS
	BITCOIN_SPEND_OURS_DONE -> CLOSED
	BITCOIN_CLOSE_DONE -> CLOSED
CLOSE_WAIT_STEAL_CLOSE_SPENDOURS:
	BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS
	BITCOIN_SPEND_OURS_DONE -> CLOSED
	BITCOIN_STEAL_DONE -> CLOSED
	BITCOIN_CLOSE_DONE -> CLOSED
CLOSE_WAIT_SPENDTHEM_CLOSE_SPENDOURS:
	BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS
	BITCOIN_SPEND_THEIRS_DONE -> CLOSED
	BITCOIN_SPEND_OURS_DONE -> CLOSED
	BITCOIN_CLOSE_DONE -> CLOSED
CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS:
	BITCOIN_SPEND_THEIRS_DONE -> CLOSED
	BITCOIN_SPEND_OURS_DONE -> CLOSED
	BITCOIN_STEAL_DONE -> CLOSED
	BITCOIN_CLOSE_DONE -> CLOSED
Pierre in reply to Rusty Russell permalink original

Thanks, this helps a lot !

I have more questions after reviewing your new branch 'state', if you have time for that ;-)

1) In PKT_UPDATE_ADD_HTLC, why is the field amount an int32 and not an uint32 ? the value shouldn't be negative right ?

2) For a given payee, is the (simplified) htlc flow always :

A->B PKT_UPDATE_ADD_HTLC B->A PKT_UPDATE_COMPLETE_HTLC A->B PKT_UPDATE_ADD_HTLC B->A PKT_UPDATE_COMPLETE_HTLC ?

can we have two successive PKT_UPDATE_ADD_HTLC with the same H, the latter updating the former ? and then a single completion ?

3) Since an htlc is completed with a PKT_UPDATE_COMPLETE_HTLC, what is the use case of PKT_UPDATE ?

4) Why is there no PKT_UPDATE_DECLINE ?

5) I understand the High/Low priorities are a way of resolving conflicts, but I don't get how it works. Could you please explain the principle of it ?

6) In a closing by mutual consent, who is supposed to publish the final transaction ?

7) How do we manage the fees ?

Thanks,

Pierre

2015-08-21 7:32 GMT+02:00 Rusty Russell <rusty at rustcorp.com.au>:

Quoted text (278 lines)
> Rusty Russell <rusty at rustcorp.com.au> writes:
> > Yeah.  Let me generate a decent text flowchart for the normal cases...
>
> I've taken out some transitions for simplicity (eg. ERR_ANCHOR_LOST and
> ERR_INFORMATION_LEAK, which shouldn't happen):
>
> Normal opening states:
>
> INIT_NOANCHOR:
>         INPUT_NONE -> OPEN_WAIT_FOR_OPEN_NOANCHOR (=>PKT_OPEN)
> INIT_WITHANCHOR:
>         INPUT_NONE -> OPEN_WAIT_FOR_OPEN_WITHANCHOR (=>PKT_OPEN)
> OPEN_WAIT_FOR_OPEN_NOANCHOR:
>         PKT_OPEN -> OPEN_WAIT_FOR_ANCHOR
>         CMD_CLOSE -> CLOSED
> OPEN_WAIT_FOR_OPEN_WITHANCHOR:
>         PKT_OPEN -> OPEN_WAIT_FOR_COMMIT_SIG (=>PKT_OPEN_ANCHOR)
>         CMD_CLOSE -> CLOSED
> OPEN_WAIT_FOR_ANCHOR:
>         PKT_OPEN_ANCHOR -> OPEN_WAITING_THEIRANCHOR (=>PKT_OPEN_COMMIT_SIG)
>         CMD_CLOSE -> CLOSED
> OPEN_WAIT_FOR_COMMIT_SIG:
>         PKT_OPEN_COMMIT_SIG -> OPEN_WAITING_OURANCHOR
>         CMD_CLOSE -> CLOSED
> OPEN_WAITING_OURANCHOR:
>         PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
>         BITCOIN_ANCHOR_DEPTHOK -> OPEN_WAIT_FOR_COMPLETE_OURANCHOR
> (=>PKT_OPEN_COMPLETE)
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
>         CMD_CLOSE -> WAIT_FOR_CLOSE_COMPLETE (=>PKT_CLOSE)
> OPEN_WAITING_THEIRANCHOR:
>         PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
>         PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
>         BITCOIN_ANCHOR_DEPTHOK -> OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR
> (=>PKT_OPEN_COMPLETE)
>         BITCOIN_ANCHOR_TIMEOUT -> ERR_ANCHOR_TIMEOUT (=>PKT_ERROR)
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
>         CMD_CLOSE -> WAIT_FOR_CLOSE_COMPLETE (=>PKT_CLOSE)
>
> OPEN_WAIT_FOR_COMPLETE_OURANCHOR:
>         PKT_OPEN_COMPLETE -> NORMAL_HIGHPRIO
>         PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
>         PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
>         CMD_CLOSE -> WAIT_FOR_CLOSE_COMPLETE (=>PKT_CLOSE)
>
> OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR:
>         PKT_OPEN_COMPLETE -> NORMAL_LOWPRIO
>         PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
>         PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
>         CMD_CLOSE -> WAIT_FOR_CLOSE_COMPLETE (=>PKT_CLOSE)
>
> Operational state loop:
> ======================
> I've folded high and low here, but we alternate.
>
> NORMAL_LOWPRIO:
> NORMAL_HIGHPRIO:
>         PKT_UPDATE -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
>         PKT_UPDATE_ADD_HTLC -> NORMAL_HIGHPRIO (=>PKT_UPDATE_DECLINE_HTLC)
>         PKT_UPDATE_ADD_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO
> (=>PKT_UPDATE_ACCEPT)
>         PKT_UPDATE_COMPLETE_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO
> (=>PKT_UPDATE_ACCEPT)
>         PKT_UPDATE_TIMEDOUT_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO
> (=>PKT_UPDATE_ACCEPT)
>         PKT_UPDATE_ROUTEFAIL_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO
> (=>PKT_UPDATE_ACCEPT)
>         PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
>         PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL (=>PKT_ERROR)
>         CMD_SEND_UPDATE -> WAIT_FOR_UPDATE_ACCEPT_LOWPRIO (=>PKT_UPDATE)
>         CMD_SEND_HTLC_UPDATE -> WAIT_FOR_HTLC_ACCEPT_LOWPRIO
> (=>PKT_UPDATE_ADD_HTLC)
>         CMD_SEND_HTLC_COMPLETE -> WAIT_FOR_HTLC_ACCEPT_LOWPRIO
> (=>PKT_UPDATE_COMPLETE_HTLC)
>         CMD_SEND_HTLC_TIMEDOUT -> WAIT_FOR_HTLC_ACCEPT_LOWPRIO
> (=>PKT_UPDATE_TIMEDOUT_HTLC)
>         CMD_SEND_HTLC_ROUTEFAIL -> WAIT_FOR_HTLC_ACCEPT_LOWPRIO
> (=>PKT_UPDATE_ROUTEFAIL_HTLC)
>         CMD_CLOSE -> WAIT_FOR_CLOSE_COMPLETE (=>PKT_CLOSE)
>
> WAIT_FOR_HTLC_ACCEPT_LOWPRIO:
> WAIT_FOR_HTLC_ACCEPT_HIGHPRIO:
>         PKT_UPDATE -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
>         PKT_UPDATE_ADD_HTLC -> NORMAL_HIGHPRIO (=>PKT_UPDATE_DECLINE_HTLC)
>         PKT_UPDATE_ADD_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO
> (=>PKT_UPDATE_ACCEPT)
>         PKT_UPDATE_ACCEPT -> WAIT_FOR_UPDATE_COMPLETE_LOWPRIO
> (=>PKT_UPDATE_SIGNATURE)
>         PKT_UPDATE_DECLINE_HTLC -> NORMAL_HIGHPRIO
>         PKT_UPDATE_COMPLETE_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO
> (=>PKT_UPDATE_ACCEPT)
>         PKT_UPDATE_TIMEDOUT_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO
> (=>PKT_UPDATE_ACCEPT)
>         PKT_UPDATE_ROUTEFAIL_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO
> (=>PKT_UPDATE_ACCEPT)
>         PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
>         PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL (=>PKT_ERROR)
> WAIT_FOR_HTLC_ACCEPT_HIGHPRIO:
>         PKT_UPDATE_ACCEPT -> WAIT_FOR_UPDATE_COMPLETE_HIGHPRIO
> (=>PKT_UPDATE_SIGNATURE)
>         PKT_UPDATE_DECLINE_HTLC -> NORMAL_LOWPRIO
>         PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
>         PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL (=>PKT_ERROR)
>
> WAIT_FOR_UPDATE_ACCEPT_LOWPRIO:
> WAIT_FOR_UPDATE_ACCEPT_HIGHPRIO:
>         PKT_UPDATE -> WAIT_FOR_UPDATE_SIG_LOWPRIO (=>PKT_UPDATE_ACCEPT)
>         PKT_UPDATE_ADD_HTLC -> NORMAL_HIGHPRIO (=>PKT_UPDATE_DECLINE_HTLC)
>         PKT_UPDATE_ADD_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO
> (=>PKT_UPDATE_ACCEPT)
>         PKT_UPDATE_ACCEPT -> WAIT_FOR_UPDATE_COMPLETE_LOWPRIO
> (=>PKT_UPDATE_SIGNATURE)
>         PKT_UPDATE_COMPLETE_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO
> (=>PKT_UPDATE_ACCEPT)
>         PKT_UPDATE_TIMEDOUT_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO
> (=>PKT_UPDATE_ACCEPT)
>         PKT_UPDATE_ROUTEFAIL_HTLC -> WAIT_FOR_UPDATE_SIG_LOWPRIO
> (=>PKT_UPDATE_ACCEPT)
>         PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
>         PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL (=>PKT_ERROR)
>
> WAIT_FOR_UPDATE_COMPLETE_LOWPRIO:
> WAIT_FOR_UPDATE_COMPLETE_HIGHPRIO:
>         PKT_UPDATE_COMPLETE -> NORMAL_HIGHPRIO
>         PKT_CLOSE -> WAIT_FOR_CLOSE_ACK (=>PKT_CLOSE_COMPLETE)
>         PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL (=>PKT_ERROR)
>
> WAIT_FOR_UPDATE_SIG_LOWPRIO:
>         PKT_UPDATE_SIGNATURE -> NORMAL_HIGHPRIO (=>PKT_UPDATE_COMPLETE)
>         ...
> WAIT_FOR_UPDATE_SIG_HIGHPRIO:
>         PKT_UPDATE_SIGNATURE -> NORMAL_LOWPRIO (=>PKT_UPDATE_COMPLETE)
>         PKT_ERROR -> CLOSE_WAIT_OURCOMMIT
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM (=>PKT_ERROR)
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL (=>PKT_ERROR)
>         CMD_CLOSE -> WAIT_FOR_CLOSE_COMPLETE (=>PKT_CLOSE)
>
> Normal closing:
> ==============
>
> WAIT_FOR_CLOSE_COMPLETE:
>         PKT_CLOSE -> CLOSE_WAIT_CLOSE (=>PKT_CLOSE_ACK)
>         PKT_CLOSE_COMPLETE -> CLOSE_WAIT_CLOSE (=>PKT_CLOSE_ACK)
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_CLOSE
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_CLOSE
>         BITCOIN_CLOSE_DONE -> CLOSED
>         INPUT_CLOSE_COMPLETE_TIMEOUT -> CLOSE_WAIT_CLOSE_OURCOMMIT
> (=>PKT_ERROR)
> WAIT_FOR_CLOSE_ACK:
>         PKT_CLOSE_ACK -> CLOSE_WAIT_CLOSE
>         PKT_ERROR -> CLOSE_WAIT_CLOSE
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_CLOSE
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_CLOSE
>         BITCOIN_CLOSE_DONE -> CLOSED
> CLOSE_WAIT_CLOSE:
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_CLOSE
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_CLOSE
>         BITCOIN_CLOSE_DONE -> CLOSED
>
> Weird close states
> ==================
> Eg. steal transction, unilateral close, combinations thereof:
>
> CLOSE_WAIT_STEAL:
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM
>         BITCOIN_STEAL_DONE -> CLOSED
> CLOSE_WAIT_SPENDTHEM:
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM
>         BITCOIN_SPEND_THEIRS_DONE -> CLOSED
> CLOSE_WAIT_STEAL_SPENDTHEM:
>         BITCOIN_SPEND_THEIRS_DONE -> CLOSED
>         BITCOIN_STEAL_DONE -> CLOSED
> CLOSE_WAIT_STEAL_CLOSE:
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE
>         BITCOIN_STEAL_DONE -> CLOSED
>         BITCOIN_CLOSE_DONE -> CLOSED
> CLOSE_WAIT_SPENDTHEM_CLOSE:
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE
>         BITCOIN_SPEND_THEIRS_DONE -> CLOSED
>         BITCOIN_CLOSE_DONE -> CLOSED
> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE:
>         BITCOIN_SPEND_THEIRS_DONE -> CLOSED
>         BITCOIN_STEAL_DONE -> CLOSED
>         BITCOIN_CLOSE_DONE -> CLOSED
> CLOSE_WAIT_OURCOMMIT:
>         BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED -> CLOSE_WAIT_SPENDOURS
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_OURCOMMIT
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_OURCOMMIT
> CLOSE_WAIT_STEAL_OURCOMMIT:
>         BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED -> CLOSE_WAIT_STEAL_SPENDOURS
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_OURCOMMIT
>         BITCOIN_STEAL_DONE -> CLOSED
> CLOSE_WAIT_SPENDTHEM_OURCOMMIT:
>         BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED ->
> CLOSE_WAIT_SPENDTHEM_SPENDOURS
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_OURCOMMIT
>         BITCOIN_SPEND_THEIRS_DONE -> CLOSED
> CLOSE_WAIT_STEAL_SPENDTHEM_OURCOMMIT:
>         BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED ->
> CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS
>         BITCOIN_SPEND_THEIRS_DONE -> CLOSED
>         BITCOIN_STEAL_DONE -> CLOSED
> CLOSE_WAIT_CLOSE_OURCOMMIT:
>         BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED -> CLOSE_WAIT_CLOSE_SPENDOURS
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_CLOSE_OURCOMMIT
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_CLOSE_OURCOMMIT
>         BITCOIN_CLOSE_DONE -> CLOSED
> CLOSE_WAIT_STEAL_CLOSE_OURCOMMIT:
>         BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED ->
> CLOSE_WAIT_STEAL_CLOSE_SPENDOURS
>         BITCOIN_ANCHOR_THEIRSPEND ->
> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_OURCOMMIT
>         BITCOIN_STEAL_DONE -> CLOSED
>         BITCOIN_CLOSE_DONE -> CLOSED
> CLOSE_WAIT_SPENDTHEM_CLOSE_OURCOMMIT:
>         BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED ->
> CLOSE_WAIT_SPENDTHEM_CLOSE_SPENDOURS
>         BITCOIN_ANCHOR_OTHERSPEND ->
> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_OURCOMMIT
>         BITCOIN_SPEND_THEIRS_DONE -> CLOSED
>         BITCOIN_CLOSE_DONE -> CLOSED
> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_OURCOMMIT:
>         BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED ->
> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS
>         BITCOIN_SPEND_THEIRS_DONE -> CLOSED
>         BITCOIN_STEAL_DONE -> CLOSED
>         BITCOIN_CLOSE_DONE -> CLOSED
> CLOSE_WAIT_SPENDOURS:
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_SPENDOURS
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_SPENDOURS
>         BITCOIN_SPEND_OURS_DONE -> CLOSED
> CLOSE_WAIT_STEAL_SPENDOURS:
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS
>         BITCOIN_SPEND_OURS_DONE -> CLOSED
>         BITCOIN_STEAL_DONE -> CLOSED
> CLOSE_WAIT_SPENDTHEM_SPENDOURS:
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS
>         BITCOIN_SPEND_THEIRS_DONE -> CLOSED
>         BITCOIN_SPEND_OURS_DONE -> CLOSED
> CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS:
>         BITCOIN_SPEND_THEIRS_DONE -> CLOSED
>         BITCOIN_SPEND_OURS_DONE -> CLOSED
>         BITCOIN_STEAL_DONE -> CLOSED
> CLOSE_WAIT_CLOSE_SPENDOURS:
>         BITCOIN_ANCHOR_THEIRSPEND -> CLOSE_WAIT_SPENDTHEM_CLOSE_SPENDOURS
>         BITCOIN_ANCHOR_OTHERSPEND -> CLOSE_WAIT_STEAL_CLOSE_SPENDOURS
>         BITCOIN_SPEND_OURS_DONE -> CLOSED
>         BITCOIN_CLOSE_DONE -> CLOSED
> CLOSE_WAIT_STEAL_CLOSE_SPENDOURS:
>         BITCOIN_ANCHOR_THEIRSPEND ->
> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS
>         BITCOIN_SPEND_OURS_DONE -> CLOSED
>         BITCOIN_STEAL_DONE -> CLOSED
>         BITCOIN_CLOSE_DONE -> CLOSED
> CLOSE_WAIT_SPENDTHEM_CLOSE_SPENDOURS:
>         BITCOIN_ANCHOR_OTHERSPEND ->
> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS
>         BITCOIN_SPEND_THEIRS_DONE -> CLOSED
>         BITCOIN_SPEND_OURS_DONE -> CLOSED
>         BITCOIN_CLOSE_DONE -> CLOSED
> CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS:
>         BITCOIN_SPEND_THEIRS_DONE -> CLOSED
>         BITCOIN_SPEND_OURS_DONE -> CLOSED
>         BITCOIN_STEAL_DONE -> CLOSED
>         BITCOIN_CLOSE_DONE -> CLOSED
>
Anthony Towns in reply to Rusty Russell permalink original

On Fri, Aug 21, 2015 at 03:02:32PM +0930, Rusty Russell wrote:

Quoted text (4 lines)
> Rusty Russell <rusty at rustcorp.com.au> writes:
> > Yeah.  Let me generate a decent text flowchart for the normal cases...
> I've taken out some transitions for simplicity (eg. ERR_ANCHOR_LOST and
> ERR_INFORMATION_LEAK, which shouldn't happen):

Some thoughs, fwiw.

I think the two INIT states is odd. I guess I would have expected
something more like:
  INIT:
    - (cmd_open_my_anchor) -> INIT_WITHANCHOR
    - (pkt_propose_channel) -> INIT_NOANCHOR

I think the "state" orientation of the dot output isn't great. ie rather than laying it out like how it is in the code with a state then all the transitions from that state:

  NORMAL:
    - UPDATE_HTLC -> X
    - UPDATE_PKT -> Y
    - COMPLETE_HTLC -> Z
I'd rather see it laid out as the protocol steps:
  UPDATE_HTLC
    NORMAL -[UPDATE_HTLC]-> X -[ACCEPT]-> X2 ->...
  UPDATE_PKT
    NORMAL -[UPDATE_PKT]-> Y ->...

ie, have multiple graphs, each starting at INIT/NORMAL with a single cmd/pkt and ending at NORMAL'/CLOSED/ERROR. I'd probably prefer including the decision points as nodes too.

A lot of the "an error occurred!" abort paths apply to a whole bunch of states, it would be nice to combine them in the output... Hmm, I think that's doable...

I've had a go at doing this with code. C is hard, so I converted to python, and came up with:

  http://azure.erisian.com.au/~aj/tmp/lightstate/statepy.svg

(source bits in the lightstate/ directory)

I was thinking it would be possible to update many HTLCs at once, so I was expecting a single PKT_UPDATE_CHANNEL rather than the ADD_HTLC, COMPLETE_HTLC, TIMEOUT_HTLC, etc variants. From a protocol POV, I guess that's something like:

  UpdateChannel:
     int  n_ops
     op   ops[]
     int  commitment_len;
     byte commitment[]

where an op would be ADD_HTLC, COMPLETE_HTLC, TIMEOUT_HTLC, ROUTEFAIL_HTLC, etc. That's a bit more complicated in the protocol, but I think it pays off in simplifying the state diagram and hence the software? (I think it's also kindof necessary for some fee models, and will be useful for batching updates when performance eventually matters)

That would look something like:

  http://azure.erisian.com.au/~aj/tmp/lightstate/update.svg

I think...

If you're going to have separate PKT_UPDATE_COMPLETE_HTLC, etc; then it might make sense to have some of them go to WAIT_FOR_UPDATE_ACCEPT instead of WAIT_FOR_HTLC_ACCEPT so that declining is forbidden -- if you reveal R, or claim a timeout has been reached, then your counterparty doesn't have the right to decline the update, and if they try, you want to close the channel anyway afaics.

Hmm, I find the PRIO stuff pretty klunky. It's only used to choose who wins in the event of simultaneous/overlapping channel updates, no? Why not just have a constant tiebreak in that case, eg, where whoever has the lowest channel balance (or the lowest IP / key id), wins?

  NORMAL
Quoted text (1 line)
    >pkt_update
  WAIT_FOR_HTLC_ACCEPT
    <pkt_update_accept
Quoted text (1 line)
    >pkt_update_signature -> WAIT_FOR_UPDATE_COMPLETE
    <pkt_update_decline -> NORMAL
    <pkt_update + <dec_win_tie
    -> WAIT_FOR_HTLC_ACCEPT
    <pkt_update + <dec_lose_tie + <dec_accept_update
Quoted text (1 line)
    >pkt_update_accept -> WAIT_FOR_UPDATE_SIG
    <pkt_update + <dec_lose_tie + <dec_decline_update
Quoted text (1 line)
    >pkt_update_decline -> NORMAL

Or alternatively, just have the high priority be given to whoever last went from WAIT_FOR_UPDATE_SIG -> NORMAL (and low priority to whoever went from WAIT_FOR_UPDATE_COMPLETE -> NORMAL). That way the priority would still swap, and you could keep the priority embedded in the state, but it would only be needed for NORMAL and WAIT_FOR_*_ACCEPT states; and not the UPDATE_COMPLETE and UPDATE_SIG states as well.

AFAICS, you still have a potential deadlock atm if you think you're high priority but your counterparty also thinks they're high priority, or just missed your update packet. I think there might be a similar deadlock if both systems think they're low priority. There should be a "timeout waiting for message -> retry/close channel", for most of the non-NORMAL states shouldn't there?

If the INIT_NOANCHOR is meant to do to single-sided-anchor idea proposed in

  https://bitcointalk.org/index.php?topic=1134319.msg11963141

then it would probably be good to have a PKT_REBALANCE_VIA_BLOCKCHAIN option; ie something like:

 0. channel is unbalanced: A has many funds, B has no funds
 1. B proposes rebalancing by $x.
 2. A accepts, chooses R, reveals #(R).
 3. B proposes HTLC from A to B of x funds, conditional on R.
 4. B posts $x in funds on the blockchain, payable to:
      [SIG_B & R | SIG_A & TIMEOUT]
 5. B tells A the transaction id (and p2sh details etc).
 6. A waits for this to be "deep enough"
 7. A claims the blockchain transaction, revealing R.
 8. B completes the HTLC, rebalancing the channel.

I think that would look like:

A:
  NORMAL
    <pkt_rebalance (x)
Quoted text (1 line)
    >pkt_update_channel (add htlc: A->B, x, R, T)
    (or pkt_rebalance_decline -> NORMAL)
  REBAL_WAIT_FOR_HTLC_ACCEPT (R,x)
Quoted text (2 lines)
    <pkt_update_accept
    >pkt_update_signature
  REBAL_WAIT_FOR_UPDATE_COMPLETE (R,x)
    <pkt_update_complete
Quoted text (1 line)
    >pkt_rebal_ok (#R,x)
  NORMAL+REBAL_2 (R,x)
    <pkt_rebal_underway (txn)
  NORMAL+REBAL_3 (R,x,txn)
    <bitcoin_depthok (txn)
Quoted text (1 line)
    >bitcoin_spend (txn,R,SIG_A -> ...)
  NORMAL
B:
  NORMAL
    <cmd_rebalance (x)
Quoted text (1 line)
    >pkt_rebalance (x)
  NORMAL+REBAL_A (x)
    <pkt_rebal_ok (#R,x)
Quoted text (2 lines)
    >bitcoin_spend (... -> R+SIG_A|TIME+SIG_B)
    >pkt_rebal_underway (txn)
    (or <pkt_rebal_decline -> NORMAL)
  NORMAL+REBAL_B (#R,x,txn)
    <bitcoin_spent (txn)
    <pkt_update_channel
  NORMAL

Not being able to tack on orthogonal state makes things a little clumsy here...

Cheers, aj

Pierre in reply to Anthony Towns permalink original

Hi Anthony,

Your svg looks great, thanks for sharing !

Regarding the init phase, you seem to be making the assumption that whoever initiates a channel will be the one funding it. But that's not necessarily the case if the requester is a merchant who will only receive payments right ?

Of course we can make this initial funding as low as we want, then have the lightning node fund the channel...

Cheers

Pierre

2015-08-29 9:42 GMT+02:00 Anthony Towns <aj at erisian.com.au>:

Quoted text (182 lines)
> On Fri, Aug 21, 2015 at 03:02:32PM +0930, Rusty Russell wrote:
> > Rusty Russell <rusty at rustcorp.com.au> writes:
> > > Yeah.  Let me generate a decent text flowchart for the normal cases...
> > I've taken out some transitions for simplicity (eg. ERR_ANCHOR_LOST and
> > ERR_INFORMATION_LEAK, which shouldn't happen):
>
> Some thoughs, fwiw.
>
> I think the two INIT states is odd. I guess I would have expected
> something more like:
>   INIT:
>     - (cmd_open_my_anchor) -> INIT_WITHANCHOR
>     - (pkt_propose_channel) -> INIT_NOANCHOR
>
> I think the "state" orientation of the dot output isn't great. ie rather
> than laying it out like how it is in the code with a state then all the
> transitions from that state:
>
>   NORMAL:
>     - UPDATE_HTLC -> X
>     - UPDATE_PKT -> Y
>     - COMPLETE_HTLC -> Z
>
> I'd rather see it laid out as the protocol steps:
>   UPDATE_HTLC
>     NORMAL -[UPDATE_HTLC]-> X -[ACCEPT]-> X2 ->...
>
>   UPDATE_PKT
>     NORMAL -[UPDATE_PKT]-> Y ->...
>
> ie, have multiple graphs, each starting at INIT/NORMAL with a single
> cmd/pkt and ending at NORMAL'/CLOSED/ERROR. I'd probably prefer
> including the decision points as nodes too.
>
> A lot of the "an error occurred!" abort paths apply to a whole bunch of
> states, it would be nice to combine them in the output... Hmm, I think
> that's doable...
>
> I've had a go at doing this with code. C is hard, so I converted to
> python, and came up with:
>
>   http://azure.erisian.com.au/~aj/tmp/lightstate/statepy.svg
>
> (source bits in the lightstate/ directory)
>
> I was thinking it would be possible to update many HTLCs at once, so
> I was expecting a single PKT_UPDATE_CHANNEL rather than the ADD_HTLC,
> COMPLETE_HTLC, TIMEOUT_HTLC, etc variants. From a protocol POV, I guess
> that's something like:
>
>   UpdateChannel:
>      int  n_ops
>      op   ops[]
>      int  commitment_len;
>      byte commitment[]
>
> where an op would be ADD_HTLC, COMPLETE_HTLC, TIMEOUT_HTLC,
> ROUTEFAIL_HTLC, etc. That's a bit more complicated in the protocol,
> but I think it pays off in simplifying the state diagram and hence the
> software? (I think it's also kindof necessary for some fee models, and
> will be useful for batching updates when performance eventually matters)
>
> That would look something like:
>
>   http://azure.erisian.com.au/~aj/tmp/lightstate/update.svg
>
> I think...
>
> If you're going to have separate PKT_UPDATE_COMPLETE_HTLC, etc; then
> it might make sense to have some of them go to WAIT_FOR_UPDATE_ACCEPT
> instead of WAIT_FOR_HTLC_ACCEPT so that declining is forbidden -- if you
> reveal R, or claim a timeout has been reached, then your counterparty
> doesn't have the right to decline the update, and if they try, you want
> to close the channel anyway afaics.
>
> Hmm, I find the PRIO stuff pretty klunky. It's only used to choose who
> wins in the event of simultaneous/overlapping channel updates, no? Why
> not just have a constant tiebreak in that case, eg, where whoever has the
> lowest channel balance (or the lowest IP / key id), wins?
>
>   NORMAL
>     >pkt_update
>
>   WAIT_FOR_HTLC_ACCEPT
>     <pkt_update_accept
>     >pkt_update_signature -> WAIT_FOR_UPDATE_COMPLETE
>
>     <pkt_update_decline -> NORMAL
>
>     <pkt_update + <dec_win_tie
>     -> WAIT_FOR_HTLC_ACCEPT
>
>     <pkt_update + <dec_lose_tie + <dec_accept_update
>     >pkt_update_accept -> WAIT_FOR_UPDATE_SIG
>
>     <pkt_update + <dec_lose_tie + <dec_decline_update
>     >pkt_update_decline -> NORMAL
>
> Or alternatively, just have the high priority be given to whoever last
> went from WAIT_FOR_UPDATE_SIG -> NORMAL (and low priority to whoever
> went from WAIT_FOR_UPDATE_COMPLETE -> NORMAL). That way the priority
> would still swap, and you could keep the priority embedded in the state,
> but it would only be needed for NORMAL and WAIT_FOR_*_ACCEPT states;
> and not the UPDATE_COMPLETE and UPDATE_SIG states as well.
>
> AFAICS, you still have a potential deadlock atm if you think you're
> high priority but your counterparty also thinks they're high priority,
> or just missed your update packet. I think there might be a similar
> deadlock if both systems think they're low priority. There should be a
> "timeout waiting for message -> retry/close channel", for most of the
> non-NORMAL states shouldn't there?
>
> If the INIT_NOANCHOR is meant to do to single-sided-anchor idea proposed
> in
>
>   https://bitcointalk.org/index.php?topic=1134319.msg11963141
>
> then it would probably be good to have a PKT_REBALANCE_VIA_BLOCKCHAIN
> option; ie something like:
>
>  0. channel is unbalanced: A has many funds, B has no funds
>  1. B proposes rebalancing by $x.
>  2. A accepts, chooses R, reveals #(R).
>  3. B proposes HTLC from A to B of x funds, conditional on R.
>  4. B posts $x in funds on the blockchain, payable to:
>       [SIG_B & R | SIG_A & TIMEOUT]
>  5. B tells A the transaction id (and p2sh details etc).
>  6. A waits for this to be "deep enough"
>  7. A claims the blockchain transaction, revealing R.
>  8. B completes the HTLC, rebalancing the channel.
>
> I think that would look like:
>
> A:
>   NORMAL
>     <pkt_rebalance (x)
>     >pkt_update_channel (add htlc: A->B, x, R, T)
>
>     (or pkt_rebalance_decline -> NORMAL)
>
>   REBAL_WAIT_FOR_HTLC_ACCEPT (R,x)
>     <pkt_update_accept
>     >pkt_update_signature
>
>   REBAL_WAIT_FOR_UPDATE_COMPLETE (R,x)
>     <pkt_update_complete
>     >pkt_rebal_ok (#R,x)
>
>   NORMAL+REBAL_2 (R,x)
>     <pkt_rebal_underway (txn)
>
>   NORMAL+REBAL_3 (R,x,txn)
>     <bitcoin_depthok (txn)
>     >bitcoin_spend (txn,R,SIG_A -> ...)
>
>   NORMAL
>
> B:
>   NORMAL
>     <cmd_rebalance (x)
>     >pkt_rebalance (x)
>
>   NORMAL+REBAL_A (x)
>     <pkt_rebal_ok (#R,x)
>     >bitcoin_spend (... -> R+SIG_A|TIME+SIG_B)
>     >pkt_rebal_underway (txn)
>
>     (or <pkt_rebal_decline -> NORMAL)
>
>   NORMAL+REBAL_B (#R,x,txn)
>     <bitcoin_spent (txn)
>     <pkt_update_channel
>
>   NORMAL
>
> Not being able to tack on orthogonal state makes things a little clumsy
> here...
>
> Cheers,
> aj
>
>
Anthony Towns in reply to Pierre permalink original

On 29 August 2015 at 22:50, Pierre <pm+lists at acinq.fr> wrote:

Quoted text (3 lines)
> Hi Anthony,
> Your svg looks great, thanks for sharing !
>

​Yeah, I like it :)

Quoted text (5 lines)
> Regarding the init phase, you seem to be making the assumption that
> whoever initiates a channel will be the one funding it. But that's not
> necessarily the case if the requester is a merchant who will only receive
> payments right ?
>

Yeah, kind-of. I figure that whoever is providing the anchor will want to make a decision beforehand (rather than just have software do that automatically when a particular packet comes in), and they'll indicate they want to proceed by issuing a "cmd_init_with_my_anchor". Maybe there's some negotiation before that (someone registers on your website, or whatever eg), but I figure that's out-of-band.

If you're a merchant, I think it would either be:

 a) register with paypal, paypal anchors the channel
 b) merchant anchors the channel with random hub, merchant has all the
funds, suggests rebalancing via the blockchain with the hub, rebalancing
protocol happens
 c) merchant anchors the channel, merchant does a HTLC to:
     - their account on an exchange
     - their "cold wallet" lightning channel
     - something else
    moving all the funds to the other side of their channel

and voila, they can receive funds.

Quoted text (3 lines)
Of course we can make this initial funding as low as we want, then have the
> lightning node fund the channel...
>

​You can't increase the capacity of a channel easily -- you need a new anchor transaction for that...​

​Cheers, aj​

Rusty Russell in reply to Pierre permalink original

Pierre <pm+lists at acinq.fr> writes:

Quoted text (7 lines)
> Thanks, this helps a lot !
>
> I have more questions after reviewing your new branch 'state', if you have
> time for that ;-)
>
> 1) In PKT_UPDATE_ADD_HTLC, why is the field amount an int32 and not an
> uint32 ? the value shouldn't be negative right ?

Indeed! Thanks, fixed (see below for patch).

Quoted text (9 lines)
> 2) For a given payee, is the (simplified) htlc flow always :
>
> A->B PKT_UPDATE_ADD_HTLC
> B->A PKT_UPDATE_COMPLETE_HTLC
> A->B PKT_UPDATE_ADD_HTLC
> B->A PKT_UPDATE_COMPLETE_HTLC ?
>
> can we have two successive PKT_UPDATE_ADD_HTLC with the same H, the latter
> updating the former ? and then a single completion ?

That's not quite right.

Adding an HTLC goes like so:

A: PKT_UPDATE_ADD_HTLC B: PKT_UPDATE_ACCEPT (or PKT_UPDATE_DECLINE_HTLC, in which case end) A: PKT_UPDATE_SIGNATURE B: PKT_UPDATE_COMPLETE

There are three ways to remove it.

It has timed out, so A wants to cancel it: A: PKT_UPDATE_TIMEDOUT_HTLC

OR, B finally gets a failure upstream, so wants to cancel it: B: PKT_UPDATE_ROUTEFAIL_HTLC

OR, B gets the R value, and wants to compleete it: B: PKT_UPDATE_COMPLETE_HTLC

All these cases lead to the PKT_UPDATE_ACCEPT/PKT_UPDATE_SIGNATURE/PKT_UPDATE_COMPLETE sequence.

I will rename PKT_UPDATE_COMPLETE_HTLC to PKT_UPDATE_FULFILL_HTLC which avoids overloading "COMPLETE".

Quoted text (2 lines)
> 3) Since an htlc is completed with a PKT_UPDATE_COMPLETE_HTLC, what is the
> use case of PKT_UPDATE ?

PKT_UPDATE is a non-htlc transfer. It was useful for writing test utilities, but I've actually now taken it out of the state machine since it's not useful in practice.

So all updates are now HTLC updates..

Quoted text (1 line)
> 4) Why is there no PKT_UPDATE_DECLINE ?

See above.

Quoted text (2 lines)
> 5) I understand the High/Low priorities are a way of resolving conflicts,
> but I don't get how it works. Could you please explain the principle of it ?

Sure. The protocol can only do one update at a time. In the case where we both propose an update, we need to decide which goes first.

Thus, each peer alternates between high and low priority states. If you're high priority, your update takes precedence. Arbitrarily, the anchor funder starts in high priority, the other one in low priority.

Quoted text (2 lines)
> 6) In a closing by mutual consent, who is supposed to publish the final
> transaction ?

Both sides should publish (they should be identical anyway, but it doesn't matter).

Quoted text (1 line)
> 7) How do we manage the fees ?

That's mainly unresolved. A node will decline if not enough fees are offered, but how those are advertised (and how routes are advertised) is still TBA.

We'll probably implement fixed fees and terrible routing to begin with.

Cheers, Rusty.

Subject: protocol: HTLC amounts can't be negative.

Reported-by: Pierre <pm+lists at acinq.fr> Signed-off-by: Rusty Russell <rusty at rustcorp.com.au>

diff --git a/lightning.pb-c.c b/lightning.pb-c.c
index b601697..3108aa5 100644
--- a/lightning.pb-c.c
+++ b/lightning.pb-c.c
@@ -1711,7 +1711,7 @@ static const ProtobufCFieldDescriptor update_add_htlc__field_descriptors[4] =
     "amount",
     2,
     PROTOBUF_C_LABEL_REQUIRED,
-    PROTOBUF_C_TYPE_INT32,
+    PROTOBUF_C_TYPE_UINT32,
     0,   /* quantifier_offset */
     offsetof(UpdateAddHtlc, amount),
     NULL,
diff --git a/lightning.pb-c.h b/lightning.pb-c.h
index 05b0a58..77a6c7b 100644
--- a/lightning.pb-c.h
+++ b/lightning.pb-c.h
@@ -278,7 +278,7 @@ struct  _UpdateAddHtlc
   /*
    * Amount for htlc
    */
-  int32_t amount;
+  uint32_t amount;
   /*
    * Hash for HTLC R value.
    */
diff --git a/lightning.proto b/lightning.proto
index 4388c5c..86ca52e 100644
--- a/lightning.proto
+++ b/lightning.proto
@@ -113,7 +113,7 @@ message update_add_htlc {
   // Hash for which I will supply preimage to revoke this commitment tx.
   required sha256_hash revocation_hash = 1;
   // Amount for htlc
-  required int32 amount = 2;
+  required uint32 amount = 2;
   // Hash for HTLC R value.
   required sha256_hash r_hash = 3;
   // Time at which HTLC expires (absolute)
Rusty Russell in reply to Anthony Towns permalink original

Anthony Towns <aj at erisian.com.au> writes:

Quoted text (33 lines)
> On Fri, Aug 21, 2015 at 03:02:32PM +0930, Rusty Russell wrote:
>> Rusty Russell <rusty at rustcorp.com.au> writes:
>> > Yeah.  Let me generate a decent text flowchart for the normal cases...
>> I've taken out some transitions for simplicity (eg. ERR_ANCHOR_LOST and
>> ERR_INFORMATION_LEAK, which shouldn't happen):
>
> Some thoughs, fwiw.
>
> I think the two INIT states is odd. I guess I would have expected
> something more like:
>   INIT:
>     - (cmd_open_my_anchor) -> INIT_WITHANCHOR
>     - (pkt_propose_channel) -> INIT_NOANCHOR
>
> I think the "state" orientation of the dot output isn't great. ie rather
> than laying it out like how it is in the code with a state then all the
> transitions from that state:
>
>   NORMAL:
>     - UPDATE_HTLC -> X
>     - UPDATE_PKT -> Y
>     - COMPLETE_HTLC -> Z
>
> I'd rather see it laid out as the protocol steps:
>   UPDATE_HTLC
>     NORMAL -[UPDATE_HTLC]-> X -[ACCEPT]-> X2 ->...
>
>   UPDATE_PKT
>     NORMAL -[UPDATE_PKT]-> Y ->...
>
> ie, have multiple graphs, each starting at INIT/NORMAL with a single
> cmd/pkt and ending at NORMAL'/CLOSED/ERROR. I'd probably prefer
> including the decision points as nodes too.

Yeah, I plan on eventually creating an RFC-style document which lays this out.

At its core, it's fairly simple, and I'm starting to think that diagrams more than a simple ASCII flowchart don't add anything.

Quoted text (29 lines)
> A lot of the "an error occurred!" abort paths apply to a whole bunch of
> states, it would be nice to combine them in the output... Hmm, I think
> that's doable...
>
> I've had a go at doing this with code. C is hard, so I converted to
> python, and came up with:
>
>   http://azure.erisian.com.au/~aj/tmp/lightstate/statepy.svg
>
> (source bits in the lightstate/ directory)
>
>
> I was thinking it would be possible to update many HTLCs at once, so
> I was expecting a single PKT_UPDATE_CHANNEL rather than the ADD_HTLC,
> COMPLETE_HTLC, TIMEOUT_HTLC, etc variants. From a protocol POV, I guess
> that's something like:
>
>   UpdateChannel:
>      int  n_ops
>      op   ops[]
>      int  commitment_len;
>      byte commitment[]
>
> where an op would be ADD_HTLC, COMPLETE_HTLC, TIMEOUT_HTLC,
> ROUTEFAIL_HTLC, etc. That's a bit more complicated in the protocol,
> but I think it pays off in simplifying the state diagram and hence the
> software? (I think it's also kindof necessary for some fee models, and
> will be useful for batching updates when performance eventually
> matters)

This is the kind of optimization we may see later, but I really shy away from doing it now. Your diagram looks simpler because you removed all the rest of the handshaking. Try this:

A: ADD_HTLC --> B: DECLINE_HTLC
  OR
A: ADD_HTLC --> B: ACCEPT --> A: SIGNATURE --> B: COMPLETE

After success:

B: FULFILL_HTLC -> A: ACCEPT --> B: SIGNATURE --> A: COMPLETE
  OR
B: ROUTEFAIL_HTLC -> A: ACCEPT --> B: SIGNATURE --> A: COMPLETE
  OR
A: TIMEDOUT_HTLC -> B: ACCEPT --> A: SIGNATURE --> B: COMPLETE

This makes the constraints clearer, eg. you can't DECLINE_HTLC anything but an ADD_HTLC.

Quoted text (4 lines)
> Hmm, I find the PRIO stuff pretty klunky. It's only used to choose who
> wins in the event of simultaneous/overlapping channel updates, no? Why
> not just have a constant tiebreak in that case, eg, where whoever has the
> lowest channel balance (or the lowest IP / key id), wins?

That could lead to livelock.

Quoted text (4 lines)
> Or alternatively, just have the high priority be given to whoever last
> went from WAIT_FOR_UPDATE_SIG -> NORMAL (and low priority to whoever
> went from WAIT_FOR_UPDATE_COMPLETE -> NORMAL). That way the priority
> would still swap

Well, that would alternate at least. But that seems more complex than basing priority on the lower bit of the commitment number, which we have to remember anyway.

Quoted text (4 lines)
> AFAICS, you still have a potential deadlock atm if you think you're
> high priority but your counterparty also thinks they're high priority,
> or just missed your update packet. I think there might be a similar
> deadlock if both systems think they're low priority.

They can't get into that state. And that's why it's spelled out in the state machine, so I can exhaustively test (and have).

Quoted text (18 lines)
> If the INIT_NOANCHOR is meant to do to single-sided-anchor idea proposed
> in
>
>   https://bitcointalk.org/index.php?topic=1134319.msg11963141
>
> then it would probably be good to have a PKT_REBALANCE_VIA_BLOCKCHAIN
> option; ie something like:
>
>  0. channel is unbalanced: A has many funds, B has no funds
>  1. B proposes rebalancing by $x.
>  2. A accepts, chooses R, reveals #(R).
>  3. B proposes HTLC from A to B of x funds, conditional on R.
>  4. B posts $x in funds on the blockchain, payable to:
>       [SIG_B & R | SIG_A & TIMEOUT]
>  5. B tells A the transaction id (and p2sh details etc).
>  6. A waits for this to be "deep enough"
>  7. A claims the blockchain transaction, revealing R.
>  8. B completes the HTLC, rebalancing the channel.

We want A to be able to increase the channel too, so this doesn't quite work. I think we need to explicitly add a new anchor for that case.

But this idea is a subset of a more general "swap to X" HTLC, which comes down to a routing problem, which is a layer up from this state machine.

Quoted text (2 lines)
> Not being able to tack on orthogonal state makes things a little clumsy
> here...

The state machine is hard to work with, but the payoff is exhaustive proof that it's sane, and handles all possible cases.

Cheers, Rusty.

Anthony Towns in reply to Rusty Russell permalink original

On 31 August 2015 at 11:04, Rusty Russell <rusty at rustcorp.com.au> wrote:

Quoted text (8 lines)
> > I was thinking it would be possible to update many HTLCs at once, so
> > I was expecting a single PKT_UPDATE_CHANNEL rather than the ADD_HTLC,
> > COMPLETE_HTLC, TIMEOUT_HTLC, etc variants. From a protocol POV, I guess
> > that's something like:
> This is the kind of optimization we may see later, but I really shy away
> from doing it now.  Your diagram looks simpler because you removed all
> the rest of the handshaking.  Try this:
>

​I think the statepy.svg includes all the handshaking (including errors, but I think excluding internal errors) apart from the nop state transitions. Having a single PKT_UPDATE_CHANNEL would just combine those into one subgraph / one edge.

I'm a bit surprised that CMD_CLOSE isn't a valid option when proposing an update -- it's valid during WAIT_FOR_UPDATE_SIG but not WAIT_FOR_HTLC_ACCEPT/WAIT_FOR_UPDATE_COMPLETE. (Correspondingly, PKT_CLOSE doesn't seem valid during WAIT_FOR_UPDATE_SIG)

Quoted text (15 lines)
A: ADD_HTLC --> B: DECLINE_HTLC
>   OR
> A: ADD_HTLC --> B: ACCEPT --> A: SIGNATURE --> B: COMPLETE
>
> After success:
>
> B: FULFILL_HTLC -> A: ACCEPT --> B: SIGNATURE --> A: COMPLETE
>   OR
> B: ROUTEFAIL_HTLC -> A: ACCEPT --> B: SIGNATURE --> A: COMPLETE
>   OR
> A: TIMEDOUT_HTLC -> B: ACCEPT --> A: SIGNATURE --> B: COMPLETE
>
> This makes the constraints clearer, eg. you can't DECLINE_HTLC anything
> but an ADD_HTLC.
>

​Your states currently allow declining those though:

        case STATE_NORMAL_LOWPRIO:
        case STATE_NORMAL_HIGHPRIO:
                } else if (input_is(input, CMD_SEND_HTLC_COMPLETE)) {
                        /* We are to send an HTLC complete. */
                        set_effect(effect, send,
                                   pkt_htlc_complete(effect, sdata,
idata->cmd));
                        return prio(state, STATE_WAIT_FOR_HTLC_ACCEPT);
        case STATE_WAIT_FOR_HTLC_ACCEPT_LOWPRIO:
        case STATE_WAIT_FOR_HTLC_ACCEPT_HIGHPRIO:
                /* HTLCs can also evoke a refusal. */
                if (input_is(input, PKT_UPDATE_DECLINE_HTLC)) {
                        fail_cmd(effect, CMD_SEND_HTLC_UPDATE, idata->pkt);
                        /* Toggle between high and low priority states. */
                        return toggle_prio(state, STATE_NORMAL);
                }

If your counterparty proposes a broken HTLC update, I'm not sure there's harm in being allowed to decline it? They can choose to close the channel if they think you're unreasonable, retry the update if they found a mistake, or just forget it and not worry (if they were issuing a ROUTEFAIL, it's not /their/ funds that are on the line eg).

I also wonder if

  A: TIMEDOUT_HTLC -> B: DECLINE (err_time_sync_lost)

might be useful.

Quoted text (5 lines)
> AFAICS, you still have a potential deadlock atm if you think you're
> > high priority but your counterparty also thinks they're high priority,
> > or just missed your update packet. I think there might be a similar
> > deadlock if both systems think they're low priority.
> They can't get into that state.

​Sorry, I was assuming that one or both implementations were buggy. I meant to make that explicit.​ You're talking with strangers on the network, so you can't assume their software is bug free, right?

BTW, your states currently switch priority even when an update is declined, so the low bit of the current commitment id (which obviously isn't changed on a declined update) doesn't actually give you the priority afaics.

Cheers, aj

Rusty Russell in reply to Anthony Towns permalink original

Anthony Towns <aj at erisian.com.au> writes:

Quoted text (15 lines)
> On 31 August 2015 at 11:04, Rusty Russell <rusty at rustcorp.com.au> wrote:
>
>> > I was thinking it would be possible to update many HTLCs at once, so
>> > I was expecting a single PKT_UPDATE_CHANNEL rather than the ADD_HTLC,
>> > COMPLETE_HTLC, TIMEOUT_HTLC, etc variants. From a protocol POV, I guess
>> > that's something like:
>> This is the kind of optimization we may see later, but I really shy away
>> from doing it now.  Your diagram looks simpler because you removed all
>> the rest of the handshaking.  Try this:
>>
>
> ​I think the statepy.svg includes all the handshaking (including errors,
> but I think excluding internal errors) apart from the nop state
> transitions. Having a single PKT_UPDATE_CHANNEL would just combine those
> into one subgraph / one edge.

My instinct is to encode as much in the state itself as possible, because it simplifies verification.

Quoted text (3 lines)
> I'm a bit surprised that CMD_CLOSE isn't a valid option when proposing an
> update -- it's valid during WAIT_FOR_UPDATE_SIG but not
> WAIT_FOR_HTLC_ACCEPT/WAIT_FOR_UPDATE_COMPLETE.

Looking at that git tree (I've done some work since then)... Ah, you can't send a command while processing an existing command. Seems a logical simplification.

Quoted text (2 lines)
> (Correspondingly, PKT_CLOSE
> doesn't seem valid during WAIT_FOR_UPDATE_SIG)

It's a corrollary of the above.

Quoted text (17 lines)
> A: ADD_HTLC --> B: DECLINE_HTLC
>>   OR
>> A: ADD_HTLC --> B: ACCEPT --> A: SIGNATURE --> B: COMPLETE
>>
>> After success:
>>
>> B: FULFILL_HTLC -> A: ACCEPT --> B: SIGNATURE --> A: COMPLETE
>>   OR
>> B: ROUTEFAIL_HTLC -> A: ACCEPT --> B: SIGNATURE --> A: COMPLETE
>>   OR
>> A: TIMEDOUT_HTLC -> B: ACCEPT --> A: SIGNATURE --> B: COMPLETE
>>
>> This makes the constraints clearer, eg. you can't DECLINE_HTLC anything
>> but an ADD_HTLC.
>>
>
> ​Your states currently allow declining those though:

Not at all (or if it does, it's a bug). Each state is defined as something which can accept inputs, and for which all those inputs are always valid.

Quoted text (5 lines)
> If your counterparty proposes a broken HTLC update, I'm not sure there's
> harm in being allowed to decline it? They can choose to close the channel
> if they think you're unreasonable, retry the update if they found a
> mistake, or just forget it and not worry (if they were issuing a ROUTEFAIL,
> it's not /their/ funds that are on the line eg).

The simplest (and safest) system is always to error and close when they break protocol. There's some game theory involved in whether you should wait or not, but in the end, they're broken and there's not much you can do.

Quoted text (5 lines)
> I also wonder if
>
>   A: TIMEDOUT_HTLC -> B: DECLINE (err_time_sync_lost)
>
> might be useful.

I don't think it's useful, though if you disagree on time you might get an error packet. (What else can you do?)

Perhaps we should add a current time field to UPDATE_ADD_HTLC so you can defuse clock sync problems earlier?

Quoted text (9 lines)
>> AFAICS, you still have a potential deadlock atm if you think you're
>> > high priority but your counterparty also thinks they're high priority,
>> > or just missed your update packet. I think there might be a similar
>> > deadlock if both systems think they're low priority.
>> They can't get into that state.
>
> ​Sorry, I was assuming that one or both implementations were buggy. I meant
> to make that explicit.​ You're talking with strangers on the network, so
> you can't assume their software is bug free, right?

That applies to any scheme you come up with, though. Propose something simpler, and I'll gladly rewrite.

Quoted text (3 lines)
> BTW, your states currently switch priority even when an update is declined,
> so the low bit of the current commitment id (which obviously isn't changed
> on a declined update) doesn't actually give you the priority afaics.

True! I will change that, since it's conceptually simpler.

Thanks, Rusty.

Anthony Towns in reply to Rusty Russell permalink original

On 1 September 2015 at 17:32, Rusty Russell <rusty at rustcorp.com.au> wrote:

Quoted text (1 line)
> Looking at that git tree (I've done some work since then)...

​Oh, github hasn't been updated since Aug 20? That's forever ago!​

Quoted text (4 lines)
> Ah, you
> can't send a command while processing an existing command.  Seems a
> logical simplification.
>

I'm thinking of CMD_CLOSE like "Ctrl-C"; why wouldn't you want it to work anytime?​

Quoted text (4 lines)
> >> This makes the constraints clearer, eg. you can't DECLINE_HTLC anything
> >> but an ADD_HTLC.
> > ​Your states currently allow declining those though:
> Not at all (or if it does, it's a bug).

​Right, it's a bug as of ​52cda01 then :)

​​

Quoted text (5 lines)
> The simplest (and safest) system is always to error and close when they
> break protocol.  There's some game theory involved in whether you should
> wait or not, but in the end, they're broken and there's not much you can
> do.
>

​Well, you can ignore some errors -- time sync problems might resolve themselves, eg. And the "error" might be because of an error on your side (your admin ran the wrong date command, there's a bug in your code), that might well be fixable. I guess what I'm thinking is that closing a channel is a real cost; if they're not actively cheating -- by not completing an HTLC update once they agree to it eg -- I'd probably rather it stay open (in degraded mode perhaps) than automatically close.

Quoted text (6 lines)
> I also wonder if
> >   A: TIMEDOUT_HTLC -> B: DECLINE (err_time_sync_lost)
> > might be useful.
> I don't think it's useful, though if you disagree on time you might get
> an error packet.  (What else can you do?)
>

​How is "an error packet" different to a decline packet or a channel close?​

Quoted text (3 lines)
Perhaps we should add a current time field to UPDATE_ADD_HTLC so you can
> defuse clock sync problems earlier?
>

Or maybe time sync is part of the p2p protocol?​ I guess channel counterparties are always peers?

Quoted text (6 lines)
> ​Sorry, I was assuming that one or both implementations were buggy. I
> meant
> > to make that explicit.​ You're talking with strangers on the network, so
> > you can't assume their software is bug free, right?
> That applies to any scheme you come up with, though.  Propose something
> simpler, and I'll gladly rewrite.

​ ​ ​I'll have another look when I can see current code :)​ ​

​Cheers, aj​

Rusty Russell in reply to Anthony Towns permalink original

Anthony Towns <aj at erisian.com.au> writes:

Quoted text (14 lines)
> On 1 September 2015 at 17:32, Rusty Russell <rusty at rustcorp.com.au> wrote:
>
>> Looking at that git tree (I've done some work since then)...
>
> ​Oh, github hasn't been updated since Aug 20? That's forever ago!​
>
>
>> Ah, you
>> can't send a command while processing an existing command.  Seems a
>> logical simplification.
>>
>
> I'm thinking of CMD_CLOSE like "Ctrl-C"; why wouldn't you want it to work
> anytime?​

Yes, I think you do. And the other side can CMD_CLOSE during an HTLC negotiation (for example), so this should be allowed.

I will update it to allow CMD_CLOSE at any time (except when already closing).

Quoted text (6 lines)
>> >> This makes the constraints clearer, eg. you can't DECLINE_HTLC anything
>> >> but an ADD_HTLC.
>> > ​Your states currently allow declining those though:
>> Not at all (or if it does, it's a bug).
>
> ​Right, it's a bug as of ​52cda01 then :)

Weird, my tester should have found that (every tested-for input must be given).

Ah, yes, it did, once I enhanced the tester to actually track htlcs. And I've fixed it :)

(Those should be going to STATE_WAIT_FOR_UPDATE_ACCEPT).

Quoted text (13 lines)
>> The simplest (and safest) system is always to error and close when they
>> break protocol.  There's some game theory involved in whether you should
>> wait or not, but in the end, they're broken and there's not much you can
>> do.
>>
>
> ​Well, you can ignore some errors -- time sync problems might resolve
> themselves, eg. And the "error" might be because of an error on your side
> (your admin ran the wrong date command, there's a bug in your code), that
> might well be fixable. I guess what I'm thinking is that closing a channel
> is a real cost; if they're not actively cheating -- by not completing an
> HTLC update once they agree to it eg -- I'd probably rather it stay open
> (in degraded mode perhaps) than automatically close.

We could add such things later, but it's really hard to know in advance what bugs will lose money and which can be ignored.

Quoted text (8 lines)
>> I also wonder if
>> >   A: TIMEDOUT_HTLC -> B: DECLINE (err_time_sync_lost)
>> > might be useful.
>> I don't think it's useful, though if you disagree on time you might get
>> an error packet.  (What else can you do?)
>>
>
> ​How is "an error packet" different to a decline packet or a channel close?​

It carries an ascii message giving you a clue :)

Quoted text (6 lines)
> Perhaps we should add a current time field to UPDATE_ADD_HTLC so you can
>> defuse clock sync problems earlier?
>>
>
> Or maybe time sync is part of the p2p protocol?​ I guess channel
> counterparties are always peers?

Yes, that makes sense.

Quoted text (8 lines)
>> ​Sorry, I was assuming that one or both implementations were buggy. I
>> meant
>> > to make that explicit.​ You're talking with strangers on the network, so
>> > you can't assume their software is bug free, right?
>> That applies to any scheme you come up with, though.  Propose something
>> simpler, and I'll gladly rewrite.
> ​
> ​I'll have another look when I can see current code :)​

NW, just running the now-glacial state tester. I'm sure there's only 1 bug left!

Cheers, Rusty.

Jasper Hugunin in reply to Rusty Russell permalink original

I had an idea regarding an alternative to the high/low priority. My understanding is that the problem is this case: Alice sends PKT_UPDATE and transitions to WAIT_FOR_UPDATE_ACCEPT. Bob sends PKT_UPDATE, and also transitions to WAIT_FOR_UPDATE_ACCEPT. Then Bob receives Alice's PKT_UPDATE. Since he is waiting for his own update, he doesn't know which *single* update is the official one. Same for Alice.

What if Bob and Alice agree on a new update that combines both updates. The protocol can only handle one update at a time, but the update doesn't have to happen one-way. This would be similar to TCP Simultaneous Open/Close. So Alice and Bob would exchange PKT_SIMULTANEOUS_ACCEPT containing signatures, and then exchange PKT_SIMULTANEOUS_COMPLETE containing revoking pre-images. Continuing my example above, Bob would receive PKT_UPDATE, send PKT_SIMULTANEOUS_ACCEPT and transition to WAIT_FOR_SIMULTANEOUS_ACCEPT. Alice would receive Bob's PKT_UPDATE, send PKT_SIMULTANEOUS_ACCEPT and transition to WAIT_FOR_SIMULTANEOUS_ACCEPT. Bob would recieve Alice's PKT_SIMULTANEOUS_ACCEPT, send PKT_SIMULTANEOUS_COMPLETE, and transition to NORMAL. Same for Alice.

I think that avoids either deadlock or livelock, and does not require duplicating states for priority.

- Jasper

On Tue, Sep 1, 2015 at 10:55 PM, Rusty Russell <rusty at rustcorp.com.au> wrote:

Quoted text (98 lines)
> Anthony Towns <aj at erisian.com.au> writes:
> > On 1 September 2015 at 17:32, Rusty Russell <rusty at rustcorp.com.au>
> wrote:
> >
> >> Looking at that git tree (I've done some work since then)...
> >
> > ​Oh, github hasn't been updated since Aug 20? That's forever ago!​
> >
> >
> >> Ah, you
> >> can't send a command while processing an existing command.  Seems a
> >> logical simplification.
> >>
> >
> > I'm thinking of CMD_CLOSE like "Ctrl-C"; why wouldn't you want it to work
> > anytime?​
>
> Yes, I think you do.  And the other side can CMD_CLOSE during an HTLC
> negotiation (for example), so this should be allowed.
>
> I will update it to allow CMD_CLOSE at any time (except when already
> closing).
>
> >> >> This makes the constraints clearer, eg. you can't DECLINE_HTLC
> anything
> >> >> but an ADD_HTLC.
> >> > ​Your states currently allow declining those though:
> >> Not at all (or if it does, it's a bug).
> >
> > ​Right, it's a bug as of ​52cda01 then :)
>
> Weird, my tester should have found that (every tested-for input must be
> given).
>
> Ah, yes, it did, once I enhanced the tester to actually track htlcs.
> And I've fixed it :)
>
> (Those should be going to STATE_WAIT_FOR_UPDATE_ACCEPT).
>
> >> The simplest (and safest) system is always to error and close when they
> >> break protocol.  There's some game theory involved in whether you should
> >> wait or not, but in the end, they're broken and there's not much you can
> >> do.
> >>
> >
> > ​Well, you can ignore some errors -- time sync problems might resolve
> > themselves, eg. And the "error" might be because of an error on your side
> > (your admin ran the wrong date command, there's a bug in your code), that
> > might well be fixable. I guess what I'm thinking is that closing a
> channel
> > is a real cost; if they're not actively cheating -- by not completing an
> > HTLC update once they agree to it eg -- I'd probably rather it stay open
> > (in degraded mode perhaps) than automatically close.
>
> We could add such things later, but it's really hard to know in advance
> what bugs will lose money and which can be ignored.
>
> >> I also wonder if
> >> >   A: TIMEDOUT_HTLC -> B: DECLINE (err_time_sync_lost)
> >> > might be useful.
> >> I don't think it's useful, though if you disagree on time you might get
> >> an error packet.  (What else can you do?)
> >>
> >
> > ​How is "an error packet" different to a decline packet or a channel
> close?​
>
> It carries an ascii message giving you a clue :)
>
> > Perhaps we should add a current time field to UPDATE_ADD_HTLC so you can
> >> defuse clock sync problems earlier?
> >>
> >
> > Or maybe time sync is part of the p2p protocol?​ I guess channel
> > counterparties are always peers?
>
> Yes, that makes sense.
>
> >> ​Sorry, I was assuming that one or both implementations were buggy. I
> >> meant
> >> > to make that explicit.​ You're talking with strangers on the network,
> so
> >> > you can't assume their software is bug free, right?
> >> That applies to any scheme you come up with, though.  Propose something
> >> simpler, and I'll gladly rewrite.
> > ​
> > ​I'll have another look when I can see current code :)​
>
> NW, just running the now-glacial state tester.  I'm sure there's only 1
> bug left!
>
> Cheers,
> Rusty.
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
>
Rusty Russell in reply to Jasper Hugunin permalink original

Jasper Hugunin <jasper at hashplex.com> writes:

Quoted text (11 lines)
> I had an idea regarding an alternative to the high/low priority. My
> understanding is that the problem is this case:
> Alice sends PKT_UPDATE and transitions to WAIT_FOR_UPDATE_ACCEPT.
> Bob sends PKT_UPDATE, and also transitions to WAIT_FOR_UPDATE_ACCEPT.
> Then Bob receives Alice's PKT_UPDATE. Since he is waiting for his own
> update, he doesn't know which *single* update is the official one. Same for
> Alice.
>
> What if Bob and Alice agree on a new update that combines both updates. The
> protocol can only handle one update at a time, but the update doesn't have
> to happen one-way.

We could, but it makes things more complex.

We might eventually want to batch updates for performance (which we certainly can do), which would also solve this problem.

The duplicated state is really an artifact of the artificial and exhaustive way I coded state.c. I did that because I wanted as much state to be expressed in the "enum state" for simpler testing.

In practice, it's a one-liner.

Cheers, Rusty.

CJP in reply to Rusty Russell permalink original

On returning home from the Scaling Bitcoin workshop, I thought it was great meeting some people in real life and discovering we basically all want to achieve the same things. Still, I think there are some differences in how we think certain problems should be addressed.

Design decisions in Lightning don't suffer from the same "consensus" necessity as the block validity rules in Bitcoin, but still, some decisions carry a strong network effect, and might be hard to reverse once made. The people I talked to felt the need to make no mistakes on these design decisions, but at the same time I feel the need to make them ASAP, so we can quickly launch a prototype and convince the world of how great this is.

Because of this, I'd like to have a well-organized process of collecting and documenting design ideas and trade-off argumentation, and hopefully reach some consensus on what are good and bad ideas. This should also serve to increase interoperability between the several pieces of software that are being developed. Some of my ideas on how to shape this:

We could have a Git repository, containing all concepts and standards. New ideas, amendments & so on can be exchanged like pull requests. We may / may not also need a process to make actual design decisions, but assigning one Git clone to be the "true" one might be too simplistic (I don't want to have a dictator). As long as the Lightning dev community is small, a "100% consensus" rule might still be workable though.

I'd like to make a distinction between concepts and standards. The idea is that a concept is a non-trivial design decision; typically people can have strong preference between different alternative, mutually incompatible concepts. Standards are based on concepts, and fill in all the trivial design decisions that have to be made to make different pieces of software inter-operable. For concept choices, I made the following template:

https://gist.github.com/anonymous/51631fe56f1f83e4af0a

What do you think?

CJP

PS. Rusty suggested something like an RFC-like process for Lightning. I'm not an expert on RFCs, and my proposal is probably a bit different from the RFC process. I'm open to other ideas and I'd like to be informed about why we should use an RFC-like process or something else.

Pierre in reply to CJP permalink original

Hi CJP,

Quoted text (5 lines)
> [...] I'd like to have a well-organized process of collecting
> and documenting design ideas and trade-off argumentation[...]. This should [...]
> serve to increase interoperability between the several pieces of
> software that are being developed.

> What do you think?

Having never contributed to any significant open-source project so far (only closed-source dictatorships), I have no idea what would be best, but I just wanted to say that I share your concerns.

Also, I am currently using Rusty's repo at ElementsProject as the reference implementation and it is perfectly fine, but I really hope we won't end up "Bitcoin style", with specification=implementation !

Cheers,

Pierre

Rusty Russell in reply to CJP permalink original

CJP <cjp at ultimatestunts.nl> writes:

Quoted text (4 lines)
> On returning home from the Scaling Bitcoin workshop, I thought it was
> great meeting some people in real life and discovering we basically all
> want to achieve the same things. Still, I think there are some
> differences in how we think certain problems should be addressed.

Indeed. And it was great to meet you, too!

Quoted text (7 lines)
> Design decisions in Lightning don't suffer from the same "consensus"
> necessity as the block validity rules in Bitcoin, but still, some
> decisions carry a strong network effect, and might be hard to reverse
> once made. The people I talked to felt the need to make no mistakes on
> these design decisions, but at the same time I feel the need to make
> them ASAP, so we can quickly launch a prototype and convince the world
> of how great this is.

Yeah. I feel torn between the desire to code, and the desire to write an RFC so we can argue properly.

Quoted text (6 lines)
> Because of this, I'd like to have a well-organized process of collecting
> and documenting design ideas and trade-off argumentation, and hopefully
> reach some consensus on what are good and bad ideas. This should also
> serve to increase interoperability between the several pieces of
> software that are being developed. Some of my ideas on how to shape
> this:

Yes, it would also help newcomers get up to speed with ideas we've already thrown around on-list. Preferably avoid the bitcoin problem where you need to ask gmaxwell for a reference to an ancient bitcointalk post where a solution was previously presented.

Quoted text (6 lines)
> We could have a Git repository, containing all concepts and standards.
> New ideas, amendments & so on can be exchanged like pull requests. We
> may / may not also need a process to make actual design decisions, but
> assigning one Git clone to be the "true" one might be too simplistic (I
> don't want to have a dictator). As long as the Lightning dev community
> is small, a "100% consensus" rule might still be workable though.

A wiki? This seems closer to what we want, at least for now.

Quoted text (11 lines)
> I'd like to make a distinction between concepts and standards. The idea
> is that a concept is a non-trivial design decision; typically people can
> have strong preference between different alternative, mutually
> incompatible concepts. Standards are based on concepts, and fill in all
> the trivial design decisions that have to be made to make different
> pieces of software inter-operable. For concept choices, I made the
> following template:
>
> https://gist.github.com/anonymous/51631fe56f1f83e4af0a
>
> What do you think?

I love separating concepts and standards. There are plenty of interesting concepts which may eventually percolate into standards once we get some kind of consensus; and I don't want to lose track of them all because they're not an immediate priority.

Quoted text (4 lines)
> PS. Rusty suggested something like an RFC-like process for Lightning.
> I'm not an expert on RFCs, and my proposal is probably a bit different
> from the RFC process. I'm open to other ideas and I'd like to be
> informed about why we should use an RFC-like process or something else.

RFCs are good for when you want to nail down proposal details. You can quote them in emails, code comments, and ideally implement the protocol off them. For example, an RFC describing the exact transactions used in lightning, or one describing the inter-node protocol. New ones appear over time as the protocols get extended. They're best when you have at least two independent implementations to make sure they're sane.

They're not so good with concepts, or brainstorming future stuff. But that stuff, too, is vital.

Cheers, Rusty.

Rusty Russell in reply to Pierre permalink original

Pierre <pm+lists at acinq.fr> writes:

Quoted text (15 lines)
> Hi CJP,
>
>> [...] I'd like to have a well-organized process of collecting
>> and documenting design ideas and trade-off argumentation[...]. This should [...]
>> serve to increase interoperability between the several pieces of
>> software that are being developed.
>
>> What do you think?
> Having never contributed to any significant open-source project so far
> (only closed-source dictatorships), I have no idea what would be best,
> but I just wanted to say that I share your concerns.
>
> Also, I am currently using Rusty's repo at ElementsProject as the
> reference implementation and it is perfectly fine, but I really hope
> we won't end up "Bitcoin style", with specification=implementation !

Yes, that would mean I've failed to do decent design :(

I always make sure that my code contains subtle flaws, to encourage alternate implementations. In fact, I've never produced bug-free code.

Cheers, Rusty.