Skip to content

Commit

Permalink
#1702 use common approach to access config variables
Browse files Browse the repository at this point in the history
  • Loading branch information
olehnikolaiev committed Nov 8, 2023
1 parent 80618dd commit 582b526
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
62 changes: 30 additions & 32 deletions libethereum/Precompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <libskale/PrecompiledConfigPatch.h>
#include <libskale/State.h>
#include <boost/algorithm/hex.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>

#include <mutex>
Expand Down Expand Up @@ -746,32 +747,18 @@ static bool isCallToHistoricData( const std::string& callData ) {
return boost::algorithm::starts_with( callData, "skaleConfig.sChain.nodes." );

Check warning on line 747 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L747

Added line #L747 was not covered by tests
}

static std::pair< std::string, unsigned > parseHistoricFieldReuqest( const std::string& callData ) {
auto idPosBegin = callData.find( '[' );
auto idPosEnd = callData.find( ']' );
if ( idPosBegin == std::string::npos || idPosEnd == std::string::npos ||
idPosBegin > idPosEnd ) {
// means the input is incorrect
return { "unknown field", 0 };
}
if ( callData.substr( 0, idPosBegin ) != "skaleConfig.sChain.nodes." ) {
// invalid input
return { "unknown field", 0 };
}
for ( size_t pos = idPosBegin + 1; pos != idPosEnd; ++pos ) {
if ( !std::isdigit( callData[pos] ) ) {
// invalid input
return { "unknown field", 0 };
}
}
size_t numberLength = idPosEnd - idPosBegin - 1;
unsigned id = std::stoul( callData.substr( idPosBegin + 1, numberLength ) );
static std::pair< std::string, unsigned > parseHistoricFieldReuqest( std::string callData ) {
std::vector< std::string > splitted;
boost::split( splitted, callData, boost::is_any_of( "." ) );

Check warning on line 752 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L750-L752

Added lines #L750 - L752 were not covered by tests
// first 3 elements are skaleConfig, sChain, nodes - it was checked before
unsigned id = std::stoul( splitted[3] );
std::string fieldName;
if ( boost::algorithm::ends_with( callData.c_str(), "id" ) ) {
boost::trim_if( splitted[4], []( char c ) { return c == '\0'; } );
if ( splitted[4] == "id" ) {
fieldName = "id";
} else if ( boost::algorithm::ends_with( callData.c_str(), "schainIndex" ) ) {
} else if ( splitted[4] == "schainIndex" ) {
fieldName = "schainIndex";
} else if ( boost::algorithm::ends_with( callData.c_str(), "owner" ) ) {
} else if ( splitted[4] == "owner" ) {
fieldName = "owner";

Check warning on line 762 in libethereum/Precompiled.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/Precompiled.cpp#L754-L762

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

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
Expand All @@ -785,14 +772,19 @@ static std::pair< std::string, unsigned > parseHistoricFieldReuqest( const std::
* 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
* to access those variables one should use the following scheme:
* prefix=skaleConfig.sChain.nodes - to access corresponding structure inside skaled
* index - node index user wants to get access to
* field - the field user wants to request
*
* example:
* to request the value for 1-st node (1 based) for the node id field the input should be
* input=skaleConfig.sChain.nodes.0.id (inside skaled node indexes are 0 based)
* so one should pass the following as calldata:
* toBytes( ( ( input.length + 1 ) / 32 ) * 32) + toBytes(input)
*/
ETH_REGISTER_PRECOMPILED( getConfigVariableUint256 )( bytesConstRef _in ) {
try {
Expand Down Expand Up @@ -854,13 +846,19 @@ ETH_REGISTER_PRECOMPILED( getConfigVariableUint256 )( bytesConstRef _in ) {
* addresses and works as key / values map input: bytes - length + path to config variable output:
* bytes - config variable value
*
* variables available through this precompiled contract:
* 1. owner - address for INDEX node in schain group for current block number
* to access those variables one should use the following scheme:
* prefix=skaleConfig.sChain.nodes - to access corresponding structure inside skaled
* index - node index user wants to get access to
* field - the field user wants to request
*
* example:
* to request value for input=skaleConfig.sChain.nodes.[1].owner
* one should pass the following
* to request the value for 2-nd node (1 based) for the owner field the input should be
* input=skaleConfig.sChain.nodes.1.owner (inside skaled node indexes are 0 based)
* so one should pass the following as calldata
* 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 {
Expand Down
16 changes: 8 additions & 8 deletions test/unittests/libethereum/PrecompiledTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1716,24 +1716,24 @@ BOOST_AUTO_TEST_CASE( getConfigVariableUint256 ) {

PrecompiledExecutor exec = PrecompiledRegistrar::executor( "getConfigVariableUint256" );

Check warning on line 1717 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1717

Added line #L1717 was not covered by tests

bytes in = fromHex( numberToHex( 32 ) + stringToHex( "skaleConfig.sChain.nodes.[0].id" ) );
bytes in = fromHex( numberToHex( 32 ) + stringToHex( "skaleConfig.sChain.nodes.0.id" ) );
auto res = exec( bytesConstRef( in.data(), in.size() ) );

Check warning on line 1720 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1719-L1720

Added lines #L1719 - L1720 were not covered by tests

BOOST_REQUIRE( res.first );
BOOST_REQUIRE( dev::fromBigEndian<dev::u256>( res.second ) == 30 );

Check warning on line 1723 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1722-L1723

Added lines #L1722 - L1723 were not covered by tests

in = fromHex( numberToHex( 64 ) + stringToHex( "skaleConfig.sChain.nodes.[0].schainIndex" ) );
in = fromHex( numberToHex( 64 ) + stringToHex( "skaleConfig.sChain.nodes.0.schainIndex" ) );
res = exec( bytesConstRef( in.data(), in.size() ) );

Check warning on line 1726 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1725-L1726

Added lines #L1725 - L1726 were not covered by tests

BOOST_REQUIRE( res.first );
BOOST_REQUIRE( dev::fromBigEndian<dev::u256>( res.second ) == 13 );

Check warning on line 1729 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1728-L1729

Added lines #L1728 - L1729 were not covered by tests

in = fromHex( numberToHex( 64 ) + stringToHex( "skaleConfig.sChain.nodes.[0].owner" ) );
in = fromHex( numberToHex( 32 ) + stringToHex( "skaleConfig.sChain.nodes.0.owner" ) );
res = exec( bytesConstRef( in.data(), in.size() ) );

Check warning on line 1732 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1731-L1732

Added lines #L1731 - L1732 were not covered by tests

BOOST_REQUIRE( !res.first );

Check warning on line 1734 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1734

Added line #L1734 was not covered by tests

in = fromHex( numberToHex( 64 ) + stringToHex( "skaleConfig.sChain.nodes.[0].unknownField" ) );
in = fromHex( numberToHex( 64 ) + stringToHex( "skaleConfig.sChain.nodes.0.unknownField" ) );
res = exec( bytesConstRef( in.data(), in.size() ) );

Check warning on line 1737 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1736-L1737

Added lines #L1736 - L1737 were not covered by tests

BOOST_REQUIRE( !res.first );
Expand Down Expand Up @@ -1767,23 +1767,23 @@ BOOST_AUTO_TEST_CASE( getConfigVariableAddress ) {

PrecompiledExecutor exec = PrecompiledRegistrar::executor( "getConfigVariableAddress" );

Check warning on line 1768 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1768

Added line #L1768 was not covered by tests

bytes in = fromHex( numberToHex( 64 ) + stringToHex( "skaleConfig.sChain.nodes.[0].owner" ) );
bytes in = fromHex( numberToHex( 32 ) + stringToHex( "skaleConfig.sChain.nodes.0.owner" ) );
auto res = exec( bytesConstRef( in.data(), in.size() ) );

Check warning on line 1771 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1770-L1771

Added lines #L1770 - L1771 were not covered by tests

BOOST_REQUIRE( res.first );
BOOST_REQUIRE( res.second == fromHex("0x23bbe8db4e347b4e8c937c1c8350e4b5ed33adb3db69cbdb7a38e1f40a1b82fe") );

Check warning on line 1774 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1773-L1774

Added lines #L1773 - L1774 were not covered by tests

in = fromHex( numberToHex( 32 ) + stringToHex( "skaleConfig.sChain.nodes.[0].id" ) );
in = fromHex( numberToHex( 32 ) + stringToHex( "skaleConfig.sChain.nodes.0.id" ) );
res = exec( bytesConstRef( in.data(), in.size() ) );

Check warning on line 1777 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1776-L1777

Added lines #L1776 - L1777 were not covered by tests

BOOST_REQUIRE( !res.first );

Check warning on line 1779 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1779

Added line #L1779 was not covered by tests

in = fromHex( numberToHex( 64 ) + stringToHex( "skaleConfig.sChain.nodes.[0].schainIndex" ) );
in = fromHex( numberToHex( 64 ) + stringToHex( "skaleConfig.sChain.nodes.0.schainIndex" ) );
res = exec( bytesConstRef( in.data(), in.size() ) );

Check warning on line 1782 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1781-L1782

Added lines #L1781 - L1782 were not covered by tests

BOOST_REQUIRE( !res.first );

Check warning on line 1784 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1784

Added line #L1784 was not covered by tests

in = fromHex( numberToHex( 64 ) + stringToHex( "skaleConfig.sChain.nodes.[0].unknownField" ) );
in = fromHex( numberToHex( 64 ) + stringToHex( "skaleConfig.sChain.nodes.0.unknownField" ) );
res = exec( bytesConstRef( in.data(), in.size() ) );

Check warning on line 1787 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1786-L1787

Added lines #L1786 - L1787 were not covered by tests

BOOST_REQUIRE( !res.first );

Check warning on line 1789 in test/unittests/libethereum/PrecompiledTest.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/libethereum/PrecompiledTest.cpp#L1789

Added line #L1789 was not covered by tests
Expand Down

0 comments on commit 582b526

Please sign in to comment.