daemon: batching of changes as per BOLT #2.

We now keep a list of commitment transaction states for "us" and
"them", as well as a "struct channel_state" for staged changes.

We manipulate these structures as we send out packets, receive
packets, or receive acknowledgement of packets.  In particular, we
update the other nodes' staging_cstate as we send out our requests,
and update our own staging_cstate are we receive acks.  When we
receive a request, we update both (as we immediately send out our
ack).

The RPC output is changed; rather than expose the complexity, we
expose our last committed state: what would happen if we have to drop
to the blockchain now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2016-03-31 17:13:20 +10:30
parent 017d199305
commit 5e7b3d02a1
12 changed files with 1318 additions and 1415 deletions

View File

@@ -25,13 +25,13 @@ typedef struct _OpenChannel OpenChannel;
typedef struct _OpenAnchor OpenAnchor;
typedef struct _OpenCommitSig OpenCommitSig;
typedef struct _OpenComplete OpenComplete;
typedef struct _Routing Routing;
typedef struct _UpdateAddHtlc UpdateAddHtlc;
typedef struct _UpdateDeclineHtlc UpdateDeclineHtlc;
typedef struct _UpdateFulfillHtlc UpdateFulfillHtlc;
typedef struct _FailReason FailReason;
typedef struct _UpdateFailHtlc UpdateFailHtlc;
typedef struct _UpdateAccept UpdateAccept;
typedef struct _UpdateSignature UpdateSignature;
typedef struct _UpdateComplete UpdateComplete;
typedef struct _UpdateCommit UpdateCommit;
typedef struct _UpdateRevocation UpdateRevocation;
typedef struct _CloseClearing CloseClearing;
typedef struct _CloseSignature CloseSignature;
typedef struct _Error Error;
@@ -267,6 +267,19 @@ struct _OpenComplete
, NULL }
/*
* FIXME: Routing information.
*/
struct _Routing
{
ProtobufCMessage base;
ProtobufCBinaryData info;
};
#define ROUTING__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&routing__descriptor) \
, {0,NULL} }
/*
* Start a new commitment tx to add an HTLC me -> you.
*/
@@ -274,9 +287,9 @@ struct _UpdateAddHtlc
{
ProtobufCMessage base;
/*
* Hash for which I will supply preimage to revoke this commitment tx.
* Unique identifier for this HTLC.
*/
Sha256Hash *revocation_hash;
uint64_t id;
/*
* Amount for htlc (millisatoshi)
*/
@@ -288,37 +301,15 @@ struct _UpdateAddHtlc
/*
* Time at which HTLC expires (absolute)
*/
/*
* FIXME: Routing information.
*/
Locktime *expiry;
/*
* Onion-wrapped routing information.
*/
Routing *route;
};
#define UPDATE_ADD_HTLC__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&update_add_htlc__descriptor) \
, NULL, 0, NULL, NULL }
typedef enum {
UPDATE_DECLINE_HTLC__REASON__NOT_SET = 0,
UPDATE_DECLINE_HTLC__REASON_INSUFFICIENT_FUNDS = 1,
UPDATE_DECLINE_HTLC__REASON_CANNOT_ROUTE = 2,
} UpdateDeclineHtlc__ReasonCase;
/*
* We can't do this HTLC, sorry (instead of update_accept)
*/
struct _UpdateDeclineHtlc
{
ProtobufCMessage base;
UpdateDeclineHtlc__ReasonCase reason_case;
union {
Funding *insufficient_funds;
protobuf_c_boolean cannot_route;
};
};
#define UPDATE_DECLINE_HTLC__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&update_decline_htlc__descriptor) \
, UPDATE_DECLINE_HTLC__REASON__NOT_SET, {} }
, 0, 0, NULL, NULL, NULL }
/*
@@ -328,9 +319,9 @@ struct _UpdateFulfillHtlc
{
ProtobufCMessage base;
/*
* Hash for which I will supply preimage to revoke this commitment tx.
* Which HTLC
*/
Sha256Hash *revocation_hash;
uint64_t id;
/*
* HTLC R value.
*/
@@ -338,83 +329,73 @@ struct _UpdateFulfillHtlc
};
#define UPDATE_FULFILL_HTLC__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&update_fulfill_htlc__descriptor) \
, NULL, NULL }
, 0, NULL }
/*
* Remove your HTLC: routing has failed upstream, we didn't like it, or timeout.
* FIXME: Failure information.
*/
struct _FailReason
{
ProtobufCMessage base;
ProtobufCBinaryData info;
};
#define FAIL_REASON__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&fail_reason__descriptor) \
, {0,NULL} }
struct _UpdateFailHtlc
{
ProtobufCMessage base;
/*
* Hash for which I will supply preimage to revoke this commitment tx.
* Which HTLC
*/
Sha256Hash *revocation_hash;
uint64_t id;
/*
* Hash for HTLC R value.
* Reason for failure (for relay to initial node)
*/
Sha256Hash *r_hash;
FailReason *reason;
};
#define UPDATE_FAIL_HTLC__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&update_fail_htlc__descriptor) \
, NULL, NULL }
, 0, NULL }
/*
* OK, I accept that update; here's your signature.
* Commit all the staged HTLCs.
*/
struct _UpdateAccept
struct _UpdateCommit
{
ProtobufCMessage base;
/*
* Signature for your new commitment tx.
*/
Signature *sig;
/*
* Hash for which I will supply preimage to revoke this new commit tx.
*/
Sha256Hash *revocation_hash;
};
#define UPDATE_ACCEPT__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&update_accept__descriptor) \
, NULL, NULL }
/*
* Thanks for accepting, here's my last bit.
*/
struct _UpdateSignature
{
ProtobufCMessage base;
/*
* Signature for your new commitment tx.
*/
Signature *sig;
/*
* Hash preimage which revokes old commitment tx.
*/
Sha256Hash *revocation_preimage;
};
#define UPDATE_SIGNATURE__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&update_signature__descriptor) \
, NULL, NULL }
#define UPDATE_COMMIT__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&update_commit__descriptor) \
, NULL }
/*
* Complete the update.
*/
struct _UpdateComplete
struct _UpdateRevocation
{
ProtobufCMessage base;
/*
* Hash preimage which revokes old commitment tx.
*/
Sha256Hash *revocation_preimage;
/*
* Revocation hash for my next commit transaction
*/
Sha256Hash *next_revocation_hash;
};
#define UPDATE_COMPLETE__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&update_complete__descriptor) \
, NULL }
#define UPDATE_REVOCATION__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&update_revocation__descriptor) \
, NULL, NULL }
/*
@@ -467,12 +448,10 @@ typedef enum {
PKT__PKT_OPEN_COMMIT_SIG = 22,
PKT__PKT_OPEN_COMPLETE = 23,
PKT__PKT_UPDATE_ADD_HTLC = 2,
PKT__PKT_UPDATE_ACCEPT = 3,
PKT__PKT_UPDATE_SIGNATURE = 4,
PKT__PKT_UPDATE_COMPLETE = 5,
PKT__PKT_UPDATE_DECLINE_HTLC = 6,
PKT__PKT_UPDATE_FULFILL_HTLC = 7,
PKT__PKT_UPDATE_FAIL_HTLC = 9,
PKT__PKT_UPDATE_FULFILL_HTLC = 3,
PKT__PKT_UPDATE_FAIL_HTLC = 4,
PKT__PKT_UPDATE_COMMIT = 5,
PKT__PKT_UPDATE_REVOCATION = 6,
PKT__PKT_CLOSE_CLEARING = 30,
PKT__PKT_CLOSE_SIGNATURE = 31,
PKT__PKT_ERROR = 40,
@@ -501,12 +480,10 @@ struct _Pkt
* Updating (most common)
*/
UpdateAddHtlc *update_add_htlc;
UpdateAccept *update_accept;
UpdateSignature *update_signature;
UpdateComplete *update_complete;
UpdateDeclineHtlc *update_decline_htlc;
UpdateFulfillHtlc *update_fulfill_htlc;
UpdateFailHtlc *update_fail_htlc;
UpdateCommit *update_commit;
UpdateRevocation *update_revocation;
/*
* Closing
*/
@@ -713,6 +690,25 @@ OpenComplete *
void open_complete__free_unpacked
(OpenComplete *message,
ProtobufCAllocator *allocator);
/* Routing methods */
void routing__init
(Routing *message);
size_t routing__get_packed_size
(const Routing *message);
size_t routing__pack
(const Routing *message,
uint8_t *out);
size_t routing__pack_to_buffer
(const Routing *message,
ProtobufCBuffer *buffer);
Routing *
routing__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data);
void routing__free_unpacked
(Routing *message,
ProtobufCAllocator *allocator);
/* UpdateAddHtlc methods */
void update_add_htlc__init
(UpdateAddHtlc *message);
@@ -732,25 +728,6 @@ UpdateAddHtlc *
void update_add_htlc__free_unpacked
(UpdateAddHtlc *message,
ProtobufCAllocator *allocator);
/* UpdateDeclineHtlc methods */
void update_decline_htlc__init
(UpdateDeclineHtlc *message);
size_t update_decline_htlc__get_packed_size
(const UpdateDeclineHtlc *message);
size_t update_decline_htlc__pack
(const UpdateDeclineHtlc *message,
uint8_t *out);
size_t update_decline_htlc__pack_to_buffer
(const UpdateDeclineHtlc *message,
ProtobufCBuffer *buffer);
UpdateDeclineHtlc *
update_decline_htlc__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data);
void update_decline_htlc__free_unpacked
(UpdateDeclineHtlc *message,
ProtobufCAllocator *allocator);
/* UpdateFulfillHtlc methods */
void update_fulfill_htlc__init
(UpdateFulfillHtlc *message);
@@ -770,6 +747,25 @@ UpdateFulfillHtlc *
void update_fulfill_htlc__free_unpacked
(UpdateFulfillHtlc *message,
ProtobufCAllocator *allocator);
/* FailReason methods */
void fail_reason__init
(FailReason *message);
size_t fail_reason__get_packed_size
(const FailReason *message);
size_t fail_reason__pack
(const FailReason *message,
uint8_t *out);
size_t fail_reason__pack_to_buffer
(const FailReason *message,
ProtobufCBuffer *buffer);
FailReason *
fail_reason__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data);
void fail_reason__free_unpacked
(FailReason *message,
ProtobufCAllocator *allocator);
/* UpdateFailHtlc methods */
void update_fail_htlc__init
(UpdateFailHtlc *message);
@@ -789,62 +785,43 @@ UpdateFailHtlc *
void update_fail_htlc__free_unpacked
(UpdateFailHtlc *message,
ProtobufCAllocator *allocator);
/* UpdateAccept methods */
void update_accept__init
(UpdateAccept *message);
size_t update_accept__get_packed_size
(const UpdateAccept *message);
size_t update_accept__pack
(const UpdateAccept *message,
/* UpdateCommit methods */
void update_commit__init
(UpdateCommit *message);
size_t update_commit__get_packed_size
(const UpdateCommit *message);
size_t update_commit__pack
(const UpdateCommit *message,
uint8_t *out);
size_t update_accept__pack_to_buffer
(const UpdateAccept *message,
size_t update_commit__pack_to_buffer
(const UpdateCommit *message,
ProtobufCBuffer *buffer);
UpdateAccept *
update_accept__unpack
UpdateCommit *
update_commit__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data);
void update_accept__free_unpacked
(UpdateAccept *message,
void update_commit__free_unpacked
(UpdateCommit *message,
ProtobufCAllocator *allocator);
/* UpdateSignature methods */
void update_signature__init
(UpdateSignature *message);
size_t update_signature__get_packed_size
(const UpdateSignature *message);
size_t update_signature__pack
(const UpdateSignature *message,
/* UpdateRevocation methods */
void update_revocation__init
(UpdateRevocation *message);
size_t update_revocation__get_packed_size
(const UpdateRevocation *message);
size_t update_revocation__pack
(const UpdateRevocation *message,
uint8_t *out);
size_t update_signature__pack_to_buffer
(const UpdateSignature *message,
size_t update_revocation__pack_to_buffer
(const UpdateRevocation *message,
ProtobufCBuffer *buffer);
UpdateSignature *
update_signature__unpack
UpdateRevocation *
update_revocation__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data);
void update_signature__free_unpacked
(UpdateSignature *message,
ProtobufCAllocator *allocator);
/* UpdateComplete methods */
void update_complete__init
(UpdateComplete *message);
size_t update_complete__get_packed_size
(const UpdateComplete *message);
size_t update_complete__pack
(const UpdateComplete *message,
uint8_t *out);
size_t update_complete__pack_to_buffer
(const UpdateComplete *message,
ProtobufCBuffer *buffer);
UpdateComplete *
update_complete__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data);
void update_complete__free_unpacked
(UpdateComplete *message,
void update_revocation__free_unpacked
(UpdateRevocation *message,
ProtobufCAllocator *allocator);
/* CloseClearing methods */
void close_clearing__init
@@ -954,26 +931,26 @@ typedef void (*OpenCommitSig_Closure)
typedef void (*OpenComplete_Closure)
(const OpenComplete *message,
void *closure_data);
typedef void (*Routing_Closure)
(const Routing *message,
void *closure_data);
typedef void (*UpdateAddHtlc_Closure)
(const UpdateAddHtlc *message,
void *closure_data);
typedef void (*UpdateDeclineHtlc_Closure)
(const UpdateDeclineHtlc *message,
void *closure_data);
typedef void (*UpdateFulfillHtlc_Closure)
(const UpdateFulfillHtlc *message,
void *closure_data);
typedef void (*FailReason_Closure)
(const FailReason *message,
void *closure_data);
typedef void (*UpdateFailHtlc_Closure)
(const UpdateFailHtlc *message,
void *closure_data);
typedef void (*UpdateAccept_Closure)
(const UpdateAccept *message,
typedef void (*UpdateCommit_Closure)
(const UpdateCommit *message,
void *closure_data);
typedef void (*UpdateSignature_Closure)
(const UpdateSignature *message,
void *closure_data);
typedef void (*UpdateComplete_Closure)
(const UpdateComplete *message,
typedef void (*UpdateRevocation_Closure)
(const UpdateRevocation *message,
void *closure_data);
typedef void (*CloseClearing_Closure)
(const CloseClearing *message,
@@ -1004,13 +981,13 @@ extern const ProtobufCEnumDescriptor open_channel__anchor_offer__descriptor;
extern const ProtobufCMessageDescriptor open_anchor__descriptor;
extern const ProtobufCMessageDescriptor open_commit_sig__descriptor;
extern const ProtobufCMessageDescriptor open_complete__descriptor;
extern const ProtobufCMessageDescriptor routing__descriptor;
extern const ProtobufCMessageDescriptor update_add_htlc__descriptor;
extern const ProtobufCMessageDescriptor update_decline_htlc__descriptor;
extern const ProtobufCMessageDescriptor update_fulfill_htlc__descriptor;
extern const ProtobufCMessageDescriptor fail_reason__descriptor;
extern const ProtobufCMessageDescriptor update_fail_htlc__descriptor;
extern const ProtobufCMessageDescriptor update_accept__descriptor;
extern const ProtobufCMessageDescriptor update_signature__descriptor;
extern const ProtobufCMessageDescriptor update_complete__descriptor;
extern const ProtobufCMessageDescriptor update_commit__descriptor;
extern const ProtobufCMessageDescriptor update_revocation__descriptor;
extern const ProtobufCMessageDescriptor close_clearing__descriptor;
extern const ProtobufCMessageDescriptor close_signature__descriptor;
extern const ProtobufCMessageDescriptor error__descriptor;