struct channel: remove most helpers.

They were not universally used, and most are trivial accessors anyway.

The exception is getting the channel reserve: we have to multiply by 1000
as well as flip direction, so keep that one.

The BOLT quotes move to `struct channel_config`.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-11-21 13:41:03 +10:30
parent 9620393109
commit b5a96765d8
5 changed files with 47 additions and 69 deletions

View File

@ -2340,7 +2340,7 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
case CHANNEL_ERR_HTLC_BELOW_MINIMUM:
failcode = WIRE_AMOUNT_BELOW_MINIMUM;
failmsg = tal_fmt(inmsg, "HTLC too small (%"PRIu64" minimum)",
htlc_minimum_msat(peer->channel, REMOTE));
peer->channel->config[REMOTE]->htlc_minimum_msat);
goto failed;
case CHANNEL_ERR_TOO_MANY_HTLCS:
failcode = WIRE_TEMPORARY_CHANNEL_FAILURE;

View File

@ -202,7 +202,7 @@ static void add_htlcs(struct bitcoin_tx ***txs,
tx = htlc_timeout_tx(*txs, &txid, i,
htlc->msatoshi,
htlc->expiry.locktime,
to_self_delay(channel, side),
channel->config[!side]->to_self_delay,
feerate_per_kw,
keyset);
wscript = bitcoin_wscript_htlc_offer(*wscripts,
@ -213,7 +213,7 @@ static void add_htlcs(struct bitcoin_tx ***txs,
} else {
tx = htlc_success_tx(*txs, &txid, i,
htlc->msatoshi,
to_self_delay(channel, side),
channel->config[!side]->to_self_delay,
feerate_per_kw,
keyset);
wscript = bitcoin_wscript_htlc_receive(*wscripts,
@ -259,10 +259,10 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx,
channel->funding_txout,
channel->funding_msat / 1000,
channel->funder,
to_self_delay(channel, side),
channel->config[!side]->to_self_delay,
&keyset,
channel->view[side].feerate_per_kw,
dust_limit_satoshis(channel, side),
channel->config[side]->dust_limit_satoshis,
channel->view[side].owed_msat[side],
channel->view[side].owed_msat[!side],
committed,
@ -348,7 +348,7 @@ static enum channel_add_err add_htlc(struct channel *channel,
if (htlc->msatoshi == 0) {
return CHANNEL_ERR_HTLC_BELOW_MINIMUM;
}
if (htlc->msatoshi < htlc_minimum_msat(channel, recipient)) {
if (htlc->msatoshi < channel->config[recipient]->htlc_minimum_msat) {
return CHANNEL_ERR_HTLC_BELOW_MINIMUM;
}
@ -373,7 +373,7 @@ static enum channel_add_err add_htlc(struct channel *channel,
*/
if (enforce_aggregate_limits
&& tal_count(committed) - tal_count(removing) + tal_count(adding)
> max_accepted_htlcs(channel, recipient)) {
> channel->config[recipient]->max_accepted_htlcs) {
return CHANNEL_ERR_TOO_MANY_HTLCS;
}
@ -389,7 +389,7 @@ static enum channel_add_err add_htlc(struct channel *channel,
* - SHOULD fail the channel.
*/
if (enforce_aggregate_limits
&& msat_in_htlcs > max_htlc_value_in_flight_msat(channel, recipient)) {
&& msat_in_htlcs > channel->config[recipient]->max_htlc_value_in_flight_msat) {
return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED;
}
@ -404,7 +404,7 @@ static enum channel_add_err add_htlc(struct channel *channel,
*/
if (channel->funder == htlc_owner(htlc)) {
u32 feerate = view->feerate_per_kw;
u64 dust = dust_limit_satoshis(channel, recipient);
u64 dust = channel->config[recipient]->dust_limit_satoshis;
size_t untrimmed;
untrimmed = commit_tx_num_untrimmed(committed, feerate, dust,
@ -701,7 +701,7 @@ u32 approx_max_feerate(const struct channel *channel)
bool can_funder_afford_feerate(const struct channel *channel, u32 feerate_per_kw)
{
u64 fee_msat, dust = dust_limit_satoshis(channel, !channel->funder);
u64 fee_msat, dust = channel->config[!channel->funder]->dust_limit_satoshis;
size_t untrimmed;
const struct htlc **committed, **adding, **removing;
gather_htlcs(tmpctx, channel, !channel->funder,

View File

@ -34,11 +34,43 @@
struct channel_config {
/* Database ID */
u64 id;
/* BOLT #2:
*
* `dust_limit_satoshis` is the threshold below which outputs should
* not be generated for this node's commitment or HTLC transaction */
u64 dust_limit_satoshis;
/* BOLT #2:
*
* `max_htlc_value_in_flight_msat` is a cap on total value of
* outstanding HTLCs, which allows a node to limit its exposure to
* HTLCs */
u64 max_htlc_value_in_flight_msat;
/* BOLT #2:
*
* `channel_reserve_satoshis` is the minimum amount that the other
* node is to keep as a direct payment. */
u64 channel_reserve_satoshis;
/* BOLT #2:
*
* `htlc_minimum_msat` indicates the smallest value HTLC this node
* will accept.
*/
u64 htlc_minimum_msat;
/* BOLT #2:
*
* `to_self_delay` is the number of blocks that the other node's
* to-self outputs must be delayed, using `OP_CHECKSEQUENCEVERIFY`
* delays */
u16 to_self_delay;
/* BOLT #2:
*
* similarly, `max_accepted_htlcs` limits the number of outstanding
* HTLCs the other node can offer. */
u16 max_accepted_htlcs;
};

View File

@ -93,10 +93,11 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx,
channel->funding_txout,
channel->funding_msat / 1000,
channel->funder,
to_self_delay(channel, side),
/* They specify our to_self_delay and v.v. */
channel->config[!side]->to_self_delay,
&keyset,
channel->view[side].feerate_per_kw,
dust_limit_satoshis(channel, side),
channel->config[side]->dust_limit_satoshis,
channel->view[side].owed_msat[side],
channel->view[side].owed_msat[!side],
channel_reserve_msat(channel, side),

View File

@ -62,68 +62,13 @@ struct channel {
const struct chainparams *chainparams;
};
/* Some requirements are self-specified (eg. my dust limit), others
* are force upon the other side (eg. minimum htlc you can add).
*
* These values are also universally in msatsoshi. These avoid
* confusion: use them! */
/* BOLT #2:
*
* `dust_limit_satoshis` is the threshold below which outputs should not be
* generated for this node's commitment or HTLC transaction */
static inline u64 dust_limit_satoshis(const struct channel *channel,
enum side side)
{
return channel->config[side]->dust_limit_satoshis;
}
/* BOLT #2:
*
* `max_htlc_value_in_flight_msat` is a cap on total value of
* outstanding HTLCs, which allows a node to limit its exposure to
* HTLCs */
static inline u64 max_htlc_value_in_flight_msat(const struct channel *channel,
enum side recipient)
{
return channel->config[recipient]->max_htlc_value_in_flight_msat;
}
/* BOLT #2:
*
* similarly, `max_accepted_htlcs` limits the number of outstanding
* HTLCs the other node can offer. */
static inline u16 max_accepted_htlcs(const struct channel *channel,
enum side recipient)
{
return channel->config[recipient]->max_accepted_htlcs;
}
/* BOLT #2:
*
* `channel_reserve_satoshis` is the minimum amount that the other
* node is to keep as a direct payment. */
/* This side's reserve is specified by the *other* side, and in satoshis:
* this is a convenience function to convert it. */
static inline u64 channel_reserve_msat(const struct channel *channel,
enum side side)
{
return channel->config[!side]->channel_reserve_satoshis * 1000;
}
/* BOLT #2:
*
* `htlc_minimum_msat` indicates the smallest value HTLC this node will accept.
*/
static inline u64 htlc_minimum_msat(const struct channel *channel,
enum side recipient)
{
return channel->config[recipient]->htlc_minimum_msat;
}
/* BOLT #2:
*
* `to_self_delay` is the number of blocks that the other node's
* to-self outputs must be delayed, using `OP_CHECKSEQUENCEVERIFY`
* delays */
static inline u16 to_self_delay(const struct channel *channel, enum side side)
{
return channel->config[!side]->to_self_delay;
}
/**
* new_initial_channel: Given initial fees and funding, what is initial state?