Skip to content

Commit

Permalink
Merge pull request #842 from skalenetwork/develop
Browse files Browse the repository at this point in the history
Pull changes from develop
  • Loading branch information
DmytroNazarenko authored Jul 19, 2024
2 parents 9187360 + 3b83e89 commit 749e2ec
Show file tree
Hide file tree
Showing 54 changed files with 1,189 additions and 268 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ jobs:
build:
runs-on: ubuntu-20.04
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: checkout
uses: actions/checkout@v1

- name: submodule update
run: git submodule update --init --recursive


- name: ls
run: ls

Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/dockerimage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ jobs:
build:
runs-on: ubuntu-20.04
steps:

- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: Free disk space
run: |
sudo apt-get remove -yq cmake libjsoncpp-dev aria2 ansible azure-cli shellcheck rpm xorriso zsync \
Expand Down
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 51 additions & 41 deletions .idea/workspace.xml

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions Consensust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ uint64_t Consensust::getRunningTimeS() {
if ( runningTimeS == 0 ) {
auto env = getenv( "TEST_TIME_S" );

if ( env != NULL ) {
runningTimeS = strtoul( env, NULL, 10 );
if ( env ) {
runningTimeS = strtoul( env, nullptr, 10 );
CHECK_STATE(runningTimeS);
} else {
runningTimeS = DEFAULT_RUNNING_TIME_S;
}
Expand Down
11 changes: 6 additions & 5 deletions catchup/server/CatchupServerAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void CatchupServerAgent::processNextAvailableConnection(


ptr< vector< uint8_t > > CatchupServerAgent::createResponseHeaderAndBinary(
const ptr< ServerConnection >&, nlohmann::json _jsonRequest,
const ptr< ServerConnection >& _connection, nlohmann::json _jsonRequest,
const ptr< Header >& _responseHeader ) {
CHECK_ARGUMENT( _responseHeader );

Expand All @@ -197,7 +197,7 @@ ptr< vector< uint8_t > > CatchupServerAgent::createResponseHeaderAndBinary(
ptr< vector< uint8_t > > serializedBinary = nullptr;

if ( type.compare( Header::BLOCK_CATCHUP_REQ ) == 0 ) {
serializedBinary = createBlockCatchupResponse( _jsonRequest,
serializedBinary = createBlockCatchupResponse( _connection, _jsonRequest,
dynamic_pointer_cast< CatchupResponseHeader >( _responseHeader ), blockID );

} else if ( type.compare( Header::BLOCK_FINALIZE_REQ ) == 0 ) {
Expand Down Expand Up @@ -227,8 +227,8 @@ ptr< vector< uint8_t > > CatchupServerAgent::createResponseHeaderAndBinary(


ptr< vector< uint8_t > > CatchupServerAgent::createBlockCatchupResponse(
nlohmann::json /*_jsonRequest */, const ptr< CatchupResponseHeader >& _responseHeader,
block_id _blockID ) {
const ptr< ServerConnection >& _connectionEnvelope, nlohmann::json /*_jsonRequest */,
const ptr< CatchupResponseHeader >& _responseHeader, block_id _blockID ) {
CHECK_ARGUMENT( _responseHeader );

MONITOR( __CLASS_NAME__, __FUNCTION__ );
Expand Down Expand Up @@ -279,7 +279,8 @@ ptr< vector< uint8_t > > CatchupServerAgent::createBlockCatchupResponse(

auto responseTimeMs = Time::getCurrentTimeMs() - responseStartTimeMs;

LOG( info, "RETURNED_CATCHUP_BLOCKS:" << blockSizes->size() << ":CRT:" << responseTimeMs )
LOG( info, "RETURNED_CATCHUP_BLOCKS:" << blockSizes->size() << ":CRT:" << responseTimeMs
<< ":TO_NODE:" << _connectionEnvelope->getIP() );

return serializedBlocks;
} catch ( ExitRequestedException& e ) {
Expand Down
5 changes: 3 additions & 2 deletions catchup/server/CatchupServerAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ class BlockFinalizeResponseHeader;
class CatchupServerAgent : public AbstractServerAgent {
ptr< CatchupWorkerThreadPool > catchupWorkerThreadPool;

ptr< vector< uint8_t > > createBlockCatchupResponse( nlohmann::json _jsonRequest,
const ptr< CatchupResponseHeader >& _responseHeader, block_id _blockID );
ptr< vector< uint8_t > > createBlockCatchupResponse( const ptr< ServerConnection >& _connectionEnvelope,
nlohmann::json _jsonRequest, const ptr< CatchupResponseHeader >& _responseHeader,
block_id _blockID );


ptr< vector< uint8_t > > createBlockFinalizeResponse( nlohmann::json _jsonRequest,
Expand Down
83 changes: 72 additions & 11 deletions chains/Schain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
#include "messages/NetworkMessageEnvelope.h"
#include "monitoring/MonitoringAgent.h"
#include "monitoring/StuckDetectionAgent.h"
#include "monitoring/OptimizerAgent.h"
#include "network/ClientSocket.h"
#include "network/IO.h"
#include "network/Sockets.h"
Expand Down Expand Up @@ -241,6 +242,7 @@ Schain::Schain( weak_ptr< Node > _node, schain_index _schainIndex, const schain_
node( _node ),
schainIndex( _schainIndex ) {
lastCommittedBlockTimeStamp = TimeStamp( 0, 0 );
setTimeStampValuesFromConfig();

// construct monitoring, timeout and stuck detection agents early
monitoringAgent = make_shared< MonitoringAgent >( *this );
Expand Down Expand Up @@ -290,12 +292,6 @@ Schain::Schain( weak_ptr< Node > _node, schain_index _schainIndex, const schain_

getNode()->registerAgent( this );


if ( getNode()->getPatchTimestamps().count( "verifyDaSigsPatchTimestamp" ) > 0 ) {
this->verifyDaSigsPatchTimestampS =
getNode()->getPatchTimestamps().at( "verifyDaSigsPatchTimestamp" );
}

} catch ( ExitRequestedException& ) {
throw;
} catch ( ... ) {
Expand All @@ -309,6 +305,7 @@ void Schain::constructChildAgents() {
MONITOR( __CLASS_NAME__, __FUNCTION__ )

try {
optimizerAgent = make_shared< OptimizerAgent >( *this );
oracleResultAssemblyAgent = make_shared< OracleResultAssemblyAgent >( *this );
pricingAgent = make_shared< PricingAgent >( *this );
catchupClientAgent = make_shared< CatchupClientAgent >( *this );
Expand Down Expand Up @@ -433,9 +430,12 @@ const atomic< bool >& Schain::getIsStateInitialized() const {
}

bool Schain::verifyDASigsPatch( uint64_t _blockTimeStampS ) {
return verifyDaSigsPatchTimestampS != 0 && _blockTimeStampS >= verifyDaSigsPatchTimestampS;
return verifyDaSigsPatchTimestamp != 0 && _blockTimeStampS >= verifyDaSigsPatchTimestamp;
}

bool Schain::verifyBlsSyncPatch( uint64_t _blockTimeStampS ) {
return verifyBlsSyncPatchTimestamp != 0 && _blockTimeStampS >= verifyBlsSyncPatchTimestamp;
}

void Schain::blockCommitArrived( block_id _committedBlockID, schain_index _proposerIndex,
const ptr< ThresholdSignature >& _thresholdSig, ptr< ThresholdSignature > _daSig ) {
Expand Down Expand Up @@ -548,6 +548,13 @@ void Schain::proposeNextBlock( bool _isCalledAfterCatchup ) {

proposedBlockArrived( myProposal );

if (getOptimizerAgent()->skipSendingProposalToTheNetwork(_proposedBlockID)) {
// a node skips sending and saving its proposal during
// optimized block consensus, if the node was not a winner
// last time
return; // dont propose
}

LOG( debug, "PROPOSING BLOCK NUMBER:" << to_string( _proposedBlockID ) );

auto db = getNode()->getProposalHashDB();
Expand Down Expand Up @@ -891,7 +898,10 @@ void Schain::daProofArrived( const ptr< DAProof >& _daProof ) {
if ( _daProof->getBlockId() <= getLastCommittedBlockID() )
return;

auto pv = getNode()->getDaProofDB()->addDAProof( _daProof );
// this will add the DAProof to DB. If there are enough DAProofs in DB
// to start binary consensus, this will return binary proposal vector of 1s and 0s
auto pv =
addDAProofToDBAndCalculateProposalVectorIfItsTimeToStartBinaryConsensus( _daProof );


if ( pv != nullptr ) {
Expand Down Expand Up @@ -1461,11 +1471,62 @@ void Schain::analyzeErrors( ptr< CommittedBlock > _block ) {
analyzer->analyze( _block );
}
}
uint64_t Schain::getVerifyDaSigsPatchTimestampS() const {
return verifyDaSigsPatchTimestampS;
uint64_t Schain::getVerifyDaSigsPatchTimeStamp() const {
return verifyDaSigsPatchTimestamp;
}


uint64_t Schain::getVerifyBlsSyncPatchTimestampS() const {
return verifyBlsSyncPatchTimestamp;
}


mutex Schain::vdsMutex;

// this function is called on arrival of each DA proof
// if it is time to make binary proposals it will return a vector of 0s and 1s
// for normal consensus it will happen when 2t+1 DA proofs arrive (which is 11)
// for optimized consensus it will happen when a DA proof from the previous winner arrives
ptr<BooleanProposalVector>
Schain::addDAProofToDBAndCalculateProposalVectorIfItsTimeToStartBinaryConsensus(
const ptr<DAProof> &_daProof) {

ptr<BooleanProposalVector> pv;

if (getOptimizerAgent()->doOptimizedConsensus(_daProof->getBlockId(), getLastCommittedBlockTimeStamp().getS())) {
// when we do optimized block consensus only the previous winner
// proposes and provides da proof
// proposals from other nodes, if sent made by mistake, are ignored
auto lastWinner = getOptimizerAgent()->getPreviousWinner( _daProof->getBlockId() );
if (_daProof->getProposerIndex() == lastWinner) {
getNode()->getDaProofDB()->addDAProof(_daProof);
pv = make_shared<BooleanProposalVector>(getNodeCount(), lastWinner);
}
} else {
// do things regular way
// the binary proposal vector is formed and the consensus is started when
// 2/3 of nodes (11) submit a da proof
pv = getNode()->getDaProofDB()->addDAProof(_daProof);
}
return pv;
}

// returns true if fastConsensusPatch ie enabled
bool Schain::fastConsensusPatchEnabled(uint64_t _blockTimeStampSec ) {
return fastConsensusPatchTimestamp != 0 && _blockTimeStampSec >= fastConsensusPatchTimestamp;
}

// macro to set patchstamp variable from connfig
#define SET_TIMESTAMP_FROM_CONFIG(__TIMESTAMP_NAME__) \
{ \
auto& timestamps = getNode()->getPatchTimestamps(); \
if (timestamps.count(#__TIMESTAMP_NAME__) > 0) { \
__TIMESTAMP_NAME__ = timestamps.at(#__TIMESTAMP_NAME__); \
} \
}

// set all timestamp values from config
void Schain::setTimeStampValuesFromConfig() {
SET_TIMESTAMP_FROM_CONFIG(verifyDaSigsPatchTimestamp)
SET_TIMESTAMP_FROM_CONFIG(fastConsensusPatchTimestamp)
SET_TIMESTAMP_FROM_CONFIG(verifyBlsSyncPatchTimestamp)
}
21 changes: 17 additions & 4 deletions chains/Schain.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class CatchupServerAgent;
class MonitoringAgent;
class TimeoutAgent;
class StuckDetectionAgent;

class OptimizerAgent;

class BlockProposalServerAgent;

Expand Down Expand Up @@ -136,6 +136,7 @@ class Schain : public Agent {

ptr< OracleResultAssemblyAgent > oracleResultAssemblyAgent;

ptr<OptimizerAgent> optimizerAgent;

ptr< IO > io;

Expand Down Expand Up @@ -175,7 +176,9 @@ class Schain : public Agent {

ptr< NodeInfo > thisNodeInfo = nullptr;

uint64_t verifyDaSigsPatchTimestampS = 0;
uint64_t verifyDaSigsPatchTimestamp = 0;
uint64_t fastConsensusPatchTimestamp = 0;
uint64_t verifyBlsSyncPatchTimestamp = 0;

// If a BlockError analyzer is added to the queue
// its analyze(CommittedBlock _block) function will be run on commit
Expand Down Expand Up @@ -344,9 +347,9 @@ class Schain : public Agent {

ptr< CryptoManager > getCryptoManager() const;

uint64_t getVerifyDaSigsPatchTimeStamp() const;

uint64_t getVerifyDaSigsPatchTimestampS() const;

uint64_t getVerifyBlsSyncPatchTimestampS() const;

bool isInCreateBlock() const;

Expand Down Expand Up @@ -386,8 +389,18 @@ class Schain : public Agent {

bool verifyDASigsPatch( uint64_t _blockTimeStampSec );

bool verifyBlsSyncPatch( uint64_t _blockTimeStampSec );

void updateInternalChainInfo( block_id _lastCommittedBlockID );

const ptr<CatchupClientAgent> &getCatchupClientAgent() const;

ptr< OptimizerAgent > getOptimizerAgent() const;

bool fastConsensusPatchEnabled( uint64_t _blockTimeStampSec );

void setTimeStampValuesFromConfig();

ptr<BooleanProposalVector>
addDAProofToDBAndCalculateProposalVectorIfItsTimeToStartBinaryConsensus(const ptr<DAProof> &_daProof);
};
9 changes: 7 additions & 2 deletions chains/SchainGettersSetters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ ptr< CryptoManager > Schain::getCryptoManager() const {
return cryptoManager;
}


ptr< OptimizerAgent > Schain::getOptimizerAgent() const {
CHECK_STATE( optimizerAgent );
return optimizerAgent;
}


void Schain::createBlockConsensusInstance() {
blockConsensusInstance = make_shared< BlockConsensusAgent >( *this );
}
Expand Down Expand Up @@ -320,8 +327,6 @@ void Schain::updateLastCommittedBlockInfo( uint64_t _lastCommittedBlockID,
tpsAverage = ( blockSizeAverage * 1000 ) / blockTimeAverageMs;
getRandomForBlockId( ( uint64_t ) lastCommittedBlockID );

if ( getNode()->isSyncOnlyNode() )
return;
}


Expand Down
14 changes: 9 additions & 5 deletions crypto/CryptoManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,10 +754,14 @@ void CryptoManager::verifyBlockSig(
}

if ( verifyRealSignatures ) {
auto _signature = make_shared< ConsensusBLSSignature >(
_sigStr, _blockId, totalSigners, requiredSigners );
if ( !getSchain()->getNode()->isSyncOnlyNode() ||
( getSchain()->getNode()->isSyncOnlyNode() &&
getSchain()->verifyBlsSyncPatch( _ts.getS() ) ) ) {
auto _signature = make_shared< ConsensusBLSSignature >(
_sigStr, _blockId, totalSigners, requiredSigners );

verifyThresholdSig( _signature, _hash, _ts );
verifyThresholdSig( _signature, _hash, _ts );
}
}

} catch ( ... ) {
Expand Down Expand Up @@ -880,7 +884,8 @@ void CryptoManager::verifyThresholdSig(

MONITOR( __CLASS_NAME__, __FUNCTION__ )

if ( verifyRealSignatures ) {
if ( verifyRealSignatures && ( !getSchain()->getNode()->isSyncOnlyNode() ||
getSchain()->verifyBlsSyncPatch( _ts.getS() ) ) ) {
auto blsSig = dynamic_pointer_cast< ConsensusBLSSignature >( _signature );

CHECK_STATE( blsSig );
Expand All @@ -907,7 +912,6 @@ void CryptoManager::verifyThresholdSig(
make_shared< array< uint8_t, HASH_LEN > >( _hash.getHash() ), libBlsSig ),
"BLS sig verification failed using both current and previous key" );
}

} else {
// mockups sigs are not verified
}
Expand Down
21 changes: 21 additions & 0 deletions datastructures/BooleanProposalVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ BooleanProposalVector::BooleanProposalVector( node_count _nodeCount, const strin
}



// This is boolean proposal constructor in case of optimized proposal. In this
// case we proposer 1 for the previos winner and 0 for everyone else
BooleanProposalVector::BooleanProposalVector( node_count _nodeCount, schain_index _previousWinner )
: nodeCount( _nodeCount ) {

CHECK_ARGUMENT( _previousWinner > 0 );
CHECK_ARGUMENT( _previousWinner <= (uint64_t )_nodeCount );
proposals.push_back( false );

for ( uint64_t i = 1; i <= _nodeCount; i++ ) {
if ( i == _previousWinner ) {
proposals.push_back( true );
trueCount++;
} else {
proposals.push_back( false );
}
}
}


bool BooleanProposalVector::getProposalValue( schain_index _index ) {
CHECK_STATE( proposals.size() == nodeCount + 1 );
CHECK_STATE( _index <= ( uint64_t ) nodeCount );
Expand Down
Loading

0 comments on commit 749e2ec

Please sign in to comment.