Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1347 data destruction #1531

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions libbatched-io/batched_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ void db_splitter::prefixed_db::forEach(
} );
}

void db_splitter::prefixed_db::forEachWithPrefix(std::string& _prefix,
std::function< bool( dev::db::Slice, dev::db::Slice ) > f ) const {
backend->forEachWithPrefix(_prefix, [&]( dev::db::Slice _key, dev::db::Slice _val ) -> bool {
if ( _key[0] != this->prefix )
return true;
dev::db::Slice key_short = dev::db::Slice( _key.data() + 1, _key.size() - 1 );
return f( key_short, _val );
} );
}



} // namespace batched_io
11 changes: 10 additions & 1 deletion libbatched-io/batched_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class db_operations_face {
virtual std::string lookup( dev::db::Slice _key ) const = 0;
virtual bool exists( dev::db::Slice _key ) const = 0;
virtual void forEach( std::function< bool( dev::db::Slice, dev::db::Slice ) > f ) const = 0;

virtual void forEachWithPrefix(
std::string& _prefix, std::function< bool( dev::db::Slice, dev::db::Slice ) > f ) const = 0;
virtual ~db_operations_face() = default;
};

Expand Down Expand Up @@ -69,6 +70,12 @@ class batched_db : public db_face {
m_db->forEach( f );
}

virtual void forEachWithPrefix(
std::string& _prefix, std::function< bool( dev::db::Slice, dev::db::Slice ) > f ) const {
std::lock_guard< std::mutex > foreach_lock( m_batch_mutex );
m_db->forEachWithPrefix( _prefix, f );
}

virtual ~batched_db();

protected:
Expand Down Expand Up @@ -105,6 +112,8 @@ class db_splitter {
virtual std::string lookup( dev::db::Slice _key ) const;
virtual bool exists( dev::db::Slice _key ) const;
virtual void forEach( std::function< bool( dev::db::Slice, dev::db::Slice ) > f ) const;
virtual void forEachWithPrefix(
std::string& _prefix, std::function< bool( dev::db::Slice, dev::db::Slice ) > f ) const;

protected:
virtual void recover() { /* nothing */
Expand Down
19 changes: 19 additions & 0 deletions libdevcore/LevelDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,25 @@ void LevelDB::forEach( std::function< bool( Slice, Slice ) > f ) const {
}
}


void LevelDB::forEachWithPrefix(std::string& _prefix, std::function< bool( Slice, Slice ) > f ) const {
cwarn << "Iterating over the entire LevelDB database: " << this->m_path;
std::unique_ptr< leveldb::Iterator > itr( m_db->NewIterator( m_readOptions ) );
if ( itr == nullptr ) {
BOOST_THROW_EXCEPTION( DatabaseError() << errinfo_comment( "null iterator" ) );
}
auto keepIterating = true;
auto prefixSlice = leveldb::Slice(_prefix);
for ( itr->Seek(prefixSlice); keepIterating && itr->Valid()
&& itr->key().starts_with(prefixSlice); itr->Next() ) {
auto const dbKey = itr->key();
auto const dbValue = itr->value();
Slice const key( dbKey.data(), dbKey.size() );
Slice const value( dbValue.data(), dbValue.size() );
keepIterating = f( key, value );
}
}

h256 LevelDB::hashBase() const {
std::unique_ptr< leveldb::Iterator > it( m_db->NewIterator( m_readOptions ) );
if ( it == nullptr ) {
Expand Down
3 changes: 3 additions & 0 deletions libdevcore/LevelDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class LevelDB : public DatabaseFace {

void forEach( std::function< bool( Slice, Slice ) > f ) const override;

void forEachWithPrefix(
std::string& _prefix, std::function< bool( Slice, Slice ) > f ) const override;

h256 hashBase() const override;
h256 hashBaseWithPrefix( char _prefix ) const;

Expand Down
9 changes: 9 additions & 0 deletions libdevcore/ManuallyRotatingLevelDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ void ManuallyRotatingLevelDB::forEach( std::function< bool( Slice, Slice ) > f )
}
}

void ManuallyRotatingLevelDB::forEachWithPrefix(
std::string& _prefix, std::function< bool( Slice, Slice ) > f ) const {
std::shared_lock< std::shared_mutex > lock( m_mutex );
for ( const auto& p : *io_backend ) {
p->forEachWithPrefix( _prefix, f );
}
}


h256 ManuallyRotatingLevelDB::hashBase() const {
std::shared_lock< std::shared_mutex > lock( m_mutex );
secp256k1_sha256_t ctx;
Expand Down
3 changes: 3 additions & 0 deletions libdevcore/ManuallyRotatingLevelDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class ManuallyRotatingLevelDB : public DatabaseFace {
}

virtual void forEach( std::function< bool( Slice, Slice ) > f ) const;
virtual void forEachWithPrefix(
std::string& _prefix, std::function< bool( Slice, Slice ) > f ) const;

virtual h256 hashBase() const;
};

Expand Down
14 changes: 14 additions & 0 deletions libdevcore/SplitDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ void SplitDB::PrefixedDB::forEach( std::function< bool( Slice, Slice ) > f ) con
} );
}


void SplitDB::PrefixedDB::forEachWithPrefix(
std::string& _prefix, std::function< bool( Slice, Slice ) > f ) const {
std::unique_lock< std::shared_mutex > lock( this->backend_mutex );
auto prefixedString = std::to_string( this->prefix ) + _prefix;
backend->forEachWithPrefix( prefixedString, [&]( Slice _key, Slice _val ) -> bool {
if ( _key[0] != this->prefix )
return true;
Slice key_short = Slice( _key.data() + 1, _key.size() - 1 );
return f( key_short, _val );
} );
}


h256 SplitDB::PrefixedDB::hashBase() const {
// HACK TODO implement that it would work with any DatabaseFace*
const LevelDB* ldb = dynamic_cast< const LevelDB* >( backend.get() );
Expand Down
3 changes: 3 additions & 0 deletions libdevcore/SplitDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class SplitDB {
virtual void commit( std::unique_ptr< WriteBatchFace > _batch );

virtual void forEach( std::function< bool( Slice, Slice ) > f ) const;
virtual void forEachWithPrefix(
std::string& _prefix, std::function< bool( Slice, Slice ) > f ) const;

virtual h256 hashBase() const;

private:
Expand Down
5 changes: 5 additions & 0 deletions libdevcore/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class DatabaseFace {
// of each record in the database. If `f` returns false, the `forEach`
// method must return immediately.
virtual void forEach( std::function< bool( Slice, Slice ) > f ) const = 0;

virtual void forEachWithPrefix(
std::string& _prefix, std::function< bool( Slice, Slice ) > f ) const = 0;


virtual h256 hashBase() const = 0;

virtual bool discardCreatedBatches() { return false; }
Expand Down
6 changes: 5 additions & 1 deletion libskale/OverlayDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@ std::unordered_map< h160, string > OverlayDB::accounts() const {
std::unordered_map< u256, u256 > OverlayDB::storage( const dev::h160& _address ) const {
unordered_map< u256, u256 > storage;
if ( m_db_face ) {
m_db_face->forEach( [&storage, &_address]( Slice key, Slice value ) {
// iterate of a keys that start with the given substring
string prefix((const char*) _address.data(), _address.size);
m_db_face->forEachWithPrefix(prefix, [&storage, &_address]( Slice key, Slice value ) {
if ( key.size() == h160::size + h256::size ) {
// key is storage address
string keyString( key.begin(), key.end() );
Expand All @@ -342,6 +344,8 @@ std::unordered_map< u256, u256 > OverlayDB::storage( const dev::h160& _address )
u256 memoryValue = h256( string( value.begin(), value.end() ),
h256::ConstructFromStringType::FromBinary );
storage[memoryAddress] = memoryValue;
} else {
cerror << "Address mismatch in:" << __FUNCTION__;
}
}
return true;
Expand Down
7 changes: 5 additions & 2 deletions libskale/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,11 @@ void State::commit( dev::eth::CommitBehaviour _commitBehaviour ) {
if ( !account.isAlive() ) {
m_db_ptr->kill( address );
m_db_ptr->killAuxiliary( address, Auxiliary::CODE );
// TODO: remove account storage

if (2023) {
clearStorage( address );
}

} else {
RLPStream rlpStream( 4 );

Expand Down Expand Up @@ -777,7 +781,6 @@ void State::clearStorage( Address const& _contract ) {

totalStorageUsed_ -= ( accStorageUsed + storageUsage[_contract] );
acc->updateStorageUsage( -accStorageUsed );
// TODO Do we need to clear storageUsage[_contract] here?
}

bytes const& State::code( Address const& _addr ) const {
Expand Down