Skip to content

Commit 2845e78

Browse files
committed
Define dynamic federations primitives
1 parent 66c5045 commit 2845e78

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

src/primitives/block.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,29 @@ std::string CBlock::ToString() const
4040
}
4141
return s.str();
4242
}
43+
44+
uint256 ConsensusParamEntry::CalculateRoot() const
45+
{
46+
if (IsNull()) {
47+
return uint256();
48+
}
49+
50+
std::vector<uint256> leaves;
51+
leaves.push_back(SerializeHash(m_signblockscript, SER_GETHASH, 0));
52+
leaves.push_back(SerializeHash(m_sbs_wit_limit, SER_GETHASH, 0));
53+
leaves.push_back(SerializeHash(m_fedpegscript, SER_GETHASH, 0));
54+
leaves.push_back(SerializeHash(m_extension_space, SER_GETHASH, 0));
55+
return ComputeFastMerkleRoot(leaves);
56+
}
57+
58+
uint256 DynaFedParams::CalculateRoot() const
59+
{
60+
if (IsNull()) {
61+
return uint256();
62+
}
63+
64+
std::vector<uint256> leaves;
65+
leaves.push_back(m_current.CalculateRoot());
66+
leaves.push_back(m_proposed.CalculateRoot());
67+
return ComputeFastMerkleRoot(leaves);
68+
}

src/primitives/block.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,116 @@ class CProof
5252
std::string ToString() const;
5353
};
5454

55+
56+
class ConsensusParamEntry
57+
{
58+
public:
59+
unsigned char m_serialize_type; // Determines how it is serialized, defaults to null
60+
CScript m_signblockscript;
61+
uint32_t m_sbs_wit_limit; // Max block signature witness serialized size
62+
CScript m_fedpegscript;
63+
// No consensus meaning to the particular bytes, currently we interpret as PAK keys, details in pak.h
64+
std::vector<std::vector<unsigned char>> m_extension_space;
65+
66+
// Each constructor sets its own serialization type implicitly based on which
67+
// arguments are given
68+
ConsensusParamEntry() { m_sbs_wit_limit = 0; m_serialize_type = 0; };
69+
ConsensusParamEntry(const CScript& signblockscript_in, const uint32_t sbs_wit_limit_in) : m_signblockscript(signblockscript_in), m_sbs_wit_limit(sbs_wit_limit_in) { m_serialize_type = 1; };
70+
ConsensusParamEntry(const CScript& signblockscript_in, const uint32_t sbs_wit_limit_in, const CScript& fedpegscript_in, const std::vector<std::vector<unsigned char>> extension_space_in) : m_signblockscript(signblockscript_in), m_sbs_wit_limit(sbs_wit_limit_in), m_fedpegscript(fedpegscript_in), m_extension_space(extension_space_in) { m_serialize_type = 2; };
71+
72+
ADD_SERIALIZE_METHODS;
73+
74+
template <typename Stream, typename Operation>
75+
inline void SerializationOp(Stream& s, Operation ser_action) {
76+
READWRITE(m_serialize_type);
77+
switch(m_serialize_type) {
78+
case 0:
79+
/* Null entry, used to signal "no vote" proposal */
80+
break;
81+
case 1:
82+
READWRITE(m_signblockscript);
83+
READWRITE(m_sbs_wit_limit);
84+
break;
85+
case 2:
86+
READWRITE(m_signblockscript);
87+
READWRITE(m_sbs_wit_limit);
88+
READWRITE(m_fedpegscript);
89+
READWRITE(m_extension_space);
90+
break;
91+
default:
92+
throw std::ios_base::failure("Invalid consensus parameter entry type");
93+
}
94+
}
95+
96+
uint256 CalculateRoot() const;
97+
98+
bool IsNull() const
99+
{
100+
return m_serialize_type == 0 &&
101+
m_signblockscript.empty() &&
102+
m_sbs_wit_limit == 0 &&
103+
m_fedpegscript.empty() &&
104+
m_extension_space.empty();
105+
106+
}
107+
108+
void SetNull()
109+
{
110+
m_serialize_type = 0;
111+
m_signblockscript = CScript();
112+
m_sbs_wit_limit = 0;
113+
m_fedpegscript = CScript();
114+
m_extension_space.clear();
115+
}
116+
117+
bool operator==(const ConsensusParamEntry &other) const
118+
{
119+
return m_serialize_type == other.m_serialize_type &&
120+
m_signblockscript == other.m_signblockscript &&
121+
m_sbs_wit_limit == other.m_sbs_wit_limit &&
122+
m_fedpegscript == other.m_fedpegscript &&
123+
m_extension_space == other.m_extension_space;
124+
}
125+
bool operator!=(const ConsensusParamEntry &other) const
126+
{
127+
return !(*this == other);
128+
}
129+
};
130+
131+
class DynaFedParams
132+
{
133+
public:
134+
135+
// Currently enforced by network, not all fields may be known
136+
ConsensusParamEntry m_current;
137+
// Proposed rules for next epoch
138+
ConsensusParamEntry m_proposed;
139+
140+
DynaFedParams() {};
141+
DynaFedParams(const ConsensusParamEntry& current, const ConsensusParamEntry& proposed) : m_current(current), m_proposed(proposed) {};
142+
143+
ADD_SERIALIZE_METHODS;
144+
145+
template <typename Stream, typename Operation>
146+
inline void SerializationOp(Stream& s, Operation ser_action) {
147+
READWRITE(m_current);
148+
READWRITE(m_proposed);
149+
}
150+
151+
uint256 CalculateRoot() const;
152+
153+
bool IsNull() const
154+
{
155+
return m_current.IsNull() && m_proposed.IsNull();
156+
}
157+
158+
void SetNull()
159+
{
160+
m_current.SetNull();
161+
m_proposed.SetNull();
162+
}
163+
};
164+
55165
/** Nodes collect new transactions into a block, hash them into a hash tree,
56166
* and scan through nonce values to make the block's hash satisfy proof-of-work
57167
* requirements. When they solve the proof-of-work, they broadcast the block

0 commit comments

Comments
 (0)