Skip to content

Commit aa0b39a

Browse files
committed
pbft WIP(28)
1 parent 0ba4b03 commit aa0b39a

File tree

8 files changed

+56
-57
lines changed

8 files changed

+56
-57
lines changed

core/block_crypt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3043,7 +3043,7 @@ namespace beam
30433043
// add everything except the QC
30443044
auto& d = Cast::Reinterpret<Block::Pbft::HdrData>(m_PoW);
30453045
hp
3046-
<< d.m_hvVsNext
3046+
<< d.m_hvVsBoth
30473047
<< d.m_Time_ms
30483048
<< d.m_Flags1;
30493049

core/block_crypt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,7 @@ namespace beam
17311731
};
17321732

17331733
struct HdrData {
1734-
ECC::Hash::Value m_hvVsNext;
1734+
ECC::Hash::Value m_hvVsBoth; // hash(prev|next)
17351735
uintBigFor<uint16_t>::Type m_Time_ms;
17361736
QC m_QC;
17371737
uint8_t m_Flags1;

core/proto.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,19 +1034,16 @@ void NodeConnection::Test(const PbftStamp& msg, const Block::SystemState::Full&
10341034
if (!r.IsPbftWhitelistMode())
10351035
ThrowUnexpected();
10361036

1037-
Block::Pbft::ValidatorSet vs;
1038-
1039-
Deserializer der;
1040-
der.reset(msg.m_vSer);
1041-
der & vs;
1042-
10431037
Merkle::Hash hv;
1044-
vs.get_Hash(hv);
1038+
msg.m_ValidatorSet.get_Hash(hv);
1039+
Merkle::Interpret(hv, msg.m_hvVsNext, true);
10451040

10461041
const auto& d = Cast::Reinterpret<Block::Pbft::HdrData>(s.m_PoW);
1042+
if (d.m_hvVsBoth != hv)
1043+
ThrowUnexpected();
10471044

10481045
s.get_Hash(hv);
1049-
if (!state.CheckQuorum(hv, d.m_QC))
1046+
if (!msg.m_ValidatorSet.CheckQuorum(hv, d.m_QC))
10501047
ThrowUnexpected();
10511048
}
10521049

core/proto.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ namespace proto {
322322
macro(Block::Pbft::Address, Address)
323323

324324
#define BeamNodeMsg_PbftStamp(macro) \
325-
macro(ByteBuffer, vSer)
325+
macro(Block::Pbft::ValidatorSet, ValidatorSet) \
326+
macro(Merkle::Hash, hvVsNext)
326327

327328
#define BeamNodeMsg_PbftPeerAssessment(macro) \
328329
macro(Block::Pbft::Address, From) \
@@ -634,6 +635,7 @@ namespace proto {
634635
inline void ZeroInit(Asset::Full& x) { x.Reset(); }
635636
inline void ZeroInit(HeightPos& x) { ZeroObject(x); }
636637
inline void ZeroInit(PbftReputationMap&) { }
638+
inline void ZeroInit(Block::Pbft::ValidatorSet&) { }
637639

638640
template <typename T> struct InitArg {
639641
typedef const T& TArg;

node/node.cpp

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,9 +1622,7 @@ void Node::Peer::PbftSendStamp()
16221622
if (m_Tip.m_hh == m_This.m_Processor.m_Cursor.m_hh)
16231623
return;
16241624

1625-
proto::PbftStamp msgStamp;
1626-
TemporarySwap ts(msgStamp.m_vSer, m_This.m_Validator.m_Stamp.m_vSer);
1627-
Send(msgStamp);
1625+
Send(m_This.m_Validator.m_Stamp.m_Msg);
16281626
}
16291627

16301628
Height Node::Peer::get_MinPeerFork()
@@ -4568,7 +4566,7 @@ void Node::Peer::OnMsg(proto::PbftStamp&& msg)
45684566

45694567
// all good
45704568
v.m_Stamp.m_ID = m_Tip.m_hh;
4571-
v.m_Stamp.m_vSer = std::move(msg.m_vSer);
4569+
v.m_Stamp.m_Msg = std::move(msg);
45724570

45734571
v.SaveStamp();
45744572

@@ -5807,17 +5805,11 @@ void Node::Validator::Initialize()
58075805
p.get_DB().ParamGet(NodeDB::ParamID::PbftStamp, nullptr, nullptr, &buf);
58085806
if (!buf.empty())
58095807
{
5810-
Block::Pbft::State state;
5811-
58125808
Deserializer der;
58135809
der.reset(buf);
5814-
der & state;
5815-
5816-
auto n = der.bytes_left();
5817-
der & m_Stamp.m_ID;
5818-
5819-
buf.resize(buf.size() - n);
5820-
m_Stamp.m_vSer = std::move(buf);
5810+
der
5811+
& m_Stamp.m_ID
5812+
& m_Stamp.m_Msg;
58215813
}
58225814
else
58235815
ZeroObject(m_Stamp.m_ID);
@@ -6265,15 +6257,11 @@ void Node::Validator::TestBlock(const Block::SystemState::Full& s, const HeightH
62656257

62666258
if (d.m_Flags1 != nFlags)
62676259
Exc::Fail("flags mismatch");
6260+
6261+
// save the
62686262
}
62696263
else
62706264
{
6271-
Merkle::Hash hv;
6272-
m_ValidatorSet.get_Hash(hv);
6273-
6274-
if (hv != Cast::Reinterpret<Block::Pbft::HdrData>(s.m_PoW).m_hvVsNext)
6275-
Exc::Fail("vs.next");
6276-
62776265
// make sure all fees were added as a block reward
62786266
struct Walker
62796267
:public TxKernel::IWalker
@@ -7075,23 +7063,25 @@ void Node::Validator::CheckState(uint32_t iR)
70757063

70767064
if (r.m_Pbft.m_Whitelist.m_NumRequired)
70777065
{
7078-
Serializer ser;
7079-
7080-
// serialize it in a form compatible with basic set
7081-
ser & m_ValidatorSet.m_mapValidators.size();
7082-
for (const auto& v : m_ValidatorSet.m_mapValidators)
7066+
Height h = s.get_Height();
7067+
if (h > m_Stamp.m_ID.m_Height)
70837068
{
7084-
ser
7085-
& v.m_Key
7086-
& v.m_Weight;
7087-
}
7069+
m_ValidatorSet.get_Hash(m_Stamp.m_Msg.m_hvVsNext);
70887070

7089-
ser.swap_buf(m_Stamp.m_vSer);
7071+
// export our validator set it in a form compatible with basic set
7072+
m_Stamp.m_Msg.m_ValidatorSet.m_map.clear();
7073+
for (const auto& vp : m_ValidatorSet.m_mapValidators)
7074+
{
7075+
auto w = vp.get_EffectiveWeight();
7076+
if (w)
7077+
m_Stamp.m_Msg.m_ValidatorSet.m_map[vp.m_Key] = w;
7078+
}
70907079

7091-
m_Stamp.m_ID.m_Height = s.get_Height();
7092-
m_Stamp.m_ID.m_Hash = id.m_Hash;
7080+
m_Stamp.m_ID.m_Height = h;
7081+
m_Stamp.m_ID.m_Hash = id.m_Hash;
70937082

7094-
SaveStamp();
7083+
SaveStamp();
7084+
}
70957085
}
70967086

70977087
auto& p = get_ParentObj().m_Processor;
@@ -7271,19 +7261,13 @@ bool Node::Validator::SelectMultisigValidators(Bitmask<Block::Pbft::s_MaxValidat
72717261

72727262
void Node::Validator::SaveStamp()
72737263
{
7274-
auto n = m_Stamp.m_vSer.size();
7264+
Serializer ser;
7265+
ser
7266+
& m_Stamp.m_ID
7267+
& m_Stamp.m_Msg;
72757268

7276-
{
7277-
Serializer ser;
7278-
ser.swap_buf(m_Stamp.m_vSer);
7279-
ser & m_Stamp.m_ID;
7280-
ser.swap_buf(m_Stamp.m_vSer);
7281-
}
7282-
7283-
Blob blob(m_Stamp.m_vSer);
7269+
Blob blob(ser.buffer().first, (uint32_t) ser.buffer().second);
72847270
get_ParentObj().m_Processor.get_DB().ParamSet(NodeDB::ParamID::PbftStamp, nullptr, &blob);
7285-
7286-
m_Stamp.m_vSer.resize(n);
72877271
}
72887272

72897273
bool Node::Validator::ShouldSendStamp()

node/node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ struct Node
832832
struct Stamp
833833
{
834834
HeightHash m_ID;
835-
ByteBuffer m_vSer;
835+
proto::PbftStamp m_Msg;
836836
} m_Stamp;
837837

838838
std::optional<ContractID> m_cid;

node/processor.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3036,6 +3036,7 @@ bool NodeProcessor::HandleBlockInternal(const HeightHash& id, const Block::Syste
30363036

30373037
const auto& r = Rules::get();
30383038
IPbftHandler* pPbft = nullptr;
3039+
Merkle::Hash hvVs;
30393040

30403041
Deserializer der;
30413042
der.reset(bufs.m_Perishable);
@@ -3057,6 +3058,8 @@ bool NodeProcessor::HandleBlockInternal(const HeightHash& id, const Block::Syste
30573058

30583059
if (bFirstTime)
30593060
pPbft->TestBlock(s, id, block, true, bTestOnly);
3061+
3062+
pPbft->get_Validators().get_Hash(hvVs);
30603063
}
30613064
}
30623065
catch (const std::exception& e) {
@@ -3206,6 +3209,13 @@ bool NodeProcessor::HandleBlockInternal(const HeightHash& id, const Block::Syste
32063209
if (bOk && pPbft)
32073210
try
32083211
{
3212+
Merkle::Hash hvVsNext;
3213+
pPbft->get_Validators().get_Hash(hvVsNext);
3214+
Merkle::Interpret(hvVs, hvVsNext, true);
3215+
3216+
if (hvVs != Cast::Reinterpret<Block::Pbft::HdrData>(s.m_PoW).m_hvVsBoth)
3217+
Exc::Fail("vs.both");
3218+
32093219
pPbft->TestBlock(s, id, block, false, bTestOnly);
32103220
}
32113221
catch (const std::exception& e) {
@@ -7539,6 +7549,9 @@ bool NodeProcessor::GenerateNewBlock(BlockContext& bc)
75397549
pPbft = get_PbftHandler();
75407550
if (!pPbft)
75417551
return false;
7552+
7553+
auto& d = Cast::Reinterpret<Block::Pbft::HdrData>(bc.m_Hdr.m_PoW);
7554+
pPbft->get_Validators().get_Hash(d.m_hvVsBoth);
75427555
}
75437556

75447557
bool bOk = HandleValidatedTx(bc.m_Block, bic);
@@ -7555,8 +7568,11 @@ bool NodeProcessor::GenerateNewBlock(BlockContext& bc)
75557568

75567569
if (Rules::Consensus::Pbft == r.m_Consensus)
75577570
{
7571+
Merkle::Hash hvVsNext;
7572+
pPbft->get_Validators().get_Hash(hvVsNext);
7573+
75587574
auto& d = Cast::Reinterpret<Block::Pbft::HdrData>(bc.m_Hdr.m_PoW);
7559-
pPbft->get_Validators().get_Hash(d.m_hvVsNext);
7575+
Merkle::Interpret(d.m_hvVsBoth, hvVsNext, true);
75607576

75617577
// Assume d.m_Time_ms and the hdr timestamp are already assigned by the caller
75627578

node/processor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ class NodeProcessor
591591
// PBFT-specific
592592
struct IPbftHandler
593593
{
594-
virtual const Block::Pbft::State::IValidatorSet& get_Validators() = 0;
594+
virtual const Block::Pbft::IValidatorSet& get_Validators() = 0;
595595
virtual void OnContractVarChange(const Blob& key, const Blob& val, bool bTemporary) = 0;
596596
virtual void OnContractStoreReset() = 0;
597597
virtual bool OnContractInvoke(const ContractID&, uint32_t iMethod, const Blob& args, bool bTemporary) = 0;

0 commit comments

Comments
 (0)