Skip to content

Commit

Permalink
Merge pull request #1114 from stevenroose/fix-dynafed-json
Browse files Browse the repository at this point in the history
Improve the JSON representation of dynafed parameters
  • Loading branch information
gwillen authored Jun 28, 2022
2 parents 03b8b0c + 3e77add commit b4d4b1a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
42 changes: 37 additions & 5 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,46 @@ double GetDifficulty(const CBlockIndex* blockindex)
UniValue paramEntryToJSON(const DynaFedParamEntry& entry)
{
UniValue result(UniValue::VOBJ);

// set the type
if (entry.m_serialize_type == 0) {
result.pushKV("type", "null");
} else if (entry.m_serialize_type == 1) {
result.pushKV("type", "compact");
} else if (entry.m_serialize_type == 2) {
result.pushKV("type", "full");
}

// nothing more to do for null
if (entry.m_serialize_type == 0) {
return result;
}

// fields all params have
result.pushKV("root", HexStr(entry.CalculateRoot()));
result.pushKV("signblockscript", HexStr(entry.m_signblockscript));
result.pushKV("max_block_witness", (uint64_t)entry.m_signblock_witness_limit);
result.pushKV("fedpegscript", HexStr(entry.m_fedpegscript));
UniValue result_extension(UniValue::VARR);
for (auto& item : entry.m_extension_space) {
result_extension.push_back(HexStr(item));

// add the extra root which is stored for compact and calculated for full
if (entry.m_serialize_type == 1) {
// compact
result.pushKV("extra_root", HexStr(entry.m_elided_root));
} else if (entry.m_serialize_type == 2) {
// full
result.pushKV("extra_root", HexStr(entry.CalculateExtraRoot()));
}

// some extra fields only present on full params
if (entry.m_serialize_type == 2) {
result.pushKV("fedpeg_program", HexStr(entry.m_fedpeg_program));
result.pushKV("fedpegscript", HexStr(entry.m_fedpegscript));
UniValue result_extension(UniValue::VARR);
for (auto& item : entry.m_extension_space) {
result_extension.push_back(HexStr(item));
}
result.pushKV("extension_space", result_extension);
}
result.pushKV("extension_space", result_extension);

return result;
}

Expand Down
16 changes: 9 additions & 7 deletions test/functional/feature_dynafed.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def go_to_epoch_end(node):
node.generatetoaddress(blocks_to_mine, node.getnewaddress())

def validate_no_vote_op_true(node, block, first_dynafed_active_block):

block_info = node.getblock(block)
dynamic_parameters = block_info["dynamic_parameters"]
block_height = block_info["height"]
Expand All @@ -48,18 +47,21 @@ def validate_no_vote_op_true(node, block, first_dynafed_active_block):
WSH_OP_TRUE = node.decodescript("51")["segwit"]["hex"]
assert_equal(dynamic_parameters["current"]["signblockscript"], WSH_OP_TRUE)
if block_height % 10 == 0 or first_dynafed_active_block:
assert_equal(dynamic_parameters["current"]["type"], "full")
assert_equal(dynamic_parameters["current"]["fedpegscript"], "51")
assert_equal(dynamic_parameters["current"]["extension_space"], initial_extension)
else:
assert_equal(dynamic_parameters["current"]["fedpegscript"], "")
assert_equal(dynamic_parameters["current"]["extension_space"], [])
assert_equal(dynamic_parameters["current"]["type"], "compact")
assert not "fedpegscript" in dynamic_parameters["proposed"]
assert not "extension_space" in dynamic_parameters["proposed"]
assert_equal(dynamic_parameters["current"]["max_block_witness"], 74)
# nothing was proposed, null fields make impossible to be valid blockheader
# due to script rules requiring bool true on stack
assert_equal(dynamic_parameters["proposed"]["signblockscript"], "")
assert_equal(dynamic_parameters["proposed"]["fedpegscript"], "")
assert_equal(dynamic_parameters["proposed"]["max_block_witness"], 0)
assert_equal(dynamic_parameters["proposed"]["extension_space"], [])
assert_equal(dynamic_parameters["proposed"]["type"], "null")
assert not "signblockscript" in dynamic_parameters["proposed"]
assert not "max_block_witness" in dynamic_parameters["proposed"]
assert not "fedpegscript" in dynamic_parameters["proposed"]
assert not "extension_space" in dynamic_parameters["proposed"]

class DynaFedTest(BitcoinTestFramework):
def set_test_params(self):
Expand Down

0 comments on commit b4d4b1a

Please sign in to comment.