Skip to content

Commit 8d571c7

Browse files
committed
#1003 use last committed blck timestamp; update parsing from serialized blocks; update tests
1 parent e913b00 commit 8d571c7

16 files changed

Lines changed: 188 additions & 120 deletions

bite/BiteCommittedBlockSerializer.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void BiteCommittedBlockSerializer::serializedSanityCheck(const ptr<vector<uint8_
9090

9191
ptr<CommittedBlock> BiteCommittedBlockSerializer::deserialize(const ptr<vector<uint8_t> > &_serializedBlock,
9292
const ptr<CryptoManager> &_cryptoManager,
93-
const ptr<BiteManager> _biteManager, bool _verifySig) {
93+
const ptr<BiteManager> _biteManager, bool _verifySig ) {
9494
CHECK_ARGUMENT(_serializedBlock);
9595
CHECK_ARGUMENT(_cryptoManager);
9696
CHECK_ARGUMENT(_biteManager);
@@ -148,9 +148,20 @@ ptr<CommittedBlock> BiteCommittedBlockSerializer::deserialize(const ptr<vector<u
148148
BiteAESKeySerializer::deserialize(fbAesKeys, *decryptedAesKeyList);
149149
}
150150

151+
#ifdef BITE2
152+
// Committed block format is self-describing for BITE2:
153+
// reencryption signature is present only for BITE2-format committed blocks.
154+
// FOr such block to have been comitted, it must have been after bite2PatchTimestamp condition
155+
bool isBite2PatchEnabledForBlock = blockHeader->getReencryptionThresholdSig().has_value();
156+
#endif
151157

152158
auto decryptedTransactionDataFields = _biteManager->verifyAndDecryptTransactionList(
153-
*transactionList, *decryptedAesKeyList, blockHeader->getEpochID(), blockHeader->getTimeStamp() );
159+
*transactionList, *decryptedAesKeyList, (uint64_t)blockHeader->getEpochID()
160+
#ifdef BITE2
161+
, isBite2PatchEnabledForBlock
162+
#endif
163+
);
164+
154165
ptr<CommittedBlock> block = nullptr;
155166

156167
try {

bite/BiteManager.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ void BiteManager::parseBITETransactions(
5454
ptr<BlockProposal> _proposal) {
5555

5656
BiteRuntimeContext runtimeCtx {
57-
.currentEpoch = _proposal->getEpochID()
57+
.currentEpoch = schain.getNode()->getCurrentEpochId()
5858
#ifdef BITE2
59-
,
60-
.isBite2PatchEnabled = schain.bite2Patch( _proposal->getTimeStampS() )
59+
, .isBite2PatchEnabled = schain.bite2Patch( schain.getLastCommittedBlockTimeStamp().getS() )
6160
#endif
6261
};
6362

@@ -175,15 +174,20 @@ std::shared_ptr<DecryptedAESKeyList> BiteManager::mergeAESKeys(
175174
DecryptedTransactions BiteManager::verifyAndDecryptTransactionList(
176175
const TransactionList &_transactionList,
177176
const DecryptedAESKeyList &_aesKeys,
178-
epoch_id _epochId,
179-
uint64_t _blockTimestampS ) {
177+
uint64_t _epochId
178+
#ifdef BITE2
179+
, bool _isBite2PatchEnabledForBlock
180+
#endif
181+
) {
180182

181183
MONITOR(__CLASS_NAME__, __FUNCTION__);
182184

183185
BiteRuntimeContext runtimeCtx {
184186
.currentEpoch = _epochId,
185-
.threadPoolExecutor = threadPoolExecutor,
186-
.isBite2PatchEnabled = schain.bite2Patch( _blockTimestampS )
187+
.threadPoolExecutor = threadPoolExecutor
188+
#ifdef BITE2
189+
, .isBite2PatchEnabled = _isBite2PatchEnabledForBlock
190+
#endif
187191
};
188192

189193
return biteEngine.decryptTransactionsListInParallel(

bite/BiteManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class BiteManager {
7575

7676
[[nodiscard]] DecryptedTransactions verifyAndDecryptTransactionList(
7777
const TransactionList &_transactionList, const DecryptedAESKeyList &_aesKeys,
78-
epoch_id _epochId, uint64_t _blockTimestampS );
78+
uint64_t _epochId, bool _isBite2PatchEnabledForBlock );
7979

8080

8181
// ============== Getters ============== //

chains/Schain.cpp

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,10 @@ void Schain::blockCommitArrived(block_id _committedBlockID, schain_index _propos
560560
if ( isBite2PatchEnabled ) {
561561
CHECK_ARGUMENT(_reencryptionThresholdSig)
562562
}
563+
else {
564+
// enforce bite2 blocks are rejected if we are not yet in bite2 patch
565+
CHECK_ARGUMENT(!_reencryptionThresholdSig)
566+
}
563567
#endif
564568
#endif
565569

@@ -950,13 +954,12 @@ void Schain::saveBlock(const ptr<CommittedBlock> &_block) {
950954

951955
try {
952956
checkForExit();
953-
// save in block db
954-
getNode()->getBlockDB()->saveBlock(_block);
955957

956958
#ifdef BITE2
959+
// save random before saving block. If block is ever available in db, random should also be
957960
// compute reencryption random from block signature & save in random db
958961
std::optional<string> reencryptionSignature = _block->getReencryptionThresholdSig();
959-
bool isBite2PatchEnabled = bite2Patch(_block->getTimeStampS());
962+
bool isBite2PatchEnabled = bite2Patch( getLastCommittedBlockTimeStamp().getS() );
960963
if (isBite2PatchEnabled) {
961964
CHECK_STATE2(reencryptionSignature.has_value(),
962965
"BITE2 patch is enabled but reencryption signature is missing for block " + to_string( _block->getBlockID() ));
@@ -967,7 +970,15 @@ void Schain::saveBlock(const ptr<CommittedBlock> &_block) {
967970
getSchain()->getNode()->getRandomDB()->writeDomainRandom(
968971
blockconsensus::REENCRYPTION_RANDOM_DOMAIN, _block->getBlockID(), random );
969972
}
973+
else {
974+
CHECK_STATE2(!reencryptionSignature.has_value(),
975+
"BITE2 patch is not enabled but reencryption signature is present for block " + to_string( _block->getBlockID() ));
976+
}
970977
#endif
978+
979+
// save in block db
980+
getNode()->getBlockDB()->saveBlock(_block);
981+
971982
} catch (ExitRequestedException &) {
972983
throw;
973984
} catch (...) {
@@ -1233,12 +1244,18 @@ void Schain::bootstrap(block_id _lastCommittedBlockID, uint64_t _lastCommittedBl
12331244
}
12341245

12351246

1236-
// Step 0 Workaround for the fact that skaled does not yet save timestampMs
1237-
1238-
if (_lastCommittedBlockTimeStampMs == 0 && _lastCommittedBlockID > 0) {
1247+
// Step 0: recover missing timestamp fields from consensus DB block if needed.
1248+
// For test/continue startup we may only know the last committed block id.
1249+
if ( _lastCommittedBlockID > 0 &&
1250+
( _lastCommittedBlockTimeStamp == 0 || _lastCommittedBlockTimeStampMs == 0 ) ) {
12391251
auto block = getNode()->getBlockDB()->getBlock(_lastCommittedBlockID, getCryptoManager());
12401252
if (block) {
1241-
_lastCommittedBlockTimeStampMs = block->getTimeStampMs();
1253+
if ( _lastCommittedBlockTimeStamp == 0 ) {
1254+
_lastCommittedBlockTimeStamp = block->getTimeStampS();
1255+
}
1256+
if ( _lastCommittedBlockTimeStampMs == 0 ) {
1257+
_lastCommittedBlockTimeStampMs = block->getTimeStampMs();
1258+
}
12421259
};
12431260
}
12441261

@@ -1262,12 +1279,16 @@ void Schain::bootstrap(block_id _lastCommittedBlockID, uint64_t _lastCommittedBl
12621279
while (lastCommittedBlockIDInConsensus > _lastCommittedBlockID)
12631280

12641281
try {
1282+
bool isBite2PatchEnabledForBlock = false;
1283+
#ifdef BITE2
1284+
isBite2PatchEnabledForBlock = bite2Patch( getLastCommittedBlockTimeStamp().getS() );
1285+
#endif
12651286
auto block = getNode()->getBlockDB()->getBlock(
1266-
_lastCommittedBlockID + 1, getCryptoManager());
1287+
_lastCommittedBlockID + 1, getCryptoManager() );
12671288
CHECK_STATE2(block, "No block in consensus, repair needed");
12681289
#ifdef BITE2
1269-
if ( bite2Patch( block->getTimeStampS() ) ) {
1270-
auto reencryptionSignature = block->getReencryptionThresholdSig();
1290+
auto reencryptionSignature = block->getReencryptionThresholdSig();
1291+
if ( isBite2PatchEnabledForBlock ) {
12711292
CHECK_STATE2( reencryptionSignature.has_value(),
12721293
"BITE2 patch is enabled but reencryption signature is missing for replayed block " +
12731294
to_string( (uint64_t) block->getBlockID() ) );
@@ -1279,6 +1300,11 @@ void Schain::bootstrap(block_id _lastCommittedBlockID, uint64_t _lastCommittedBl
12791300
getNode()->getRandomDB()->writeDomainRandom(
12801301
blockconsensus::REENCRYPTION_RANDOM_DOMAIN, block->getBlockID(), random );
12811302
}
1303+
else {
1304+
CHECK_STATE2( !reencryptionSignature.has_value(),
1305+
"BITE2 patch is not enabled but reencryption signature is present for replayed block " +
1306+
to_string( (uint64_t) block->getBlockID() ) );
1307+
}
12821308
#endif
12831309
pushBlockToExtFace(block);
12841310
_lastCommittedBlockID = _lastCommittedBlockID + 1;
@@ -1650,8 +1676,12 @@ void Schain::finalizeDecidedAndSignedBlockInThread(block_id _blockId, schain_ind
16501676
CHECK_STATE(keys);
16511677

16521678
auto transactions = proposal->getTransactionList();
1679+
bool isBite2PatchEnabledForBlock = false;
1680+
#ifdef BITE2
1681+
isBite2PatchEnabledForBlock = bite2Patch( getLastCommittedBlockTimeStamp().getS() );
1682+
#endif
16531683
auto decryptedTransactions = getBiteManager()->verifyAndDecryptTransactionList(
1654-
*transactions, (*keys), proposal->getEpochID(), proposal->getTimeStampS() );
1684+
*transactions, (*keys), (uint64_t)proposal->getEpochID(), isBite2PatchEnabledForBlock );
16551685
#endif
16561686

16571687
auto daProofSig = getNode()->getDaProofDB()->getDASig( _blockId, _proposerIndex );

datastructures/CommittedBlock.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,10 @@ ptr<CommittedBlock> CommittedBlock::deserialize(const ptr<vector<uint8_t> > &_se
213213
#ifdef BITE
214214
const ptr<BiteManager> &_biteManager,
215215
#endif
216-
bool _verifySig) {
216+
bool _verifySig ) {
217217
#ifdef BITE
218-
return BiteCommittedBlockSerializer::deserialize(_serializedBlock, _manager, _biteManager, _verifySig);
218+
return BiteCommittedBlockSerializer::deserialize(
219+
_serializedBlock, _manager, _biteManager, _verifySig );
219220
#endif
220221

221222
CHECK_ARGUMENT(_serializedBlock);

messages/NetworkMessage.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ ptr< NetworkMessage > NetworkMessage::parseMessage(
303303
#ifdef BITE
304304
uint64_t epochID;
305305
#ifdef BITE2
306-
string offchainSigShare;
306+
string reencryptionSignature;
307307
#endif // BITE2
308308
#endif // BITE
309309
uint64_t blockProposerIndex;
@@ -393,8 +393,18 @@ ptr< NetworkMessage > NetworkMessage::parseMessage(
393393
sigShare, srcSchainIndex, ecdsaSig, publicKey, pkSig, _sChain );
394394
} else if ( type == BasicHeader::BLOCK_SIG_BROADCAST ) {
395395
#ifdef BITE2
396-
if ( d.HasMember( "ofss" ) ) {
397-
offchainSigShare = getStringRapid( d, "ofss" );
396+
// Only try parsing member if the message is past bite2 patch time
397+
if ( _sChain->bite2Patch( _sChain->getLastCommittedBlockTimeStamp().getS() ) ) {
398+
if ( d.HasMember( "rsig" ) ) {
399+
reencryptionSignature = getStringRapid( d, "rsig" );
400+
}
401+
else {
402+
CONS_LOG( warn, "BITE2 patch is enabled but reencryption signature is missing in message for block " + to_string( blockID ) );
403+
CHECK_STATE( false )
404+
}
405+
}
406+
else {
407+
CHECK_STATE2( !d.HasMember( "rsig" ), "BITE2 patch is not enabled but reencryption signature is present in message for block " + to_string( blockID ) );
398408
}
399409
#endif
400410
nwkMsg = make_shared< BlockSignBroadcastMessage >( node_id( srcNodeID ),
@@ -406,7 +416,7 @@ ptr< NetworkMessage > NetworkMessage::parseMessage(
406416
schain_id( sChainID ), msg_id( msgID ), sigShare, srcSchainIndex, ecdsaSig,
407417
publicKey, pkSig, _sChain
408418
#ifdef BITE2
409-
, offchainSigShare
419+
, reencryptionSignature
410420
#endif
411421
);
412422
#ifndef FAIR

node/ConsensusEngine.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -940,38 +940,6 @@ u256 ConsensusEngine::getRandomForBlockId( uint64_t _blockId ) const {
940940
return 0; // make compiler happy
941941
}
942942

943-
ptr< CommittedBlock > ConsensusEngine::getCommittedBlockForBlockId( uint64_t _blockId ) const {
944-
CHECK_STATE( nodes.size() > 0 );
945-
946-
for ( auto&& item : nodes ) {
947-
CHECK_STATE( item.second );
948-
auto schain = item.second->getSchain();
949-
CHECK_STATE( schain );
950-
CHECK_STATE( _blockId <= schain->readLastCommittedBlockIDFromDb() );
951-
952-
auto block = item.second->getBlockDB()->getBlock( _blockId, schain->getCryptoManager() );
953-
CHECK_STATE( block );
954-
return block;
955-
}
956-
return nullptr; // make compiler happy
957-
}
958-
959-
ptr< CommittedBlock > ConsensusEngine::getCommittedBlockForBlockIdForNode( uint64_t _blockId, node_id _nodeId ) const {
960-
CHECK_STATE( nodes.size() >= 1 );
961-
962-
auto it = nodes.find( _nodeId );
963-
CHECK_STATE2( it != nodes.end(), "Node with id " + to_string( _nodeId ) + " not found" );
964-
965-
CHECK_STATE( it->second );
966-
auto schain = it->second->getSchain();
967-
CHECK_STATE( schain );
968-
CHECK_STATE( _blockId <= schain->readLastCommittedBlockIDFromDb() );
969-
970-
auto block = it->second->getBlockDB()->getBlock( _blockId, schain->getCryptoManager() );
971-
CHECK_STATE( block );
972-
return block;
973-
}
974-
975943
#ifdef BITE2
976944

977945
u256 ConsensusEngine::getReencryptionRandomForBlockId( uint64_t _blockId ) const {

node/ConsensusEngine.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ using namespace spdlog::level;
4646
class GlobalThreadRegistry;
4747
class StorageLimits;
4848
class CommittedBlock;
49+
class ConsensusEngineTestAccess;
4950

5051

5152
#include "thirdparty/lrucache.hpp"
5253

5354
class ConsensusEngine : public ConsensusInterface {
55+
// allow access to private members for testing purposes
56+
friend class ConsensusEngineTestAccess;
57+
5458
map< node_id, ptr< Node > > nodes; // tsafe
5559

5660
mutable cache::lru_cache< uint64_t, u256 > prices; // tsafe
@@ -321,17 +325,6 @@ class ConsensusEngine : public ConsensusInterface {
321325

322326
std::shared_ptr< std::vector< std::uint8_t > > getSerializedBlock( std::uint64_t _blockNumber );
323327

324-
/**
325-
* @brief Gets the committed block for a given block ID, if available.
326-
* Uses the first node available.
327-
*/
328-
ptr< CommittedBlock > getCommittedBlockForBlockId( uint64_t _blockId ) const;
329-
330-
/**
331-
* @brief Gets the committed block for a given block ID and node ID, if available.
332-
*/
333-
ptr< CommittedBlock > getCommittedBlockForBlockIdForNode( uint64_t _blockId, node_id _nodeId ) const;
334-
335328
// return sync information as requested by eth_syncing API of geth
336329
// if isSyncing is false, all fields will be set to zero.
337330

node/Node.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,8 @@ void Node::startServers( ptr< vector< uint8_t > > _startingFromSnapshotWithThisA
382382
// now save the block into the blocks dd
383383
getBlockDB()->saveBlock( block );
384384
#ifdef BITE2
385-
if ( getSchain()->bite2Patch( block->getTimeStampS() ) ) {
386-
auto reencryptionSignature = block->getReencryptionThresholdSig();
385+
auto reencryptionSignature = block->getReencryptionThresholdSig();
386+
if ( getSchain()->bite2Patch( getSchain()->getLastCommittedBlockTimeStamp().getS() ) ) {
387387
CHECK_STATE2( reencryptionSignature.has_value(),
388388
"BITE2 patch is enabled but reencryption signature is missing for imported snapshot block " +
389389
to_string( (uint64_t) block->getBlockID() ) );
@@ -395,6 +395,10 @@ void Node::startServers( ptr< vector< uint8_t > > _startingFromSnapshotWithThisA
395395
getRandomDB()->writeDomainRandom(
396396
blockconsensus::REENCRYPTION_RANDOM_DOMAIN, block->getBlockID(), random );
397397
}
398+
else {
399+
CHECK_STATE2( !reencryptionSignature.has_value(),
400+
"BITE2 patch is not enabled but reencryption signature is present for imported snapshot block " + to_string( (uint64_t) block->getBlockID() ) );
401+
}
398402
#endif
399403
// now do a sanitity check, that the block was imported OK
400404
CHECK_STATE2( block->getBlockID() == getBlockDB()->readLastCommittedBlockID(),

0 commit comments

Comments
 (0)