Skip to content

Commit afd5868

Browse files
committed
pbft WIP(24)
1 parent a12b20a commit afd5868

File tree

4 files changed

+42
-50
lines changed

4 files changed

+42
-50
lines changed

core/block_crypt.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,12 +2937,13 @@ namespace beam
29372937
uint64_t m_wVoted = 0;
29382938
uint32_t m_nWhite = 0;
29392939
uint32_t m_iIdx = 0;
2940-
uint32_t m_iSig = 0;
29412940
uint32_t m_iWhitePos = 0;
29422941

29432942
const Merkle::Hash& m_msg;
29442943
const Quorum& m_qc;
29452944

2945+
std::vector<ECC::Point::Native> m_vPks;
2946+
29462947
Target(const Merkle::Hash& msg, const Quorum& qc)
29472948
:m_msg(msg)
29482949
,m_qc(qc)
@@ -2954,10 +2955,7 @@ namespace beam
29542955

29552956
if (m_qc.IsInMask(m_iIdx++))
29562957
{
2957-
if (m_iSig >= m_qc.m_vSigs.size())
2958-
return false;
2959-
2960-
if (!addr.CheckSignature(m_msg, m_qc.m_vSigs[m_iSig++]))
2958+
if (!addr.ExportNnz(m_vPks.emplace_back()))
29612959
return false;
29622960

29632961
m_wVoted += weight;
@@ -2975,12 +2973,15 @@ namespace beam
29752973
if (!EnumValidators(x))
29762974
return false;
29772975

2978-
if (x.m_iSig != qc.m_vSigs.size())
2979-
return false; // not all sigs used
2980-
29812976
if (qc.m_vValidatorsMsk.size() > qc.get_MaxMaskSize(x.m_iIdx))
29822977
return false;
29832978

2979+
ECC::Signature::Config cfg = ECC::Context::get().m_Sig.m_CfgG1; // copy
2980+
cfg.m_nKeys = (uint32_t) x.m_vPks.size();
2981+
2982+
if (!Cast::Down<ECC::SignatureBase>(qc.m_Signature).IsValid(cfg, msg, &qc.m_Signature.m_k, x.m_vPks.empty() ? nullptr : &x.m_vPks.front()))
2983+
return false;
2984+
29842985
// is quorum reached?
29852986
return IsMajorityReached(x.m_wVoted, x.m_wTotal, x.m_nWhite);
29862987
}

core/block_crypt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,7 +1743,7 @@ namespace beam
17431743
struct Quorum
17441744
{
17451745
std::vector<uint8_t> m_vValidatorsMsk;
1746-
std::vector<ECC::Signature> m_vSigs; // TODO: compress sigs, sig.k can be aggregated (whereas sig.NoncePub can not)
1746+
ECC::Signature m_Signature;
17471747

17481748
static bool IsInMaskEx(uint32_t, const Blob& mask);
17491749
static uint32_t get_MaxMaskSize(uint32_t);
@@ -1755,7 +1755,7 @@ namespace beam
17551755
{
17561756
ar
17571757
& m_vValidatorsMsk
1758-
& m_vSigs;
1758+
& m_Signature;
17591759
}
17601760
};
17611761

node/node.cpp

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6464,9 +6464,7 @@ void Node::Validator::GenerateProposal()
64646464
rd.m_Proposal = std::move(m_FutureCandidate);
64656465
m_FutureCandidate.m_State = Proposal::State::None;
64666466

6467-
Block::SystemState::Full s;
6468-
MakeFullHdr(s, rd.m_Proposal.m_Msg.m_Hdr);
6469-
rd.SetHashes(s);
6467+
rd.SetHashes();
64706468
}
64716469
else
64726470
{
@@ -6709,7 +6707,7 @@ void Node::Validator::OnMsg(proto::PbftVote&& msg, const Peer& src)
67096707
if (!msg.m_Address.CheckSignature(pwr.m_hv, msg.m_Signature))
67106708
src.ThrowUnexpected("invalid validator sig");
67116709

6712-
if ((VoteKind::Commit == msg.m_iKind) && (vp.m_hvCommitted != pwr.m_hv))
6710+
if ((VoteKind::Commit == msg.m_iKind) && (vp.m_hvCommitted != rd.m_Proposal.m_hv))
67136711
{
67146712
if (vp.m_hvCommitted != Zero)
67156713
{
@@ -6846,6 +6844,8 @@ void Node::Validator::OnSigRequestReceived(uint32_t iR, const Peer* pSrc)
68466844

68476845
// 2nd phase: derive the challenges
68486846
ECC::Oracle oracle;
6847+
ms.m_SigAggregate.Expose(oracle, rd.m_Proposal.m_hv);
6848+
68496849
iIdx = 0;
68506850
for (auto& vp : m_ValidatorSet.m_mapValidators)
68516851
{
@@ -6961,7 +6961,8 @@ void Node::Validator::OnMsg(proto::PbftProposal&& msg, const Peer& src)
69616961

69626962
Block::SystemState::Full s;
69636963
MakeFullHdr(s, rd.m_Proposal.m_Msg.m_Hdr);
6964-
rd.SetHashes(s);
6964+
s.get_Hash(rd.m_Proposal.m_hv);
6965+
rd.SetHashes();
69656966

69666967
Merkle::Hash hv;
69676968
get_ProposalMsg(hv, rd);
@@ -6980,7 +6981,7 @@ void Node::Validator::OnMsg(proto::PbftProposal&& msg, const Peer& src)
69806981
}
69816982

69826983
HeightHash id;
6983-
id.m_Hash = rd.m_pPwr[VoteKind::Commit].m_hv;
6984+
id.m_Hash = rd.m_Proposal.m_hv;
69846985
id.m_Height = s.get_Height();
69856986

69866987
if (!p.TestBlock(id, s, rd.m_Proposal.m_Msg.m_Body))
@@ -7002,7 +7003,7 @@ void Node::Validator::OnProposalReceived(uint32_t iR, const Peer* pSrc)
70027003
auto& rd = m_pR[iR];
70037004
assert(Proposal::State::Received == rd.m_Proposal.m_State);
70047005

7005-
PBFT_LOG(INFO, "proposal received " << rd.m_pPwr[VoteKind::Commit].m_hv << ", iR=" << iR);
7006+
PBFT_LOG(INFO, "proposal received " << rd.m_Proposal.m_hv << ", iR=" << iR);
70067007
Broadcast(rd.m_Proposal.m_Msg, pSrc);
70077008
}
70087009

@@ -7011,7 +7012,7 @@ void Node::Validator::get_ProposalMsg(Merkle::Hash& hv, const RoundData& rd) con
70117012
ECC::Hash::Processor()
70127013
<< m_hAnchor.v
70137014
<< "pbft.propose.2"
7014-
<< rd.m_pPwr[VoteKind::Commit].m_hv
7015+
<< rd.m_Proposal.m_hv
70157016
<< rd.m_Proposal.m_Msg.m_iRound
70167017
>> hv;
70177018
}
@@ -7021,7 +7022,7 @@ void Node::Validator::get_SigRequestMsg(Merkle::Hash& hv, const RoundData& rd, c
70217022
ECC::Hash::Processor()
70227023
<< m_hAnchor.v
70237024
<< "pbft.sigreq.2"
7024-
<< rd.m_pPwr[VoteKind::Commit].m_hv
7025+
<< rd.m_Proposal.m_hv
70257026
<< msg.m_iRound
70267027
<< msg.m_Mask.size()
70277028
<< Blob(msg.m_Mask)
@@ -7051,14 +7052,14 @@ void Node::Validator::MakeFullHdr(Block::SystemState::Full& s, const Block::Syst
70517052
Cast::Down<Block::SystemState::Sequence::Element>(s) = el;
70527053
}
70537054

7054-
void Node::Validator::RoundData::SetHashes(const Block::SystemState::Full& s)
7055+
void Node::Validator::RoundData::SetHashes()
70557056
{
7056-
s.get_Hash(m_pPwr[VoteKind::Commit].m_hv);
7057-
7058-
ECC::Hash::Processor()
7059-
<< "pbft.vote.1"
7060-
<< m_pPwr[VoteKind::Commit].m_hv
7061-
>> m_pPwr[VoteKind::PreVote].m_hv;
7057+
for (uint32_t iKind = 0; iKind < _countof(m_pPwr); iKind++)
7058+
ECC::Hash::Processor()
7059+
<< "pbft.vote.2.kind"
7060+
<< m_Proposal.m_hv
7061+
<< iKind
7062+
>> m_pPwr[iKind].m_hv;
70627063
}
70637064

70647065
void Node::Validator::CheckState(uint32_t iR)
@@ -7070,7 +7071,10 @@ void Node::Validator::CheckState(uint32_t iR)
70707071
CheckStateCurrent();
70717072

70727073
auto& rd = m_pR[iR];
7073-
if (!rd.m_Multisig || rd.m_Multisig->m_SigsRemaining)
7074+
if (!rd.m_Multisig)
7075+
return;
7076+
auto& ms = *rd.m_Multisig;
7077+
if (ms.m_SigsRemaining)
70747078
return;
70757079

70767080
// We have the quorum
@@ -7079,23 +7083,8 @@ void Node::Validator::CheckState(uint32_t iR)
70797083

70807084
Block::Pbft::Quorum qc;
70817085
const Rules& r = Rules::get();
7082-
7083-
uint32_t iIdx = 0;
7084-
for (const auto& v : m_ValidatorSet.m_mapValidators)
7085-
{
7086-
const auto& sig = v.m_pR[iR].m_pVote[VoteKind::Commit];
7087-
if (sig)
7088-
{
7089-
qc.m_vSigs.push_back(*sig);
7090-
7091-
uint32_t iByte = iIdx >> 3;
7092-
if (qc.m_vValidatorsMsk.size() <= iByte)
7093-
qc.m_vValidatorsMsk.resize(iByte + 1);
7094-
qc.m_vValidatorsMsk[iByte] |= (1 << (7 & iIdx));
7095-
}
7096-
7097-
iIdx++;
7098-
}
7086+
qc.m_vValidatorsMsk = ms.m_Msg.m_Mask; // copy
7087+
qc.m_Signature = ms.m_SigAggregate;
70997088

71007089
Block::SystemState::Full s;
71017090
MakeFullHdr(s, rd.m_Proposal.m_Msg.m_Hdr);
@@ -7118,7 +7107,7 @@ void Node::Validator::CheckState(uint32_t iR)
71187107
ser.swap_buf(m_Stamp.m_vSer);
71197108

71207109
m_Stamp.m_ID.m_Height = s.get_Height();
7121-
m_Stamp.m_ID.m_Hash = rd.m_pPwr[VoteKind::Commit].m_hv;
7110+
m_Stamp.m_ID.m_Hash = rd.m_Proposal.m_hv;
71227111

71237112
SaveStamp();
71247113
}
@@ -7149,7 +7138,7 @@ void Node::Validator::CheckState(uint32_t iR)
71497138

71507139
Block::SystemState::ID id;
71517140
id.m_Number = s.m_Number;
7152-
id.m_Hash = rd.m_pPwr[VoteKind::Commit].m_hv;
7141+
id.m_Hash = rd.m_Proposal.m_hv;
71537142

71547143
eVal = p.OnBlock(id, rd.m_Proposal.m_Msg.m_Body.m_Perishable, rd.m_Proposal.m_Msg.m_Body.m_Eternal, Zero);
71557144

@@ -7191,7 +7180,7 @@ void Node::Validator::CheckStateCurrent()
71917180
OnProposalReceived(iR, nullptr);
71927181
}
71937182

7194-
bool bAlreadyCommitted = (m_pMe->m_hvCommitted == rd.m_pPwr[VoteKind::Commit].m_hv);
7183+
bool bAlreadyCommitted = (m_pMe->m_hvCommitted == rd.m_Proposal.m_hv);
71957184
if (!bAlreadyCommitted && !ShouldAcceptProposal())
71967185
return;
71977186

@@ -7215,7 +7204,7 @@ void Node::Validator::CheckStateCurrent()
72157204

72167205
PBFT_LOG(INFO, "committed");
72177206
m_State = State::Committed;
7218-
m_pMe->m_hvCommitted = rd.m_pPwr[VoteKind::Commit].m_hv;
7207+
m_pMe->m_hvCommitted = rd.m_Proposal.m_hv;
72197208
}
72207209

72217210
if (!rd.m_pPwr[VoteKind::Commit].IsMajorityReached(m_wTotal))
@@ -7433,9 +7422,10 @@ bool Node::Validator::CreateProposal()
74337422
if (bRes)
74347423
{
74357424
auto& rdcurr = m_pR[0];
7436-
rdcurr.SetHashes(bc.m_Hdr);
7425+
bc.m_Hdr.get_Hash(rdcurr.m_Proposal.m_hv);
74377426
rdcurr.m_Proposal.m_Msg.m_Hdr = std::move(bc.m_Hdr);
74387427
rdcurr.m_Proposal.m_Msg.m_Body = std::move(bc.m_Body);
7428+
rdcurr.SetHashes();
74397429
}
74407430

74417431
return bRes;

node/node.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ struct Node
948948
struct Proposal
949949
{
950950
proto::PbftProposal m_Msg;
951+
Merkle::Hash m_hv;
951952

952953
enum struct State {
953954
None,
@@ -973,7 +974,7 @@ struct Node
973974
std::optional<Multisig> m_Multisig;
974975

975976
void Reset();
976-
void SetHashes(const Block::SystemState::Full&);
977+
void SetHashes();
977978
};
978979

979980
RoundData m_pR[2]; // current + next, some peers may send data a little too early, we'll accumulate them before processing

0 commit comments

Comments
 (0)