Skip to content

Commit

Permalink
Merge pull request #1905 from skalenetwork/enhancement/code-style-has…
Browse files Browse the repository at this point in the history
…h-improvements

Enhancement/code style hash improvements
  • Loading branch information
dimalit authored May 27, 2024
2 parents 850b36b + c8c58e4 commit 93c07eb
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 55 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.18.1
3.18.2
1 change: 1 addition & 0 deletions libethcore/ChainOperationParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ struct ChainOperationParams {
u256 externalGasDifficulty = ~u256( 0 );
typedef std::vector< std::string > vecAdminOrigins_t;
vecAdminOrigins_t vecAdminOrigins; // wildcard based folters for IP addresses
int getLogsBlocksLimit = -1;
};

} // namespace eth
Expand Down
3 changes: 3 additions & 0 deletions libethcore/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,8 @@ DEV_SIMPLE_EXCEPTION( FailedToDownloadDaoForkBlockHeader );
DEV_SIMPLE_EXCEPTION( AccountLocked );
DEV_SIMPLE_EXCEPTION( TransactionRefused );
DEV_SIMPLE_EXCEPTION( UnknownAccount );

DEV_SIMPLE_EXCEPTION( TooBigResponse );

} // namespace eth
} // namespace dev
3 changes: 2 additions & 1 deletion libethereum/ChainParams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ ChainParams ChainParams::loadConfig(
cp.skaleDisableChainIdCheck = params.count( c_skaleDisableChainIdCheck ) ?
params[c_skaleDisableChainIdCheck].get_bool() :
false;

cp.getLogsBlocksLimit =
params.count( "getLogsBlocksLimit" ) ? params.at( "getLogsBlocksLimit" ).get_int() : -1;

if ( obj.count( c_skaleConfig ) ) {
processSkaleConfigItems( cp, obj );
Expand Down
15 changes: 3 additions & 12 deletions libethereum/ClientBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ LocalisedLogEntries ClientBase::logs( LogFilter const& _f ) const {
unsigned begin = min( bc().number() + 1, ( unsigned ) _f.latest() );
unsigned end = min( bc().number(), min( begin, ( unsigned ) _f.earliest() ) );

if ( begin >= end && begin - end > ( unsigned ) bc().chainParams().getLogsBlocksLimit )
BOOST_THROW_EXCEPTION( TooBigResponse() );

// Handle pending transactions differently as they're not on the block chain.
if ( begin > bc().number() ) {
Block temp = postSeal();
Expand Down Expand Up @@ -343,18 +346,6 @@ bool ClientBase::uninstallWatch( unsigned _i ) {
return true;
}

LocalisedLogEntries ClientBase::peekWatch( unsigned _watchId ) const {
Guard l( x_filtersWatches );

// LOG(m_loggerWatch) << "peekWatch" << _watchId;
auto& w = m_watches.at( _watchId );
// LOG(m_loggerWatch) << "lastPoll updated to " <<
// chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count();
if ( w.lastPoll != chrono::system_clock::time_point::max() )
w.lastPoll = chrono::system_clock::now();
return w.get_changes();
}

LocalisedLogEntries ClientBase::checkWatch( unsigned _watchId ) {
Guard l( x_filtersWatches );
LocalisedLogEntries ret;
Expand Down
11 changes: 9 additions & 2 deletions libethereum/ClientBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ClientBase : public Interface {
/// @param _callback Optional callback function for progress reporting
std::pair< u256, ExecutionResult > estimateGas( Address const& _from, u256 _value,
Address _dest, bytes const& _data, int64_t _maxGas, u256 _gasPrice,
GasEstimationCallback const& _callback ) override;
GasEstimationCallback const& _callback = GasEstimationCallback() ) override;

u256 balanceAt( Address _a ) const override;
u256 countAt( Address _a ) const override;
Expand All @@ -114,9 +114,16 @@ class ClientBase : public Interface {
fnClientWatchHandlerMulti_t fnOnNewChanges = fnClientWatchHandlerMulti_t(),
bool isWS = false ) override;
bool uninstallWatch( unsigned _watchId ) override;
LocalisedLogEntries peekWatch( unsigned _watchId ) const override;
LocalisedLogEntries checkWatch( unsigned _watchId ) override;


using Interface::blockDetails;
using Interface::blockInfo; // for another overload
using Interface::transactionHashes;
using Interface::uncle;
using Interface::uncleCount;
using Interface::uncleHashes;

h256 hashFromNumber( BlockNumber _number ) const override;
BlockNumber numberFromHash( h256 _blockHash ) const override;
int compareBlockHashes( h256 _h1, h256 _h2 ) const override;
Expand Down
9 changes: 0 additions & 9 deletions libethereum/Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,13 @@ class Interface {
fnClientWatchHandlerMulti_t fnOnNewChanges = fnClientWatchHandlerMulti_t(),
bool isWS = false ) = 0;
virtual bool uninstallWatch( unsigned _watchId ) = 0;
LocalisedLogEntries peekWatchSafe( unsigned _watchId ) const {
try {
return peekWatch( _watchId );
} catch ( ... ) {
return LocalisedLogEntries();
}
}
LocalisedLogEntries checkWatchSafe( unsigned _watchId ) {
try {
return checkWatch( _watchId );
} catch ( ... ) {
return LocalisedLogEntries();
}
}
virtual LocalisedLogEntries peekWatch( unsigned _watchId ) const = 0;
virtual LocalisedLogEntries checkWatch( unsigned _watchId ) = 0;

// [BLOCK QUERY API]
Expand Down Expand Up @@ -328,7 +320,6 @@ class Watch : public boost::noncopyable {
}

LocalisedLogEntries check() { return m_c ? m_c->checkWatch( m_id ) : LocalisedLogEntries(); }
LocalisedLogEntries peek() { return m_c ? m_c->peekWatch( m_id ) : LocalisedLogEntries(); }
LocalisedLogEntries logs() const { return m_c->logs( m_id ); }

private:
Expand Down
6 changes: 4 additions & 2 deletions libskale/httpserveroverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2607,12 +2607,14 @@ bool SkaleServerOverride::implStartListening( // proxygen HTTP
cc::success( "/" ) + cc::notice( esm2str( esm ) ) + " " );
return true;
} catch ( const std::exception& ex ) {
logTraceServerEvent( false, ipVer, bIsSSL ? "HTTPS" : "HTTP", pSrv->serverIndex(), esm,
logTraceServerEvent( false, ipVer, bIsSSL ? "HTTPS" : "HTTP",
pSrv ? pSrv->serverIndex() : -1, esm,
cc::fatal( "FAILED" ) + cc::error( " to start " ) + cc::attention( "proxygen" ) +
cc::debug( "/" ) + cc::warn( bIsSSL ? "HTTPS" : "HTTP" ) +
cc::error( " server: " ) + cc::warn( ex.what() ) );
} catch ( ... ) {
logTraceServerEvent( false, ipVer, bIsSSL ? "HTTPS" : "HTTP", pSrv->serverIndex(), esm,
logTraceServerEvent( false, ipVer, bIsSSL ? "HTTPS" : "HTTP",
pSrv ? pSrv->serverIndex() : -1, esm,
cc::fatal( "FAILED" ) + cc::error( " to start " ) + cc::attention( "proxygen" ) +
cc::debug( "/" ) + cc::warn( bIsSSL ? "HTTPS" : "HTTP" ) +
cc::error( " server: " ) + cc::warn( "unknown exception" ) );
Expand Down
9 changes: 5 additions & 4 deletions libskutils/include/skutils/console_colors.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,13 @@ extern bool string2duration( const std::string& s, std::chrono::duration< uint64
const std::chrono::seconds& seconds = std::chrono::seconds::zero() );
extern std::string duration2string( std::chrono::nanoseconds time );
extern std::string time2string(
std::time_t tt, uint64_t nMicroSeconds, bool isUTC = false, bool isColored = true );
extern std::string time2string( const std::tm& aTm, uint64_t nMicroSeconds, bool isColored = true );
std::time_t tt, uint64_t nMicroSeconds, bool isUTC = false, bool isColored = false );
extern std::string time2string(
const std::tm& aTm, uint64_t nMicroSeconds, bool isColored = false );
extern std::string time2string( const default_clock_t::time_point& ptTime, bool isUTC = false,
bool isDaysInsteadOfYMD = false, bool isColored = true );
bool isDaysInsteadOfYMD = false, bool isColored = false );
extern std::string now2string(
bool isUTC = false, bool isDaysInsteadOfYMD = false, bool isColored = true );
bool isUTC = false, bool isDaysInsteadOfYMD = false, bool isColored = false );
extern std::string jsNow2string( bool isUTC = true );

inline std::string c() {
Expand Down
8 changes: 4 additions & 4 deletions libskutils/src/console_colors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1185,12 +1185,12 @@ std::string duration2string( std::chrono::nanoseconds time ) {
}

std::string time2string(
std::time_t tt, uint64_t nMicroSeconds, bool isUTC, bool isColored /*= true*/ ) {
std::time_t tt, uint64_t nMicroSeconds, bool isUTC, bool isColored /*= false*/ ) {
std::lock_guard< std::mutex > lock( g_libcall_mutex );
struct std::tm aTm = isUTC ? ( *std::gmtime( &tt ) ) : ( *std::localtime( &tt ) );
return time2string( aTm, nMicroSeconds, isColored );
}
std::string time2string( const std::tm& aTm, uint64_t nMicroSeconds, bool isColored /*= true*/ ) {
std::string time2string( const std::tm& aTm, uint64_t nMicroSeconds, bool isColored /*= false*/ ) {
const std::tm& effective_tm = aTm;
std::stringstream ss;
ss << std::setfill( '0' );
Expand Down Expand Up @@ -1255,7 +1255,7 @@ inline time_t clock_2_time_t( const typename clock_type_t::time_point& ptTime )
}

std::string time2string( const default_clock_t::time_point& ptTime, bool isUTC,
bool isDaysInsteadOfYMD, bool isColored /*= true*/ ) {
bool isDaysInsteadOfYMD, bool isColored /*= false*/ ) {
std::stringstream ss;
typedef std::chrono::duration< int,
std::ratio_multiply< std::chrono::hours::period, std::ratio< 24 > >::type >
Expand Down Expand Up @@ -1363,7 +1363,7 @@ std::string time2string( const default_clock_t::time_point& ptTime, bool isUTC,
std::string s = ss.str();
return s;
}
std::string now2string( bool isUTC, bool isDaysInsteadOfYMD, bool isColored /*= true*/ ) {
std::string now2string( bool isUTC, bool isDaysInsteadOfYMD, bool isColored /*= false*/ ) {
default_clock_t::time_point ptTimeNow = default_clock_t::now();
std::string s = time2string( ptTimeNow, isUTC, isDaysInsteadOfYMD, isColored );
return s;
Expand Down
38 changes: 31 additions & 7 deletions libweb3jsonrpc/Eth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ bool hasPotentialInvalidTransactionsInBlock( BlockNumber _bn, const Interface& _

#endif

Eth::Eth( const std::string& configPath, eth::Interface& _eth, eth::AccountHolder& _ethAccounts )
Eth::Eth( const std::string& configPath, eth::Client& _eth, eth::AccountHolder& _ethAccounts )
: skutils::json_config_file_accessor( configPath ),
m_eth( _eth ),
m_ethAccounts( _ethAccounts ),
Expand Down Expand Up @@ -505,11 +505,9 @@ string Eth::eth_call( TransactionSkeleton& t, string const&
if ( strRevertReason.empty() )
strRevertReason = "EVM revert instruction without description message";
std::string strTx = t.toString();
std::string strOut = cc::fatal( "Error message from eth_call():" ) + cc::error( " " ) +
cc::warn( strRevertReason ) + cc::error( ", with call arguments: " ) +
cc::j( strTx ) + cc::error( ", and using " ) +
cc::info( "blockNumber" ) + cc::error( "=" ) +
cc::bright( blockNumber );
std::string strOut = "Error message from eth_call(): " + strRevertReason +
", with call arguments: " + strTx +
", and using blockNumber=" + blockNumber;
cerror << strOut;
throw std::logic_error( strRevertReason );
}
Expand Down Expand Up @@ -833,6 +831,10 @@ Json::Value Eth::eth_getFilterChangesEx( string const& _filterId ) {
Json::Value Eth::eth_getFilterLogs( string const& _filterId ) {
try {
return toJson( client()->logs( static_cast< unsigned int >( jsToInt( _filterId ) ) ) );
} catch ( const TooBigResponse& ) {
BOOST_THROW_EXCEPTION( JsonRpcException( Errors::ERROR_RPC_INVALID_PARAMS,
"Log response size exceeded. Maximum allowed number of requested blocks is " +
to_string( this->client()->chainParams().getLogsBlocksLimit ) ) );
} catch ( ... ) {
BOOST_THROW_EXCEPTION( JsonRpcException( Errors::ERROR_RPC_INVALID_PARAMS ) );
}
Expand All @@ -849,7 +851,29 @@ Json::Value Eth::eth_getFilterLogs( string const& _filterId ) {

Json::Value Eth::eth_getLogs( Json::Value const& _json ) {
try {
return toJson( client()->logs( toLogFilter( _json ) ) );
LogFilter filter = toLogFilter( _json );
if ( !_json["blockHash"].isNull() ) {
if ( !_json["fromBlock"].isNull() || !_json["toBlock"].isNull() )
BOOST_THROW_EXCEPTION( JsonRpcException( Errors::ERROR_RPC_INVALID_PARAMS,
"fromBlock and toBlock are not allowed if blockHash is present" ) );
string strHash = _json["blockHash"].asString();
if ( strHash.empty() )
throw std::invalid_argument( "blockHash cannot be an empty string" );
uint64_t number = m_eth.numberFromHash( jsToFixed< 32 >( strHash ) );
if ( number == PendingBlock )
BOOST_THROW_EXCEPTION( JsonRpcException( Errors::ERROR_RPC_INVALID_PARAMS,
"A block with this hash does not exist in the database. If this is an old "
"block, try connecting to an archive node" ) );
filter.withEarliest( number );
filter.withLatest( number );
}
return toJson( client()->logs( filter ) );
} catch ( const TooBigResponse& ) {
BOOST_THROW_EXCEPTION( JsonRpcException( Errors::ERROR_RPC_INVALID_PARAMS,
"Log response size exceeded. Maximum allowed number of requested blocks is " +
to_string( this->client()->chainParams().getLogsBlocksLimit ) ) );
} catch ( const JsonRpcException& ) {
throw;
} catch ( ... ) {
BOOST_THROW_EXCEPTION( JsonRpcException( Errors::ERROR_RPC_INVALID_PARAMS ) );
}
Expand Down
8 changes: 4 additions & 4 deletions libweb3jsonrpc/Eth.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class KeyPair;
namespace eth {
class AccountHolder;
struct TransactionSkeleton;
class Interface;
class Client;
class LocalisedTransactionReceipt;
} // namespace eth
} // namespace dev
Expand Down Expand Up @@ -135,7 +135,7 @@ std::string exceptionToErrorMessage();
*/
class Eth : public dev::rpc::EthFace, public skutils::json_config_file_accessor {
public:
Eth( const std::string& configPath, eth::Interface& _eth, eth::AccountHolder& _ethAccounts );
Eth( const std::string& configPath, eth::Client& _eth, eth::AccountHolder& _ethAccounts );

virtual RPCModules implementedModules() const override {
return RPCModules{ RPCModule{ "eth", "1.0" } };
Expand Down Expand Up @@ -220,9 +220,9 @@ class Eth : public dev::rpc::EthFace, public skutils::json_config_file_accessor
void setTransactionDefaults( eth::TransactionSkeleton& _t );

protected:
eth::Interface* client() { return &m_eth; }
eth::Client* client() { return &m_eth; }

eth::Interface& m_eth;
eth::Client& m_eth;
eth::AccountHolder& m_ethAccounts;

// a cache that maps the call request to the pair of response string and block number
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/libethereum/ClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ using namespace dev::test;
using namespace dev::p2p;
namespace fs = boost::filesystem;

static size_t rand_port = 1024 + rand() % 64000;
static size_t rand_port = ( srand(time(nullptr)), 1024 + rand() % 64000 );

struct FixtureCommon {
const string BTRFS_FILE_PATH = "btrfs.file";
Expand Down
Loading

0 comments on commit 93c07eb

Please sign in to comment.