Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The number of replacements is bounded by the fee gained

Proved
BitcoinReplacement.replacement_count_bound

by pitimespi · Oct 29, 2026 · Mathlib 0df444a (Lean v4.33.1)

bip125bitcoinfee-marketformal-verificationmathlibmempoolpinningpolicyrbf

For a chain of nnn replacements: t0.fee+n⋅getFee(1)≤tn.feet_0.\text{fee} + n \cdot \text{getFee}(1) \le t_n.\text{fee}t0​.fee+n⋅getFee(1)≤tn​.fee.

Preamble
import Definitions.Def_BitcoinReplacement
Formal statement
theorem BitcoinReplacement.replacement_count_bound (rate : BitcoinReplacement.FeeRate)
    (t₀ : BitcoinReplacement.Tx) (Rs : List BitcoinReplacement.Replacement)
    (h : BitcoinReplacement.IsChain rate t₀ Rs) :
    t₀.fee + Rs.length * rate.getFee 1 ≤ (BitcoinReplacement.chainEnd t₀ Rs).fee := by sorry
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: PaysForRBF (src/policy/rbf.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/policy/rbf.cpp), CFeeRate::GetFee = FeePerVSize::EvaluateFeeUp (policy/feerate.cpp, util/feefrac.h), DEFAULT_INCREMENTAL_RELAY_FEE (policy/policy.h); PaysMoreThanConflicts as last present in policy/rbf.cpp at v29.0; BIP125
Read-back

What the Lean code literally says, in plain math · claude-fable-5-1 (independent sub-agent, blind: Lean code + auditor instructions only)

Read-back: BitcoinReplacement.replacement_count_bound

Setting (unfolded from the preamble). All declarations live in a namespace BitcoinReplacement. A transaction ttt is a pair of natural numbers (fee⁡(t),vsize⁡(t))(\operatorname{fee}(t), \operatorname{vsize}(t))(fee(t),vsize(t)) together with a proof that vsize⁡(t)>0\operatorname{vsize}(t) > 0vsize(t)>0; fee⁡(t)\operatorname{fee}(t)fee(t) may be 000. Two transactions are equal exactly when their fees and virtual sizes agree. A fee rate rrr is a pair of natural numbers (rfee,rsize)(r_{\mathrm{fee}}, r_{\mathrm{size}})(rfee​,rsize​) together with a proof that rsize>0r_{\mathrm{size}} > 0rsize​>0; rfeer_{\mathrm{fee}}rfee​ may be 000, and nothing identifies (1,2)(1,2)(1,2) with (2,4)(2,4)(2,4). For a fee rate rrr and a natural number vvv,

getFee⁡r(v)  =  ⌊rfee⋅v+(rsize−1)rsize⌋,\operatorname{getFee}_r(v) \;=\; \left\lfloor \frac{r_{\mathrm{fee}}\cdot v + (r_{\mathrm{size}} - 1)}{r_{\mathrm{size}}} \right\rfloor ,getFeer​(v)=⌊rsize​rfee​⋅v+(rsize​−1)​⌋,

computed with natural-number (rounding-down) division; since rsize≥1r_{\mathrm{size}} \ge 1rsize​≥1 the inner subtraction rsize−1r_{\mathrm{size}}-1rsize​−1 is exact, and the value equals ⌈rfee v/rsize⌉\lceil r_{\mathrm{fee}}\, v / r_{\mathrm{size}}\rceil⌈rfee​v/rsize​⌉. In particular getFee⁡r(0)=0\operatorname{getFee}_r(0) = 0getFeer​(0)=0, and getFee⁡r(v)=0\operatorname{getFee}_r(v) = 0getFeer​(v)=0 for every vvv whenever rfee=0r_{\mathrm{fee}} = 0rfee​=0.

A replacement RRR consists of a transaction R.newR.\mathrm{new}R.new, a multiset of transactions R.evictedR.\mathrm{evicted}R.evicted, a multiset of transactions R.directR.\mathrm{direct}R.direct, and a proof that R.direct≤R.evictedR.\mathrm{direct} \le R.\mathrm{evicted}R.direct≤R.evicted as multisets (every transaction value occurs in R.directR.\mathrm{direct}R.direct at most as many times as it occurs in R.evictedR.\mathrm{evicted}R.evicted). Either multiset may be empty, R.newR.\mathrm{new}R.new may itself occur in R.evictedR.\mathrm{evicted}R.evicted, and no other relation between the three components is imposed. For a multiset sss of transactions, totalFee⁡(s)=∑u∈sfee⁡(u)\operatorname{totalFee}(s) = \sum_{u \in s} \operatorname{fee}(u)totalFee(s)=∑u∈s​fee(u), summed with multiplicity, so totalFee⁡(∅)=0\operatorname{totalFee}(\varnothing) = 0totalFee(∅)=0.

RRR is valid at rate rrr (written Valid⁡(r,R)\operatorname{Valid}(r,R)Valid(r,R)) when all three of the following hold:

  1. (PaysMoreThanConflicts⁡(R)\operatorname{PaysMoreThanConflicts}(R)PaysMoreThanConflicts(R)) for every transaction value uuu occurring in R.directR.\mathrm{direct}R.direct, fee⁡(u)⋅vsize⁡(R.new)<fee⁡(R.new)⋅vsize⁡(u)\operatorname{fee}(u)\cdot\operatorname{vsize}(R.\mathrm{new}) < \operatorname{fee}(R.\mathrm{new})\cdot\operatorname{vsize}(u)fee(u)⋅vsize(R.new)<fee(R.new)⋅vsize(u) (a strict cross-multiplied comparison in N\mathbb{N}N; vacuously true when R.directR.\mathrm{direct}R.direct is empty; nothing is required of elements of R.evictedR.\mathrm{evicted}R.evicted that do not occur in R.directR.\mathrm{direct}R.direct);
  2. (PaysForRBF⁡(r,R)\operatorname{PaysForRBF}(r,R)PaysForRBF(r,R), first conjunct) totalFee⁡(R.evicted)≤fee⁡(R.new)\operatorname{totalFee}(R.\mathrm{evicted}) \le \operatorname{fee}(R.\mathrm{new})totalFee(R.evicted)≤fee(R.new);
  3. (PaysForRBF⁡(r,R)\operatorname{PaysForRBF}(r,R)PaysForRBF(r,R), second conjunct) getFee⁡r(vsize⁡(R.new))≤fee⁡(R.new)−˙totalFee⁡(R.evicted)\operatorname{getFee}_r(\operatorname{vsize}(R.\mathrm{new})) \le \operatorname{fee}(R.\mathrm{new}) \mathbin{\dot-} \operatorname{totalFee}(R.\mathrm{evicted})getFeer​(vsize(R.new))≤fee(R.new)−˙​totalFee(R.evicted), where −˙\dot-−˙​ is truncated natural-number subtraction (it returns 000 when the subtrahend exceeds the minuend; under item 2 it coincides with ordinary subtraction).

A list [R1,…,Rn][R_1, \dots, R_n][R1​,…,Rn​] of replacements (n≥0n \ge 0n≥0) is a chain at rate rrr from t0t_0t0​ (written IsChain⁡(r,t0,[R1,…,Rn])\operatorname{IsChain}(r, t_0, [R_1,\dots,R_n])IsChain(r,t0​,[R1​,…,Rn​])) when, writing ti=Ri.newt_i = R_i.\mathrm{new}ti​=Ri​.new for 1≤i≤n1 \le i \le n1≤i≤n, for every iii with 1≤i≤n1 \le i \le n1≤i≤n: RiR_iRi​ is valid at rate rrr, and the transaction value ti−1t_{i-1}ti−1​ occurs in the multiset Ri.evictedR_i.\mathrm{evicted}Ri​.evicted. Membership is by value: any element of Ri.evictedR_i.\mathrm{evicted}Ri​.evicted with the same fee and virtual size as ti−1t_{i-1}ti−1​ counts; it is not required that ti−1t_{i-1}ti−1​ occur in Ri.directR_i.\mathrm{direct}Ri​.direct, nor that Ri.evictedR_i.\mathrm{evicted}Ri​.evicted contain nothing else. The empty list is a chain from any t0t_0t0​ at any rate. The chain end chainEnd⁡(t0,[R1,…,Rn])\operatorname{chainEnd}(t_0,[R_1,\dots,R_n])chainEnd(t0​,[R1​,…,Rn​]) is tn=Rn.newt_n = R_n.\mathrm{new}tn​=Rn​.new when n≥1n \ge 1n≥1 and is t0t_0t0​ itself when n=0n = 0n=0 (it depends only on the last replacement). The chain vsizes chainVsizes⁡([R1,…,Rn])\operatorname{chainVsizes}([R_1,\dots,R_n])chainVsizes([R1​,…,Rn​]) is the list [vsize⁡(t1),…,vsize⁡(tn)][\operatorname{vsize}(t_1), \dots, \operatorname{vsize}(t_n)][vsize(t1​),…,vsize(tn​)], empty when n=0n = 0n=0. Below, tnt_ntn​ always denotes chainEnd⁡(t0,[R1,…,Rn])\operatorname{chainEnd}(t_0, [R_1,\dots,R_n])chainEnd(t0​,[R1​,…,Rn​]), i.e. it is t0t_0t0​ when n=0n = 0n=0.

Statement. For every fee rate rrr, every transaction t0t_0t0​, and every list of replacements [R1,…,Rn][R_1,\dots,R_n][R1​,…,Rn​] (n≥0n \ge 0n≥0) such that IsChain⁡(r,t0,[R1,…,Rn])\operatorname{IsChain}(r, t_0, [R_1,\dots,R_n])IsChain(r,t0​,[R1​,…,Rn​]) holds, the following inequality of natural numbers holds:

fee⁡(t0)  +  n⋅getFee⁡r(1)  ≤  fee⁡(tn),\operatorname{fee}(t_0) \;+\; n \cdot \operatorname{getFee}_r(1) \;\le\; \operatorname{fee}(t_n),fee(t0​)+n⋅getFeer​(1)≤fee(tn​),

where nnn is the length of the list and

getFee⁡r(1)=⌊rfee+rsize−1rsize⌋=⌈rfeersize⌉,\operatorname{getFee}_r(1) = \left\lfloor \frac{r_{\mathrm{fee}} + r_{\mathrm{size}} - 1}{r_{\mathrm{size}}} \right\rfloor = \left\lceil \frac{r_{\mathrm{fee}}}{r_{\mathrm{size}}} \right\rceil ,getFeer​(1)=⌊rsize​rfee​+rsize​−1​⌋=⌈rsize​rfee​​⌉,

which equals 000 exactly when rfee=0r_{\mathrm{fee}} = 0rfee​=0 and is at least 111 otherwise.

Edge cases. When n=0n = 0n=0, tn=t0t_n = t_0tn​=t0​ and the claim reads fee⁡(t0)≤fee⁡(t0)\operatorname{fee}(t_0) \le \operatorname{fee}(t_0)fee(t0​)≤fee(t0​). When rfee=0r_{\mathrm{fee}} = 0rfee​=0 the claim reads fee⁡(t0)≤fee⁡(tn)\operatorname{fee}(t_0) \le \operatorname{fee}(t_n)fee(t0​)≤fee(tn​). The statement places no lower bound on the virtual sizes of the Ri.newR_i.\mathrm{new}Ri​.new beyond the positivity built into every transaction. The relation ≤\le≤ is non-strict.

Human review
  • Endorsed by Shuze Chen · Oct 29, 2026

    Confirmed by the moderator at approval.

  • Endorsed by pitimespi · Oct 29, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me