Skip to content

Commit af485a8

Browse files
committed
Add fedpegscript-fetching helper
1 parent d1207bd commit af485a8

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/pegins.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <script/standard.h>
2121
#include <streams.h>
2222
#include <util/system.h>
23+
#include <dynafed.h>
2324

2425
//
2526
// ELEMENTS
@@ -443,3 +444,44 @@ bool MatchLiquidWatchman(const CScript& script)
443444
// No more pushes
444445
return (it == script.end());
445446
}
447+
448+
std::vector<CScript> GetValidFedpegScripts(const CBlockIndex* pblockindex, const Consensus::Params& params, bool nextblock_validation)
449+
{
450+
assert(pblockindex);
451+
452+
std::vector<CScript> fedpegscripts;
453+
454+
const int32_t epoch_length = params.dynamic_epoch_length;
455+
const int32_t epoch_age = pblockindex->nHeight % epoch_length;
456+
const int32_t epoch_start_height = pblockindex->nHeight - epoch_age;
457+
458+
// In mempool and general "enforced next block" RPC we need to look ahead one block
459+
// to see if we're on a boundary. If so, put that epoch's fedpegscript in place
460+
if (nextblock_validation && epoch_age == epoch_length - 1) {
461+
fedpegscripts.push_back(ComputeNextBlockFullCurrentParameters(pblockindex, params).m_fedpegscript);
462+
}
463+
464+
// Next we walk backwards up to two epoch start blocks
465+
const CBlockIndex* p_current_epoch_start = pblockindex->GetAncestor(epoch_start_height);
466+
const CBlockIndex* p_prev_epoch_start = pblockindex->GetAncestor(epoch_start_height-epoch_length);
467+
468+
if (p_current_epoch_start) {
469+
if (!p_current_epoch_start->d_params.IsNull()) {
470+
fedpegscripts.push_back(p_current_epoch_start->d_params.m_current.m_fedpegscript);
471+
} else {
472+
fedpegscripts.push_back(params.fedpegScript);
473+
}
474+
}
475+
476+
if (p_prev_epoch_start) {
477+
if (!p_prev_epoch_start->d_params.IsNull()) {
478+
fedpegscripts.push_back(p_prev_epoch_start->d_params.m_current.m_fedpegscript);
479+
} else {
480+
fedpegscripts.push_back(params.fedpegScript);
481+
}
482+
}
483+
484+
// Only return up to the latest two of three possible fedpegscripts, which are enforced
485+
fedpegscripts.resize(std::min((int)fedpegscripts.size(), 2));
486+
return fedpegscripts;
487+
}

0 commit comments

Comments
 (0)