Skip to content

Commit c88af1f

Browse files
committed
806 fix formatting
1 parent aed6810 commit c88af1f

File tree

5 files changed

+63
-27
lines changed

5 files changed

+63
-27
lines changed

chains/Schain.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,14 @@ void Schain::writeToVisualizationStream( string& _s ) {
14291429
u256 Schain::getRandomForBlockId( block_id _blockId ) {
14301430
auto block = getBlock( _blockId );
14311431
CHECK_STATE( block );
1432-
auto signature = block->getThresholdSig();
1432+
return getRandomForBlock(block);
1433+
}
1434+
1435+
u256 Schain::getRandomForBlock(const ptr<CommittedBlock> &_block) const {
1436+
1437+
CHECK_STATE(_block);
1438+
1439+
auto signature = _block->getThresholdSig();
14331440

14341441
auto data = make_shared< vector< uint8_t > >();
14351442

@@ -1490,7 +1497,7 @@ Schain::calculateBooleanProposalVectorIfItsTimeToStartBinaryConsensus(const ptr<
14901497
// when we do optimized block consensus only a single block proposer
14911498
// proposes and provides da proof, which is the previous winner.
14921499
// proposals from other nodes, if sent made by mistake, are ignored
1493-
auto lastWinner = getOptimizerAgent()->getLastWinner(_daProof->getBlockId());
1500+
auto lastWinner = getOptimizerAgent()->getPreviousWinner(_daProof->getBlockId());
14941501
if (_daProof->getProposerIndex() == lastWinner) {
14951502
getNode()->getDaProofDB()->addDAProof(_daProof);
14961503
pv = make_shared<BooleanProposalVector>(getNodeCount(), lastWinner);

chains/Schain.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,6 @@ class Schain : public Agent {
402402

403403
ptr<BooleanProposalVector>
404404
calculateBooleanProposalVectorIfItsTimeToStartBinaryConsensus(const ptr<DAProof> &_daProof);
405+
406+
u256 getRandomForBlock(const ptr<CommittedBlock> &_block) const;
405407
};

monitoring/OptimizerAgent.cpp

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ bool OptimizerAgent::doOptimizedConsensus(block_id _blockId, uint64_t _lastBlock
4141
}
4242

4343

44-
auto lastWinner = getLastWinner(_blockId);
44+
auto lastWinner = getPreviousWinner(_blockId);
4545

4646
// if last time there was no winner (default block)
4747
// we do not optimize
@@ -52,17 +52,41 @@ bool OptimizerAgent::doOptimizedConsensus(block_id _blockId, uint64_t _lastBlock
5252
}
5353

5454
// redo full consensus each 17 blocks to
55-
// determine the winner. Othewise optimize
56-
return (uint64_t) _blockId % (nodeCount + 1) != 0;
55+
// determine the winner.
5756

58-
}
57+
if ((uint64_t )_blockId % (nodeCount + 1) == 0) {
58+
return false;
59+
}
5960

61+
// optimize
62+
return true;
63+
}
6064

65+
// get the schain index for block priority leader. Note that for convinience
66+
// the priority leader is numbered from 0 to 15
6167
uint64_t OptimizerAgent::getPriorityLeaderForBlock(uint64_t _nodeCount, block_id &_blockID) {
6268
uint64_t priorityLeader;
6369

64-
uint64_t seed;
70+
if (doOptimizedConsensus(_blockID,
71+
getSchain()->getLastCommittedBlockTimeStamp().getS())) {
72+
// in optimized consensus the priority leader is just the previous winner from this block_id minus 16
73+
// we want the previous winner to win again
74+
auto previousWinner = (uint64_t ) getSchain()->getOptimizerAgent()->getPreviousWinner(_blockID);
75+
CHECK_STATE(previousWinner > 0)
76+
priorityLeader = previousWinner - 1;
77+
} else {
78+
// full consensus the priority leader is randomly selected for each block
79+
// based on random seed
80+
auto seed = calculateRandomSeedForConsensus(_blockID);
81+
priorityLeader = (seed % _nodeCount);
82+
}
6583

84+
CHECK_STATE(priorityLeader < _nodeCount);
85+
return priorityLeader;
86+
}
87+
88+
uint64_t OptimizerAgent::calculateRandomSeedForConsensus(const block_id &_blockID) const {
89+
uint64_t seed;
6690
if (_blockID <= 1) {
6791
seed = 1;
6892
} else {
@@ -72,26 +96,27 @@ uint64_t OptimizerAgent::getPriorityLeaderForBlock(uint64_t _nodeCount, block_id
7296
BOOST_THROW_EXCEPTION(InvalidStateException(
7397
"Can not read block " + to_string(_blockID - 1) + " from LevelDB",
7498
__CLASS_NAME__ ));
75-
seed = *((uint64_t *) previousBlock->getHash().data());
76-
}
77-
78-
priorityLeader = ((uint64_t) seed) % _nodeCount;
7999

80-
if (doOptimizedConsensus(_blockID,
81-
getSchain()->getLastCommittedBlockTimeStamp().getS())) {
82-
priorityLeader = (uint64_t) getSchain()->getOptimizerAgent()->getLastWinner(_blockID);
100+
// now get the seed. In full consensus priority leader for the block id will be derived from it
101+
if (getSchain()->fastConsensusPatch(previousBlock->getTimeStampS())) {
102+
// for newer consensus, the nodes a given priority in round robin fashion
103+
seed = (uint64_t ) _blockID;
104+
} else {
105+
// the legacy seed is not perfect since blockhash can be manipulated by block proposer
106+
seed = *((uint64_t *) previousBlock->getHash().data());
107+
}
83108
}
84-
CHECK_STATE(priorityLeader <= _nodeCount);
85-
return priorityLeader;
109+
return seed;
86110
}
87111

88-
89-
schain_index OptimizerAgent::getLastWinner(block_id _blockId) {
90-
// first 16 blocks we do not know the winner
112+
// will return the index for the proposer that won 16 blocks ago
113+
schain_index OptimizerAgent::getPreviousWinner(block_id _blockId) {
114+
// first 16 blocks we do not know the previous winner
91115
if ((uint64_t) _blockId <= getSchain()->getNodeCount()) {
92116
return 0;
93117
}
94-
auto block = getSchain()->getBlock((uint64_t) _blockId - (uint64_t) getSchain()->getNodeCount());
118+
auto block =
119+
getSchain()->getBlock((uint64_t) _blockId - (uint64_t) getSchain()->getNodeCount());
95120

96121
if (!block) {
97122
return 0;
@@ -105,7 +130,7 @@ schain_index OptimizerAgent::skipSendingProposalToTheNetwork(block_id _blockId)
105130
// if node chain index is not equal to the last winner
106131
return (getSchain()->getOptimizerAgent()->doOptimizedConsensus(_blockId,
107132
getSchain()->getLastCommittedBlockTimeStamp().getS()) &&
108-
(getSchain()->getOptimizerAgent()->getLastWinner(_blockId) != getSchain()->getSchainIndex()));
133+
(getSchain()->getOptimizerAgent()->getPreviousWinner(_blockId) != getSchain()->getSchainIndex()));
109134
}
110135

111136

monitoring/OptimizerAgent.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ class OptimizerAgent : public Agent {
3838
// we determine consensus winner each 16 blocks
3939
[[nodiscard]] bool doOptimizedConsensus(block_id _blockId, uint64_t _lastBlockTimeStamp);
4040

41-
schain_index getLastWinner(block_id _block);
41+
schain_index getPreviousWinner(block_id _blockId);
4242

4343

4444
schain_index skipSendingProposalToTheNetwork(block_id _blockId);
4545

4646

4747
uint64_t getPriorityLeaderForBlock(uint64_t _nodeCount, block_id &_blockID);
48+
49+
uint64_t calculateRandomSeedForConsensus(const block_id &_blockID) const;
4850
};

protocols/blockconsensus/BlockConsensusAgent.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void BlockConsensusAgent::startConsensusProposal(
111111
// Optimized consensus. Start N binary consensuses
112112
// for optimized block consensus, we only propose and initiated binary consensus
113113
// for the last block winner
114-
auto lastWinner = getSchain()->getOptimizerAgent()->getLastWinner(_blockID);
114+
auto lastWinner = getSchain()->getOptimizerAgent()->getPreviousWinner(_blockID);
115115
auto x = bin_consensus_value(_proposal->getProposalValue(schain_index(lastWinner)) ? 1 : 0);
116116
propose(x, lastWinner, _blockID);
117117
return;
@@ -241,7 +241,7 @@ void BlockConsensusAgent::reportConsensusAndDecideIfNeeded(
241241
// winner and ignoring all other messages, even if someone sends them by mistake
242242
if (getSchain()->getOptimizerAgent()->doOptimizedConsensus(blockID,
243243
getSchain()->getLastCommittedBlockTimeStamp().getS()) &&
244-
(uint64_t) blockProposerIndex != getSchain()->getOptimizerAgent()->getLastWinner(blockID)) {
244+
(uint64_t) blockProposerIndex != getSchain()->getOptimizerAgent()->getPreviousWinner(blockID)) {
245245
LOG(warn, "Consensus got ChildBVBroadcastMessage for non-winner in optimized round:" + blockProposerIndex);
246246
return;
247247
}
@@ -491,10 +491,10 @@ bool BlockConsensusAgent::haveFalseDecision(block_id _blockId, schain_index _pro
491491
void BlockConsensusAgent::decideNormalBlockConsensusIfCan(block_id _blockId) {
492492
auto nodeCount = (uint64_t) getSchain()->getNodeCount();
493493
// note, priorityLeader is numbered from 0 to N-1, so
494-
uint64_t priorityLeader = getSchain()->getOptimizerAgent()->getPriorityLeaderForBlock(
494+
auto priorityLeader = getSchain()->getOptimizerAgent()->getPriorityLeaderForBlock(
495495
(uint64_t) nodeCount, _blockId);
496496

497-
for (uint64_t i = priorityLeader; i < priorityLeader + nodeCount; i++) {
497+
for (uint64_t i = (uint64_t ) priorityLeader; i < priorityLeader + nodeCount; i++) {
498498
auto proposerIndex = schain_index(i % nodeCount) + 1;
499499

500500
if (haveTrueDecision(_blockId, proposerIndex)) {
@@ -524,7 +524,7 @@ void BlockConsensusAgent::decideNormalBlockConsensusIfCan(block_id _blockId) {
524524
void BlockConsensusAgent::decideOptimizedBlockConsensusIfCan( block_id _blockId ) {
525525

526526

527-
schain_index lastWinner = getSchain()->getOptimizerAgent()->getLastWinner( _blockId );
527+
schain_index lastWinner = getSchain()->getOptimizerAgent()->getPreviousWinner(_blockId);
528528

529529
if ( haveTrueDecision(_blockId, lastWinner) ) {
530530
// last winner consensus completed with 1

0 commit comments

Comments
 (0)