Skip to content

Commit

Permalink
SKALE-5059 update uninstall watch, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olehnikolaiev committed Apr 5, 2022
1 parent 1710a0a commit 40cba16
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
8 changes: 7 additions & 1 deletion libskale/httpserveroverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3690,7 +3690,8 @@ const SkaleServerOverride::protocol_rpc_map_t SkaleServerOverride::g_protocol_rp
{"eth_getCode", &SkaleServerOverride::eth_getCode},
{"eth_newFilter", &SkaleServerOverride::eth_newFilter},
{"eth_newBlockFilter", &SkaleServerOverride::eth_newBlockFilter},
{"eth_newPendingTransactionFilter", &SkaleServerOverride::eth_newPendingTransactionFilter}};
{"eth_newPendingTransactionFilter", &SkaleServerOverride::eth_newPendingTransactionFilter},
{"eth_uninstallFilter", &SkaleServerOverride::eth_uninstallFilter}};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -3866,6 +3867,11 @@ void SkaleServerOverride::eth_newPendingTransactionFilter( const std::string& st
opts_.fn_eth_newPendingTransactionFilter_( strOrigin, joRequest, joResponse );
}

void SkaleServerOverride::eth_uninstallFilter( const std::string& strOrigin,
const rapidjson::Document& joRequest, rapidjson::Document& joResponse ) {
opts_.fn_eth_uninstallFilter_( strOrigin, joRequest, joResponse );
}

bool SkaleServerOverride::handleHttpSpecificRequest( const std::string& strOrigin,
e_server_mode_t esm, const std::string& strRequest, std::string& strResponse ) {
strResponse.clear();
Expand Down
5 changes: 5 additions & 0 deletions libskale/httpserveroverride.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ class SkaleServerOverride : public jsonrpc::AbstractServerConnector,
fn_jsonrpc_call_t fn_eth_newFilter_;
fn_jsonrpc_call_t fn_eth_newBlockFilter_;
fn_jsonrpc_call_t fn_eth_newPendingTransactionFilter_;
fn_jsonrpc_call_t fn_eth_uninstallFilter_;
double lfExecutionDurationMaxForPerformanceWarning_ = 0; // in seconds
bool isTraceCalls_ = false;
bool isTraceSpecialCalls_ = false;
Expand All @@ -446,6 +447,7 @@ class SkaleServerOverride : public jsonrpc::AbstractServerConnector,
fn_eth_newFilter_ = other.fn_eth_newFilter_;
fn_eth_newBlockFilter_ = other.fn_eth_newBlockFilter_;
fn_eth_newPendingTransactionFilter_ = other.fn_eth_newPendingTransactionFilter_;
fn_eth_uninstallFilter_ = other.fn_eth_uninstallFilter_;
lfExecutionDurationMaxForPerformanceWarning_ =
other.lfExecutionDurationMaxForPerformanceWarning_;
isTraceCalls_ = other.isTraceCalls_;
Expand Down Expand Up @@ -622,6 +624,9 @@ class SkaleServerOverride : public jsonrpc::AbstractServerConnector,
void eth_newPendingTransactionFilter( const std::string& strOrigin,
const rapidjson::Document& joRequest, rapidjson::Document& joResponse );

void eth_uninstallFilter( const std::string& strOrigin, const rapidjson::Document& joRequest,
rapidjson::Document& joResponse );

unsigned iwBlockStats_ = unsigned( -1 ), iwPendingTransactionStats_ = unsigned( -1 );
mutex_type mtxStats_;
skutils::stats::named_event_stats statsBlocks_, statsTransactions_, statsPendingTx_;
Expand Down
37 changes: 35 additions & 2 deletions libweb3jsonrpc/rapidjson_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ void inject_rapidjson_handlers( SkaleServerOverride::opts_t& serverOpts, dev::rp
throw jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS );
}

if ( joRequest["params"].GetArray().Size() > 0 ) {
if ( joRequest["params"].GetArray().Size() != 0 ) {
throw jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS );
}

Expand All @@ -344,7 +344,7 @@ void inject_rapidjson_handlers( SkaleServerOverride::opts_t& serverOpts, dev::rp
throw jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS );
}

if ( joRequest["params"].GetArray().Size() > 0 ) {
if ( joRequest["params"].GetArray().Size() != 0 ) {
throw jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS );
}

Expand All @@ -362,6 +362,38 @@ void inject_rapidjson_handlers( SkaleServerOverride::opts_t& serverOpts, dev::rp
}
};

SkaleServerOverride::fn_jsonrpc_call_t fn_eth_uninstallFilter =
[=]( const std::string& strOrigin, const rapidjson::Document& joRequest,
rapidjson::Document& joResponse ) -> void {
try {
if ( !joRequest.HasMember( "params" ) || !joRequest["params"].IsArray() ) {
throw jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS );
}

if ( joRequest["params"].GetArray().Size() != 1 ) {
throw jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS );
}

if ( !joRequest["params"].GetArray()[0].IsString() ) {
throw jsonrpc::JsonRpcException( jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS );
}

std::string filterId = joRequest["params"].GetArray()[0].GetString();

bool response = pEthFace->eth_uninstallFilter( filterId, strOrigin );

rapidjson::Value& v = joResponse["result"];
v.SetBool( response ) /*joResponse.GetAllocator() )*/;
} catch ( const jsonrpc::JsonRpcException& ex ) {
wrapJsonRpcException( joRequest, ex, joResponse );
} catch ( const dev::Exception& ) {
wrapJsonRpcException( joRequest,
jsonrpc::JsonRpcException(
ERROR_RPC_CUSTOM_ERROR, dev::rpc::exceptionToErrorMessage() ),
joResponse );
}
};

serverOpts.fn_eth_sendRawTransaction_ = fn_eth_sendRawTransaction;
serverOpts.fn_eth_getTransactionReceipt_ = fn_eth_getTransactionReceipt;
serverOpts.fn_eth_call_ = fn_eth_call;
Expand All @@ -372,4 +404,5 @@ void inject_rapidjson_handlers( SkaleServerOverride::opts_t& serverOpts, dev::rp
serverOpts.fn_eth_newFilter_ = fn_eth_newFilter;
serverOpts.fn_eth_newBlockFilter_ = fn_eth_newBlockFilter;
serverOpts.fn_eth_newPendingTransactionFilter_ = fn_eth_newPendingTransactionFilter;
serverOpts.fn_eth_uninstallFilter_ = fn_eth_uninstallFilter;
}
26 changes: 22 additions & 4 deletions test/unittests/libweb3jsonrpc/jsonrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,11 +967,29 @@ BOOST_AUTO_TEST_CASE( create_filters ) {
string filterId = fixture.rpcClient->eth_newFilter( filterObj );
}

Json::Value filterObj;
filterObj["address"] = contractAddress;
filterObj["fromBlock"] = "0x1";
Json::Value filterObjFail;
filterObjFail["address"] = contractAddress;
filterObjFail["fromBlock"] = "0x1";

BOOST_REQUIRE_THROW( fixture.rpcClient->eth_newFilter( filterObjFail ), jsonrpc::JsonRpcException );

for (size_t i = 0; i < 100; ++i) {
BOOST_REQUIRE( fixture.rpcClient->eth_uninstallFilter( std::to_string( i ) ) );
}

std::cout << "HERE\n";

for (size_t i = 0; i < 100; ++i) {
Json::Value filterObj;
filterObj["address"] = contractAddress;
filterObj["fromBlock"] = "0x1";
string filterId = fixture.rpcClient->eth_newFilter( filterObj );
}

filterObjFail["address"] = contractAddress;
filterObjFail["fromBlock"] = "0x1";

BOOST_REQUIRE_THROW( fixture.rpcClient->eth_newFilter( filterObj ), jsonrpc::JsonRpcException );
BOOST_REQUIRE_THROW( fixture.rpcClient->eth_newFilter( filterObjFail ), jsonrpc::JsonRpcException );
}

BOOST_AUTO_TEST_CASE( deploy_contract_from_owner ) {
Expand Down

0 comments on commit 40cba16

Please sign in to comment.