MCPcopy Create free account
hub / github.com/ElementsProject/lightning / anchor_psbt

Function anchor_psbt

lightningd/anchorspend.c:228–289  ·  view source on GitHub ↗

total_weight includes the commitment tx we're trying to push, and the anchor fee output */

Source from the content-addressed store, hash-verified

226
227/* total_weight includes the commitment tx we're trying to push, and the anchor fee output */
228static struct wally_psbt *anchor_psbt(const tal_t *ctx,
229 struct channel *channel,
230 const struct one_anchor *anch,
231 struct utxo **utxos,
232 u32 feerate_target,
233 size_t total_weight,
234 bool insufficient_funds)
235{
236 struct lightningd *ld = channel->peer->ld;
237 struct wally_psbt *psbt;
238 struct amount_sat change, fee;
239 struct pubkey final_key;
240
241 /* PSBT knows how to spend utxos. */
242 psbt = psbt_using_utxos(ctx, ld->wallet, utxos,
243 default_locktime(ld->topology),
244 BITCOIN_TX_RBF_SEQUENCE, NULL);
245
246 /* BOLT #3:
247 * #### `to_local_anchor` and `to_remote_anchor` Output (option_anchors)
248 *...
249 * The amount of the output is fixed at 330 sats, the default
250 * dust limit for P2WSH.
251 */
252 psbt_append_input(psbt, &anch->info.anchor_point, BITCOIN_TX_RBF_SEQUENCE,
253 NULL, anch->adet->anchor_wscript, NULL);
254 psbt_input_set_wit_utxo(psbt, psbt->num_inputs - 1,
255 scriptpubkey_p2wsh(tmpctx, anch->adet->anchor_wscript),
256 AMOUNT_SAT(330));
257 psbt_input_add_pubkey(psbt, psbt->num_inputs - 1, &channel->local_funding_pubkey, false);
258
259 /* Calculate fee we need, given rate and weight */
260 fee = amount_tx_fee(feerate_target, total_weight);
261 /* Some fee already paid by commitment tx */
262 if (!amount_sat_sub(&fee, fee, anch->info.commitment_fee))
263 fee = AMOUNT_SAT(0);
264
265 /* How much do we have? */
266 change = psbt_compute_fee(psbt);
267
268 /* We have to pay dust, at least! */
269 if (!amount_sat_sub(&change, change, fee)
270 || amount_sat_less(change, chainparams->dust_limit)) {
271 /* If we didn't run out of UTXOs, this implies our estimation was wrong! */
272 if (!insufficient_funds)
273 log_broken(channel->log, "anchor: could not afford fee %s from change %s, reducing fee",
274 fmt_amount_sat(tmpctx, fee),
275 fmt_amount_sat(tmpctx, psbt_compute_fee(psbt)));
276 change = chainparams->dust_limit;
277 }
278
279 /* Use BIP86 derivation for P2TR if available, otherwise BIP32 */
280 if (ld->bip86_base) {
281 bip86_pubkey(ld, &final_key, channel->final_key_idx);
282 } else {
283 bip32_pubkey(ld, &final_key, channel->final_key_idx);
284 }
285 psbt_append_output(psbt,

Callers 1

try_anchor_psbtFunction · 0.85

Calls 15

psbt_using_utxosFunction · 0.85
default_locktimeFunction · 0.85
psbt_append_inputFunction · 0.85
psbt_input_set_wit_utxoFunction · 0.85
scriptpubkey_p2wshFunction · 0.85
psbt_input_add_pubkeyFunction · 0.85
psbt_compute_feeFunction · 0.85
amount_sat_lessFunction · 0.85
psbt_append_outputFunction · 0.85
scriptpubkey_p2trFunction · 0.85
bip86_pubkeyFunction · 0.70
bip32_pubkeyFunction · 0.70

Tested by

no test coverage detected