Skip to content

Commit b7bd07d

Browse files
committed
pbft WIP(8) block commission (fees) go into PBFT contract. Restored block balance formula.
1 parent c93bed2 commit b7bd07d

File tree

5 files changed

+109
-46
lines changed

5 files changed

+109
-46
lines changed

core/block_validation.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -247,47 +247,40 @@ namespace beam
247247
{
248248
const auto& r = Rules::get();
249249

250-
AmountBig::Number subsTotal;
250+
AmountBig::Number subsidy = Zero;
251251

252252
bool bPbft = (Rules::Consensus::Pbft == r.m_Consensus);
253253
if (bPbft)
254-
subsTotal = m_Stats.m_Fee; // the value subtracted from the circulation
254+
{
255+
if (m_Stats.m_Coinbase != Zero)
256+
Exc::Fail("Coinbase in pbft");
257+
}
255258
else
256259
{
257-
r.get_Emission(subsTotal, m_Height);
260+
r.get_Emission(subsidy, m_Height);
258261
m_Sigma = -m_Sigma;
262+
263+
AmountBig::AddTo(m_Sigma, subsidy);
259264
}
260265

261-
AmountBig::AddTo(m_Sigma, subsTotal);
262266
TestSigma();
263267

264-
if (!m_Params.m_bAllowUnsignedOutputs)
268+
if (!m_Params.m_bAllowUnsignedOutputs && !bPbft)
265269
{
266270
bool bHasEnoughLocked = true;
267271
bool bShortRange = (m_Height.m_Max - m_Height.m_Min < r.Maturity.Coinbase); // All the supposed coinbase UTXOs must be included (i.e. couldn't be spent and cut-through)
268272

269-
if (bPbft)
270-
{
271-
if (bShortRange)
272-
bHasEnoughLocked = (m_Stats.m_Coinbase == Zero);
273-
274-
// for long range we don't really know which part of the coinbase could be spent already, hence we omit this verification
275-
}
273+
if (bShortRange)
274+
bHasEnoughLocked = (m_Stats.m_Coinbase == subsidy);
276275
else
277276
{
278-
if (bShortRange)
279-
bHasEnoughLocked = (m_Stats.m_Coinbase == subsTotal);
280-
else
281-
{
282-
// calculate the minimum coinbase value that's supposed still to be locked
283-
HeightRange hr;
284-
hr.m_Min = m_Height.m_Max - r.Maturity.Coinbase;
285-
hr.m_Max = m_Height.m_Max;
286-
r.get_Emission(subsTotal, hr);
277+
// calculate the minimum coinbase value that's supposed still to be locked
278+
HeightRange hr;
279+
hr.m_Min = m_Height.m_Max - r.Maturity.Coinbase;
280+
hr.m_Max = m_Height.m_Max;
281+
r.get_Emission(subsidy, hr);
287282

288-
bHasEnoughLocked = (m_Stats.m_Coinbase >= subsTotal);
289-
290-
}
283+
bHasEnoughLocked = (m_Stats.m_Coinbase >= subsidy);
291284
}
292285

293286
if (!bHasEnoughLocked)

node/node.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4597,6 +4597,11 @@ const Block::Pbft::State::IValidatorSet* Node::Processor::get_Validators()
45974597
return &get_ParentObj().m_Validator.m_ValidatorSet;
45984598
}
45994599

4600+
TxKernel::Ptr Node::Processor::GeneratePbftRewardKernel(Amount fees, ECC::Scalar::Native& sk)
4601+
{
4602+
return get_ParentObj().m_Validator.GeneratePbftRewardKernel(fees, sk);
4603+
}
4604+
46004605
void Node::Server::OnAccepted(io::TcpStream::Ptr&& newStream, int errorCode)
46014606
{
46024607
if (newStream)
@@ -6114,6 +6119,31 @@ void Node::Validator::OnContractVarChange(const Blob& key, const Blob& val, bool
61146119
pV->m_Flags = vd.m_Flags;
61156120
}
61166121

6122+
TxKernel::Ptr Node::Validator::GeneratePbftRewardKernel(Amount fees, ECC::Scalar::Native& sk)
6123+
{
6124+
if (!m_cid)
6125+
return nullptr; // shouldn't happen though
6126+
6127+
using namespace Shaders::PBFT;
6128+
6129+
auto pKrn = std::make_unique<TxKernelContractInvoke>();
6130+
pKrn->m_Cid = *m_cid;
6131+
pKrn->m_iMethod = Method::AddReward::s_iMethod;
6132+
6133+
pKrn->m_Args.resize(sizeof(Method::AddReward));
6134+
auto& r = *(Method::AddReward*) &pKrn->m_Args.front();
6135+
r.m_Amount = ByteOrder::to_le(fees);
6136+
6137+
pKrn->m_Height.m_Min = get_ParentObj().m_Processor.m_Cursor.m_hh.m_Height + m_iRound + 1;
6138+
6139+
ECC::Point::Native ptFunds = ECC::Context::get().H * fees;
6140+
6141+
sk = get_ParentObj().m_Keys.m_Validator.m_sk;
6142+
pKrn->Sign(&sk, 1, ptFunds, nullptr);
6143+
6144+
return pKrn;
6145+
}
6146+
61176147
bool Node::Validator::ValidatorSet::EnumValidators(ITarget& x) const
61186148
{
61196149
for (const auto& v : m_mapValidators)

node/node.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ struct Node
264264
void OnContractVarChange(const Blob& key, const Blob& val, bool bTemporary) override;
265265
void OnContractStoreReset() override;
266266
const Block::Pbft::State::IValidatorSet* get_Validators() override;
267+
TxKernel::Ptr GeneratePbftRewardKernel(Amount fees, ECC::Scalar::Native& sk) override;
267268

268269
void Stop();
269270

@@ -810,6 +811,7 @@ struct Node
810811
void SendState(Peer&) const;
811812
void OnContractVarChange(const Blob& key, const Blob& val, bool bTemporary);
812813
void OnContractStoreReset();
814+
TxKernel::Ptr GeneratePbftRewardKernel(Amount fees, ECC::Scalar::Native& sk);
813815

814816
uint64_t get_RefTime_ms() const;
815817

node/processor.cpp

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,11 @@ const Block::Pbft::State::IValidatorSet* NodeProcessor::get_Validators()
562562
return nullptr;
563563
}
564564

565+
TxKernel::Ptr NodeProcessor::GeneratePbftRewardKernel(Amount fees, ECC::Scalar::Native& sk)
566+
{
567+
return nullptr;
568+
}
569+
565570
NodeProcessor::CongestionCache::TipCongestion* NodeProcessor::CongestionCache::Find(const NodeDB::StateID& sid)
566571
{
567572
TipCongestion* pRet = nullptr;
@@ -7297,27 +7302,41 @@ size_t NodeProcessor::GenerateNewBlockInternal(BlockContext& bc, BlockInterpretC
72977302
}
72987303
}
72997304

7300-
if (Rules::Consensus::Pbft != r.m_Consensus)
7305+
if (!m_nReserveBlockSizeForFees)
73017306
{
7302-
// estimate the size of the fees UTXO
7303-
if (!m_nSizeUtxoComissionUpperLimit)
7307+
SerializerSizeCounter ssc2;
7308+
7309+
if (Rules::Consensus::Pbft != r.m_Consensus)
73047310
{
7311+
// size of extra UTXO
73057312
Output outp;
73067313
outp.m_pConfidential.reset(new ECC::RangeProof::Confidential);
73077314
ZeroObject(*outp.m_pConfidential);
73087315
outp.m_pAsset = std::make_unique<Asset::Proof>();
73097316
outp.m_pAsset->InitArrays(r.CA.m_ProofCfg);
73107317
outp.m_pAsset->m_Begin = static_cast<Asset::ID>(-1);
73117318

7312-
SerializerSizeCounter ssc2;
73137319
ssc2 & outp;
7314-
m_nSizeUtxoComissionUpperLimit = ssc2.m_Counter.m_Value;
7320+
}
7321+
else
7322+
{
7323+
// size of extra kernel
7324+
TxKernelContractInvoke krn;
7325+
krn.m_Height.m_Min = MaxHeight;
7326+
krn.m_Commitment = Zero;
7327+
ZeroObject(krn.m_Signature);
7328+
krn.m_iMethod = std::numeric_limits<uint32_t>::max();
7329+
krn.m_Args.resize(100); // excessive
7330+
7331+
ssc2 & krn;
73157332
}
73167333

7317-
if (bc.m_Fees)
7318-
ssc.m_Counter.m_Value += m_nSizeUtxoComissionUpperLimit;
7334+
m_nReserveBlockSizeForFees = ssc2.m_Counter.m_Value;
73197335
}
73207336

7337+
if (bc.m_Fees)
7338+
ssc.m_Counter.m_Value += m_nReserveBlockSizeForFees;
7339+
73217340
const size_t nSizeMax = r.MaxBodySize;
73227341
if (ssc.m_Counter.m_Value > nSizeMax)
73237342
{
@@ -7351,8 +7370,8 @@ size_t NodeProcessor::GenerateNewBlockInternal(BlockContext& bc, BlockInterpretC
73517370

73527371

73537372
size_t nSizeNext = ssc.m_Counter.m_Value + nSize;
7354-
if (!bc.m_Fees && feesNext && (Rules::Consensus::Pbft != r.m_Consensus))
7355-
nSizeNext += m_nSizeUtxoComissionUpperLimit;
7373+
if (!bc.m_Fees && feesNext)
7374+
nSizeNext += m_nReserveBlockSizeForFees;
73567375

73577376
if (nSizeNext > nSizeMax)
73587377
break;
@@ -7383,8 +7402,8 @@ size_t NodeProcessor::GenerateNewBlockInternal(BlockContext& bc, BlockInterpretC
73837402
continue; // huge fees are unsupported
73847403

73857404
size_t nSizeNext = ssc.m_Counter.m_Value + x.m_Profit.m_Stats.m_Size;
7386-
if (!bc.m_Fees && feesNext && (Rules::Consensus::Pbft != r.m_Consensus))
7387-
nSizeNext += m_nSizeUtxoComissionUpperLimit;
7405+
if (!bc.m_Fees && feesNext)
7406+
nSizeNext += m_nReserveBlockSizeForFees;
73887407

73897408
if (nSizeNext > nSizeMax)
73907409
{
@@ -7434,24 +7453,42 @@ size_t NodeProcessor::GenerateNewBlockInternal(BlockContext& bc, BlockInterpretC
74347453

74357454
if (BlockContext::Mode::Assemble != bc.m_Mode)
74367455
{
7437-
if (bc.m_Fees && (Rules::Consensus::Pbft != r.m_Consensus))
7456+
if (bc.m_Fees)
74387457
{
7458+
// make size estimation more precise
7459+
size_t n0 = ssc.m_Counter.m_Value;
7460+
ssc.m_Counter.m_Value -= m_nReserveBlockSizeForFees;
7461+
7462+
if (Rules::Consensus::Pbft != r.m_Consensus)
74397463
{
7440-
Asset::Proof::Params::Override po(get_AidMax());
7441-
bb.AddFees(bc.m_Fees, pOutp);
7464+
{
7465+
Asset::Proof::Params::Override po(get_AidMax());
7466+
bb.AddFees(bc.m_Fees, pOutp);
7467+
}
7468+
7469+
if (!HandleBlockElement(*pOutp, bic))
7470+
return 0;
7471+
7472+
ssc & (*pOutp);
7473+
bc.m_Block.m_vOutputs.push_back(std::move(pOutp));
74427474
}
7475+
else
7476+
{
7477+
pKrn = GeneratePbftRewardKernel(bc.m_Fees, bb.m_Offset);
7478+
if (!pKrn)
7479+
return 0;
74437480

7444-
if (!HandleBlockElement(*pOutp, bic))
7445-
return 0;
7481+
if (!HandleBlockElement(*pKrn, bic))
7482+
return 0;
7483+
7484+
yas::detail::SaveKrn(ssc, *pKrn, false);
7485+
7486+
bc.m_Block.m_vKernels.push_back(std::move(pKrn));
7487+
}
74467488

7447-
// make size estimation more precise
7448-
size_t n0 = ssc.m_Counter.m_Value;
7449-
ssc.m_Counter.m_Value -= m_nSizeUtxoComissionUpperLimit;
7450-
ssc& (*pOutp);
74517489
assert(ssc.m_Counter.m_Value <= n0);
74527490
(n0); // suppress 'unused' warning in release build
74537491

7454-
bc.m_Block.m_vOutputs.push_back(std::move(pOutp));
74557492
}
74567493

74577494
bb.m_Offset = -bb.m_Offset;

node/processor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class NodeProcessor
129129

130130
Mapped m_Mapped;
131131

132-
size_t m_nSizeUtxoComissionUpperLimit = 0;
132+
size_t m_nReserveBlockSizeForFees = 0;
133133

134134
struct InputAux {
135135
TxoID m_ID = 0;
@@ -592,6 +592,7 @@ class NodeProcessor
592592
virtual void OnContractVarChange(const Blob& key, const Blob& val, bool bTemporary);
593593
virtual void OnContractStoreReset();
594594
virtual const Block::Pbft::State::IValidatorSet* get_Validators();
595+
virtual TxKernel::Ptr GeneratePbftRewardKernel(Amount fees, ECC::Scalar::Native& sk);
595596

596597
struct MyExecutor
597598
:public Executor

0 commit comments

Comments
 (0)