Skip to content

Commit 47db75e

Browse files
committed
Dynafed RPC support, tests, and deployment for custom chains
1 parent aac354b commit 47db75e

File tree

15 files changed

+855
-348
lines changed

15 files changed

+855
-348
lines changed

src/chainparams.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,10 @@ class CCustomParams : public CRegTestParams {
605605
consensus.vDeployments[Consensus::DEPLOYMENT_CSV].nStartTime = args.GetArg("-con_csv_deploy_start", Consensus::BIP9Deployment::ALWAYS_ACTIVE);
606606
consensus.vDeployments[Consensus::DEPLOYMENT_CSV].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;
607607

608+
consensus.vDeployments[Consensus::DEPLOYMENT_DYNA_FED].bit = 25;
609+
consensus.vDeployments[Consensus::DEPLOYMENT_DYNA_FED].nStartTime = args.GetArg("-con_dyna_deploy_start", Consensus::BIP9Deployment::ALWAYS_ACTIVE);
610+
consensus.vDeployments[Consensus::DEPLOYMENT_DYNA_FED].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;
611+
608612
}
609613

610614
void SetGenesisBlock() {

src/rpc/blockchain.cpp

Lines changed: 114 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include <validationinterface.h>
3434
#include <versionbitsinfo.h>
3535
#include <warnings.h>
36+
#include <pegins.h>
37+
#include <dynafed.h>
3638

3739
#include <assert.h>
3840
#include <stdint.h>
@@ -79,6 +81,29 @@ double GetDifficulty(const CBlockIndex* blockindex)
7981
return dDiff;
8082
}
8183

84+
UniValue paramEntryToJSON(const ConsensusParamEntry& entry)
85+
{
86+
UniValue result(UniValue::VOBJ);
87+
result.pushKV("signblockscript", HexStr(entry.m_signblockscript));
88+
result.pushKV("max_block_witness", (uint64_t)entry.m_sbs_wit_limit);
89+
result.pushKV("fedpegscript", HexStr(entry.m_fedpegscript));
90+
UniValue result_extension(UniValue::VARR);
91+
for (auto& item : entry.m_extension_space) {
92+
result_extension.push_back(HexStr(item));
93+
}
94+
result.pushKV("extension_space", result_extension);
95+
return result;
96+
}
97+
98+
UniValue dynaParamsToJSON(const DynaFedParams& d_params)
99+
{
100+
AssertLockHeld(cs_main);
101+
UniValue ret(UniValue::VOBJ);
102+
ret.pushKV("current", paramEntryToJSON(d_params.m_current));
103+
ret.pushKV("proposed", paramEntryToJSON(d_params.m_proposed));
104+
return ret;
105+
}
106+
82107
static int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* blockindex, const CBlockIndex*& next)
83108
{
84109
next = tip->GetAncestor(blockindex->nHeight + 1);
@@ -110,6 +135,9 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
110135
} else {
111136
result.pushKV("signblock_witness_asm", ScriptToAsmStr(blockindex->proof.solution));
112137
result.pushKV("signblock_witness_hex", HexStr(blockindex->proof.solution));
138+
if (!blockindex->d_params.IsNull()) {
139+
result.pushKV("dynamic_parameters", dynaParamsToJSON(blockindex->d_params));
140+
}
113141
}
114142
result.pushKV("nTx", (uint64_t)blockindex->nTx);
115143
if (blockindex->pprev)
@@ -154,9 +182,13 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIn
154182
result.pushKV("difficulty", GetDifficulty(blockindex));
155183
result.pushKV("chainwork", blockindex->nChainWork.GetHex());
156184
} else {
157-
result.pushKV("signblock_witness_asm", ScriptToAsmStr(blockindex->proof.solution));
158-
result.pushKV("signblock_witness_hex", HexStr(blockindex->proof.solution));
159-
result.pushKV("signblock_challenge", HexStr(blockindex->proof.challenge));
185+
if (block.m_dyna_params.IsNull()) {
186+
result.pushKV("signblock_witness_asm", ScriptToAsmStr(blockindex->proof.solution));
187+
result.pushKV("signblock_witness_hex", HexStr(blockindex->proof.solution));
188+
result.pushKV("signblock_challenge", HexStr(blockindex->proof.challenge));
189+
} else {
190+
result.pushKV("dynamic_parameters", dynaParamsToJSON(block.m_dyna_params));
191+
}
160192
}
161193
result.pushKV("nTx", (uint64_t)blockindex->nTx);
162194

@@ -771,6 +803,24 @@ static UniValue getblockheader(const JSONRPCRequest& request)
771803
" \"nTx\" : n, (numeric) The number of transactions in the block.\n"
772804
" \"signblock_witness_asm\" : \"xxxx\", (string) ASM of sign block witness data.\n"
773805
" \"signblock_witness_hex\" : \"xxxx\", (string) Hex of sign block witness data.\n"
806+
" \"dynamic_parameters\" : (obj) Dynamic federation parameters in the block, if any.\n"
807+
" {\n"
808+
" \"current\" : (obj) enforced dynamic federation parameters. Do note that only the signblockscript is published for each block, while others are published only at epoch start.\n"
809+
" {\n"
810+
" \"signblockscript\" : \"xxxx\", (string) signblock script in hex\n"
811+
" \"max_block_witness\" : x, (numeric) Maximum serialized size of the block witness stack\n"
812+
" \"fedpegscript\" : \"xxxx\", (string) fedpegscript in hex\n"
813+
" \"extension_space\" : (array) Array of hex-encoded strings\n"
814+
" [\n"
815+
" xxxx,\n"
816+
" ...\n"
817+
" ]\n"
818+
" }\n"
819+
" \"proposed\" : (obj) Proposed paramaters. Uninforced. Must be published in full.\n"
820+
" {\n"
821+
" ... same entries as current\n"
822+
" }\n"
823+
" }\n"
774824
" \"previousblockhash\" : \"hash\", (string) The hash of the previous block\n"
775825
" \"nextblockhash\" : \"hash\", (string) The hash of the next block\n"
776826
"}\n"
@@ -873,6 +923,24 @@ static UniValue getblock(const JSONRPCRequest& request)
873923
" \"nTx\" : n, (numeric) The number of transactions in the block.\n"
874924
" \"signblock_witness_asm\" : \"xxxx\", (string) ASM of sign block witness data.\n"
875925
" \"signblock_witness_hex\" : \"xxxx\", (string) Hex of sign block witness data.\n"
926+
" \"dynamic_parameters\" : (obj) Dynamic federation parameters in the block, if any.\n"
927+
" {\n"
928+
" \"current\" : (obj) enforced dynamic federation parameters. Do note that only the signblockscript is published for each block, while others are published only at epoch start.\n"
929+
" {\n"
930+
" \"signblockscript\" : \"xxxx\", (string) signblock script in hex\n"
931+
" \"max_block_witness\" : x, (numeric) Maximum serialized size of the block witness stack\n"
932+
" \"fedpegscript\" : \"xxxx\", (string) fedpegscript in hex\n"
933+
" \"extension_space\" : (array) Array of hex-encoded strings\n"
934+
" [\n"
935+
" xxxx,\n"
936+
" ...\n"
937+
" ]\n"
938+
" }\n"
939+
" \"proposed\" : (obj) Proposed paramaters. Uninforced. Must be published in full.\n"
940+
" {\n"
941+
" ... same entries as current\n"
942+
" }\n"
943+
" }\n"
876944
" \"previousblockhash\" : \"hash\", (string) The hash of the previous block\n"
877945
" \"nextblockhash\" : \"hash\" (string) The hash of the next block\n"
878946
"}\n"
@@ -1310,8 +1378,14 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
13101378
" \"chainwork\": \"xxxx\" (string) total amount of work in active chain, in hexadecimal\n"
13111379
" \"size_on_disk\": xxxxxx, (numeric) the estimated size of the block and undo files on disk\n"
13121380
" \"pruned\": xx, (boolean) if the blocks are subject to pruning\n"
1313-
" \"signblock_asm\" : \"xxxx\", (string) ASM of sign block challenge data.\n"
1314-
" \"signblock_hex\" : \"xxxx\", (string) Hex of sign block challenge data.\n"
1381+
" \"signblock_asm\" : \"xxxx\", (string) ASM of sign block challenge data from genesis block.\n"
1382+
" \"signblock_hex\" : \"xxxx\", (string) Hex of sign block challenge data from genesis block.\n"
1383+
" \"current_signblock_asm\" : \"xxxx\", (string) ASM of sign block challenge data enforced on the next block.\n"
1384+
" \"current_signblock_hex\" : \"xxxx\", (string) Hex of sign block challenge data enforced on the next block.\n"
1385+
" \"max_block_witness\" : xx, (numeric) maximum sized block witness serialized size for the next block.\n"
1386+
" \"epoch_length\" : xx, (numeric) Length of dynamic federations epoch, or signaling period\n"
1387+
" \"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"
1388+
" \"extension_space\" : [\"xxxx\", ...], (array) Array of extension fields in dynamic blockheader\n"
13151389
" \"pruneheight\": xxxxxx, (numeric) lowest-height complete block stored (only present if pruning is enabled)\n"
13161390
" \"automatic_pruning\": xx, (boolean) whether automatic pruning is enabled (only present if pruning is enabled)\n"
13171391
" \"prune_target_size\": xxxxxx, (numeric) the target size used by pruning (only present if automatic pruning is enabled)\n"
@@ -1374,6 +1448,28 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
13741448
CScript sign_block_script = chainparams.GetConsensus().signblockscript;
13751449
obj.pushKV("signblock_asm", ScriptToAsmStr(sign_block_script));
13761450
obj.pushKV("signblock_hex", HexStr(sign_block_script));
1451+
if (!IsDynaFedEnabled(chainActive.Tip(), chainparams.GetConsensus())) {
1452+
obj.pushKV("current_signblock_asm", ScriptToAsmStr(sign_block_script));
1453+
obj.pushKV("current_signblock_hex", HexStr(sign_block_script));
1454+
obj.pushKV("max_block_witness", (uint64_t)chainparams.GetConsensus().max_block_signature_size);
1455+
UniValue arr(UniValue::VARR);
1456+
for (const auto& extension : chainparams.GetConsensus().first_extension_space) {
1457+
arr.push_back(HexStr(extension));
1458+
}
1459+
obj.pushKV("extension_space", arr);
1460+
} else {
1461+
const ConsensusParamEntry entry = ComputeNextBlockFullCurrentParameters(chainActive.Tip(), chainparams.GetConsensus());
1462+
obj.pushKV("current_signblock_asm", ScriptToAsmStr(entry.m_signblockscript));
1463+
obj.pushKV("current_signblock_hex", HexStr(entry.m_signblockscript));
1464+
obj.pushKV("max_block_witness", (uint64_t)entry.m_sbs_wit_limit);
1465+
UniValue arr(UniValue::VARR);
1466+
for (const auto& extension : entry.m_extension_space) {
1467+
arr.push_back(HexStr(extension));
1468+
}
1469+
obj.pushKV("extension_space", arr);
1470+
obj.pushKV("epoch_length", (uint64_t)chainparams.GetConsensus().dynamic_epoch_length);
1471+
obj.pushKV("epoch_age", (uint64_t)(chainActive.Tip()->nHeight % chainparams.GetConsensus().dynamic_epoch_length));
1472+
}
13771473
}
13781474

13791475
if (fPruneMode) {
@@ -2407,7 +2503,12 @@ UniValue getsidechaininfo(const JSONRPCRequest& request)
24072503
{},
24082504
RPCResult{
24092505
"{\n"
2410-
" \"fedpegscript\": \"xxxx\", (string) The fedpegscript in hex\n"
2506+
" \"fedpegscript\": \"xxxx\", (string) The fedpegscript in hex from genesis block\n"
2507+
" \"current_fedpegscripts\": (array) The currently-enforced fedpegscripts in hex. Peg-ins for any entries on this list are honored by consensus and policy. Oldest first. Two total entries are possible.\n"
2508+
" [\n"
2509+
" \"xxxx\", (string) Hex-encoded active fedpegscript\n"
2510+
" ...\n"
2511+
" ]\n"
24112512
" \"pegged_asset\" : \"xxxx\", (string) Pegged asset type in hex\n"
24122513
" \"min_peg_diff\" : \"xxxx\", (string) The minimum difficulty parent chain header target. Peg-in headers that have less work will be rejected as an anti-Dos measure.\n"
24132514
" \"parent_blockhash\" : \"xxxx\", (string) The parent genesis blockhash as source of pegged-in funds.\n"
@@ -2432,6 +2533,13 @@ UniValue getsidechaininfo(const JSONRPCRequest& request)
24322533

24332534
UniValue obj(UniValue::VOBJ);
24342535
obj.pushKV("fedpegscript", HexStr(consensus.fedpegScript.begin(), consensus.fedpegScript.end()));
2536+
// We use mempool_validation as true to show what is enforced for *next* block
2537+
std::vector<CScript> fedpegscripts = GetValidFedpegScripts(chainActive.Tip(), consensus, true /* nextblock_validation */);
2538+
UniValue fedpeg_entries(UniValue::VARR);
2539+
for (const auto& script : fedpegscripts) {
2540+
fedpeg_entries.push_back(HexStr(script));
2541+
}
2542+
obj.pushKV("current_fedpegscripts", fedpeg_entries);
24352543
obj.pushKV("pegged_asset", consensus.pegged_asset.GetHex());
24362544
obj.pushKV("min_peg_diff", consensus.parentChainPowLimit.GetHex());
24372545
obj.pushKV("parent_blockhash", parent_blockhash.GetHex());

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
176176
{ "rawissueasset", 1, "issuances" },
177177
{ "rawreissueasset", 1, "reissuances" },
178178
{ "getnewblockhex", 0, "min_tx_age" },
179+
{ "getnewblockhex", 1, "proposed_parameters" },
179180
{ "testproposedblock", 1, "acceptnonstd" },
180181
{ "issueasset", 0, "assetamount" },
181182
{ "issueasset", 1, "tokenamount" },

0 commit comments

Comments
 (0)