Skip to content

Commit

Permalink
Merge branch 'v3.20.0' into bug/IS-1022-sigsegv-in-rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
dimalit authored Jul 25, 2024
2 parents 90766d7 + 96a9822 commit 1e67c5f
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 351 deletions.
4 changes: 4 additions & 0 deletions libweb3jsonrpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ set(sources
Web3Face.h
WhisperFace.h

Tracing.h
Tracing.cpp
TracingFace.h

SkalePerformanceTracker.h
SkalePerformanceTracker.cpp
SkalePerformanceTrackerFace.h
Expand Down
255 changes: 2 additions & 253 deletions libweb3jsonrpc/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,246 +26,8 @@ using namespace dev::eth;
using namespace skale;


#define THROW_TRACE_JSON_EXCEPTION( __MSG__ ) \
throw jsonrpc::JsonRpcException( std::string( __FUNCTION__ ) + ":" + \
std::to_string( __LINE__ ) + ":" + std::string( __MSG__ ) )


void Debug::checkPrivilegedAccess() const {
if ( !m_enablePrivilegedApis ) {
BOOST_THROW_EXCEPTION( jsonrpc::JsonRpcException( "This API call is not enabled" ) );
}
}

void Debug::checkHistoricStateEnabled() const {
#ifndef HISTORIC_STATE
BOOST_THROW_EXCEPTION(
jsonrpc::JsonRpcException( "This API call is available on archive nodes only" ) );
#endif
}

Debug::Debug( eth::Client& _eth, SkaleDebugInterface* _debugInterface, const string& argv,
bool _enablePrivilegedApis )
: m_eth( _eth ),
m_debugInterface( _debugInterface ),
m_argvOptions( argv ),
m_blockTraceCache( MAX_BLOCK_TRACES_CACHE_ITEMS, MAX_BLOCK_TRACES_CACHE_SIZE ),
m_enablePrivilegedApis( _enablePrivilegedApis ) {}


h256 Debug::blockHash( string const& _blockNumberOrHash ) const {
checkPrivilegedAccess();
if ( isHash< h256 >( _blockNumberOrHash ) )
return h256( _blockNumberOrHash.substr( _blockNumberOrHash.size() - 64, 64 ) );
try {
return m_eth.blockChain().numberHash( stoul( _blockNumberOrHash ) );
} catch ( ... ) {
THROW_TRACE_JSON_EXCEPTION( "Invalid argument" );
}
}

Json::Value Debug::debug_traceBlockByNumber( const string&
#ifdef HISTORIC_STATE
_blockNumber
#endif
,
Json::Value const&
#ifdef HISTORIC_STATE
_jsonTraceConfig
#endif
) {
Json::Value ret;
checkHistoricStateEnabled();
#ifdef HISTORIC_STATE
auto bN = jsToBlockNumber( _blockNumber );

if ( bN == LatestBlock || bN == PendingBlock ) {
bN = m_eth.number();
}

if ( !m_eth.isKnown( bN ) ) {
THROW_TRACE_JSON_EXCEPTION( "Unknown block number:" + _blockNumber );
}

if ( bN == 0 ) {
THROW_TRACE_JSON_EXCEPTION( "Block number must be more than zero" );
}

try {
return m_eth.traceBlock( bN, _jsonTraceConfig );
} catch ( std::exception const& _e ) {
THROW_TRACE_JSON_EXCEPTION( _e.what() );
} catch ( ... ) {
THROW_TRACE_JSON_EXCEPTION( "Unknown server error" );
}
#else
THROW_TRACE_JSON_EXCEPTION( "This API call is only supported on archive nodes" );
#endif
}

Json::Value Debug::debug_traceBlockByHash( string const&
#ifdef HISTORIC_STATE
_blockHash
#endif
,
Json::Value const&
#ifdef HISTORIC_STATE
_jsonTraceConfig
#endif
) {
checkHistoricStateEnabled();

#ifdef HISTORIC_STATE
h256 h = jsToFixed< 32 >( _blockHash );

if ( !m_eth.isKnown( h ) ) {
THROW_TRACE_JSON_EXCEPTION( "Unknown block hash" + _blockHash );
}

BlockNumber bN = m_eth.numberFromHash( h );

if ( bN == 0 ) {
THROW_TRACE_JSON_EXCEPTION( "Block number must be more than zero" );
}

try {
return m_eth.traceBlock( bN, _jsonTraceConfig );
} catch ( std::exception const& _e ) {
THROW_TRACE_JSON_EXCEPTION( _e.what() );
} catch ( ... ) {
THROW_TRACE_JSON_EXCEPTION( "Unknown server error" );
}
#else
THROW_TRACE_JSON_EXCEPTION( "This API call is only supported on archive nodes" );
#endif
}


Json::Value Debug::debug_traceTransaction( string const&
#ifdef HISTORIC_STATE
_txHashStr
#endif
,
Json::Value const&
#ifdef HISTORIC_STATE
_jsonTraceConfig
#endif
) {

checkHistoricStateEnabled();
#ifdef HISTORIC_STATE
auto txHash = h256( _txHashStr );

LocalisedTransaction localisedTransaction = m_eth.localisedTransaction( txHash );

if ( localisedTransaction.blockHash() == h256( 0 ) ) {
THROW_TRACE_JSON_EXCEPTION(
"Can't find committed transaction with this hash:" + _txHashStr );
}

auto blockNumber = localisedTransaction.blockNumber();


if ( !m_eth.isKnown( blockNumber ) ) {
THROW_TRACE_JSON_EXCEPTION( "Unknown block number:" + to_string( blockNumber ) );
}

if ( blockNumber == 0 ) {
THROW_TRACE_JSON_EXCEPTION( "Block number must be more than zero" );
}

try {
Json::Value tracedBlock;

tracedBlock = m_eth.traceBlock( blockNumber, _jsonTraceConfig );
STATE_CHECK( tracedBlock.isArray() )
STATE_CHECK( !tracedBlock.empty() )


string lowerCaseTxStr = _txHashStr;
for ( auto& c : lowerCaseTxStr ) {
c = std::tolower( static_cast< unsigned char >( c ) );
}


for ( Json::Value::ArrayIndex i = 0; i < tracedBlock.size(); i++ ) {
Json::Value& transactionTrace = tracedBlock[i];
STATE_CHECK( transactionTrace.isObject() );
STATE_CHECK( transactionTrace.isMember( "txHash" ) );
if ( transactionTrace["txHash"] == lowerCaseTxStr ) {
STATE_CHECK( transactionTrace.isMember( "result" ) );
return transactionTrace["result"];
}
}

THROW_TRACE_JSON_EXCEPTION( "Transaction not found in block" );

} catch ( jsonrpc::JsonRpcException& ) {
throw;
} catch ( std::exception const& _e ) {
THROW_TRACE_JSON_EXCEPTION( _e.what() );
} catch ( ... ) {
THROW_TRACE_JSON_EXCEPTION( "Unknown server error" );
}
#else
BOOST_THROW_EXCEPTION(
jsonrpc::JsonRpcException( "This API call is only supported on archive nodes" ) );
#endif
}

Json::Value Debug::debug_traceCall( Json::Value const&
#ifdef HISTORIC_STATE
_call
#endif
,
std::string const&
#ifdef HISTORIC_STATE
_blockNumber
#endif
,
Json::Value const&
#ifdef HISTORIC_STATE
_jsonTraceConfig
#endif
) {

Json::Value ret;
checkHistoricStateEnabled();

#ifdef HISTORIC_STATE

try {
auto bN = jsToBlockNumber( _blockNumber );

if ( bN == LatestBlock || bN == PendingBlock ) {
bN = m_eth.number();
}

if ( !m_eth.isKnown( bN ) ) {
THROW_TRACE_JSON_EXCEPTION( "Unknown block number:" + _blockNumber );
}

if ( bN == 0 ) {
THROW_TRACE_JSON_EXCEPTION( "Block number must be more than zero" );
}

TransactionSkeleton ts = toTransactionSkeleton( _call );

return m_eth.traceCall(
ts.from, ts.value, ts.to, ts.data, ts.gas, ts.gasPrice, bN, _jsonTraceConfig );
} catch ( jsonrpc::JsonRpcException& ) {
throw;
} catch ( std::exception const& _e ) {
THROW_TRACE_JSON_EXCEPTION( _e.what() );
} catch ( ... ) {
THROW_TRACE_JSON_EXCEPTION( "Unknown server error" );
}

#else
BOOST_THROW_EXCEPTION(
jsonrpc::JsonRpcException( "This API call is only supported on archive nodes" ) );
#endif
}
Debug::Debug( eth::Client& _eth, SkaleDebugInterface* _debugInterface, const string& argv )
: m_eth( _eth ), m_debugInterface( _debugInterface ), m_argvOptions( argv ) {}


Json::Value Debug::debug_accountRangeAt( string const&, int, string const&, int ) {
Expand All @@ -280,22 +42,17 @@ string Debug::debug_preimage( string const& ) {
BOOST_THROW_EXCEPTION( jsonrpc::JsonRpcException( "This API call is not supported" ) );
}


void Debug::debug_pauseBroadcast( bool _pause ) {
checkPrivilegedAccess();
m_eth.skaleHost()->pauseBroadcast( _pause );
}
void Debug::debug_pauseConsensus( bool _pause ) {
checkPrivilegedAccess();
m_eth.skaleHost()->pauseConsensus( _pause );
}
void Debug::debug_forceBlock() {
checkPrivilegedAccess();
m_eth.skaleHost()->forceEmptyBlock();
}

void Debug::debug_forceBroadcast( const string& _transactionHash ) {
checkPrivilegedAccess();
try {
h256 h = jsToFixed< 32 >( _transactionHash );
if ( !m_eth.isKnownTransaction( h ) )
Expand All @@ -311,32 +68,26 @@ void Debug::debug_forceBroadcast( const string& _transactionHash ) {
}

string Debug::debug_interfaceCall( const string& _arg ) {
checkPrivilegedAccess();
return m_debugInterface->call( _arg );
}

string Debug::debug_getVersion() {
checkPrivilegedAccess();
return Version;
}

string Debug::debug_getArguments() {
checkPrivilegedAccess();
return m_argvOptions;
}

string Debug::debug_getConfig() {
checkPrivilegedAccess();
return m_eth.chainParams().getOriginalJson();
}

string Debug::debug_getSchainName() {
checkPrivilegedAccess();
return m_eth.chainParams().sChain.name;
}

uint64_t Debug::debug_getSnapshotCalculationTime() {
checkPrivilegedAccess();
return m_eth.getSnapshotCalculationTime();
}

Expand All @@ -345,7 +96,6 @@ uint64_t Debug::debug_getSnapshotHashCalculationTime() {
}

uint64_t Debug::debug_doStateDbCompaction() {
checkPrivilegedAccess();
auto t1 = boost::chrono::high_resolution_clock::now();
m_eth.doStateDbCompaction();
auto t2 = boost::chrono::high_resolution_clock::now();
Expand All @@ -354,7 +104,6 @@ uint64_t Debug::debug_doStateDbCompaction() {
}

uint64_t Debug::debug_doBlocksDbCompaction() {
checkPrivilegedAccess();
auto t1 = boost::chrono::high_resolution_clock::now();
m_eth.doBlocksDbCompaction();
auto t2 = boost::chrono::high_resolution_clock::now();
Expand Down
22 changes: 1 addition & 21 deletions libweb3jsonrpc/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,17 @@ class Client;
namespace rpc {
class SessionManager;

constexpr size_t MAX_BLOCK_TRACES_CACHE_SIZE = 64 * 1024 * 1024;
constexpr size_t MAX_BLOCK_TRACES_CACHE_ITEMS = 1024 * 1024;

class Debug : public DebugFace {
public:
explicit Debug( eth::Client& _eth, SkaleDebugInterface* _debugInterface = nullptr,
const std::string& argv = std::string(), bool _enablePrivilegedApis = false );
const std::string& argv = std::string() );

virtual RPCModules implementedModules() const override {
return RPCModules{ RPCModule{ "debug", "1.0" } };
}

virtual Json::Value debug_accountRangeAt( std::string const& _blockHashOrNumber, int _txIndex,
std::string const& _addressHash, int _maxResults ) override;
virtual Json::Value debug_traceTransaction(
std::string const& _txHash, Json::Value const& _json ) override;
virtual Json::Value debug_traceCall( Json::Value const& _call, std::string const& _blockNumber,
Json::Value const& _options ) override;
virtual Json::Value debug_traceBlockByNumber(
std::string const& _blockNumber, Json::Value const& _json ) override;
virtual Json::Value debug_traceBlockByHash(
std::string const& _blockHash, Json::Value const& _json ) override;
virtual Json::Value debug_storageRangeAt( std::string const& _blockHashOrNumber, int _txIndex,
std::string const& _address, std::string const& _begin, int _maxResults ) override;
virtual std::string debug_preimage( std::string const& _hashedKey ) override;
Expand Down Expand Up @@ -68,15 +57,6 @@ class Debug : public DebugFace {
eth::Client& m_eth;
SkaleDebugInterface* m_debugInterface = nullptr;
std::string m_argvOptions;
cache::lru_ordered_memory_constrained_cache< std::string, Json::Value > m_blockTraceCache;
bool m_enablePrivilegedApis;


h256 blockHash( std::string const& _blockHashOrNumber ) const;

void checkPrivilegedAccess() const;

void checkHistoricStateEnabled() const;
};

} // namespace rpc
Expand Down
Loading

0 comments on commit 1e67c5f

Please sign in to comment.