Skip to content

Commit

Permalink
#813 eth_sync
Browse files Browse the repository at this point in the history
  • Loading branch information
kladkogex committed Nov 30, 2023
1 parent 91f69db commit 88e3085
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 9 deletions.
25 changes: 17 additions & 8 deletions catchup/client/CatchupClientAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@
@date 2018
*/

#include "SkaleCommon.h"

#include "thirdparty/json.hpp"
#include "SkaleCommon.h"
#include "Log.h"
#include "datastructures/PeerStateInfo.h"
#include "exceptions/ExitRequestedException.h"
#include "exceptions/FatalError.h"

#include "thirdparty/json.hpp"

#include "abstracttcpserver/ConnectionStatus.h"
#include "crypto/CryptoManager.h"

Expand Down Expand Up @@ -59,6 +57,12 @@ CatchupClientAgent::CatchupClientAgent( Schain& _sChain ) : Agent( _sChain, fals
this->catchupClientThreadPool = make_shared< CatchupClientThreadPool >( 1, this );
catchupClientThreadPool->startService();
}

for (int i = 0; i < _sChain.getNodeCount(); i++) {
// init peer state infos
peerStateInfos.push_back(nullptr);
}

} catch ( ExitRequestedException& ) {
throw;
} catch ( ... ) {
Expand Down Expand Up @@ -128,6 +132,14 @@ nlohmann::json CatchupClientAgent::readCatchupResponseHeader(
throw_with_nested( NetworkProtocolException( errString, __CLASS_NAME__ ) );
}

// now see if peerinfo information returned by the peer
ptr<PeerStateInfo> peerStateInfo = PeerStateInfo::extract(response);

if (peerStateInfo) {
// update the info
LOCK(peerStateInfosMutex)
peerStateInfos.at((uint64_t)_dstIndex) = peerStateInfo;
}

LOG( debug, "Catchupc step 2: read catchup response requestHeader" );

Expand All @@ -138,16 +150,13 @@ nlohmann::json CatchupClientAgent::readCatchupResponseHeader(
return 0;
}


if ( status != CONNECTION_PROCEED ) {
BOOST_THROW_EXCEPTION( NetworkProtocolException(
"Server error in catchup response:" + to_string( status ), __CLASS_NAME__ ) );
}


ptr< CommittedBlockList > blocks;


try {
blocks = readMissingBlocks( socket, response, requestHeader );

Expand Down
7 changes: 6 additions & 1 deletion catchup/client/CatchupClientAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@

#pragma once


class CommittedBlockList;
class ClientSocket;
class Schain;
class CatchupClientThreadPool;
class CatchupRequestHeader;
class CatchupResponseHeader;
class PeerStateInfo;


class CatchupClientAgent : public Agent {

ptr< CatchupClientThreadPool > catchupClientThreadPool = nullptr;

vector<ptr<PeerStateInfo>> peerStateInfos;
recursive_mutex peerStateInfosMutex;

public:
explicit CatchupClientAgent( Schain& _sChain );

Expand Down
59 changes: 59 additions & 0 deletions datastructures/PeerStateInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright (C) 2023- SKALE Labs
This file is part of skale-consensus.
skale-consensus is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
skale-consensus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with skale-consensus. If not, see <https://www.gnu.org/licenses/>.
*/

#include "thirdparty/json.hpp"
#include "SkaleCommon.h"
#include "PeerStateInfo.h"

const block_id &PeerStateInfo::getLastBlockId() const {
return lastBlockId;
}

uint64_t PeerStateInfo::getLastBlockTimestampS() const {
return lastBlockTimestampS;
}

PeerStateInfo::PeerStateInfo(const block_id &lastBlockId, uint64_t lastBlockTimestampS) : lastBlockId(lastBlockId),
lastBlockTimestampS(
lastBlockTimestampS) {

}

// extract PeerStateInfo object from catchup response header
ptr<PeerStateInfo> PeerStateInfo::extract(nlohmann::json _catchupResponseHeasder) {

uint64_t lastBid = 0;
uint64_t lastTs = 0;

if (_catchupResponseHeasder.find("lastBid") != _catchupResponseHeasder.end()) {
lastBid = _catchupResponseHeasder.at("lastBid").get<uint64_t>();
}

if (_catchupResponseHeasder.find("lastTs") != _catchupResponseHeasder.end()) {
lastTs = _catchupResponseHeasder.at("lastTs").get<uint64_t >();
}

if (lastBid > 0 && lastTs > 0) {
return make_shared<PeerStateInfo>(block_id(lastBid), lastTs);
} else {
// the node did not provide info
return nullptr;
}
}
42 changes: 42 additions & 0 deletions datastructures/PeerStateInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright (C) 2023- SKALE Labs
This file is part of skale-consensus.
skale-consensus is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
skale-consensus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with skale-consensus. If not, see <https://www.gnu.org/licenses/>.
*/


#pragma once



class PeerStateInfo {

block_id lastBlockId = 0;
uint64_t lastBlockTimestampS = 0;

public:
const block_id &getLastBlockId() const;

uint64_t getLastBlockTimestampS() const;

PeerStateInfo(const block_id &lastBlockId, uint64_t lastBlockTimestampS);

static ptr<PeerStateInfo> extract(nlohmann::json _catchupResponseHeasder);

};


0 comments on commit 88e3085

Please sign in to comment.