@@ -435,6 +435,9 @@ static void load_peer_commit_info(struct peer *peer)
435
435
fatal ("load_peer_commit_info:no remote commit info found" );
436
436
}
437
437
438
+ /* Because their HTLCs are not ordered wrt to ours, we can go negative
439
+ * and do normally-impossible things in intermediate states. So we
440
+ * mangle cstate balances manually. */
438
441
static void apply_htlc (struct channel_state * cstate , const struct htlc * htlc ,
439
442
enum htlc_side side )
440
443
{
@@ -444,39 +447,24 @@ static void apply_htlc(struct channel_state *cstate, const struct htlc *htlc,
444
447
return ;
445
448
446
449
log_debug (htlc -> peer -> log , " %s committed" , sidestr );
447
- if (!cstate_add_htlc (cstate , htlc ))
448
- fatal ("load_peer_htlcs:can't add %s HTLC" , sidestr );
450
+ force_add_htlc (cstate , htlc );
449
451
450
452
if (!htlc_has (htlc , HTLC_FLAG (side , HTLC_F_COMMITTED ))) {
451
453
log_debug (htlc -> peer -> log , " %s %s" ,
452
454
sidestr , htlc -> r ? "resolved" : "failed" );
453
455
if (htlc -> r )
454
- cstate_fulfill_htlc (cstate , htlc );
456
+ force_fulfill_htlc (cstate , htlc );
455
457
else
456
- cstate_fail_htlc (cstate , htlc );
458
+ force_fail_htlc (cstate , htlc );
457
459
}
458
460
}
459
461
460
- static void apply_feechange (struct channel_state * cstate ,
461
- const struct feechange * f ,
462
- enum htlc_side side )
463
- {
464
- /* We only ever apply feechanges to the owner. */
465
- if (feechange_side (f -> state ) != side )
466
- return ;
467
-
468
- if (!feechange_has (f , HTLC_FLAG (side ,HTLC_F_WAS_COMMITTED )))
469
- return ;
470
-
471
- adjust_fee (cstate , f -> fee_rate );
472
- }
473
-
474
462
/* As we load the HTLCs, we apply them to get the final channel_state.
475
463
* We also get the last used htlc id.
476
464
* This is slow, but sure. */
477
465
static void load_peer_htlcs (struct peer * peer )
478
466
{
479
- int err , i ;
467
+ int err ;
480
468
sqlite3_stmt * stmt ;
481
469
sqlite3 * sql = peer -> dstate -> db -> sql ;
482
470
char * ctx = tal (peer , char );
@@ -518,7 +506,7 @@ static void load_peer_htlcs(struct peer *peer)
518
506
if (sqlite3_column_count (stmt ) != 10 )
519
507
fatal ("load_peer_htlcs:step gave %i cols, not 10" ,
520
508
sqlite3_column_count (stmt ));
521
- /* CREATE TABLE htlcs (peer "SQL_PUBKEY", id INT, state TEXT, msatoshis INT, expiry INT, rhash "SQL_RHASH", r "SQL_R", routing "SQL_ROUTING", src_peer "SQL_PUBKEY", src_id INT, PRIMARY KEY(peer, id)); */
509
+ /* CREATE TABLE htlcs (peer "SQL_PUBKEY", id INT, state TEXT, msatoshis INT, expiry INT, rhash "SQL_RHASH", r "SQL_R", routing "SQL_ROUTING", src_peer "SQL_PUBKEY", src_id INT, PRIMARY KEY(peer, id, state )); */
522
510
sha256_from_sql (stmt , 5 , & rhash );
523
511
524
512
hstate = htlc_state_from_name (sqlite3_column_str (stmt , 2 ));
@@ -596,16 +584,9 @@ static void load_peer_htlcs(struct peer *peer)
596
584
fatal ("load_peer_htlcs:finalize gave %s:%s" ,
597
585
sqlite3_errstr (err ), sqlite3_errmsg (sql ));
598
586
599
- /* Apply feechanges from oldest to newest (newest counts). */
600
- for (i = FEECHANGE_STATE_INVALID - 1 ; i >= SENT_FEECHANGE ; i -- ) {
601
- if (!peer -> feechanges [i ])
602
- continue ;
603
-
604
- apply_feechange (peer -> local .commit -> cstate ,
605
- peer -> feechanges [i ], LOCAL );
606
- apply_feechange (peer -> remote .commit -> cstate ,
607
- peer -> feechanges [i ], REMOTE );
608
- }
587
+ if (!balance_after_force (peer -> local .commit -> cstate )
588
+ || !balance_after_force (peer -> remote .commit -> cstate ))
589
+ fatal ("load_peer_htlcs:channel didn't balance" );
609
590
610
591
/* Update commit->tx and commit->map */
611
592
peer -> local .commit -> tx = create_commit_tx (peer -> local .commit ,
@@ -1041,7 +1022,8 @@ void db_init(struct lightningd_state *dstate)
1041
1022
errmsg = db_exec (dstate , dstate ,
1042
1023
"CREATE TABLE wallet (privkey " SQL_PRIVKEY ");"
1043
1024
"CREATE TABLE anchors (peer " SQL_PUBKEY ", txid " SQL_TXID ", idx INT, amount INT, ok_depth INT, min_depth INT, bool ours, PRIMARY KEY(peer));"
1044
- "CREATE TABLE htlcs (peer " SQL_PUBKEY ", id INT, state TEXT, msatoshis INT, expiry INT, rhash " SQL_RHASH ", r " SQL_R ", routing " SQL_ROUTING ", src_peer " SQL_PUBKEY ", src_id INT, PRIMARY KEY(peer, id));"
1025
+ /* FIXME: state in primary key is overkill: just need side */
1026
+ "CREATE TABLE htlcs (peer " SQL_PUBKEY ", id INT, state TEXT, msatoshis INT, expiry INT, rhash " SQL_RHASH ", r " SQL_R ", routing " SQL_ROUTING ", src_peer " SQL_PUBKEY ", src_id INT, PRIMARY KEY(peer, id, state));"
1045
1027
"CREATE TABLE feechanges (peer " SQL_PUBKEY ", state TEXT, fee_rate INT, PRIMARY KEY(peer,state));"
1046
1028
"CREATE TABLE commit_info (peer " SQL_PUBKEY ", side TEXT, commit_num INT, revocation_hash " SQL_SHA256 ", xmit_order INT, sig " SQL_SIGNATURE ", prev_revocation_hash " SQL_SHA256 ", PRIMARY KEY(peer, side));"
1047
1029
"CREATE TABLE shachain (peer " SQL_PUBKEY ", shachain BINARY(%zu), PRIMARY KEY(peer));"
0 commit comments