Skip to content

Commit

Permalink
ZIP 207: improvements to the pseudocode.
Browse files Browse the repository at this point in the history
Signed-off-by: Daira Hopwood <[email protected]>
  • Loading branch information
daira committed Mar 27, 2020
1 parent 93a7b08 commit 90ab3de
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 38 deletions.
49 changes: 30 additions & 19 deletions zip-0207.html
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@
<span class="n">MAX_FUNDING_STREAMS</span><span class="p">,</span>
<span class="p">};</span>

<span class="k">const</span> <span class="k">auto</span> <span class="n">FIRST_FUNDING_STREAM</span> <span class="o">=</span> <span class="n">FS_ECC</span><span class="p">;</span>

<span class="k">struct</span> <span class="n">Params</span> <span class="p">{</span>
<span class="p">...</span>
<span class="kt">int</span> <span class="n">nFundingPeriodLength</span><span class="p">;</span>
Expand All @@ -234,16 +236,17 @@
<span class="n">Consensus</span><span class="o">::</span><span class="n">FundingStream</span> <span class="n">idx</span><span class="p">,</span>
<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&gt;</span> <span class="n">addresses</span><span class="p">,</span>
<span class="kt">uint64_t</span> <span class="n">valueNumerator</span><span class="p">,</span>
<span class="kt">uint64_t</span> <span class="n">valueDenominator</span><span class="p">)</span>
<span class="kt">uint64_t</span> <span class="n">valueDenominator</span><span class="p">,</span>
<span class="kt">int</span> <span class="n">startHeight</span><span class="p">,</span>
<span class="kt">int</span> <span class="n">endHeight</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">assert</span><span class="p">(</span><span class="n">valueNumerator</span> <span class="o">&lt;</span> <span class="n">valueDenominator</span><span class="p">);</span>
<span class="n">assert</span><span class="p">(</span><span class="n">valueNumerator</span> <span class="o">&lt;</span> <span class="n">INT64_MAX</span> <span class="o">/</span> <span class="n">MAX_MONEY</span><span class="p">);</span>
<span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">addresses</span> <span class="o">=</span> <span class="n">addresses</span><span class="p">;</span>
<span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">valueNumerator</span> <span class="o">=</span> <span class="n">valueNumerator</span><span class="p">;</span>
<span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">valueDenominator</span> <span class="o">=</span> <span class="n">valueDenominator</span><span class="p">;</span>
<span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">startHeight</span> <span class="o">=</span>
<span class="n">params</span><span class="p">.</span><span class="n">vUpgrades</span><span class="p">[</span><span class="n">Consensus</span><span class="o">::</span><span class="n">UPGRADE_BLOSSOM</span><span class="p">].</span><span class="n">nActivationHeight</span><span class="p">;</span>
<span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">endHeight</span> <span class="o">=</span> <span class="n">HeightForHalving</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">startHeight</span> <span class="o">=</span> <span class="n">startHeight</span><span class="p">;</span>
<span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">endHeight</span> <span class="o">=</span> <span class="n">endHeight</span><span class="p">;</span>
<span class="n">assert</span><span class="p">(</span><span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">startHeight</span> <span class="o">&lt;</span> <span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">endHeight</span><span class="p">);</span>
<span class="p">};</span>

Expand All @@ -252,9 +255,11 @@

<span class="n">consensus</span><span class="p">.</span><span class="n">nFundingPeriodLength</span> <span class="o">=</span> <span class="n">consensus</span><span class="p">.</span><span class="n">nSubsidyPostBlossomHalvingInterval</span> <span class="o">/</span> <span class="mi">48</span><span class="p">;</span>

<span class="n">AddZIP207FundingStream</span><span class="p">(</span><span class="n">consensus</span><span class="p">,</span> <span class="n">Consensus</span><span class="o">::</span><span class="n">FS_ECC</span><span class="p">,</span> <span class="n">FS_ECC_ADDRESSES</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">100</span><span class="p">);</span>
<span class="n">AddZIP207FundingStream</span><span class="p">(</span><span class="n">consensus</span><span class="p">,</span> <span class="n">Consensus</span><span class="o">::</span><span class="n">FS_ZF</span><span class="p">,</span> <span class="n">FS_ZF_ADDRESSES</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">100</span><span class="p">);</span>
<span class="n">AddZIP207FundingStream</span><span class="p">(</span><span class="n">consensus</span><span class="p">,</span> <span class="n">Consensus</span><span class="o">::</span><span class="n">FS_MG</span><span class="p">,</span> <span class="n">FS_MG_ADDRESSES</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">100</span><span class="p">);</span>
<span class="kt">int</span> <span class="n">devFundStartHeight</span> <span class="o">=</span> <span class="n">HeightForHalving</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="kt">int</span> <span class="n">devFundEndHeight</span> <span class="o">=</span> <span class="n">HeightForHalving</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
<span class="n">AddZIP207FundingStream</span><span class="p">(</span><span class="n">consensus</span><span class="p">,</span> <span class="n">Consensus</span><span class="o">::</span><span class="n">FS_ECC</span><span class="p">,</span> <span class="n">FS_ECC_ADDRESSES</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="n">devFundStartHeight</span><span class="p">,</span> <span class="n">devFundEndHeight</span><span class="p">);</span>
<span class="n">AddZIP207FundingStream</span><span class="p">(</span><span class="n">consensus</span><span class="p">,</span> <span class="n">Consensus</span><span class="o">::</span><span class="n">FS_ZF</span><span class="p">,</span> <span class="n">FS_ZF_ADDRESSES</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="n">devFundStartHeight</span><span class="p">,</span> <span class="n">devFundEndHeight</span><span class="p">);</span>
<span class="n">AddZIP207FundingStream</span><span class="p">(</span><span class="n">consensus</span><span class="p">,</span> <span class="n">Consensus</span><span class="o">::</span><span class="n">FS_MG</span><span class="p">,</span> <span class="n">FS_MG_ADDRESSES</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="n">devFundStartHeight</span><span class="p">,</span> <span class="n">devFundEndHeight</span><span class="p">);</span>

<span class="p">...</span>
<span class="p">}</span>
Expand All @@ -264,25 +269,31 @@
<span class="k">const</span> <span class="n">Consensus</span><span class="o">::</span><span class="n">Params</span><span class="o">&amp;</span> <span class="n">params</span><span class="p">,</span>
<span class="n">Consensus</span><span class="o">::</span><span class="n">FundingStream</span> <span class="n">idx</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// Integer division is floor division in C++</span>
<span class="k">auto</span> <span class="n">curPeriod</span> <span class="o">=</span> <span class="p">(</span>
<span class="n">nHeight</span> <span class="o">+</span> <span class="n">params</span><span class="p">.</span><span class="n">nSubsidyPostBlossomHalvingInterval</span> <span class="o">-</span> <span class="n">HeightForHalving</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">)</span> <span class="o">/</span> <span class="n">params</span><span class="p">.</span><span class="n">nFundingPeriodLength</span><span class="p">;</span>
<span class="k">auto</span> <span class="n">startPeriod</span> <span class="o">=</span> <span class="p">(</span>
<span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">startHeight</span>
<span class="o">+</span> <span class="n">params</span><span class="p">.</span><span class="n">nSubsidyPostBlossomHalvingInterval</span>
<span class="o">-</span> <span class="n">HeightForHalving</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">)</span> <span class="o">/</span> <span class="n">params</span><span class="p">.</span><span class="n">nFundingPeriodLength</span><span class="p">;</span>
<span class="n">assert</span><span class="p">(</span><span class="n">nHeight</span> <span class="o">&lt;=</span> <span class="n">INT_MAX</span> <span class="o">-</span> <span class="n">params</span><span class="p">.</span><span class="n">nSubsidyPostBlossomHalvingInterval</span><span class="p">);</span>
<span class="n">assert</span><span class="p">(</span><span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">startHeight</span> <span class="o">&lt;=</span> <span class="n">INT_MAX</span> <span class="o">-</span> <span class="n">params</span><span class="p">.</span><span class="n">nSubsidyPostBlossomHalvingInterval</span><span class="p">);</span>

<span class="kt">int</span> <span class="n">curPeriodNumerator</span> <span class="o">=</span> <span class="n">nHeight</span> <span class="o">+</span> <span class="n">params</span><span class="p">.</span><span class="n">nSubsidyPostBlossomHalvingInterval</span> <span class="o">-</span> <span class="n">HeightForHalving</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="kt">int</span> <span class="n">startPeriodNumerator</span> <span class="o">=</span> <span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">startHeight</span> <span class="o">+</span> <span class="n">params</span><span class="p">.</span><span class="n">nSubsidyPostBlossomHalvingInterval</span>
<span class="o">-</span> <span class="n">HeightForHalving</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>

<span class="c1">// Integer division is floor division for nonnegative integers in C++</span>
<span class="n">assert</span><span class="p">(</span><span class="n">curPeriodNumerator</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">);</span>
<span class="n">assert</span><span class="p">(</span><span class="n">startPeriodNumerator</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">);</span>
<span class="k">auto</span> <span class="n">curPeriod</span> <span class="o">=</span> <span class="n">curPeriodNumerator</span> <span class="o">/</span> <span class="n">params</span><span class="p">.</span><span class="n">nFundingPeriodLength</span><span class="p">;</span>
<span class="k">auto</span> <span class="n">startPeriod</span> <span class="o">=</span> <span class="n">startPeriodNumerator</span> <span class="o">/</span> <span class="n">params</span><span class="p">.</span><span class="n">nFundingPeriodLength</span><span class="p">;</span>
<span class="k">auto</span> <span class="n">addressIndex</span> <span class="o">=</span> <span class="n">curPeriod</span> <span class="o">-</span> <span class="n">startPeriod</span><span class="p">;</span>
<span class="k">return</span> <span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">addresses</span><span class="p">[</span><span class="n">addressIndex</span><span class="p">];</span>

<span class="k">auto</span> <span class="n">addresses</span> <span class="o">=</span> <span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">addresses</span><span class="p">;</span>
<span class="n">assert</span><span class="p">(</span><span class="n">addressIndex</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">addressIndex</span> <span class="o">&lt;</span> <span class="n">addresses</span><span class="p">.</span><span class="n">size</span><span class="p">());</span>
<span class="k">return</span> <span class="n">addresses</span><span class="p">[</span><span class="n">addressIndex</span><span class="p">];</span>
<span class="p">};</span>

<span class="n">CAmount</span> <span class="nf">FundingStreamValue</span><span class="p">(</span>
<span class="kt">int</span> <span class="n">nHeight</span><span class="p">,</span>
<span class="k">const</span> <span class="n">Consensus</span><span class="o">::</span><span class="n">Params</span><span class="o">&amp;</span> <span class="n">params</span><span class="p">,</span>
<span class="n">Consensus</span><span class="o">::</span><span class="n">FundingStream</span> <span class="n">idx</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// Integer division is floor division in C++</span>
<span class="c1">// Integer division is floor division for nonnegative integers in C++</span>
<span class="k">return</span> <span class="n">CAmount</span><span class="p">((</span>
<span class="n">GetBlockSubsidy</span><span class="p">(</span><span class="n">nHeight</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span> <span class="o">*</span> <span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">valueNumerator</span>
<span class="p">)</span> <span class="o">/</span> <span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">valueDenominator</span><span class="p">);</span>
Expand All @@ -293,7 +304,7 @@
<span class="k">const</span> <span class="n">Consensus</span><span class="o">::</span><span class="n">Params</span><span class="o">&amp;</span> <span class="n">params</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">std</span><span class="o">::</span><span class="n">set</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">pair</span><span class="o">&lt;</span><span class="n">CScript</span><span class="p">,</span> <span class="n">CAmount</span><span class="o">&gt;&gt;</span> <span class="n">requiredStreams</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">idx</span> <span class="o">=</span> <span class="n">Consensus</span><span class="o">::</span><span class="n">FS_ZECC_EF</span><span class="p">;</span> <span class="n">idx</span> <span class="o">&lt;</span> <span class="n">Consensus</span><span class="o">::</span><span class="n">MAX_FUNDING_STREAMS</span><span class="p">;</span> <span class="n">idx</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">idx</span> <span class="o">=</span> <span class="n">Consensus</span><span class="o">::</span><span class="n">FIRST_FUNDING_STREAM</span><span class="p">;</span> <span class="n">idx</span> <span class="o">&lt;</span> <span class="n">Consensus</span><span class="o">::</span><span class="n">MAX_FUNDING_STREAMS</span><span class="p">;</span> <span class="n">idx</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// Funding period is [startHeight, endHeight)</span>
<span class="k">if</span> <span class="p">(</span><span class="n">nHeight</span> <span class="o">&gt;=</span> <span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">startHeight</span> <span class="o">&amp;&amp;</span>
<span class="n">nHeight</span> <span class="o">&lt;</span> <span class="n">params</span><span class="p">.</span><span class="n">vFundingPeriods</span><span class="p">[</span><span class="n">idx</span><span class="p">].</span><span class="n">endHeight</span><span class="p">)</span>
Expand Down
Loading

0 comments on commit 90ab3de

Please sign in to comment.