Skip to content

Commit aec8b6c

Browse files
committed
HF: Dyanfed pruned header now has extra root
1 parent 687e427 commit aec8b6c

File tree

6 files changed

+54
-17
lines changed

6 files changed

+54
-17
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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,25 @@ class CProof
5858
class DynaFedParamEntry
5959
{
6060
public:
61-
unsigned char m_serialize_type; // Determines how it is serialized, defaults to null
61+
// Determines how these entries are serialized and stored
62+
// 0 -> Null. Only used for proposed parameter "null votes"
63+
// 1 -> Pruned. Doesn't have non-signblockscript data. That elided data
64+
// is committed to in m_elided_root, and validated against chainstate.
65+
// 2 -> Full. Typically only consensus-legal at epoch start.
66+
unsigned char m_serialize_type;
67+
6268
CScript m_signblockscript;
6369
uint32_t m_signblock_witness_limit; // Max block signature witness serialized size
6470
CScript m_fedpeg_program; // The "scriptPubKey" of the fedpegscript
6571
CScript m_fedpegscript; // The witnessScript for witness v0 or undefined otherwise.
6672
// No consensus meaning to the particular bytes, currently we interpret as PAK keys, details in pak.h
6773
std::vector<std::vector<unsigned char>> m_extension_space;
74+
uint256 m_elided_root; // non-zero only when m_serialize_type == 1
6875

6976
// Each constructor sets its own serialization type implicitly based on which
7077
// arguments are given
7178
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; };
79+
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; };
7380
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; };
7481

7582
ADD_SERIALIZE_METHODS;
@@ -84,6 +91,7 @@ class DynaFedParamEntry
8491
case 1:
8592
READWRITE(m_signblockscript);
8693
READWRITE(m_signblock_witness_limit);
94+
READWRITE(m_elided_root);
8795
break;
8896
case 2:
8997
READWRITE(m_signblockscript);
@@ -98,6 +106,8 @@ class DynaFedParamEntry
98106
}
99107

100108
uint256 CalculateRoot() const;
109+
// Calculates root for the non-blocksigning merkle fields
110+
uint256 CalculateExtraRoot() const;
101111

102112
bool IsNull() const
103113
{

test/functional/feature_blocksign.py

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

217217
# Next let's activate dynafed
218218
blocks_til_dynafed = 431 - self.nodes[0].getblockcount()
219+
self.log.info("Activating dynafed")
219220
self.mine_blocks(blocks_til_dynafed, False)
220221
self.check_height(111+blocks_til_dynafed)
221222

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

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

228230
if __name__ == '__main__':
229231
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_elided_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_elided_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_elided_root = m_elided_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_elided_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_elided_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_elided_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)