Skip to content

Commit

Permalink
#1702 improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
olehnikolaiev committed Nov 7, 2023
1 parent cfbd435 commit 80618dd
Showing 1 changed file with 55 additions and 34 deletions.
89 changes: 55 additions & 34 deletions libethereum/Precompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <cryptopp/files.h>
#include <cryptopp/hex.h>
#include <cryptopp/sha.h>
#include <libdevcore/CommonJS.h>
#include <libdevcore/FileSystem.h>
#include <libdevcore/Log.h>
#include <libdevcore/SHA3.h>
Expand Down Expand Up @@ -675,27 +676,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",
//"genesis.*"
//"params.*",

// skaled-1702
// remove these config field from public access for security reasons
// "skaleConfig.nodeInfo.wallets.ima.commonBLSPublicKey*",
// "skaleConfig.nodeInfo.wallets.ima.BLSPublicKey*",

// "skaleConfig.nodeInfo.nodeName", "skaleConfig.nodeInfo.nodeID",
// "skaleConfig.nodeInfo.basePort*", "skaleConfig.nodeInfo.*RpcPort*",
// "skaleConfig.nodeInfo.acceptors", "skaleConfig.nodeInfo.max-connections",
// "skaleConfig.nodeInfo.max-http-queues", "skaleConfig.nodeInfo.ws-mode",

// "skaleConfig.contractSettings.*",

// "skaleConfig.sChain.emptyBlockIntervalMs",

// "skaleConfig.sChain.schainName", "skaleConfig.sChain.schainID",

"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 All @@ -704,7 +685,7 @@ static bool stat_is_accessible_json_path( const std::string& strPath ) {
itEnd = g_listReadableConfigParts.cend();
for ( ; itWalk != itEnd; ++itWalk ) {
const std::string strWildCard = ( *itWalk );
if ( skutils::tools::wildcmp( strWildCard.c_str(), strPath.c_str() ) )
if ( boost::algorithm::starts_with( strPath, strWildCard ) )

Check warning on line 688 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L688

Added line #L688 was not covered by tests
return true;
}
return false;
Expand Down Expand Up @@ -766,21 +747,53 @@ static bool isCallToHistoricData( const std::string& callData ) {
}

static std::pair< std::string, unsigned > parseHistoricFieldReuqest( const std::string& callData ) {
size_t numberLength = callData.find( ']' ) - callData.find( '[' ) - 1;
unsigned id = std::stoul( callData.substr( callData.find( '[' ) + 1, numberLength ) );
auto idPosBegin = callData.find( '[' );
auto idPosEnd = callData.find( ']' );
if ( idPosBegin == std::string::npos || idPosEnd == std::string::npos ||

Check warning on line 752 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L749-L752

Added lines #L749 - L752 were not covered by tests
idPosBegin > idPosEnd ) {
// means the input is incorrect
return { "unknown field", 0 };

Check warning on line 755 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L755

Added line #L755 was not covered by tests
}
if ( callData.substr( 0, idPosBegin ) != "skaleConfig.sChain.nodes." ) {

Check warning on line 757 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L757

Added line #L757 was not covered by tests
// invalid input
return { "unknown field", 0 };

Check warning on line 759 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L759

Added line #L759 was not covered by tests
}
for ( size_t pos = idPosBegin + 1; pos != idPosEnd; ++pos ) {
if ( !std::isdigit( callData[pos] ) ) {

Check warning on line 762 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L761-L762

Added lines #L761 - L762 were not covered by tests
// invalid input
return { "unknown field", 0 };

Check warning on line 764 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L764

Added line #L764 was not covered by tests
}
}
size_t numberLength = idPosEnd - idPosBegin - 1;
unsigned id = std::stoul( callData.substr( idPosBegin + 1, numberLength ) );
std::string fieldName;
if ( callData.find( "id" ) != std::string::npos ) {
if ( boost::algorithm::ends_with( callData.c_str(), "id" ) ) {
fieldName = "id";
} else if ( callData.find( "schainIndex" ) != std::string::npos ) {
} else if ( boost::algorithm::ends_with( callData.c_str(), "schainIndex" ) ) {
fieldName = "schainIndex";
} else if ( callData.find( "owner" ) != std::string::npos ) {
} else if ( boost::algorithm::ends_with( callData.c_str(), "owner" ) ) {
fieldName = "owner";

Check warning on line 775 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L767-L775

Added lines #L767 - L775 were not covered by tests
} else {
fieldName = "unknown field";

Check warning on line 777 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L777

Added line #L777 was not covered by tests
}
return { fieldName, id };

Check warning on line 779 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L779

Added line #L779 was not covered by tests
}

/*
* this precompiled contract is designed to get access to specific integer config values
* and works as key / values map
* input: bytes - length + path to config variable
* output: bytes - config variable value
*
* example:
* to request value for input=skaleConfig.sChain.nodes.[0].id
* one should pass the following
* toBytes( ( ( input.length + 1 ) / 32 ) * 32) + toBytes(input)
*
* variables available through this precompiled contract:
* 1. id - node id for INDEX node in schain group for current block number
* 2. schainIndex - schain index for INDEX node in schain group for current block number
*/
ETH_REGISTER_PRECOMPILED( getConfigVariableUint256 )( bytesConstRef _in ) {
try {
size_t lengthName;
Expand Down Expand Up @@ -818,11 +831,7 @@ ETH_REGISTER_PRECOMPILED( getConfigVariableUint256 )( bytesConstRef _in ) {
joValue.is_string() ? joValue.get< std::string >() : joValue.dump() );

Check warning on line 831 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L829-L831

Added lines #L829 - L831 were not covered by tests
}

// 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 = jsToInt( strValue );

Check warning on line 834 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L834

Added line #L834 was not covered by tests
bytes response = toBigEndian( uValue );
return { true, response };
} catch ( std::exception& ex ) {
Expand All @@ -840,6 +849,19 @@ ETH_REGISTER_PRECOMPILED( getConfigVariableUint256 )( bytesConstRef _in ) {
return { false, response }; // 1st false - means bad error occur
}

/*
* this precompiled contract is designed to get access to specific config values that are ETH
* addresses and works as key / values map input: bytes - length + path to config variable output:
* bytes - config variable value
*
* example:
* to request value for input=skaleConfig.sChain.nodes.[1].owner
* one should pass the following
* toBytes( ( ( input.length + 1 ) / 32 ) * 32) + toBytes(input)
*
* variables available through this precompiled contract:
* 1. owner - address for INDEX node in schain group for current block number
*/
ETH_REGISTER_PRECOMPILED( getConfigVariableAddress )( bytesConstRef _in ) {
try {
size_t lengthName;
Expand Down Expand Up @@ -875,8 +897,7 @@ ETH_REGISTER_PRECOMPILED( getConfigVariableAddress )( bytesConstRef _in ) {
joValue.is_string() ? joValue.get< std::string >() : joValue.dump() );

Check warning on line 897 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L895-L897

Added lines #L895 - L897 were not covered by tests
}

dev::u256 uValue( strValue.c_str() );

dev::u256 uValue( strValue );

Check warning on line 900 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L900

Added line #L900 was not covered by tests
bytes response = toBigEndian( uValue );
return { true, response };
} catch ( std::exception& ex ) {
Expand Down

0 comments on commit 80618dd

Please sign in to comment.