Skip to content

Commit f7905d6

Browse files
committed
Generalize the number of epochs old a peg-in can be and still be valid
1 parent b105ff4 commit f7905d6

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

src/chainparams.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class CMainParams : public CChainParams {
134134
g_signed_blocks = false;
135135
g_con_elementsmode = false;
136136
g_con_blockheightinheader = false;
137+
consensus.total_valid_epochs = 0;
137138

138139
/**
139140
* The message start string is designed to be unlikely to occur in normal data.
@@ -263,6 +264,7 @@ class CTestNetParams : public CChainParams {
263264
g_signed_blocks = false;
264265
g_con_elementsmode = false;
265266
g_con_blockheightinheader = false;
267+
consensus.total_valid_epochs = 0;
266268

267269
pchMessageStart[0] = 0x0b;
268270
pchMessageStart[1] = 0x11;
@@ -366,6 +368,7 @@ class CRegTestParams : public CChainParams {
366368
g_signed_blocks = false;
367369
g_con_elementsmode = false;
368370
g_con_blockheightinheader = false;
371+
consensus.total_valid_epochs = 0;
369372

370373
pchMessageStart[0] = 0xfa;
371374
pchMessageStart[1] = 0xbf;
@@ -582,6 +585,8 @@ class CCustomParams : public CRegTestParams {
582585
uint256 entropy;
583586
GenerateAssetEntropy(entropy, COutPoint(uint256(commit), 0), parentGenesisBlockHash);
584587

588+
consensus.total_valid_epochs = args.GetArg("-total_valid_epochs", 2);
589+
585590
// Elements serialization uses derivation, bitcoin serialization uses 0x00
586591
if (g_con_elementsmode) {
587592
CalculateAsset(consensus.pegged_asset, entropy);

src/chainparamsbase.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ void SetupChainParamsBaseOptions()
4848
gArgs.AddArg("-con_csv_deploy_start", "Starting height for CSV deployment. (default: -1, which means ACTIVE from genesis)", false, OptionsCategory::ELEMENTS);
4949
gArgs.AddArg("-con_dyna_deploy_start", "Starting height for Dynamic Federations deployment. Once active, signblockscript becomes a BIP141 WSH scriptPubKey of the original signblockscript. All other dynamic parameters stay constant.(default: -1, which means ACTIVE from genesis)", false, OptionsCategory::ELEMENTS);
5050
gArgs.AddArg("-dynamic_epoch_length", "Per-chain parameter that sets how many blocks dynamic federation voting and enforcement are in effect for.", false, OptionsCategory::ELEMENTS);
51+
gArgs.AddArg("-total_valid_epochs", "Per-chain parameter that sets how long a particular fedpegscript is in effect for.", false, OptionsCategory::ELEMENTS);
5152
// END ELEMENTS
5253
//
5354
}

src/consensus/params.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ struct Params {
105105
uint32_t dynamic_epoch_length = std::numeric_limits<uint32_t>::max();
106106
// Used to seed the extension space for first dynamic blocks
107107
std::vector<std::vector<unsigned char>> first_extension_space;
108+
// Used to allow M-epoch-old peg-in addresses as deposits
109+
size_t total_valid_epochs;
108110
};
109111
} // namespace Consensus
110112

src/pegins.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -476,27 +476,23 @@ std::vector<std::pair<CScript, CScript>> GetValidFedpegScripts(const CBlockIndex
476476
fedpegscripts.push_back(std::make_pair(next_param.m_fedpeg_program, next_param.m_fedpegscript));
477477
}
478478

479-
// Next we walk backwards up to two epoch start blocks
480-
const CBlockIndex* p_current_epoch_start = pblockindex->GetAncestor(epoch_start_height);
481-
const CBlockIndex* p_prev_epoch_start = pblockindex->GetAncestor(epoch_start_height-epoch_length);
479+
// Next we walk backwards up to M epoch starts
480+
for (size_t i = 0; i < params.total_valid_epochs; i++) {
482481

483-
if (p_current_epoch_start) {
484-
if (!p_current_epoch_start->dynafed_params.IsNull()) {
485-
fedpegscripts.push_back(std::make_pair(p_current_epoch_start->dynafed_params.m_current.m_fedpeg_program, p_current_epoch_start->dynafed_params.m_current.m_fedpegscript));
486-
} else {
487-
fedpegscripts.push_back(std::make_pair(GetScriptForDestination(ScriptHash(GetScriptForDestination(WitnessV0ScriptHash(params.fedpegScript)))), params.fedpegScript));
482+
const CBlockIndex* p_epoch_start = pblockindex->GetAncestor(epoch_start_height-i*epoch_length);
483+
484+
// We're done here, for whatever reason.
485+
if (!p_epoch_start) {
486+
break;
488487
}
489-
}
490488

491-
if (p_prev_epoch_start) {
492-
if (!p_prev_epoch_start->dynafed_params.IsNull()) {
493-
fedpegscripts.push_back(std::make_pair(p_prev_epoch_start->dynafed_params.m_current.m_fedpeg_program, p_prev_epoch_start->dynafed_params.m_current.m_fedpegscript));
489+
if (!p_epoch_start->dynafed_params.IsNull()) {
490+
fedpegscripts.push_back(std::make_pair(p_epoch_start->dynafed_params.m_current.m_fedpeg_program, p_epoch_start->dynafed_params.m_current.m_fedpegscript));
494491
} else {
495492
fedpegscripts.push_back(std::make_pair(GetScriptForDestination(ScriptHash(GetScriptForDestination(WitnessV0ScriptHash(params.fedpegScript)))), params.fedpegScript));
496493
}
497494
}
498-
499495
// Only return up to the latest two of three possible fedpegscripts, which are enforced
500-
fedpegscripts.resize(std::min((int)fedpegscripts.size(), 2));
496+
fedpegscripts.resize(std::min(fedpegscripts.size(), params.total_valid_epochs));
501497
return fedpegscripts;
502498
}

src/rpc/blockchain.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
13841384
" \"current_signblock_hex\" : \"xxxx\", (string) Hex of sign block challenge data enforced on the next block.\n"
13851385
" \"max_block_witness\" : xx, (numeric) maximum sized block witness serialized size for the next block.\n"
13861386
" \"epoch_length\" : xx, (numeric) Length of dynamic federations epoch, or signaling period\n"
1387+
" \"total_valid_epochs\" : xx, (numeric) Number of epochs a given fedpscript is valid for, defined per chain.\n"
13871388
" \"epoch_age\" : xx, (numeric) number of blocks into a dynamic federation epoch chain tip is. This number is between 0 to epoch_length-1\n"
13881389
" \"extension_space\" : [\"xxxx\", ...], (array) Array of extension fields in dynamic blockheader\n"
13891390
" \"pruneheight\": xxxxxx, (numeric) lowest-height complete block stored (only present if pruning is enabled)\n"
@@ -1468,6 +1469,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
14681469
}
14691470
obj.pushKV("extension_space", arr);
14701471
obj.pushKV("epoch_length", (uint64_t)chainparams.GetConsensus().dynamic_epoch_length);
1472+
obj.pushKV("total_valid_epochs", (uint64_t)chainparams.GetConsensus().total_valid_epochs);
14711473
obj.pushKV("epoch_age", (uint64_t)(chainActive.Tip()->nHeight % chainparams.GetConsensus().dynamic_epoch_length));
14721474
}
14731475
}

0 commit comments

Comments
 (0)