Skip to content

Commit 565b23f

Browse files
committed
HF: Dyanfed pruned header now has extra root
1 parent 08023b0 commit 565b23f

File tree

7 files changed

+52
-21
lines changed

7 files changed

+52
-21
lines changed

src/dynafed.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ DynaFedParamEntry ComputeNextBlockCurrentParameters(const CBlockIndex* pindexPre
9494
// Return appropriate format based on epoch age
9595
if (epoch_age > 0) {
9696
// TODO implement "prune" function to remove fields in place and change serialize type
97-
return DynaFedParamEntry(entry.m_signblockscript, entry.m_signblock_witness_limit);
97+
return DynaFedParamEntry(entry.m_signblockscript, entry.m_signblock_witness_limit, entry.CalculateExtraRoot());
9898
} else {
9999
return entry;
100100
}

src/miner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
151151
fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus());
152152

153153
if (IsDynaFedEnabled(pindexPrev, chainparams.GetConsensus())) {
154-
DynaFedParamEntry current_params = ComputeNextBlockCurrentParameters(chainActive.Tip(), chainparams.GetConsensus());
155-
DynaFedParams block_params(current_params, proposed_entry ? *proposed_entry : DynaFedParamEntry());
154+
const DynaFedParamEntry current_params = ComputeNextBlockCurrentParameters(chainActive.Tip(), chainparams.GetConsensus());
155+
const DynaFedParams block_params(current_params, proposed_entry ? *proposed_entry : DynaFedParamEntry());
156156
pblock->m_dynafed_params = block_params;
157157
nBlockWeight += ::GetSerializeSize(block_params, PROTOCOL_VERSION)*WITNESS_SCALE_FACTOR;
158158
nBlockWeight += current_params.m_signblock_witness_limit; // Note witness discount

src/primitives/block.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,39 @@ std::string CBlock::ToString() const
4343

4444
uint256 DynaFedParamEntry::CalculateRoot() const
4545
{
46-
if (IsNull()) {
46+
if (m_serialize_type == 0) {
4747
return uint256();
4848
}
4949

50+
std::vector<uint256> compact_leaves;
51+
compact_leaves.push_back(SerializeHash(m_signblockscript, SER_GETHASH, 0));
52+
compact_leaves.push_back(SerializeHash(m_signblock_witness_limit, SER_GETHASH, 0));
53+
uint256 compact_root(ComputeFastMerkleRoot(compact_leaves));
54+
55+
uint256 extra_root;
56+
if (m_serialize_type ==1 ) {
57+
// It's pruned, take the stored value
58+
extra_root = m_elided_root;
59+
} else if (m_serialize_type == 2) {
60+
// It's unpruned, compute the node value
61+
extra_root = CalculateExtraRoot();
62+
}
63+
5064
std::vector<uint256> leaves;
51-
leaves.push_back(SerializeHash(m_signblockscript, SER_GETHASH, 0));
52-
leaves.push_back(SerializeHash(m_signblock_witness_limit, SER_GETHASH, 0));
53-
leaves.push_back(SerializeHash(m_fedpeg_program, SER_GETHASH, 0));
54-
leaves.push_back(SerializeHash(m_fedpegscript, SER_GETHASH, 0));
55-
leaves.push_back(SerializeHash(m_extension_space, SER_GETHASH, 0));
65+
leaves.push_back(compact_root);
66+
leaves.push_back(extra_root);
5667
return ComputeFastMerkleRoot(leaves);
5768
}
5869

70+
uint256 DynaFedParamEntry::CalculateExtraRoot() const
71+
{
72+
std::vector<uint256> extra_leaves;
73+
extra_leaves.push_back(SerializeHash(m_fedpeg_program, SER_GETHASH, 0));
74+
extra_leaves.push_back(SerializeHash(m_fedpegscript, SER_GETHASH, 0));
75+
extra_leaves.push_back(SerializeHash(m_extension_space, SER_GETHASH, 0));
76+
return ComputeFastMerkleRoot(extra_leaves);
77+
}
78+
5979
uint256 DynaFedParams::CalculateRoot() const
6080
{
6181
if (IsNull()) {

src/primitives/block.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ class DynaFedParamEntry
6464
CScript m_fedpeg_program; // The "scriptPubKey" of the fedpegscript
6565
CScript m_fedpegscript; // The witnessScript for witness v0 or undefined otherwise.
6666
// No consensus meaning to the particular bytes, currently we interpret as PAK keys, details in pak.h
67-
std::vector<std::vector<unsigned char>> m_extension_space;
67+
std::vector<std::vector<unsigned char>> m_extension_space;\
68+
uint256 m_elided_root; // non-zero only when m_serialize_type == 1
6869

6970
// Each constructor sets its own serialization type implicitly based on which
7071
// arguments are given
7172
DynaFedParamEntry() { m_signblock_witness_limit = 0; m_serialize_type = 0; };
72-
DynaFedParamEntry(const CScript& signblockscript_in, const uint32_t sbs_wit_limit_in) : m_signblockscript(signblockscript_in), m_signblock_witness_limit(sbs_wit_limit_in) { m_serialize_type = 1; };
73+
DynaFedParamEntry(const CScript& signblockscript_in, const uint32_t sbs_wit_limit_in, const uint256 elided_root_in) : m_signblockscript(signblockscript_in), m_signblock_witness_limit(sbs_wit_limit_in), m_elided_root(elided_root_in) { m_serialize_type = 1; };
7374
DynaFedParamEntry(const CScript& signblockscript_in, const uint32_t sbs_wit_limit_in, const CScript& fedpeg_program_in, const CScript& fedpegscript_in, const std::vector<std::vector<unsigned char>> extension_space_in) : m_signblockscript(signblockscript_in), m_signblock_witness_limit(sbs_wit_limit_in), m_fedpeg_program(fedpeg_program_in), m_fedpegscript(fedpegscript_in), m_extension_space(extension_space_in) { m_serialize_type = 2; };
7475

7576
ADD_SERIALIZE_METHODS;
@@ -84,6 +85,7 @@ class DynaFedParamEntry
8485
case 1:
8586
READWRITE(m_signblockscript);
8687
READWRITE(m_signblock_witness_limit);
88+
READWRITE(m_elided_root);
8789
break;
8890
case 2:
8991
READWRITE(m_signblockscript);
@@ -98,6 +100,8 @@ class DynaFedParamEntry
98100
}
99101

100102
uint256 CalculateRoot() const;
103+
// Calculates root for the non-blocksigning merkle fields
104+
uint256 CalculateExtraRoot() const;
101105

102106
bool IsNull() const
103107
{

src/test/dynafed_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ BOOST_AUTO_TEST_CASE(dynafed_params_root)
2121
CScript fp_script(opcodetype(4));
2222
std::vector<std::vector<unsigned char>> ext{ {5, 6}, {7} };
2323

24-
DynaFedParamEntry compact_entry = DynaFedParamEntry(signblockscript, signblock_wl);
24+
DynaFedParamEntry compact_entry = DynaFedParamEntry(signblockscript, signblock_wl, uint256());
2525
BOOST_CHECK_EQUAL(
2626
compact_entry.CalculateRoot().GetHex(),
27-
"dff5f3793abc06a6d75e80fe3cfd47406f732fa4ec9305960ae2a229222a1ad5"
27+
"f98f149fd11da6fbe26d0ee53cadd28372fa9eed2cb7080f41da7ca311531777"
2828
);
2929

3030
DynaFedParamEntry full_entry =
3131
DynaFedParamEntry(signblockscript, signblock_wl, fp_program, fp_script, ext);
3232
BOOST_CHECK_EQUAL(
3333
full_entry.CalculateRoot().GetHex(),
34-
"175be2087ba7cc0e33348bef493bd3e34f31f64bf9226e5881ab310dafa432ff"
34+
"8eb1b83cce69a3d8b0bfb7fbe77ae8f1d24b57a9cae047b8c0aba084ad878249"
3535
);
3636

3737
DynaFedParams params = DynaFedParams(compact_entry, full_entry);
3838
BOOST_CHECK_EQUAL(
3939
params.CalculateRoot().GetHex(),
40-
"e56cf79487952dfa85fe6a85829600adc19714ba6ab1157fdff02b25ae60cee2"
40+
"113160f76dc17fe367a2def79aefe06feeea9c795310c9e88aeedc23e145982e"
4141
);
4242
}
4343

test/functional/feature_blocksign.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,16 @@ def run_test(self):
218218

219219
# Next let's activate dynafed
220220
blocks_til_dynafed = 431 - self.nodes[0].getblockcount()
221+
self.log.info("Activating dynafed")
221222
self.mine_blocks(blocks_til_dynafed, False)
222223
self.check_height(111+blocks_til_dynafed)
223224

224225
assert_equal(self.nodes[0].getblockchaininfo()['bip9_softforks']['dynafed']['status'], "active")
225226

226-
self.log.info("Mine some dynamic federation blocks without and with txns")
227-
self.mine_blocks(50, False)
228-
self.mine_blocks(50, True)
227+
self.log.info("Mine some dynamic federation blocks without txns")
228+
self.mine_blocks(10, False)
229+
self.log.info("Mine some dynamic federation blocks with txns")
230+
self.mine_blocks(10, True)
229231

230232
if __name__ == '__main__':
231233
BlockSignTest().main()

test/functional/test_framework/messages.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -836,22 +836,24 @@ def __repr__(self):
836836
% (self.challenge, self.solution)
837837

838838
class DynaFedParamEntry:
839-
__slots__ = ("m_serialize_type", "m_signblockscript", "m_signblock_witness_limit", "m_fedpeg_program", "m_fedpegscript", "m_extension_space")
839+
__slots__ = ("m_serialize_type", "m_signblockscript", "m_signblock_witness_limit", "m_fedpeg_program", "m_fedpegscript", "m_extension_space", "m_extra_root")
840840

841841
# Constructor args will define serialization type:
842842
# null = 0
843843
# signblock-related fields = 1, required for m_current on non-epoch-starts
844844
# all fields = 2, required for epoch starts
845-
def __init__(self, m_signblockscript=b"", m_signblock_witness_limit=0, m_fedpeg_program=b"", m_fedpegscript=b"", m_extension_space=[]):
845+
def __init__(self, m_signblockscript=b"", m_signblock_witness_limit=0, m_fedpeg_program=b"", m_fedpegscript=b"", m_extension_space=[], m_extra_root=0):
846846
self.m_signblockscript = m_signblockscript
847847
self.m_signblock_witness_limit = m_signblock_witness_limit
848848
self.m_fedpeg_program = m_fedpeg_program
849849
self.m_fedpegscript = m_fedpegscript
850850
self.m_extension_space = m_extension_space
851851
if self.is_null():
852852
self.m_serialize_type = 0
853-
elif m_fedpegscript==b"" and m_extension_space == []:
853+
elif m_fedpegscript==b"" and m_fedpeg_program==b"" and m_extension_space == []:
854854
self.m_serialize_type = 1
855+
# We also set the "extra root" in this case
856+
self.m_extra_root = m_extra_root
855857
else:
856858
self.m_serialize_type = 2
857859

@@ -862,6 +864,7 @@ def set_null(self):
862864
self.m_fedpegscript = b""
863865
self.m_extension_space = []
864866
self.m_serialize_type = 0
867+
self.m_extra_root = 0
865868

866869
def is_null(self):
867870
return self.m_signblockscript == b"" and self.m_signblock_witness_limit == 0 and \
@@ -874,6 +877,7 @@ def serialize(self):
874877
if self.m_serialize_type == 1:
875878
r += ser_string(self.m_signblockscript)
876879
r += struct.pack("<I", self.m_signblock_witness_limit)
880+
r += ser_uint256(self.m_extra_root)
877881
elif self.m_serialize_type == 2:
878882
r += ser_string(self.m_signblockscript)
879883
r += struct.pack("<I", self.m_signblock_witness_limit)
@@ -889,6 +893,7 @@ def deserialize(self, f):
889893
if self.m_serialize_type == 1:
890894
self.m_signblockscript = deser_string(f)
891895
self.m_signblock_witness_limit = struct.unpack("<I", f.read(4))[0]
896+
self.m_extra_root = deser_uint256(f)
892897
elif self.m_serialize_type == 2:
893898
self.m_signblockscript = deser_string(f)
894899
self.m_signblock_witness_limit = struct.unpack("<I", f.read(4))[0]

0 commit comments

Comments
 (0)