Skip to content

Commit

Permalink
#1702 simplified parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kladkogex committed Nov 7, 2023
1 parent 47bc822 commit 7c8653a
Showing 1 changed file with 8 additions and 30 deletions.
38 changes: 8 additions & 30 deletions libethereum/Precompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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" ) {
Expand All @@ -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 ) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7c8653a

Please sign in to comment.