From 7c8653a31c348d8c45dd931a34c1e61730f39373 Mon Sep 17 00:00:00 2001 From: Stan Kladko <13399135+kladkogex@users.noreply.github.com> Date: Tue, 7 Nov 2023 13:41:43 +0000 Subject: [PATCH] #1702 simplified parsing --- libethereum/Precompiled.cpp | 38 ++++++++----------------------------- 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/libethereum/Precompiled.cpp b/libethereum/Precompiled.cpp index 4f59be395..5e0d05e63 100644 --- a/libethereum/Precompiled.cpp +++ b/libethereum/Precompiled.cpp @@ -675,8 +675,7 @@ ETH_REGISTER_PRECOMPILED( logTextMessage )( bytesConstRef _in ) { return { false, response }; // 1st false - means bad error occur } -static const std::list< std::string > g_listReadableConfigParts{ "sealEngine", - "skaleConfig.sChain.nodes." }; +static const std::list< std::string > g_listReadableConfigParts{ "skaleConfig.sChain.nodes." }; static bool stat_is_accessible_json_path( const std::string& strPath ) { if ( strPath.empty() ) @@ -721,23 +720,10 @@ static bytes stat_string_to_bytes_with_length( std::string& s ) { return rv; } -static dev::u256 stat_parse_u256_hex_or_dec( const std::string& strValue ) { +static dev::u256 statParseU256HexorDec( const std::string& strValue ) { if ( strValue.empty() ) return dev::u256( 0 ); - const size_t cnt = strValue.length(); - if ( cnt >= 2 && strValue[0] == '0' && ( strValue[1] == 'x' || strValue[1] == 'X' ) ) { - dev::u256 uValue( strValue.c_str() ); - return uValue; - } - dev::u256 uValue = 0; - for ( size_t i = 0; i < cnt; ++i ) { - char chr = strValue[i]; - if ( !( '0' <= chr && chr <= '9' ) ) - throw std::runtime_error( "Bad u256 value \"" + strValue + "\" cannot be parsed" ); - int nDigit = int( chr - '0' ); - uValue *= 10; - uValue += nDigit; - } + dev::u256 uValue( strValue.c_str() ); return uValue; } @@ -746,7 +732,7 @@ static bool isCallToHistoricData( const std::string& callData ) { return boost::algorithm::starts_with( callData, "skaleConfig.sChain.nodes." ); } -static std::pair< std::string, unsigned > parseHistoricFieldReuqest( const std::string& callData ) { +static std::pair< std::string, unsigned > parseHistoricFieldRequest( const std::string& callData ) { size_t numberLength = callData.find( ']' ) - callData.find( '[' ) - 1; unsigned id = std::stoul( callData.substr( callData.find( '[' ) + 1, numberLength ) ); std::string fieldName; @@ -783,7 +769,7 @@ ETH_REGISTER_PRECOMPILED( getConfigVariableUint256 )( bytesConstRef _in ) { std::string field; unsigned id; - std::tie( field, id ) = parseHistoricFieldReuqest( rawName ); + std::tie( field, id ) = parseHistoricFieldRequest( rawName ); if ( field == "id" ) { strValue = g_skaleHost->getHistoricNodeId( id ); } else if ( field == "schainIndex" ) { @@ -792,18 +778,10 @@ ETH_REGISTER_PRECOMPILED( getConfigVariableUint256 )( bytesConstRef _in ) { throw std::runtime_error( "Incorrect config field" ); } } else { - nlohmann::json joConfig = g_configAccesssor->getConfigJSON(); - nlohmann::json joValue = - skutils::json_config_file_accessor::stat_extract_at_path( joConfig, rawName ); - strValue = skutils::tools::trim_copy( - joValue.is_string() ? joValue.get< std::string >() : joValue.dump() ); + throw std::runtime_error( "Incorrect config field" ); } - // dev::u256 uValue( strValue.c_str() ); - dev::u256 uValue = stat_parse_u256_hex_or_dec( strValue ); - // std::cout << "------------ Loaded config var \"" - // << rawName << "\" value is " << uValue - // << "\n"; + dev::u256 uValue = statParseU256HexorDec( strValue ); bytes response = toBigEndian( uValue ); return { true, response }; } catch ( std::exception& ex ) { @@ -842,7 +820,7 @@ ETH_REGISTER_PRECOMPILED( getConfigVariableAddress )( bytesConstRef _in ) { std::string field; unsigned id; - std::tie( field, id ) = parseHistoricFieldReuqest( rawName ); + std::tie( field, id ) = parseHistoricFieldRequest( rawName ); if ( field == "owner" ) { strValue = g_skaleHost->getHistoricNodeOwner( id ); } else {