From 8f38a4658463f4bb3fbb0c61fbab435e08b3fb74 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 31 Jul 2018 14:04:42 +0930 Subject: [PATCH] lightningd: correctly store our own channel_reserve_satoshis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit openingd calculates our reserve based on the channel amount (even if we're funding, to keep the calculation in one place), but it wasn't reporting it back to the master daemon. We initialized it to 0 so that valgrind wouldn't get upset, as it's part of a structure we send over the wire. Have openingd report back, and also initialize it to an impossible value as extra assurance. And remove a stray (harmless but weird) semicolon. Reported-by: Gálli Zoltán Signed-off-by: Rusty Russell --- lightningd/opening_control.c | 10 ++++++---- openingd/opening.c | 6 ++++-- openingd/opening_wire.csv | 2 ++ tests/test_lightningd.py | 5 +++-- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index f06335705..f418f12b5 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -314,7 +314,8 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp, &fc->uc->minimum_depth, &channel_info.remote_fundingkey, &expected_txid, - &feerate)) { + &feerate, + &fc->uc->our_config.channel_reserve_satoshis)) { log_broken(fc->uc->log, "bad OPENING_FUNDER_REPLY %s", tal_hex(resp, resp)); @@ -502,7 +503,8 @@ static void opening_fundee_finished(struct subd *openingd, &push_msat, &channel_flags, &feerate, - &funding_signed)) { + &funding_signed, + &uc->our_config.channel_reserve_satoshis)) { log_broken(uc->log, "bad OPENING_FUNDEE_REPLY %s", tal_hex(reply, reply)); tal_free(uc); @@ -674,8 +676,8 @@ static void channel_config(struct lightningd *ld, ours->max_accepted_htlcs = 483; /* This is filled in by lightning_openingd, for consistency. */ - ours->channel_reserve_satoshis = 0; -}; + ours->channel_reserve_satoshis = -1; +} /* Peer has spontaneously exited from connectd due to open msg. Return * NULL if we took over, otherwise hand back to connectd with this diff --git a/openingd/opening.c b/openingd/opening.c index 491809ad0..499f87b9f 100644 --- a/openingd/opening.c +++ b/openingd/opening.c @@ -544,7 +544,8 @@ static u8 *funder_channel(struct state *state, minimum_depth, &their_funding_pubkey, &state->funding_txid, - state->feerate_per_kw); + state->feerate_per_kw, + state->localconf.channel_reserve_satoshis); } /* This is handed the message the peer sent which caused gossip to stop: @@ -819,7 +820,8 @@ static u8 *fundee_channel(struct state *state, state->push_msat, channel_flags, state->feerate_per_kw, - msg); + msg, + state->localconf.channel_reserve_satoshis); } #ifndef TESTING diff --git a/openingd/opening_wire.csv b/openingd/opening_wire.csv index 210a8c7f7..ba2453567 100644 --- a/openingd/opening_wire.csv +++ b/openingd/opening_wire.csv @@ -44,6 +44,7 @@ opening_funder_reply,,minimum_depth,u32 opening_funder_reply,,remote_fundingkey,struct pubkey opening_funder_reply,,funding_txid,struct bitcoin_txid opening_funder_reply,,feerate_per_kw,u32 +opening_funder_reply,,our_channel_reserve_satoshis,u64 # This means they offer the open (contains their offer packet) opening_fundee,6003 @@ -74,3 +75,4 @@ opening_fundee_reply,,feerate_per_kw,u32 # The (encrypted) funding signed message: send this and we're committed. opening_fundee_reply,,msglen,u16 opening_fundee_reply,,funding_signed_msg,msglen*u8 +opening_fundee_reply,,our_channel_reserve_satoshis,u64 diff --git a/tests/test_lightningd.py b/tests/test_lightningd.py index 706a51ccd..c86209732 100644 --- a/tests/test_lightningd.py +++ b/tests/test_lightningd.py @@ -7,7 +7,6 @@ from utils import wait_for import copy import json import logging -import pytest import queue import os import random @@ -5114,7 +5113,6 @@ class LightningDTests(BaseLightningDTests): # fundee will also forget and disconnect from peer. assert len(l2.rpc.listpeers(l1.info['id'])['peers']) == 0 - @pytest.mark.xfail(strict=True) def test_reserve_enforcement(self): """Channeld should disallow you spending into your reserve""" l1, l2 = self.connect(may_reconnect=True) @@ -5125,6 +5123,9 @@ class LightningDTests(BaseLightningDTests): l2.stop() + # They should both aim for 1%. + assert l2.db_query('SELECT channel_reserve_satoshis FROM channel_configs') == [{'channel_reserve_satoshis': 10**6 // 100}, {'channel_reserve_satoshis': 10**6 // 100}] + # Edit db to reduce reserve to 0 so it will try to violate it. l2.db_query('UPDATE channel_configs SET channel_reserve_satoshis=0', use_copy=False)