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 c4c4037
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 36 deletions.
45 changes: 27 additions & 18 deletions zip-0207.html
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,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 +253,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 +267,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 Down
45 changes: 27 additions & 18 deletions zip-0207.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,17 @@ Example implementation
Consensus::FundingStream idx,
std::vector<std::string> addresses,
uint64_t valueNumerator,
uint64_t valueDenominator)
uint64_t valueDenominator,
int startHeight,
int endHeight)
{
assert(valueNumerator < valueDenominator);
assert(valueNumerator < INT64_MAX / MAX_MONEY);
params.vFundingPeriods[idx].addresses = addresses;
params.vFundingPeriods[idx].valueNumerator = valueNumerator;
params.vFundingPeriods[idx].valueDenominator = valueDenominator;
params.vFundingPeriods[idx].startHeight =
params.vUpgrades[Consensus::UPGRADE_BLOSSOM].nActivationHeight;
params.vFundingPeriods[idx].endHeight = HeightForHalving(params, 1);
params.vFundingPeriods[idx].startHeight = startHeight;
params.vFundingPeriods[idx].endHeight = endHeight;
assert(params.vFundingPeriods[idx].startHeight < params.vFundingPeriods[idx].endHeight);
};
Expand All @@ -254,9 +255,11 @@ Example implementation
consensus.nFundingPeriodLength = consensus.nSubsidyPostBlossomHalvingInterval / 48;
AddZIP207FundingStream(consensus, Consensus::FS_ECC, FS_ECC_ADDRESSES, 7, 100);
AddZIP207FundingStream(consensus, Consensus::FS_ZF, FS_ZF_ADDRESSES, 5, 100);
AddZIP207FundingStream(consensus, Consensus::FS_MG, FS_MG_ADDRESSES, 8, 100);
int devFundStartHeight = HeightForHalving(params, 1);
int devFundEndHeight = HeightForHalving(params, 2);
AddZIP207FundingStream(consensus, Consensus::FS_ECC, FS_ECC_ADDRESSES, 7, 100, devFundStartHeight, devFundEndHeight);
AddZIP207FundingStream(consensus, Consensus::FS_ZF, FS_ZF_ADDRESSES, 5, 100, devFundStartHeight, devFundEndHeight);
AddZIP207FundingStream(consensus, Consensus::FS_MG, FS_MG_ADDRESSES, 8, 100, devFundStartHeight, devFundEndHeight);
...
}
Expand All @@ -266,25 +269,31 @@ Example implementation
const Consensus::Params& params,
Consensus::FundingStream idx)
{
// Integer division is floor division in C++
auto curPeriod = (
nHeight + params.nSubsidyPostBlossomHalvingInterval - HeightForHalving(params, 1)
) / params.nFundingPeriodLength;
auto startPeriod = (
params.vFundingPeriods[idx].startHeight
+ params.nSubsidyPostBlossomHalvingInterval
- HeightForHalving(params, 1)
) / params.nFundingPeriodLength;
assert(nHeight <= INT_MAX - params.nSubsidyPostBlossomHalvingInterval);
assert(params.vFundingPeriods[idx].startHeight <= INT_MAX - params.nSubsidyPostBlossomHalvingInterval);
int curPeriodNumerator = nHeight + params.nSubsidyPostBlossomHalvingInterval - HeightForHalving(params, 1);
int startPeriodNumerator = params.vFundingPeriods[idx].startHeight + params.nSubsidyPostBlossomHalvingInterval
- HeightForHalving(params, 1);
// Integer division is floor division for nonnegative integers in C++
assert(curPeriodNumerator >= 0);
assert(startPeriodNumerator >= 0);
auto curPeriod = curPeriodNumerator / params.nFundingPeriodLength;
auto startPeriod = startPeriodNumerator / params.nFundingPeriodLength;
auto addressIndex = curPeriod - startPeriod;
return params.vFundingPeriods[idx].addresses[addressIndex];
auto addresses = params.vFundingPeriods[idx].addresses;
assert(addressIndex >= 0 && addressIndex < addresses.size());
return addresses[addressIndex];
};
CAmount FundingStreamValue(
int nHeight,
const Consensus::Params& params,
Consensus::FundingStream idx)
{
// Integer division is floor division in C++
// Integer division is floor division for nonnegative integers in C++
return CAmount((
GetBlockSubsidy(nHeight, params) * params.vFundingPeriods[idx].valueNumerator
) / params.vFundingPeriods[idx].valueDenominator);
Expand Down

0 comments on commit c4c4037

Please sign in to comment.