Skip to content

Commit

Permalink
#1702 pass historic publicKey instead of address
Browse files Browse the repository at this point in the history
  • Loading branch information
olehnikolaiev committed Nov 22, 2023
1 parent edd62d4 commit 4b51081
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 74 deletions.
1 change: 0 additions & 1 deletion libethcore/ChainOperationParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ struct GroupNode {
u256 id;
u256 schainIndex;
std::string publicKey;
std::string address;
};

/// skale
Expand Down
6 changes: 1 addition & 5 deletions libethereum/ChainParams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,10 @@ ChainParams ChainParams::loadConfig(
u256 sChainIndex = groupNodeConfObj.at( 0 ).get_uint64();
u256 id = groupNodeConfObj.at( 1 ).get_uint64();
std::string publicKey = groupNodeConfObj.at( 2 ).get_str();
std::string address = groupNodeConfObj.at( 3 ).get_str();
if ( publicKey.empty() ) {
BOOST_THROW_EXCEPTION( std::runtime_error( "Empty public key in config" ) );
}
if ( address.empty() ) {
BOOST_THROW_EXCEPTION( std::runtime_error( "Empty address in config" ) );
}
groupNodes.push_back( { id, sChainIndex, publicKey, address } );
groupNodes.push_back( { id, sChainIndex, publicKey } );
}
std::sort( groupNodes.begin(), groupNodes.end(),
[]( const GroupNode& lhs, const GroupNode& rhs ) {
Expand Down
4 changes: 2 additions & 2 deletions libethereum/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ class Client : public ClientBase, protected Worker {
}

// get node owner for historic node in chain
std::string getHistoricNodeOwner( unsigned _idx ) const {
return chainParams().sChain.nodeGroups[historicGroupIndex].nodes[_idx].address;
std::string getHistoricNodePublicKey( unsigned _idx ) const {
return chainParams().sChain.nodeGroups[historicGroupIndex].nodes[_idx].publicKey;
}

void doStateDbCompaction() const { m_state.getOriginalDb()->doCompaction(); }
Expand Down
91 changes: 46 additions & 45 deletions libethereum/Precompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ static std::pair< std::string, unsigned > parseHistoricFieldRequest( std::string
// first 3 elements are skaleConfig, sChain, nodes - it was checked before
unsigned id = std::stoul( splitted.at( 3 ) );
std::string fieldName;
std::set< std::string > allowedValues{ "id", "schainIndex", "owner" };
std::set< std::string > allowedValues{ "id", "schainIndex", "publicKey" };
fieldName = splitted.at( 4 );
if ( allowedValues.count( fieldName ) ) {
return { fieldName, id };
Expand Down Expand Up @@ -848,25 +848,61 @@ ETH_REGISTER_PRECOMPILED( getConfigVariableUint256 )( bytesConstRef _in ) {
return { false, response }; // 1st false - means bad error occur
}

ETH_REGISTER_PRECOMPILED( getConfigVariableAddress )( bytesConstRef _in ) {
try {
size_t lengthName;
std::string rawName;
convertBytesToString( _in, 0, rawName, lengthName );
if ( !stat_is_accessible_json_path( rawName ) )
throw std::runtime_error(
"Security poicy violation, inaccessible configuration JSON path: " + rawName );

if ( !g_configAccesssor )
throw std::runtime_error( "Config accessor was not initialized" );

nlohmann::json joConfig = g_configAccesssor->getConfigJSON();
nlohmann::json joValue =
skutils::json_config_file_accessor::stat_extract_at_path( joConfig, rawName );
std::string strValue = skutils::tools::trim_copy(
joValue.is_string() ? joValue.get< std::string >() : joValue.dump() );

dev::u256 uValue( strValue );
bytes response = toBigEndian( uValue );
return { true, response };
} catch ( std::exception& ex ) {
std::string strError = ex.what();
if ( strError.empty() )
strError = "exception without description";
LOG( getLogger( VerbosityError ) )
<< "Exception in precompiled/getConfigVariableAddress(): " << strError << "\n";
} catch ( ... ) {
LOG( getLogger( VerbosityError ) )
<< "Unknown exception in precompiled/getConfigVariableAddress()\n";
}
u256 code = 0;
bytes response = toBigEndian( code );
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:
* this precompiled contract is designed to get access to specific config values that are
* strings 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
* 1. publicKey - ETH public key 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 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)
* to request the value for 2-nd node (1 based) for the publicKey field the input should be
* input=skaleConfig.sChain.nodes.1.publicKey (inside skaled node indexes are 0 based)
* so one should pass the following as calldata
* toBytes( input.length + toBytes(input) )
*/
ETH_REGISTER_PRECOMPILED( getConfigVariableAddress )( bytesConstRef _in ) {
ETH_REGISTER_PRECOMPILED( getConfigVariableString )( bytesConstRef _in ) {
try {
size_t lengthName;
std::string rawName;
Expand All @@ -877,7 +913,6 @@ ETH_REGISTER_PRECOMPILED( getConfigVariableAddress )( bytesConstRef _in ) {

if ( !g_configAccesssor )
throw std::runtime_error( "Config accessor was not initialized" );

std::string strValue;
// call to skaleConfig.sChain.nodes means call to the historic data
// need to proccess it in a different way
Expand All @@ -888,8 +923,8 @@ ETH_REGISTER_PRECOMPILED( getConfigVariableAddress )( bytesConstRef _in ) {
std::string field;
unsigned id;
std::tie( field, id ) = parseHistoricFieldRequest( rawName );
if ( field == "owner" ) {
strValue = g_skaleHost->getHistoricNodeOwner( id );
if ( field == "publicKey" ) {
strValue = g_skaleHost->getHistoricNodePublicKey( id );
} else {
throw std::runtime_error( "Incorrect config field" );
}
Expand All @@ -900,41 +935,7 @@ ETH_REGISTER_PRECOMPILED( getConfigVariableAddress )( bytesConstRef _in ) {
strValue = skutils::tools::trim_copy(
joValue.is_string() ? joValue.get< std::string >() : joValue.dump() );
}

dev::u256 uValue( strValue );
bytes response = toBigEndian( uValue );
return { true, response };
} catch ( std::exception& ex ) {
std::string strError = ex.what();
if ( strError.empty() )
strError = "exception without description";
LOG( getLogger( VerbosityError ) )
<< "Exception in precompiled/getConfigVariableAddress(): " << strError << "\n";
} catch ( ... ) {
LOG( getLogger( VerbosityError ) )
<< "Unknown exception in precompiled/getConfigVariableAddress()\n";
}
u256 code = 0;
bytes response = toBigEndian( code );
return { false, response }; // 1st false - means bad error occur
}

ETH_REGISTER_PRECOMPILED( getConfigVariableString )( bytesConstRef _in ) {
try {
size_t lengthName;
std::string rawName;
convertBytesToString( _in, 0, rawName, lengthName );
if ( !stat_is_accessible_json_path( rawName ) )
throw std::runtime_error(
"Security poicy violation, inaccessible configuration JSON path: " + rawName );

if ( !g_configAccesssor )
throw std::runtime_error( "Config accessor was not initialized" );
nlohmann::json joConfig = g_configAccesssor->getConfigJSON();
nlohmann::json joValue =
skutils::json_config_file_accessor::stat_extract_at_path( joConfig, rawName );
std::string strValue = joValue.is_string() ? joValue.get< std::string >() : joValue.dump();
bytes response = stat_string_to_bytes_with_length( strValue );
bytes response = dev::asBytes( strValue );
return { true, response };
} catch ( std::exception& ex ) {
std::string strError = ex.what();
Expand Down
4 changes: 2 additions & 2 deletions libethereum/SkaleHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,8 @@ std::string SkaleHost::getHistoricNodeIndex( unsigned _index ) const {
return m_client.getHistoricNodeIndex( _index );
}

std::string SkaleHost::getHistoricNodeOwner( unsigned _idx ) const {
return m_client.getHistoricNodeOwner( _idx );
std::string SkaleHost::getHistoricNodePublicKey( unsigned _idx ) const {
return m_client.getHistoricNodePublicKey( _idx );
}

uint64_t SkaleHost::submitOracleRequest(
Expand Down
4 changes: 2 additions & 2 deletions libethereum/SkaleHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ class SkaleHost {
// get schain index for historic node in chain
std::string getHistoricNodeIndex( unsigned _idx ) const;

// get node owner for historic node in chain
std::string getHistoricNodeOwner( unsigned _idx ) const;
// get public key for historic node in chain
std::string getHistoricNodePublicKey( unsigned _idx ) const;

uint64_t submitOracleRequest( const string& _spec, string& _receipt, string& _errorMessage );
uint64_t checkOracleResult( const string& _receipt, string& _result );
Expand Down
12 changes: 5 additions & 7 deletions test/unittests/libethereum/ClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ BOOST_AUTO_TEST_CASE( consumptionWithReverts ) {

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE( IMABLSPublicKey )
BOOST_AUTO_TEST_SUITE( getHistoricNodesData )

static std::string const c_genesisInfoSkaleIMABLSPublicKeyTest = std::string() +
R"E(
Expand Down Expand Up @@ -872,8 +872,7 @@ static std::string const c_genesisInfoSkaleIMABLSPublicKeyTest = std::string() +
"30": [
0,
30,
"0x6180cde2cbbcc6b6a17efec4503a7d4316f8612f411ee171587089f770335f484003ad236c534b9afa82befc1f69533723abdb6ec2601e582b72dcfd7919338b",
"0x23bbe8db4e347b4e8c937c1c8350e4b5ed33adb3db69cbdb7a38e1f40a1b82fe"
"0x6180cde2cbbcc6b6a17efec4503a7d4316f8612f411ee171587089f770335f484003ad236c534b9afa82befc1f69533723abdb6ec2601e582b72dcfd7919338b"
]
},
"finish_ts": null,
Expand All @@ -889,8 +888,7 @@ static std::string const c_genesisInfoSkaleIMABLSPublicKeyTest = std::string() +
"26": [
3,
26,
"0x3a581d62b12232dade30c3710215a271984841657449d1f474295a13737b778266f57e298f123ae80cbab7cc35ead1b62a387556f94b326d5c65d4a7aa2abcba",
"0x47bbe8db4e347b4e8c937c1c8350e4b7ed30adb3db69bbdb7a38c1f40a1b82fd"
"0x3a581d62b12232dade30c3710215a271984841657449d1f474295a13737b778266f57e298f123ae80cbab7cc35ead1b62a387556f94b326d5c65d4a7aa2abcba"
]
},
"finish_ts": 4294967290,
Expand Down Expand Up @@ -927,7 +925,7 @@ BOOST_AUTO_TEST_CASE( initAndUpdateHistoricConfigFields ) {
std::array< std::string, 4 > imaBLSPublicKeyOnStartUp = { "12457351342169393659284905310882617316356538373005664536506840512800919345414", "11573096151310346982175966190385407867176668720531590318594794283907348596326", "13929944172721019694880576097738949215943314024940461401664534665129747139387", "7375214420811287025501422512322868338311819657776589198925786170409964211914" };

BOOST_REQUIRE( testClient->getIMABLSPublicKey() == imaBLSPublicKeyOnStartUp );
BOOST_REQUIRE( testClient->getHistoricNodeOwner( 0 ) == "0x47bbe8db4e347b4e8c937c1c8350e4b7ed30adb3db69bbdb7a38c1f40a1b82fd" );
BOOST_REQUIRE( testClient->getHistoricNodePublicKey( 0 ) == "0x3a581d62b12232dade30c3710215a271984841657449d1f474295a13737b778266f57e298f123ae80cbab7cc35ead1b62a387556f94b326d5c65d4a7aa2abcba" );
BOOST_REQUIRE( testClient->getHistoricNodeId( 0 ) == "26" );
BOOST_REQUIRE( testClient->getHistoricNodeIndex( 0 ) == "3" );

Expand All @@ -938,7 +936,7 @@ BOOST_AUTO_TEST_CASE( initAndUpdateHistoricConfigFields ) {
std::array< std::string, 4 > imaBLSPublicKeyAfterBlock = { "10860211539819517237363395256510340030868592687836950245163587507107792195621", "2419969454136313127863904023626922181546178935031521540751337209075607503568", "3399776985251727272800732947224655319335094876742988846345707000254666193993", "16982202412630419037827505223148517434545454619191931299977913428346639096984" };

BOOST_REQUIRE( testClient->getIMABLSPublicKey() == imaBLSPublicKeyAfterBlock );
BOOST_REQUIRE( testClient->getHistoricNodeOwner( 0 ) == "0x23bbe8db4e347b4e8c937c1c8350e4b5ed33adb3db69cbdb7a38e1f40a1b82fe" );
BOOST_REQUIRE( testClient->getHistoricNodePublicKey( 0 ) == "0x6180cde2cbbcc6b6a17efec4503a7d4316f8612f411ee171587089f770335f484003ad236c534b9afa82befc1f69533723abdb6ec2601e582b72dcfd7919338b" );
BOOST_REQUIRE( testClient->getHistoricNodeId( 0 ) == "30" );
BOOST_REQUIRE( testClient->getHistoricNodeIndex( 0 ) == "0" );
}
Expand Down
6 changes: 2 additions & 4 deletions test/unittests/libethereum/PrecompiledConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
"30": [
0,
30,
"0x6180cde2cbbcc6b6a17efec4503a7d4316f8612f411ee171587089f770335f484003ad236c534b9afa82befc1f69533723abdb6ec2601e582b72dcfd7919338b",
"0x23bbe8db4e347b4e8c937c1c8350e4b5ed33adb3db69cbdb7a38e1f40a1b82fe"
"0x6180cde2cbbcc6b6a17efec4503a7d4316f8612f411ee171587089f770335f484003ad236c534b9afa82befc1f69533723abdb6ec2601e582b72dcfd7919338b"
]
},
"finish_ts": null,
Expand All @@ -69,8 +68,7 @@
"26": [
3,
26,
"0x3a581d62b12232dade30c3710215a271984841657449d1f474295a13737b778266f57e298f123ae80cbab7cc35ead1b62a387556f94b326d5c65d4a7aa2abcba",
"0x47bbe8db4e347b4e8c937c1c8350e4b7ed30adb3db69bbdb7a38c1f40a1b82fd"
"0x3a581d62b12232dade30c3710215a271984841657449d1f474295a13737b778266f57e298f123ae80cbab7cc35ead1b62a387556f94b326d5c65d4a7aa2abcba"
]
},
"finish_ts": 4294967290,
Expand Down
10 changes: 4 additions & 6 deletions test/unittests/libethereum/PrecompiledTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,6 @@ static std::string const genesisInfoSkaleConfigTest = std::string() +
13,
30,
"0x6180cde2cbbcc6b6a17efec4503a7d4316f8612f411ee171587089f770335f484003ad236c534b9afa82befc1f69533723abdb6ec2601e582b72dcfd7919338b",
"0x23bbe8db4e347b4e8c937c1c8350e4b5ed33adb3db69cbdb7a38e1f40a1b82fe"
]
},
"finish_ts": null,
Expand All @@ -1652,7 +1651,6 @@ static std::string const genesisInfoSkaleConfigTest = std::string() +
3,
26,
"0x3a581d62b12232dade30c3710215a271984841657449d1f474295a13737b778266f57e298f123ae80cbab7cc35ead1b62a387556f94b326d5c65d4a7aa2abcba",
"0x47bbe8db4e347b4e8c937c1c8350e4b7ed30adb3db69bbdb7a38c1f40a1b82fd"
]
},
"finish_ts": 4294967290,
Expand Down Expand Up @@ -1732,7 +1730,7 @@ BOOST_AUTO_TEST_CASE( getConfigVariable ) {
BOOST_REQUIRE( res.first );
BOOST_REQUIRE( dev::fromBigEndian<dev::u256>( res.second ) == 13 );

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

Expand All @@ -1745,14 +1743,14 @@ BOOST_AUTO_TEST_CASE( getConfigVariable ) {

BOOST_REQUIRE( !res.first );

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

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

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

input = stringToHex( "skaleConfig.sChain.nodes.0.id" );
input = input.substr(0, 58); // remove 0s in the end
Expand Down

0 comments on commit 4b51081

Please sign in to comment.