Skip to content

Commit

Permalink
#1588 change snapshot hash computation
Browse files Browse the repository at this point in the history
  • Loading branch information
olehnikolaiev committed Jan 12, 2024
1 parent 7469839 commit 1bf2cd7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
11 changes: 7 additions & 4 deletions libdevcore/LevelDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ h256 LevelDB::hashBase() const {
if ( it == nullptr ) {
BOOST_THROW_EXCEPTION( DatabaseError() << errinfo_comment( "null iterator" ) );
}

secp256k1_sha256_t ctx;
secp256k1_sha256_initialize( &ctx );
for ( it->SeekToFirst(); it->Valid(); it->Next() ) {
Expand All @@ -251,6 +252,7 @@ h256 LevelDB::hashBase() const {
bytesConstRef str_key_value( usc.data(), usc.size() );
secp256k1_sha256_write( &ctx, str_key_value.data(), str_key_value.size() );
}

h256 hash;
secp256k1_sha256_finalize( &ctx, hash.data() );
return hash;
Expand Down Expand Up @@ -285,6 +287,7 @@ void LevelDB::hashBasePartially(
if ( it == nullptr ) {
BOOST_THROW_EXCEPTION( DatabaseError() << errinfo_comment( "null iterator" ) );
}

for ( it->Seek( start ); it->Valid() && it->key().ToString() < finish; it->Next() ) {
std::string key_ = it->key().ToString();
std::string value_ = it->value().ToString();
Expand All @@ -293,10 +296,10 @@ void LevelDB::hashBasePartially(
// TODO Move this logic to separate "compatiliblity layer"!
if ( key_ == "pieceUsageBytes" )
continue;
std::string key_value = key_ + value_;
const std::vector< uint8_t > usc( key_value.begin(), key_value.end() );
bytesConstRef str_key_value( usc.data(), usc.size() );
secp256k1_sha256_write( ctx, str_key_value.data(), str_key_value.size() );
std::string keyValue = key_ + value_;
const std::vector< uint8_t > usc( keyValue.begin(), keyValue.end() );
bytesConstRef strKeyValue( usc.data(), usc.size() );
secp256k1_sha256_write( ctx, strKeyValue.data(), strKeyValue.size() );
}
}

Expand Down
12 changes: 7 additions & 5 deletions libskale/SnapshotManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,13 @@ void SnapshotManager::computeDatabaseHash(
BOOST_THROW_EXCEPTION( InvalidPath( _dbDir ) );
}

// std::array< std::string, 23 > lexographicKeysSegments = { "0", "1", "2", "3", "4", "5",
// "6",
// "7", "8", "9", "A", "B", "C", "D", "E", "F", "a", "b", "c", "d", "e", "f", "{" };
std::array< std::string, 11 > lexographicKeysSegments = { "0", "2", "4", "6", "8", "A", "F",
"a", "c", "e", "{" };
std::array< std::string, 17 > lexographicKeysSegments = { std::string( 1, char( 0 ) ),
std::string( 1, char( 16 ) ), std::string( 1, char( 32 ) ), std::string( 1, char( 48 ) ),
std::string( 1, char( 64 ) ), std::string( 1, char( 80 ) ), std::string( 1, char( 96 ) ),
std::string( 1, char( 112 ) ), std::string( 1, char( 128 ) ), std::string( 1, char( 144 ) ),
std::string( 1, char( 160 ) ), std::string( 1, char( 176 ) ), std::string( 1, char( 192 ) ),
std::string( 1, char( 208 ) ), std::string( 1, char( 224 ) ), std::string( 1, char( 240 ) ),
std::string( 1000, char( 255 ) ) };

secp256k1_sha256_t dbCtx;
secp256k1_sha256_initialize( &dbCtx );
Expand Down
9 changes: 7 additions & 2 deletions test/unittests/libweb3core/LevelDBHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ BOOST_AUTO_TEST_CASE( hash ) {
db_copy->insert( dev::db::Slice( "ppieceUsageBytes" ), dev::db::Slice( "123456789" ) );
}

std::array< std::string, 11 > lexographicKeysSegments = { "0", "2", "4", "6", "8", "A",
"F", "a", "c", "e", "{" };
std::array< std::string, 17 > lexographicKeysSegments = { std::string( 1, char( 0 ) ),
std::string( 1, char( 16 ) ), std::string( 1, char( 32 ) ), std::string( 1, char( 48 ) ),
std::string( 1, char( 64 ) ), std::string( 1, char( 80 ) ), std::string( 1, char( 96 ) ),
std::string( 1, char( 112 ) ), std::string( 1, char( 128 ) ), std::string( 1, char( 144 ) ),
std::string( 1, char( 160 ) ), std::string( 1, char( 176 ) ), std::string( 1, char( 192 ) ),
std::string( 1, char( 208 ) ), std::string( 1, char( 224 ) ), std::string( 1, char( 240 ) ),
std::string( 1000, char( 255 ) ) };

secp256k1_sha256_t dbCtx;
secp256k1_sha256_initialize( &dbCtx );
Expand Down

0 comments on commit 1bf2cd7

Please sign in to comment.