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

Bug/skale 5059 limit number of filters per ip #1062

Draft
wants to merge 10 commits into
base: develop
Choose a base branch
from
18 changes: 14 additions & 4 deletions libethereum/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1320,19 +1320,29 @@ void Client::updateIMABLSPublicKey() {

// new block watch
unsigned Client::installNewBlockWatch(
std::function< void( const unsigned&, const Block& ) >& fn ) {
std::function< void( const unsigned&, const Block& ) >& fn, const std::string& strOrigin ) {
if ( m_filtersByIp[strOrigin] == MAX_FILTERS_PER_IP_COUNT )
throw std::invalid_argument( "Too many filters created from " + strOrigin );
m_filtersByIp[strOrigin] += 1;
return m_new_block_watch.install( fn );
}
bool Client::uninstallNewBlockWatch( const unsigned& k ) {
bool Client::uninstallNewBlockWatch( const unsigned& k, const std::string& strOrigin ) {
m_filtersByIp[strOrigin] -= 1;
return m_new_block_watch.uninstall( k );
}

// new pending transation watch
unsigned Client::installNewPendingTransactionWatch(
std::function< void( const unsigned&, const Transaction& ) >& fn ) {
std::function< void( const unsigned&, const Transaction& ) >& fn,
const std::string& strOrigin ) {
if ( m_filtersByIp[strOrigin] == MAX_FILTERS_PER_IP_COUNT )
throw std::invalid_argument( "Too many filters created from " + strOrigin );
m_filtersByIp[strOrigin] += 1;
return m_new_pending_transaction_watch.install( fn );
}
bool Client::uninstallNewPendingTransactionWatch( const unsigned& k ) {
bool Client::uninstallNewPendingTransactionWatch(
const unsigned& k, const std::string& strOrigin ) {
m_filtersByIp[strOrigin] -= 1;
return m_new_pending_transaction_watch.uninstall( k );
}

Expand Down
10 changes: 6 additions & 4 deletions libethereum/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,15 @@ class Client : public ClientBase, protected Worker {
public:
// new block watch
virtual unsigned installNewBlockWatch(
std::function< void( const unsigned&, const Block& ) >& ) override;
virtual bool uninstallNewBlockWatch( const unsigned& ) override;
std::function< void( const unsigned&, const Block& ) >&, const std::string& ) override;
virtual bool uninstallNewBlockWatch( const unsigned&, const std::string& ) override;

// new pending transation watch
virtual unsigned installNewPendingTransactionWatch(
std::function< void( const unsigned&, const Transaction& ) >& ) override;
virtual bool uninstallNewPendingTransactionWatch( const unsigned& ) override;
std::function< void( const unsigned&, const Transaction& ) >&,
const std::string& ) override;
virtual bool uninstallNewPendingTransactionWatch(
const unsigned&, const std::string& ) override;
};

} // namespace eth
Expand Down
18 changes: 12 additions & 6 deletions libethereum/ClientBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ using skale::Permanence;
using skale::State;

static const int64_t c_maxGasEstimate = 50000000;
const size_t ClientBase::MAX_FILTERS_PER_IP_COUNT = 100;

ClientWatch::ClientWatch() : lastPoll( std::chrono::system_clock::now() ) {}

Expand Down Expand Up @@ -251,21 +252,25 @@ void ClientBase::prependLogsFromBlock( LogFilter const& _f, h256 const& _blockHa
}
}

unsigned ClientBase::installWatch(
LogFilter const& _f, Reaping _r, fnClientWatchHandlerMulti_t fnOnNewChanges, bool isWS ) {
unsigned ClientBase::installWatch( LogFilter const& _f, const std::string& _strOrigin, Reaping _r,
fnClientWatchHandlerMulti_t fnOnNewChanges, bool isWS ) {
if ( m_filtersByIp[_strOrigin] == MAX_FILTERS_PER_IP_COUNT )
throw std::invalid_argument( "Too many filters created from " + _strOrigin );
h256 h = _f.sha3();
{
Guard l( x_filtersWatches );
if ( !m_filters.count( h ) ) {
LOG( m_loggerWatch ) << "FFF" << _f << h;
m_filters.insert( make_pair( h, _f ) );
}
if ( !_strOrigin.empty() )
m_filtersByIp[_strOrigin] += 1;
}
return installWatch( h, _r, fnOnNewChanges, isWS );
return installWatch( h, _strOrigin, _r, fnOnNewChanges, isWS );
}

unsigned ClientBase::installWatch(
h256 _h, Reaping _r, fnClientWatchHandlerMulti_t fnOnNewChanges, bool isWS ) {
unsigned ClientBase::installWatch( h256 _h, const std::string& _strOrigin, Reaping _r,
fnClientWatchHandlerMulti_t fnOnNewChanges, bool isWS ) {
unsigned ret;
{
Guard l( x_filtersWatches );
Expand All @@ -285,7 +290,7 @@ unsigned ClientBase::installWatch(
return ret;
}

bool ClientBase::uninstallWatch( unsigned _i ) {
bool ClientBase::uninstallWatch( unsigned _i, const std::string& _strOrigin ) {
LOG( m_loggerWatch ) << "XXX" << _i;

Guard l( x_filtersWatches );
Expand All @@ -302,6 +307,7 @@ bool ClientBase::uninstallWatch( unsigned _i ) {
LOG( m_loggerWatch ) << "*X*" << fit->first << ":" << fit->second.filter;
m_filters.erase( fit );
}
m_filtersByIp[_strOrigin] -= 1;
return true;
}

Expand Down
14 changes: 11 additions & 3 deletions libethereum/ClientBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@ class ClientBase : public Interface {
BlockPolarity _polarity, LocalisedLogEntries& io_logs ) const;

/// Install, uninstall and query watches.
unsigned installWatch( LogFilter const& _filter, Reaping _r = Reaping::Automatic,
unsigned installWatch( LogFilter const& _filter, const std::string& _strOrigin = "",
Reaping _r = Reaping::Automatic,
fnClientWatchHandlerMulti_t fnOnNewChanges = fnClientWatchHandlerMulti_t(),
bool isWS = false ) override;
unsigned installWatch( h256 _filterId, Reaping _r = Reaping::Automatic,
unsigned installWatch( h256 _filterId, const std::string& _strOrigin = "",
Reaping _r = Reaping::Automatic,
fnClientWatchHandlerMulti_t fnOnNewChanges = fnClientWatchHandlerMulti_t(),
bool isWS = false ) override;
bool uninstallWatch( unsigned _watchId ) override;
bool uninstallWatch( unsigned _watchId, const std::string& strOrigin = "" ) override;
LocalisedLogEntries peekWatch( unsigned _watchId ) const override;
LocalisedLogEntries checkWatch( unsigned _watchId ) override;

Expand Down Expand Up @@ -206,6 +208,10 @@ class ClientBase : public Interface {
mutable Mutex x_filtersWatches; ///< Our lock.
std::unordered_map< h256, InstalledFilter > m_filters; ///< The dictionary of filters that are
///< active.

std::unordered_map< std::string, size_t > m_filtersByIp; ///< How many filters that are active
///< created by per ip

std::unordered_map< h256, h256s > m_specialFilters =
std::unordered_map< h256, std::vector< h256 > >{
{PendingChangedFilter, {}}, {ChainChangedFilter, {}}};
Expand All @@ -215,6 +221,8 @@ class ClientBase : public Interface {

Logger m_loggerWatch{createLogger( VerbosityDebug, "watch" )};

static const size_t MAX_FILTERS_PER_IP_COUNT;

private:
std::pair< bool, ExecutionResult > estimateGasStep( int64_t _gas, Block& _latestBlock,
Address const& _from, Address const& _destination, u256 const& _value,
Expand Down
22 changes: 13 additions & 9 deletions libethereum/Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@ class Interface {
virtual LocalisedLogEntries logs( LogFilter const& _filter ) const = 0;

/// Install, uninstall and query watches.
virtual unsigned installWatch( LogFilter const& _filter, Reaping _r = Reaping::Automatic,
virtual unsigned installWatch( LogFilter const& _filter, const std::string& _strOrigin = "",
Reaping _r = Reaping::Automatic,
fnClientWatchHandlerMulti_t fnOnNewChanges = fnClientWatchHandlerMulti_t(),
bool isWS = false ) = 0;
virtual unsigned installWatch( h256 _filterId, Reaping _r = Reaping::Automatic,
virtual unsigned installWatch( h256 _filterId, const std::string& _strOrigin = "",
Reaping _r = Reaping::Automatic,
fnClientWatchHandlerMulti_t fnOnNewChanges = fnClientWatchHandlerMulti_t(),
bool isWS = false ) = 0;
virtual bool uninstallWatch( unsigned _watchId ) = 0;
virtual bool uninstallWatch( unsigned _watchId, const std::string& strOrigin = "" ) = 0;
LocalisedLogEntries peekWatchSafe( unsigned _watchId ) const {
try {
return peekWatch( _watchId );
Expand Down Expand Up @@ -262,21 +264,23 @@ class Interface {

public:
// new block watch
virtual unsigned installNewBlockWatch(
std::function< void( const unsigned&, const Block& ) >& ) { // not implemented here
virtual unsigned installNewBlockWatch( std::function< void( const unsigned&, const Block& ) >&,
const std::string& ) { // not implemented here
return unsigned( -1 );
}
virtual bool uninstallNewBlockWatch( const unsigned& ) { // not implemented here
virtual bool uninstallNewBlockWatch( const unsigned&, const std::string& ) { // not implemented
// here
return false;
}

// new pending transation watch
virtual unsigned installNewPendingTransactionWatch( // not implemented here
std::function< void( const unsigned&, const Transaction& ) >& ) {
std::function< void( const unsigned&, const Transaction& ) >&, const std::string& ) {
return unsigned( -1 );
}
virtual bool uninstallNewPendingTransactionWatch( const unsigned& ) { // not implemented
// here
virtual bool uninstallNewPendingTransactionWatch(
const unsigned&, const std::string& ) { // not implemented
// here
return false;
}
};
Expand Down
Loading