| 205 | } |
| 206 | |
| 207 | const char * |
| 208 | calculate_our_funding(struct funder_policy *policy, |
| 209 | struct node_id id, |
| 210 | struct amount_sat their_funding, |
| 211 | struct amount_sat available_funds, |
| 212 | struct amount_sat channel_max, |
| 213 | struct amount_sat requested_lease, |
| 214 | struct amount_sat *our_funding) |
| 215 | { |
| 216 | struct amount_sat avail_channel_space, net_available_funds; |
| 217 | |
| 218 | /* Are we only funding lease requests ? */ |
| 219 | if (policy->leases_only && amount_sat_zero(requested_lease)) { |
| 220 | *our_funding = AMOUNT_SAT(0); |
| 221 | return tal_fmt(tmpctx, |
| 222 | "Skipping funding open; leases-only=true" |
| 223 | " and this open isn't asking for a lease"); |
| 224 | } |
| 225 | |
| 226 | /* Are we skipping this one? */ |
| 227 | if (pseudorand(100) >= policy->fund_probability |
| 228 | /* We don't skip lease requests */ |
| 229 | && amount_sat_zero(requested_lease)) { |
| 230 | *our_funding = AMOUNT_SAT(0); |
| 231 | return tal_fmt(tmpctx, |
| 232 | "Skipping, failed fund_probability test"); |
| 233 | } |
| 234 | |
| 235 | /* Figure out amount of actual headroom we have */ |
| 236 | if (!amount_sat_sub(&avail_channel_space, channel_max, their_funding) |
| 237 | || amount_sat_zero(avail_channel_space)) { |
| 238 | *our_funding = AMOUNT_SAT(0); |
| 239 | return tal_fmt(tmpctx, "No space available in channel." |
| 240 | " channel_max %s, their_funding %s", |
| 241 | type_to_string(tmpctx, struct amount_sat, |
| 242 | &channel_max), |
| 243 | type_to_string(tmpctx, struct amount_sat, |
| 244 | &their_funding)); |
| 245 | } |
| 246 | |
| 247 | /* Figure out actual available funds, given our requested |
| 248 | * 'reserve_tank' */ |
| 249 | if (!amount_sat_sub(&net_available_funds, available_funds, |
| 250 | policy->reserve_tank) |
| 251 | || amount_sat_zero(net_available_funds)) { |
| 252 | *our_funding = AMOUNT_SAT(0); |
| 253 | return tal_fmt(tmpctx, "Reserve tank too low." |
| 254 | " available_funds %s, reserve_tank requires %s", |
| 255 | type_to_string(tmpctx, struct amount_sat, |
| 256 | &available_funds), |
| 257 | type_to_string(tmpctx, struct amount_sat, |
| 258 | &policy->reserve_tank)); |
| 259 | } |
| 260 | |
| 261 | /* Are they funding enough ? */ |
| 262 | if (amount_sat_less(their_funding, policy->min_their_funding)) { |
| 263 | *our_funding = AMOUNT_SAT(0); |
| 264 | return tal_fmt(tmpctx, "Peer's funding too little." |