struct bitcoin_tx *create_close_tx(const tal_t *ctx, const u8 *our_script, const u8 *their_script, const struct bitcoin_txid *anchor_txid, unsigned int anchor_index, struct amount_sat funding, struct amount_sat to_us, struct amount_sat to_them, struct amount_sat dust_limit) { struct bitcoin_tx *tx; size_t num_outputs = 0; struct amount_sat total_out; u8 *script; assert(amount_sat_add(&total_out, to_us, to_them)); assert(amount_sat_less_eq(total_out, funding)); /* BOLT #3: * * ## Closing Transaction * * Note that there are two possible variants for each node. * * * version: 2 * * locktime: 0 * * txin count: 1 */ /* Now create close tx: one input, two outputs. */ tx = bitcoin_tx(ctx, 1, 2); /* Our input spends the anchor tx output. */ bitcoin_tx_add_input(tx, anchor_txid, anchor_index, BITCOIN_TX_DEFAULT_SEQUENCE, &funding, NULL); if (amount_sat_greater_eq(to_us, dust_limit)) { script = tal_dup_arr(tx, u8, our_script, tal_count(our_script), 0); /* One output is to us. */ bitcoin_tx_add_output(tx, script, &to_us); num_outputs++; } if (amount_sat_greater_eq(to_them, dust_limit)) { script = tal_dup_arr(tx, u8, their_script, tal_count(their_script), 0); /* Other output is to them. */ bitcoin_tx_add_output(tx, script, &to_them); num_outputs++; } /* Can't have no outputs at all! */ if (num_outputs == 0) return tal_free(tx); permute_outputs(tx, NULL, NULL); assert(bitcoin_tx_check(tx)); return tx; }
void queue_pkt_htlc_add(struct peer *peer, struct htlc *htlc) { UpdateAddHtlc *u = tal(peer, UpdateAddHtlc); union htlc_staging stage; update_add_htlc__init(u); u->id = htlc->id; u->amount_msat = htlc->msatoshis; u->r_hash = sha256_to_proto(u, &htlc->rhash); u->expiry = abs_locktime_to_proto(u, &htlc->expiry); u->route = tal(u, Routing); routing__init(u->route); u->route->info.data = tal_dup_arr(u, u8, htlc->routing, tal_count(htlc->routing), 0); u->route->info.len = tal_count(u->route->info.data); /* BOLT #2: * * The sending node MUST add the HTLC addition to the unacked * changeset for its remote commitment */ if (!cstate_add_htlc(peer->remote.staging_cstate, htlc, OURS)) fatal("Could not add HTLC?"); stage.add.add = HTLC_ADD; stage.add.htlc = htlc; add_unacked(&peer->remote, &stage); remote_changes_pending(peer); queue_pkt(peer, PKT__PKT_UPDATE_ADD_HTLC, u); }
BitcoinPubkey *pubkey_to_proto(const tal_t *ctx, const struct pubkey *key) { BitcoinPubkey *p = tal(ctx, BitcoinPubkey); bitcoin_pubkey__init(p); p->key.len = pubkey_len(key); p->key.data = tal_dup_arr(p, u8, key->key, p->key.len, 0); assert(pubkey_valid(p->key.data, p->key.len)); return p; }
Pkt *accept_pkt_close_clearing(struct peer *peer, const Pkt *pkt) { const CloseClearing *c = pkt->close_clearing; /* FIXME: Filter for non-standardness? */ peer->closing.their_script = tal_dup_arr(peer, u8, c->scriptpubkey.data, c->scriptpubkey.len, 0); return NULL; }
BitcoinPubkey *pubkey_to_proto(const tal_t *ctx, const struct pubkey *key) { BitcoinPubkey *p = tal(ctx, BitcoinPubkey); struct pubkey check; bitcoin_pubkey__init(p); p->key.len = pubkey_derlen(key); p->key.data = tal_dup_arr(p, u8, key->der, p->key.len, 0); { secp256k1_context *secpctx = secp256k1_context_create(0); assert(pubkey_from_der(secpctx, p->key.data, p->key.len, &check)); assert(pubkey_eq(&check, key)); secp256k1_context_destroy(secpctx); } return p; }
void queue_pkt_close_clearing(struct peer *peer) { u8 *redeemscript; CloseClearing *c = tal(peer, CloseClearing); close_clearing__init(c); redeemscript = bitcoin_redeem_single(c, &peer->us.finalkey); peer->closing.our_script = scriptpubkey_p2sh(peer, redeemscript); c->scriptpubkey.data = tal_dup_arr(c, u8, peer->closing.our_script, tal_count(peer->closing.our_script), 0); c->scriptpubkey.len = tal_count(c->scriptpubkey.data); queue_pkt(peer, PKT__PKT_CLOSE_CLEARING, c); }
jsmntok_t *json_tok_copy(const tal_t *ctx, const jsmntok_t *tok) { return tal_dup_arr(ctx, jsmntok_t, tok, json_next(tok) - tok, 0); }
static enum channel_add_err add_htlc(struct channel *channel, enum htlc_state state, u64 id, struct amount_msat amount, u32 cltv_expiry, const struct sha256 *payment_hash, const u8 routing[TOTAL_PACKET_SIZE], struct htlc **htlcp, bool enforce_aggregate_limits) { struct htlc *htlc, *old; struct amount_msat msat_in_htlcs, committed_msat, adding_msat, removing_msat; struct amount_sat fee; enum side sender = htlc_state_owner(state), recipient = !sender; const struct htlc **committed, **adding, **removing; const struct channel_view *view; bool ok; size_t i; htlc = tal(tmpctx, struct htlc); htlc->id = id; htlc->amount = amount; htlc->state = state; htlc->shared_secret = NULL; /* FIXME: Change expiry to simple u32 */ /* BOLT #2: * * A receiving node: *... * - if sending node sets `cltv_expiry` to greater or equal to * 500000000: * - SHOULD fail the channel. */ if (!blocks_to_abs_locktime(cltv_expiry, &htlc->expiry)) { return CHANNEL_ERR_INVALID_EXPIRY; } htlc->rhash = *payment_hash; htlc->fail = NULL; htlc->failcode = 0; htlc->failed_scid = NULL; htlc->r = NULL; htlc->routing = tal_dup_arr(htlc, u8, routing, TOTAL_PACKET_SIZE, 0); old = htlc_get(channel->htlcs, htlc->id, htlc_owner(htlc)); if (old) { if (old->state != htlc->state || !amount_msat_eq(old->amount, htlc->amount) || old->expiry.locktime != htlc->expiry.locktime || !sha256_eq(&old->rhash, &htlc->rhash)) return CHANNEL_ERR_DUPLICATE_ID_DIFFERENT; else return CHANNEL_ERR_DUPLICATE; } /* We're always considering the recipient's view of the channel here */ view = &channel->view[recipient]; /* BOLT #2: * * A receiving node: * - receiving an `amount_msat` equal to 0, OR less than its own * `htlc_minimum_msat`: * - SHOULD fail the channel. */ if (amount_msat_eq(htlc->amount, AMOUNT_MSAT(0))) { return CHANNEL_ERR_HTLC_BELOW_MINIMUM; } if (amount_msat_less(htlc->amount, channel->config[recipient].htlc_minimum)) { return CHANNEL_ERR_HTLC_BELOW_MINIMUM; } /* BOLT #2: * * - for channels with `chain_hash` identifying the Bitcoin blockchain: * - MUST set the four most significant bytes of `amount_msat` to 0. */ if (amount_msat_greater(htlc->amount, channel->chainparams->max_payment)) { return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED; } /* Figure out what receiver will already be committed to. */ gather_htlcs(tmpctx, channel, recipient, &committed, &removing, &adding); htlc_arr_append(&adding, htlc); /* BOLT #2: * * - if a sending node adds more than receiver `max_accepted_htlcs` * HTLCs to its local commitment transaction... * - SHOULD fail the channel. */ if (tal_count(committed) - tal_count(removing) + tal_count(adding) > channel->config[recipient].max_accepted_htlcs) { return CHANNEL_ERR_TOO_MANY_HTLCS; } /* These cannot overflow with HTLC amount limitations, but * maybe adding could later if they try to add a maximal HTLC. */ if (!sum_offered_msatoshis(&committed_msat, committed, htlc_owner(htlc)) || !sum_offered_msatoshis(&removing_msat, removing, htlc_owner(htlc)) || !sum_offered_msatoshis(&adding_msat, adding, htlc_owner(htlc))) { return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED; } if (!amount_msat_add(&msat_in_htlcs, committed_msat, adding_msat) || !amount_msat_sub(&msat_in_htlcs, msat_in_htlcs, removing_msat)) { return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED; } /* BOLT #2: * * - if a sending node... adds more than receiver * `max_htlc_value_in_flight_msat` worth of offered HTLCs to its * local commitment transaction: * - SHOULD fail the channel. */ /* We don't enforce this for channel_force_htlcs: some might already * be fulfilled/failed */ if (enforce_aggregate_limits && amount_msat_greater(msat_in_htlcs, channel->config[recipient].max_htlc_value_in_flight)) { return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED; } /* BOLT #2: * * A receiving node: *... * - receiving an `amount_msat` that the sending node cannot afford at * the current `feerate_per_kw` (while maintaining its channel * reserve): * - SHOULD fail the channel. */ if (channel->funder == htlc_owner(htlc)) { u32 feerate = view->feerate_per_kw; struct amount_sat dust_limit = channel->config[recipient].dust_limit; size_t untrimmed; untrimmed = commit_tx_num_untrimmed(committed, feerate, dust_limit, recipient) + commit_tx_num_untrimmed(adding, feerate, dust_limit, recipient) - commit_tx_num_untrimmed(removing, feerate, dust_limit, recipient); fee = commit_tx_base_fee(feerate, untrimmed); } else fee = AMOUNT_SAT(0); assert((s64)fee.satoshis >= 0); /* Raw: explicit signedness test */ if (enforce_aggregate_limits) { /* Figure out what balance sender would have after applying all * pending changes. */ struct amount_msat balance = view->owed[sender]; /* This is a little subtle: * * The change is being applied to the receiver but it will * come back to the sender after revoke_and_ack. So the check * here is that the balance to the sender doesn't go below the * sender's reserve. */ const struct amount_sat reserve = channel->config[!sender].channel_reserve; assert(amount_msat_greater_eq(balance, AMOUNT_MSAT(0))); ok = true; for (i = 0; i < tal_count(removing); i++) ok &= balance_remove_htlc(&balance, removing[i], sender); assert(amount_msat_greater_eq(balance, AMOUNT_MSAT(0))); for (i = 0; i < tal_count(adding); i++) ok &= balance_add_htlc(&balance, adding[i], sender); /* Overflow shouldn't happen, but if it does, complain */ if (!ok) { status_broken("Failed to add %zu remove %zu htlcs", tal_count(adding), tal_count(removing)); return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED; } if (!amount_msat_sub_sat(&balance, balance, fee)) { status_trace("Cannot afford fee %s with balance %s", type_to_string(tmpctx, struct amount_sat, &fee), type_to_string(tmpctx, struct amount_msat, &balance)); return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED; } if (!amount_msat_greater_eq_sat(balance, reserve)) { status_trace("Cannot afford fee %s: would make balance %s" " below reserve %s", type_to_string(tmpctx, struct amount_sat, &fee), type_to_string(tmpctx, struct amount_msat, &balance), type_to_string(tmpctx, struct amount_sat, &reserve)); return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED; }