From 74660aebf15bf3c5b0252d467275b1812ff8ef8d Mon Sep 17 00:00:00 2001 From: cyan317 <120398799+cindyyan317@users.noreply.github.com> Date: Mon, 20 Nov 2023 17:53:34 +0000 Subject: [PATCH] binary (#993) Fix #984 --- src/rpc/handlers/Tx.h | 12 ++++--- unittests/rpc/handlers/TxTests.cpp | 52 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/rpc/handlers/Tx.h b/src/rpc/handlers/Tx.h index aa8da298..a63ebdb8 100644 --- a/src/rpc/handlers/Tx.h +++ b/src/rpc/handlers/Tx.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -67,17 +68,18 @@ public: } static RpcSpecConstRef - spec([[maybe_unused]] uint32_t apiVersion) + spec(uint32_t apiVersion) { - static const RpcSpec rpcSpec = { + static const RpcSpec rpcSpecForV1 = { {JS(transaction), validation::Uint256HexStringValidator}, - {JS(binary), validation::Type{}}, {JS(min_ledger), validation::Type{}}, {JS(max_ledger), validation::Type{}}, {JS(ctid), validation::Type{}}, }; - return rpcSpec; + static auto const rpcSpec = RpcSpec{rpcSpecForV1, {{JS(binary), validation::Type{}}}}; + + return apiVersion == 1 ? rpcSpecForV1 : rpcSpec; } Result @@ -273,7 +275,7 @@ private: } if (jsonObject.contains(JS(binary))) - input.binary = jv.at(JS(binary)).as_bool(); + input.binary = boost::json::value_to(jsonObject.at(JS(binary))); if (jsonObject.contains(JS(min_ledger))) input.minLedger = jv.at(JS(min_ledger)).as_int64(); diff --git a/unittests/rpc/handlers/TxTests.cpp b/unittests/rpc/handlers/TxTests.cpp index 235e0a1d..3cea84c3 100644 --- a/unittests/rpc/handlers/TxTests.cpp +++ b/unittests/rpc/handlers/TxTests.cpp @@ -143,6 +143,58 @@ TEST_F(RPCTxTest, ExcessiveLgrRange) }); } +TEST_F(RPCTxTest, InvalidBinaryV1) +{ + auto const rawBackendPtr = dynamic_cast(mockBackendPtr.get()); + ASSERT_NE(rawBackendPtr, nullptr); + TransactionAndMetadata tx; + tx.metadata = CreateMetaDataForCreateOffer(CURRENCY, ACCOUNT, 100, 200, 300).getSerializer().peekData(); + tx.transaction = + CreateCreateOfferTransactionObject(ACCOUNT, 2, 100, CURRENCY, ACCOUNT2, 200, 300).getSerializer().peekData(); + tx.date = 123456; + tx.ledgerSequence = 100; + EXPECT_CALL(*rawBackendPtr, fetchTransaction(ripple::uint256{TXNID}, _)).WillOnce(Return(tx)); + + auto const rawETLPtr = dynamic_cast(mockETLServicePtr.get()); + ASSERT_NE(rawETLPtr, nullptr); + EXPECT_CALL(*rawETLPtr, getETLState).WillOnce(Return(etl::ETLState{})); + + runSpawn([this](auto yield) { + auto const handler = AnyHandler{TestTxHandler{mockBackendPtr, mockETLServicePtr}}; + auto const req = json::parse(fmt::format( + R"({{ + "command": "tx", + "transaction": "{}", + "binary": 12 + }})", + TXNID + )); + auto const output = handler.process(req, Context{.yield = yield, .apiVersion = 1u}); + ASSERT_TRUE(output); + }); +} + +TEST_F(RPCTxTest, InvalidBinaryV2) +{ + runSpawn([this](auto yield) { + auto const handler = AnyHandler{TestTxHandler{mockBackendPtr, mockETLServicePtr}}; + auto const req = json::parse(fmt::format( + R"({{ + "command": "tx", + "transaction": "{}", + "binary": 12 + }})", + TXNID + )); + auto const output = handler.process(req, Context{.yield = yield, .apiVersion = 2u}); + ASSERT_FALSE(output); + + auto const err = rpc::makeError(output.error()); + EXPECT_EQ(err.at("error").as_string(), "invalidParams"); + EXPECT_EQ(err.at("error_message").as_string(), "Invalid parameters."); + }); +} + TEST_F(RPCTxTest, InvalidLgrRange) { runSpawn([this](auto yield) {