From 53a94a68bc678548a054b6bcfe77ae65dc1fb215 Mon Sep 17 00:00:00 2001 From: 766C6164 Date: Tue, 13 Jun 2023 14:41:38 -0400 Subject: [PATCH 1/5] if actions.data & actions.hex_data provided, use the hex_data --- plugins/chain_api_plugin/chain_api_plugin.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/plugins/chain_api_plugin/chain_api_plugin.cpp b/plugins/chain_api_plugin/chain_api_plugin.cpp index ba5c92272b..6d34173596 100644 --- a/plugins/chain_api_plugin/chain_api_plugin.cpp +++ b/plugins/chain_api_plugin/chain_api_plugin.cpp @@ -41,6 +41,45 @@ parse_params +chain_apis::read_only::get_transaction_id_params +parse_params(const std::string& body) { + if (body.empty()) { + EOS_THROW(chain::invalid_http_request, "A Request body is required"); + } + + try { + fc::variant trx_var = fc::json::from_string( body ); + if( trx_var.is_object() ) { + fc::variant_object& vo = trx_var.get_object(); + if( vo.contains("actions") && vo["actions"].is_array() ) { + fc::mutable_variant_object mvo = vo; + fc::variants& action_variants = mvo["actions"].get_array(); + for( auto& action_v : action_variants ) { + if( action_v.is_object() ) { + fc::variant_object& action_vo = action_v.get_object(); + if( action_vo.contains( "data" ) && action_vo.contains( "hex_data" ) ) { + fc::mutable_variant_object maction_vo = action_vo; + maction_vo["data"] = maction_vo["hex_data"]; + action_vo = maction_vo; + vo = mvo; + } + } + } + } + else { + throw false; + } + } + auto trx = trx_var.as(); + if( trx.id() == transaction().id() ) throw false; + return trx; + } catch( ... ) { + EOS_THROW(chain::invalid_http_request, "Invalid transaction"); + } +} + #define CALL_WITH_400(api_name, api_handle, api_namespace, call_name, http_response_code, params_type) \ {std::string("/v1/" #api_name "/" #call_name), \ [api_handle](string&&, string&& body, url_response_callback&& cb) mutable { \ From 167820fcd71798171d170ac67272bed1082a2992 Mon Sep 17 00:00:00 2001 From: 766C6164 Date: Tue, 13 Jun 2023 18:52:33 -0400 Subject: [PATCH 2/5] Addresing feedback - tests, asserts --- plugins/chain_api_plugin/chain_api_plugin.cpp | 9 +++- tests/plugin_http_api_test.py | 46 +++++++++++++++++++ tests/read_only_trx_test.py | 2 +- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/plugins/chain_api_plugin/chain_api_plugin.cpp b/plugins/chain_api_plugin/chain_api_plugin.cpp index 6d34173596..0ac95c70ae 100644 --- a/plugins/chain_api_plugin/chain_api_plugin.cpp +++ b/plugins/chain_api_plugin/chain_api_plugin.cpp @@ -69,11 +69,16 @@ parse_params(); - if( trx.id() == transaction().id() ) throw false; + if( trx.id() == transaction().id() ) { + throw false; // invalid transaction + } return trx; } catch( ... ) { EOS_THROW(chain::invalid_http_request, "Invalid transaction"); diff --git a/tests/plugin_http_api_test.py b/tests/plugin_http_api_test.py index 686639c72e..b4f09a9c3a 100755 --- a/tests/plugin_http_api_test.py +++ b/tests/plugin_http_api_test.py @@ -600,6 +600,35 @@ def test_ChainApi(self) : ret_json = self.nodeos.processUrllibRequest(resource, command, self.http_post_invalid_param) self.assertEqual(ret_json["code"], 400) self.assertEqual(ret_json["error"]["code"], 3200006) + # get_transaction_id with missing actions + payload_no_actions = {"expiration":"2020-08-01T07:15:49", + "ref_block_num": 34881, + "ref_block_prefix":2972818865, + "max_net_usage_words":0, + "max_cpu_usage_ms":0, + "delay_sec":0, + "context_free_actions":[], + "transaction_extensions": [], + "signatures": ["SIG_K1_KeqfqiZu1GwUxQb7jzK9Fdks6HFaVBQ9AJtCZZj56eG9qGgvVMVtx8EerBdnzrhFoX437sgwtojf2gfz6S516Ty7c22oEp"], + "context_free_data": []} + ret_json = self.nodeos.processUrllibRequest(resource, command, payload_no_actions) + self.assertEqual(ret_json["code"], 400) + self.assertEqual(ret_json["error"]["code"], 3200006) + # get_transaction_id with invalid actions + payload_invalid_actions = {"expiration":"2020-08-01T07:15:49", + "ref_block_num": 34881, + "ref_block_prefix":2972818865, + "max_net_usage_words":0, + "max_cpu_usage_ms":0, + "delay_sec":0, + "context_free_actions":[], + "actions": "hello_world", + "transaction_extensions": [], + "signatures": ["SIG_K1_KeqfqiZu1GwUxQb7jzK9Fdks6HFaVBQ9AJtCZZj56eG9qGgvVMVtx8EerBdnzrhFoX437sgwtojf2gfz6S516Ty7c22oEp"], + "context_free_data": []} + ret_json = self.nodeos.processUrllibRequest(resource, command, payload_invalid_actions) + self.assertEqual(ret_json["code"], 400) + self.assertEqual(ret_json["error"]["code"], 3200006) # get_transaction_id with valid parameter payload = {"expiration":"2020-08-01T07:15:49", "ref_block_num": 34881, @@ -614,6 +643,23 @@ def test_ChainApi(self) : "context_free_data": []} ret_str = self.nodeos.processUrllibRequest(resource, command, payload, returnType=ReturnType.raw).decode('ascii') self.assertEqual(ret_str, "\"0be762a6406bab15530e87f21e02d1c58e77944ee55779a76f4112e3b65cac48\"") + # transaction that has hex_data + payload_hex = {"expiration":"2020-08-01T07:15:49", + "ref_block_num": 34881, + "ref_block_prefix":2972818865, + "max_net_usage_words":0, + "max_cpu_usage_ms":0, + "delay_sec":0, + "context_free_actions":[], + "actions":[{"account":"eosio.token","name": "transfer","authorization": [{"actor": "han","permission": "active"}], + "data": "{\"entry\":774831,\"miner\":\"eosminer1111\",\"nonce\":139429}\"}", + "hex_data": "000000000000a6690000000000ea305501000000000000000453595300000000016d"}], + "transaction_extensions": [], + "signatures": ["SIG_K1_KeqfqiZu1GwUxQb7jzK9Fdks6HFaVBQ9AJtCZZj56eG9qGgvVMVtx8EerBdnzrhFoX437sgwtojf2gfz6S516Ty7c22oEp"], + "context_free_data": []} + ret_str = self.nodeos.processUrllibRequest(resource, command, payload_hex, returnType=ReturnType.raw).decode('ascii') + self.assertEqual(ret_str, "\"0be762a6406bab15530e87f21e02d1c58e77944ee55779a76f4112e3b65cac48\"") + # push_block with empty parameter command = "push_block" diff --git a/tests/read_only_trx_test.py b/tests/read_only_trx_test.py index 9d0beb68e6..4f5f80f782 100755 --- a/tests/read_only_trx_test.py +++ b/tests/read_only_trx_test.py @@ -265,7 +265,7 @@ def chainApiTests(): runReadOnlyTrxAndRpcInParallel("chain", "get_currency_balance", code=200, payload = {"code":"eosio.token", "account":testAccountName}) runReadOnlyTrxAndRpcInParallel("chain", "get_currency_stats", fieldIn="SYS", payload = {"code":"eosio.token", "symbol":"SYS"}) runReadOnlyTrxAndRpcInParallel("chain", "get_required_keys", code=400) - runReadOnlyTrxAndRpcInParallel("chain", "get_transaction_id", code=200, payload = {"ref_block_num":"1"}) + runReadOnlyTrxAndRpcInParallel("chain", "get_transaction_id", code=400, payload = {"ref_block_num":"1"}) runReadOnlyTrxAndRpcInParallel("chain", "push_block", code=202, payload = {"block":"signed_block"}) runReadOnlyTrxAndRpcInParallel("chain", "get_producer_schedule", "active") runReadOnlyTrxAndRpcInParallel("chain", "get_scheduled_transactions", "transactions", payload = {"json":"true","lower_bound":""}) From 6446f4a9e4c013faf25a174a09d170d8862320bb Mon Sep 17 00:00:00 2001 From: 766C6164 Date: Wed, 14 Jun 2023 12:56:17 -0400 Subject: [PATCH 3/5] Updated exceptions --- plugins/chain_api_plugin/chain_api_plugin.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/chain_api_plugin/chain_api_plugin.cpp b/plugins/chain_api_plugin/chain_api_plugin.cpp index 0ac95c70ae..2ee6aa5af2 100644 --- a/plugins/chain_api_plugin/chain_api_plugin.cpp +++ b/plugins/chain_api_plugin/chain_api_plugin.cpp @@ -73,13 +73,15 @@ parse_params(); if( trx.id() == transaction().id() ) { - throw false; // invalid transaction + EOS_THROW(chain::invalid_http_request, "Invalid transaction object"); } return trx; + } catch(chain::invalid_http_request & e) { + throw; } catch( ... ) { EOS_THROW(chain::invalid_http_request, "Invalid transaction"); } From 72f87b87a510553c6942651bb3c11d2f037413f7 Mon Sep 17 00:00:00 2001 From: 766C6164 Date: Wed, 14 Jun 2023 13:13:07 -0400 Subject: [PATCH 4/5] use EOS_RETHROW_EXCEPTIONS --- plugins/chain_api_plugin/chain_api_plugin.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/plugins/chain_api_plugin/chain_api_plugin.cpp b/plugins/chain_api_plugin/chain_api_plugin.cpp index 2ee6aa5af2..525795b9a9 100644 --- a/plugins/chain_api_plugin/chain_api_plugin.cpp +++ b/plugins/chain_api_plugin/chain_api_plugin.cpp @@ -80,11 +80,7 @@ parse_params Date: Wed, 14 Jun 2023 16:09:40 -0400 Subject: [PATCH 5/5] Added two more validation exceptions from cleos --- plugins/chain_api_plugin/chain_api_plugin.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/plugins/chain_api_plugin/chain_api_plugin.cpp b/plugins/chain_api_plugin/chain_api_plugin.cpp index 525795b9a9..277a28e527 100644 --- a/plugins/chain_api_plugin/chain_api_plugin.cpp +++ b/plugins/chain_api_plugin/chain_api_plugin.cpp @@ -64,8 +64,15 @@ parse_params