Skip to content

Commit

Permalink
Add IAppState::setLastReachableBlockNum() to the interface to avoid d…
Browse files Browse the repository at this point in the history
…owncasting.
  • Loading branch information
Toly Kournik committed Mar 12, 2020
1 parent 335573c commit 71d6eb5
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 39 deletions.
Expand Up @@ -83,6 +83,7 @@ class IAppState {
// returns the maximal block number n such that all blocks 1 <= i <= n exist.
// if block 1 does not exist, returns 0.
virtual uint64_t getLastReachableBlockNum() = 0;
virtual void setLastReachableBlockNum(uint64_t) = 0;

// returns the maximum block number that is currently stored in
// the application/storage layer.
Expand Down
2 changes: 1 addition & 1 deletion bftengine/src/bcstatetransfer/BCStateTran.cpp
Expand Up @@ -616,7 +616,7 @@ void BCStateTran::onTimer() {
auto currTime = getMonotonicTimeMilli();
FetchingState fs = getFetchingState();
if (fs == FetchingState::GettingCheckpointSummaries) {
if ((currTime - lastTimeSentAskForCheckpointSummariesMsg) > config_.checkpointSummariesRetransmissionTimeoutMilli) {
if ((currTime - lastTimeSentAskForCheckpointSummariesMsg) > config_.checkpointSummariesRetransmissionTimeoutMilli) {
if (++retransmissionNumberOfAskForCheckpointSummariesMsg > kResetCount_AskForCheckpointSummaries)
clearInfoAboutGettingCheckpointSummary();

Expand Down
1 change: 1 addition & 0 deletions bftengine/src/simplestatetransfer/SimpleStateTran.cpp
Expand Up @@ -104,6 +104,7 @@ class SimpleStateTran : public ISimpleInMemoryStateTransfer {
bool putBlock(uint64_t blockId, char* block, uint32_t blockSize) override;

uint64_t getLastReachableBlockNum() override;
void setLastReachableBlockNum(uint64_t blockId) override {}

uint64_t getLastBlockNum() override;
};
Expand Down
3 changes: 2 additions & 1 deletion bftengine/tests/bcstatetransfer/test_app_state.hpp
Expand Up @@ -66,7 +66,8 @@ class TestAppState : public IAppState {
}

// TODO(AJS): How does this differ from getLastBlockNum?
uint64_t getLastReachableBlockNum() override { return last_block_; };
uint64_t getLastReachableBlockNum() override { return last_block_; }
void setLastReachableBlockNum(uint64_t blockId) override { last_block_ = blockId; }

uint64_t getLastBlockNum() override { return last_block_; };

Expand Down
21 changes: 7 additions & 14 deletions kvbc/include/ReplicaImp.h
Expand Up @@ -243,13 +243,14 @@ class ReplicaImp : public IReplica,
public:
BlockchainAppState(std::shared_ptr<ReplicaImp> parent);

virtual bool hasBlock(uint64_t blockId) override;
virtual bool getBlock(uint64_t blockId, char *outBlock, uint32_t *outBlockSize) override;
virtual bool getPrevDigestFromBlock(
bool hasBlock(uint64_t blockId) override;
bool getBlock(uint64_t blockId, char *outBlock, uint32_t *outBlockSize) override;
bool getPrevDigestFromBlock(
uint64_t blockId, bftEngine::SimpleBlockchainStateTransfer::StateTransferDigest *outPrevBlockDigest) override;
virtual bool putBlock(uint64_t blockId, char *block, uint32_t blockSize) override;
virtual uint64_t getLastReachableBlockNum() override;
virtual uint64_t getLastBlockNum() override;
bool putBlock(uint64_t blockId, char *block, uint32_t blockSize) override;
uint64_t getLastReachableBlockNum() override;
void setLastReachableBlockNum(uint64_t blockNum) override { m_lastReachableBlock = blockNum; }
uint64_t getLastBlockNum() override;

private:
std::shared_ptr<ReplicaImp> m_ptrReplicaImpl = nullptr;
Expand All @@ -258,16 +259,8 @@ class ReplicaImp : public IReplica,
// from IAppState. represents maximal block number n such that all
// blocks 1 <= i <= n exist
std::atomic<concord::storage::blockchain::BlockId> m_lastReachableBlock{0};

friend class ReplicaImp;
};

inline std::shared_ptr<BlockchainAppState> downcast_appstate() {
auto res = std::dynamic_pointer_cast<BlockchainAppState>(m_appState);
assert(res);
return res;
}

// DATA
private:
concordlogger::Logger logger;
Expand Down
17 changes: 9 additions & 8 deletions kvbc/include/ror_app_state.hpp
Expand Up @@ -19,38 +19,39 @@ namespace consensus {

class RoRAppState : public bftEngine::SimpleBlockchainStateTransfer::IAppState {
public:
virtual ~RoRAppState();
~RoRAppState();

RoRAppState(std::shared_ptr<concord::storage::blockchain::DBKeyManipulator> keyManipulator,
std::shared_ptr<concord::storage::IDBClient> localClient,
std::shared_ptr<concord::storage::IDBClient> objectStoreClient);

// returns true IFF block blockId exists
// (i.e. the block is stored in the application/storage layer).
virtual bool hasBlock(uint64_t blockId) override;
bool hasBlock(uint64_t blockId) override;

// If block blockId exists, then its content is returned via the arguments
// outBlock and outBlockSize. Returns true IFF block blockId exists.
virtual bool getBlock(uint64_t blockId, char *outBlock, uint32_t *outBlockSize) override;
bool getBlock(uint64_t blockId, char *outBlock, uint32_t *outBlockSize) override;

// If block blockId exists, then the digest of block blockId-1 is returned via
// the argument outPrevBlockDigest. Returns true IFF block blockId exists.
virtual bool getPrevDigestFromBlock(
uint64_t blockId, bftEngine::SimpleBlockchainStateTransfer::StateTransferDigest *outPrevBlockDigest) override;
bool getPrevDigestFromBlock(uint64_t blockId,
bftEngine::SimpleBlockchainStateTransfer::StateTransferDigest *) override;

// adds block
// blockId - the block number
// block - pointer to a buffer that contains the new block
// blockSize - the size of the new block
virtual bool putBlock(uint64_t blockId, char *block, uint32_t blockSize) override;
bool putBlock(uint64_t blockId, char *block, uint32_t blockSize) override;

// returns the maximal block number n such that all blocks 1 <= i <= n exist.
// if block 1 does not exist, returns 0.
virtual uint64_t getLastReachableBlockNum() override;
uint64_t getLastReachableBlockNum() override;
void setLastReachableBlockNum(uint64_t blockId) override { pImpl_->setLastReachableBlockNum(blockId); }

// returns the maximum block number that is currently stored in
// the application/storage layer.
virtual uint64_t getLastBlockNum() override;
uint64_t getLastBlockNum() override;

// When the state is updated by the application, getLastReachableBlockNum()
// and getLastBlockNum() should always return the same block number.
Expand Down
20 changes: 7 additions & 13 deletions kvbc/src/ReplicaImp.cpp
Expand Up @@ -85,13 +85,11 @@ void ReplicaImp::createReplicaAndSyncState() {
uint64_t removedBlocksNum = replicaStateSync_->execute(
logger, *m_bcDbAdapter, m_appState->getLastReachableBlockNum(), m_replicaPtr->getLastExecutedSequenceNum());
m_lastBlock -= removedBlocksNum;
auto t = std::dynamic_pointer_cast<BlockchainAppState>(m_appState);
assert(t);
t->m_lastReachableBlock -= removedBlocksNum;
LOG_INFO(
logger,
"createReplicaAndSyncState: removedBlocksNum = " << removedBlocksNum << ", new m_lastBlock = " << m_lastBlock
<< ", new m_lastReachableBlock = " << t->m_lastReachableBlock);
m_appState->setLastReachableBlockNum(m_appState->getLastReachableBlockNum() - removedBlocksNum);
LOG_INFO(logger,
"createReplicaAndSyncState: removedBlocksNum = "
<< removedBlocksNum << ", new m_lastBlock = " << m_lastBlock
<< ", new m_lastReachableBlock = " << m_appState->getLastReachableBlockNum());
}
}

Expand Down Expand Up @@ -241,8 +239,7 @@ ReplicaImp::~ReplicaImp() {

Status ReplicaImp::addBlockInternal(const SetOfKeyValuePairs &updates, BlockId &outBlockId) {
m_lastBlock++;
auto pt = downcast_appstate();
pt->m_lastReachableBlock++;
m_appState->setLastReachableBlockNum(m_appState->getLastReachableBlockNum() + 1);

BlockId block = m_lastBlock;
SetOfKeyValuePairs updatesInNewBlock;
Expand Down Expand Up @@ -276,10 +273,7 @@ void ReplicaImp::insertBlockInternal(BlockId blockId, Sliver block) {
// when ST runs, blocks arrive in batches in reverse order. we need to keep
// track on the "Gap" and to close it. Only when it is closed, the last
// reachable block becomes the same as the last block
auto pt = downcast_appstate();
if (blockId == pt->m_lastReachableBlock + 1) {
pt->m_lastReachableBlock = m_lastBlock;
}
if (blockId == m_appState->getLastReachableBlockNum() + 1) m_appState->setLastReachableBlockNum(m_lastBlock);

bool found = false;
Sliver existingBlock;
Expand Down
1 change: 1 addition & 0 deletions kvbc/src/ror_app_state.cpp
Expand Up @@ -114,6 +114,7 @@ class RoRAppStateImpl : public IAppState {
}

uint64_t getLastReachableBlockNum() override { return lastReachableBlockId_; }
void setLastReachableBlockNum(uint64_t blockNum) override { lastReachableBlockId_ = blockNum; }

uint64_t getLastBlockNum() override { return lastBlockId_; }

Expand Down
4 changes: 2 additions & 2 deletions storage/src/merkle_tree_db_adapter.cpp
Expand Up @@ -258,8 +258,8 @@ BlockId DBAdapter::getLastReachableBlock() const {
if (!foundKey.empty() && getDBKeyType(foundKey) == EDBKeyType::Block) {
const auto blockId = DBKeyManipulator::extractBlockIdFromKey(foundKey);
LOG_TRACE(logger_, "Latest reachable block ID " << blockId);
return blockId;
}
return blockId;
}
// No blocks in the system.
return 0;
}
Expand Down

0 comments on commit 71d6eb5

Please sign in to comment.