Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/skale 4921 encrypted analyzer #982

Draft
wants to merge 13 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Next Next commit
SKALE-4921 Move getEncryptedData to ConsensusExtFace
  • Loading branch information
dimalit committed Feb 3, 2022
commit 29087710d0c30d13b0dc6c11e519bca45dbbfc95
3 changes: 1 addition & 2 deletions libethereum/ConsensusStub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ ConsensusStub::~ConsensusStub() {
// TODO Auto-generated destructor stub
}

void ConsensusStub::parseFullConfigAndCreateNode( const std::string&, const std::string&,
std::shared_ptr< EncryptedTransactionAnalyzerInterface > ) {
void ConsensusStub::parseFullConfigAndCreateNode( const std::string&, const std::string& ) {
// TODO think this architecture thoroughly
}

Expand Down
3 changes: 1 addition & 2 deletions libethereum/ConsensusStub.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ class ConsensusStub : private dev::Worker, public ConsensusInterface {
public:
ConsensusStub( ConsensusExtFace& _extFace, uint64_t _lastCommittedBlockID, u256 _stateRoot );
~ConsensusStub() override;
void parseFullConfigAndCreateNode( const std::string& _jsonConfig, const string& _gethURL,
std::shared_ptr< EncryptedTransactionAnalyzerInterface > _analyzer ) override;
void parseFullConfigAndCreateNode( const std::string& _jsonConfig, const string& _gethURL ) override;
void startAll() override;
void bootStrapAll() override;
void exitGracefully() override;
Expand Down
22 changes: 19 additions & 3 deletions libethereum/SkaleHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class ConsensusExtImpl : public ConsensusExtFace {
const shared_ptr< map< uint64_t, shared_ptr< vector< uint8_t > > > > decryptedArgs )
override;
virtual void terminateApplication() override;
std::shared_ptr< std::vector< uint8_t > > getEncryptedData( const std::vector< uint8_t >& transaction );
virtual ~ConsensusExtImpl() override = default;

private:
Expand Down Expand Up @@ -213,9 +214,24 @@ void ConsensusExtImpl::terminateApplication() {
dev::ExitHandler::exitHandler( SIGINT, dev::ExitHandler::ec_consensus_terminate_request );
}

std::shared_ptr< std::vector< uint8_t > > ConsensusExtImpl::getEncryptedData(
const std::vector< uint8_t >& transaction ) {

static const std::vector< uint8_t > ms = dev::fromHex( TE_MAGIC_START );
static const std::vector< uint8_t > me = dev::fromHex( TE_MAGIC_END );

dev::eth::Transaction t( transaction, dev::eth::CheckTransaction::None );
auto data = t.data();
auto first = std::search( data.begin(), data.end(), ms.begin(), ms.end() );
auto last = std::search( data.begin(), data.end(), me.begin(), me.end() );
if ( first != data.end() && last != data.end() && std::distance( first, last ) > 0 ) {
return std::make_shared< std::vector< uint8_t > >( first + ms.size(), last );
}
return nullptr;
}

SkaleHost::SkaleHost( dev::eth::Client& _client, const ConsensusFactory* _consFactory,
std::shared_ptr< InstanceMonitor > _instanceMonitor, const std::string& _gethURL,
std::shared_ptr< EncryptedTransactionAnalyzer > _analyzer ) try
std::shared_ptr< InstanceMonitor > _instanceMonitor, const std::string& _gethURL ) try
: m_client( _client ),
m_tq( _client.m_tq ),
m_instanceMonitor( _instanceMonitor ),
Expand Down Expand Up @@ -256,7 +272,7 @@ SkaleHost::SkaleHost( dev::eth::Client& _client, const ConsensusFactory* _consFa
m_consensus = _consFactory->create( *m_extFace );

m_consensus->parseFullConfigAndCreateNode(
m_client.chainParams().getOriginalJson(), _gethURL, _analyzer );
m_client.chainParams().getOriginalJson(), _gethURL );
} catch ( const std::exception& ) {
std::throw_with_nested( CreationException() );
}
Expand Down
4 changes: 1 addition & 3 deletions libethereum/SkaleHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include <libethereum/InstanceMonitor.h>
#include <libethereum/Transaction.h>
#include <libskale/SkaleClient.h>
#include <libskale/encryption.h>

#include <jsonrpccpp/client/client.h>

Expand Down Expand Up @@ -108,8 +107,7 @@ class SkaleHost {

SkaleHost( dev::eth::Client& _client, const ConsensusFactory* _consFactory = nullptr,
std::shared_ptr< InstanceMonitor > _instanceMonitor = nullptr,
const std::string& _gethURL = "",
std::shared_ptr< EncryptedTransactionAnalyzer > _analyzer = nullptr );
const std::string& _gethURL = "" );
virtual ~SkaleHost();

void startWorking();
Expand Down
2 changes: 0 additions & 2 deletions libskale/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ set(sources
SnapshotManager.cpp
SnapshotHashAgent.cpp
UnsafeRegion.cpp
encryption.cpp
)

set(headers
Expand All @@ -25,7 +24,6 @@ set(headers
SnapshotManager.h
SnapshotHashAgent.h
UnsafeRegion.h
encryption.h
)

add_library(skale ${sources} ${headers})
Expand Down
40 changes: 0 additions & 40 deletions libskale/encryption.cpp

This file was deleted.

40 changes: 0 additions & 40 deletions libskale/encryption.h

This file was deleted.

4 changes: 1 addition & 3 deletions skaled/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@

#include <libskale/ConsensusGasPricer.h>
#include <libskale/UnsafeRegion.h>
#include <libskale/encryption.h>

#include <libdevcrypto/LibSnark.h>

Expand Down Expand Up @@ -1727,8 +1726,7 @@ int main( int argc, char** argv ) try {
setenv( "DATA_DIR", getDataDir().c_str(), 0 );

std::shared_ptr< SkaleHost > skaleHost = std::make_shared< SkaleHost >( *g_client,
&cons_fact, instanceMonitor, skutils::json_config_file_accessor::g_strImaMainNetURL,
std::shared_ptr< EncryptedTransactionAnalyzer >() );
&cons_fact, instanceMonitor, skutils::json_config_file_accessor::g_strImaMainNetURL );

// XXX nested lambdas and strlen hacks..
auto skaleHost_debug_handler = skaleHost->getDebugHandler();
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/libethereum/SkaleHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ConsensusTestStub : public ConsensusInterface {
block_gas_prices.push_back( 1000 );
}
~ConsensusTestStub() override {}
void parseFullConfigAndCreateNode( const std::string& _jsonConfig, const string& _gethURL, std::shared_ptr<EncryptedTransactionAnalyzerInterface> _analyzer ) override {}
void parseFullConfigAndCreateNode( const std::string& _jsonConfig, const string& _gethURL ) override {}
void startAll() override {}
void bootStrapAll() override {}
void exitGracefully() override { need_exit = true; }
Expand Down
113 changes: 55 additions & 58 deletions test/unittests/libweb3jsonrpc/jsonrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include <libethereum/TransactionQueue.h>
#include <libp2p/Network.h>
#include <libskale/httpserveroverride.h>
#include <libskale/encryption.h>
#include <libweb3jsonrpc/AccountHolder.h>
#include <libweb3jsonrpc/AdminEth.h>
#include <libweb3jsonrpc/JsonHelper.h>
Expand Down Expand Up @@ -276,7 +275,6 @@ struct JsonRpcFixture : public TestOutputHelperFixture {
// true ) );

auto monitor = make_shared< InstanceMonitor >("test");
analyzer = new EncryptedTransactionAnalyzer();

setenv("DATA_DIR", tempDir.path().c_str(), 1);
client.reset( new eth::ClientTest( chainParams, ( int ) chainParams.networkID,
Expand Down Expand Up @@ -610,7 +608,6 @@ struct JsonRpcFixture : public TestOutputHelperFixture {

TransientDirectory tempDir; // ! should exist before client!
unique_ptr< Client > client;
EncryptedTransactionAnalyzer* analyzer;

dev::KeyPair coinbase{KeyPair::create()};
dev::KeyPair account2{KeyPair::create()};
Expand Down Expand Up @@ -2618,61 +2615,61 @@ BOOST_AUTO_TEST_CASE( mtm_import_future_txs ) {
BOOST_REQUIRE( tq->status().current == 5);
}

BOOST_AUTO_TEST_CASE( test_analyzer ) {
JsonRpcFixture fixture( c_genesisConfigString );

Json::Value txJson;
txJson["from"] = fixture.coinbase.address().hex();
txJson["gas"] = "100000";
txJson["nonce"] = "0";
txJson["data"] = "0xf84a1cf7214ae051cae8aaaa98a773d884b2f1c4ac27";
TransactionSkeleton ts1 = toTransactionSkeleton( txJson );
ts1 = fixture.client->populateTransactionWithDefaults( ts1 );
pair< bool, Secret > ar1 = fixture.accountHolder->authenticate( ts1 );
Transaction tx1( ts1, ar1.second );

auto res = fixture.analyzer->getEncryptedData(tx1.rlp());
auto strResult = toHex( *res );
BOOST_REQUIRE( strResult == "aaaa" );

txJson["data"] = "0xaaaaf84a1cf7214ae051cae8aaaa98a773d884b2f1c4ac27bbbb";
ts1 = toTransactionSkeleton( txJson );
ts1 = fixture.client->populateTransactionWithDefaults( ts1 );
ar1 = fixture.accountHolder->authenticate( ts1 );

Transaction tx2( ts1, ar1.second );
res = fixture.analyzer->getEncryptedData(tx2.rlp());
strResult = toHex( *res );
BOOST_REQUIRE( strResult == "aaaa" );

txJson["data"] = "0xaaaaf84a1cf7214ae051cae898a773d884b2f1c4ac27bbbb";
ts1 = toTransactionSkeleton( txJson );
ts1 = fixture.client->populateTransactionWithDefaults( ts1 );
ar1 = fixture.accountHolder->authenticate( ts1 );

Transaction tx3( ts1, ar1.second );
res = fixture.analyzer->getEncryptedData(tx3.rlp());
strResult = toHex( *res );
BOOST_REQUIRE( strResult == "" );

txJson["data"] = "0xaaaa98a773d884b2f1c4ac27aaaaf84a1cf7214ae051cae8bbbb";
ts1 = toTransactionSkeleton( txJson );
ts1 = fixture.client->populateTransactionWithDefaults( ts1 );
ar1 = fixture.accountHolder->authenticate( ts1 );

Transaction tx4( ts1, ar1.second );
res = fixture.analyzer->getEncryptedData(tx4.rlp());
BOOST_REQUIRE( res == nullptr );

txJson["data"] = "0xaaaa98a773d884b2fdddddddddddddf84a1cf74ae051cae8bbbb";
ts1 = toTransactionSkeleton( txJson );
ts1 = fixture.client->populateTransactionWithDefaults( ts1 );
ar1 = fixture.accountHolder->authenticate( ts1 );

Transaction tx5( ts1, ar1.second );
res = fixture.analyzer->getEncryptedData(tx5.rlp());
BOOST_REQUIRE( res == nullptr );
}
//BOOST_AUTO_TEST_CASE( test_analyzer ) {
// JsonRpcFixture fixture( c_genesisConfigString );

// Json::Value txJson;
// txJson["from"] = fixture.coinbase.address().hex();
// txJson["gas"] = "100000";
// txJson["nonce"] = "0";
// txJson["data"] = "0xf84a1cf7214ae051cae8aaaa98a773d884b2f1c4ac27";
// TransactionSkeleton ts1 = toTransactionSkeleton( txJson );
// ts1 = fixture.client->populateTransactionWithDefaults( ts1 );
// pair< bool, Secret > ar1 = fixture.accountHolder->authenticate( ts1 );
// Transaction tx1( ts1, ar1.second );

// auto res = fixture.analyzer->getEncryptedData(tx1.rlp());
// auto strResult = toHex( *res );
// BOOST_REQUIRE( strResult == "aaaa" );

// txJson["data"] = "0xaaaaf84a1cf7214ae051cae8aaaa98a773d884b2f1c4ac27bbbb";
// ts1 = toTransactionSkeleton( txJson );
// ts1 = fixture.client->populateTransactionWithDefaults( ts1 );
// ar1 = fixture.accountHolder->authenticate( ts1 );

// Transaction tx2( ts1, ar1.second );
// res = fixture.analyzer->getEncryptedData(tx2.rlp());
// strResult = toHex( *res );
// BOOST_REQUIRE( strResult == "aaaa" );

// txJson["data"] = "0xaaaaf84a1cf7214ae051cae898a773d884b2f1c4ac27bbbb";
// ts1 = toTransactionSkeleton( txJson );
// ts1 = fixture.client->populateTransactionWithDefaults( ts1 );
// ar1 = fixture.accountHolder->authenticate( ts1 );

// Transaction tx3( ts1, ar1.second );
// res = fixture.analyzer->getEncryptedData(tx3.rlp());
// strResult = toHex( *res );
// BOOST_REQUIRE( strResult == "" );

// txJson["data"] = "0xaaaa98a773d884b2f1c4ac27aaaaf84a1cf7214ae051cae8bbbb";
// ts1 = toTransactionSkeleton( txJson );
// ts1 = fixture.client->populateTransactionWithDefaults( ts1 );
// ar1 = fixture.accountHolder->authenticate( ts1 );

// Transaction tx4( ts1, ar1.second );
// res = fixture.analyzer->getEncryptedData(tx4.rlp());
// BOOST_REQUIRE( res == nullptr );

// txJson["data"] = "0xaaaa98a773d884b2fdddddddddddddf84a1cf74ae051cae8bbbb";
// ts1 = toTransactionSkeleton( txJson );
// ts1 = fixture.client->populateTransactionWithDefaults( ts1 );
// ar1 = fixture.accountHolder->authenticate( ts1 );

// Transaction tx5( ts1, ar1.second );
// res = fixture.analyzer->getEncryptedData(tx5.rlp());
// BOOST_REQUIRE( res == nullptr );
//}

// TODO: Enable for multitransaction mode checking

Expand Down
12 changes: 10 additions & 2 deletions test/unittests/mapreduce_consensus/ConsensusEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class SingleNodeConsensusFixture : public ConsensusExtFace {

m_consensus.reset( new ConsensusEngine(
*this, 0, BlockHeader( chainParams.genesisBlock() ).timestamp(), 0 ) );
m_consensus->parseFullConfigAndCreateNode( chainParams.getOriginalJson(), "", nullptr );
m_consensus->parseFullConfigAndCreateNode( chainParams.getOriginalJson(), "" );

m_consensusThread = std::thread( [this]() {
sleep(1);
Expand Down Expand Up @@ -142,6 +142,9 @@ class SingleNodeConsensusFixture : public ConsensusExtFace {
std::make_tuple( tmp_vec, _timeStamp, _timeStampMs, _blockID, _gasPrice ) );
}

virtual std::shared_ptr<std::vector<uint8_t>> getEncryptedData(
const std::vector<uint8_t>& transaction) { return nullptr; }

virtual ~SingleNodeConsensusFixture() override {
transaction_promise.set_value( transactions_vector() );
m_consensus->exitGracefully();
Expand Down Expand Up @@ -214,7 +217,7 @@ class ConsensusExtFaceFixture : public ConsensusExtFace {

m_consensus.reset( new ConsensusEngine(
*this, 0, BlockHeader( chainParams.genesisBlock() ).timestamp(), 0 ) );
m_consensus->parseFullConfigAndCreateNode( chainParams.getOriginalJson(), "", nullptr );
m_consensus->parseFullConfigAndCreateNode( chainParams.getOriginalJson(), "" );

m_consensusThread = std::thread( [this]() {
m_consensus->startAll();
Expand Down Expand Up @@ -288,6 +291,11 @@ class ConsensusExtFaceFixture : public ConsensusExtFace {
m_blockCond.notify_one();
}

virtual std::shared_ptr<std::vector<uint8_t>> getEncryptedData(
const std::vector<uint8_t>& transaction) override {
return nullptr;
}

virtual ~ConsensusExtFaceFixture() override {
// TODO Kill everyone
m_consensus->exitGracefully();
Expand Down