Skip to content

Commit

Permalink
806 fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kladkogex committed Apr 30, 2024
1 parent aed6810 commit c88af1f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 27 deletions.
11 changes: 9 additions & 2 deletions chains/Schain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,14 @@ void Schain::writeToVisualizationStream( string& _s ) {
u256 Schain::getRandomForBlockId( block_id _blockId ) {
auto block = getBlock( _blockId );
CHECK_STATE( block );
auto signature = block->getThresholdSig();
return getRandomForBlock(block);
}

u256 Schain::getRandomForBlock(const ptr<CommittedBlock> &_block) const {

CHECK_STATE(_block);

auto signature = _block->getThresholdSig();

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

Expand Down Expand Up @@ -1490,7 +1497,7 @@ Schain::calculateBooleanProposalVectorIfItsTimeToStartBinaryConsensus(const ptr<
// when we do optimized block consensus only a single block proposer
// proposes and provides da proof, which is the previous winner.
// proposals from other nodes, if sent made by mistake, are ignored
auto lastWinner = getOptimizerAgent()->getLastWinner(_daProof->getBlockId());
auto lastWinner = getOptimizerAgent()->getPreviousWinner(_daProof->getBlockId());
if (_daProof->getProposerIndex() == lastWinner) {
getNode()->getDaProofDB()->addDAProof(_daProof);
pv = make_shared<BooleanProposalVector>(getNodeCount(), lastWinner);
Expand Down
2 changes: 2 additions & 0 deletions chains/Schain.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,6 @@ class Schain : public Agent {

ptr<BooleanProposalVector>
calculateBooleanProposalVectorIfItsTimeToStartBinaryConsensus(const ptr<DAProof> &_daProof);

u256 getRandomForBlock(const ptr<CommittedBlock> &_block) const;
};
63 changes: 44 additions & 19 deletions monitoring/OptimizerAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bool OptimizerAgent::doOptimizedConsensus(block_id _blockId, uint64_t _lastBlock
}


auto lastWinner = getLastWinner(_blockId);
auto lastWinner = getPreviousWinner(_blockId);

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

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

}
if ((uint64_t )_blockId % (nodeCount + 1) == 0) {
return false;
}

// optimize
return true;
}

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

uint64_t seed;
if (doOptimizedConsensus(_blockID,
getSchain()->getLastCommittedBlockTimeStamp().getS())) {
// in optimized consensus the priority leader is just the previous winner from this block_id minus 16
// we want the previous winner to win again
auto previousWinner = (uint64_t ) getSchain()->getOptimizerAgent()->getPreviousWinner(_blockID);
CHECK_STATE(previousWinner > 0)
priorityLeader = previousWinner - 1;
} else {
// full consensus the priority leader is randomly selected for each block
// based on random seed
auto seed = calculateRandomSeedForConsensus(_blockID);
priorityLeader = (seed % _nodeCount);
}

CHECK_STATE(priorityLeader < _nodeCount);
return priorityLeader;
}

uint64_t OptimizerAgent::calculateRandomSeedForConsensus(const block_id &_blockID) const {
uint64_t seed;
if (_blockID <= 1) {
seed = 1;
} else {
Expand All @@ -72,26 +96,27 @@ uint64_t OptimizerAgent::getPriorityLeaderForBlock(uint64_t _nodeCount, block_id
BOOST_THROW_EXCEPTION(InvalidStateException(
"Can not read block " + to_string(_blockID - 1) + " from LevelDB",
__CLASS_NAME__ ));
seed = *((uint64_t *) previousBlock->getHash().data());
}

priorityLeader = ((uint64_t) seed) % _nodeCount;

if (doOptimizedConsensus(_blockID,
getSchain()->getLastCommittedBlockTimeStamp().getS())) {
priorityLeader = (uint64_t) getSchain()->getOptimizerAgent()->getLastWinner(_blockID);
// now get the seed. In full consensus priority leader for the block id will be derived from it
if (getSchain()->fastConsensusPatch(previousBlock->getTimeStampS())) {
// for newer consensus, the nodes a given priority in round robin fashion
seed = (uint64_t ) _blockID;
} else {
// the legacy seed is not perfect since blockhash can be manipulated by block proposer
seed = *((uint64_t *) previousBlock->getHash().data());
}
}
CHECK_STATE(priorityLeader <= _nodeCount);
return priorityLeader;
return seed;
}


schain_index OptimizerAgent::getLastWinner(block_id _blockId) {
// first 16 blocks we do not know the winner
// will return the index for the proposer that won 16 blocks ago
schain_index OptimizerAgent::getPreviousWinner(block_id _blockId) {
// first 16 blocks we do not know the previous winner
if ((uint64_t) _blockId <= getSchain()->getNodeCount()) {
return 0;
}
auto block = getSchain()->getBlock((uint64_t) _blockId - (uint64_t) getSchain()->getNodeCount());
auto block =
getSchain()->getBlock((uint64_t) _blockId - (uint64_t) getSchain()->getNodeCount());

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


4 changes: 3 additions & 1 deletion monitoring/OptimizerAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ class OptimizerAgent : public Agent {
// we determine consensus winner each 16 blocks
[[nodiscard]] bool doOptimizedConsensus(block_id _blockId, uint64_t _lastBlockTimeStamp);

schain_index getLastWinner(block_id _block);
schain_index getPreviousWinner(block_id _blockId);


schain_index skipSendingProposalToTheNetwork(block_id _blockId);


uint64_t getPriorityLeaderForBlock(uint64_t _nodeCount, block_id &_blockID);

uint64_t calculateRandomSeedForConsensus(const block_id &_blockID) const;
};
10 changes: 5 additions & 5 deletions protocols/blockconsensus/BlockConsensusAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void BlockConsensusAgent::startConsensusProposal(
// Optimized consensus. Start N binary consensuses
// for optimized block consensus, we only propose and initiated binary consensus
// for the last block winner
auto lastWinner = getSchain()->getOptimizerAgent()->getLastWinner(_blockID);
auto lastWinner = getSchain()->getOptimizerAgent()->getPreviousWinner(_blockID);
auto x = bin_consensus_value(_proposal->getProposalValue(schain_index(lastWinner)) ? 1 : 0);
propose(x, lastWinner, _blockID);
return;
Expand Down Expand Up @@ -241,7 +241,7 @@ void BlockConsensusAgent::reportConsensusAndDecideIfNeeded(
// winner and ignoring all other messages, even if someone sends them by mistake
if (getSchain()->getOptimizerAgent()->doOptimizedConsensus(blockID,
getSchain()->getLastCommittedBlockTimeStamp().getS()) &&
(uint64_t) blockProposerIndex != getSchain()->getOptimizerAgent()->getLastWinner(blockID)) {
(uint64_t) blockProposerIndex != getSchain()->getOptimizerAgent()->getPreviousWinner(blockID)) {
LOG(warn, "Consensus got ChildBVBroadcastMessage for non-winner in optimized round:" + blockProposerIndex);
return;
}
Expand Down Expand Up @@ -491,10 +491,10 @@ bool BlockConsensusAgent::haveFalseDecision(block_id _blockId, schain_index _pro
void BlockConsensusAgent::decideNormalBlockConsensusIfCan(block_id _blockId) {
auto nodeCount = (uint64_t) getSchain()->getNodeCount();
// note, priorityLeader is numbered from 0 to N-1, so
uint64_t priorityLeader = getSchain()->getOptimizerAgent()->getPriorityLeaderForBlock(
auto priorityLeader = getSchain()->getOptimizerAgent()->getPriorityLeaderForBlock(
(uint64_t) nodeCount, _blockId);

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

if (haveTrueDecision(_blockId, proposerIndex)) {
Expand Down Expand Up @@ -524,7 +524,7 @@ void BlockConsensusAgent::decideNormalBlockConsensusIfCan(block_id _blockId) {
void BlockConsensusAgent::decideOptimizedBlockConsensusIfCan( block_id _blockId ) {


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

if ( haveTrueDecision(_blockId, lastWinner) ) {
// last winner consensus completed with 1
Expand Down

0 comments on commit c88af1f

Please sign in to comment.