rippled
Fee_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2020 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/ledger/LedgerMaster.h>
21 #include <ripple/app/misc/TxQ.h>
22 #include <ripple/basics/mulDiv.h>
23 #include <ripple/protocol/ErrorCodes.h>
24 #include <ripple/protocol/jss.h>
25 #include <ripple/rpc/GRPCHandlers.h>
26 #include <ripple/rpc/impl/RPCHelpers.h>
27 #include <test/jtx.h>
28 #include <test/jtx/Env.h>
29 #include <test/jtx/envconfig.h>
30 #include <test/rpc/GRPCTestClientBase.h>
31 
32 namespace ripple {
33 namespace test {
34 
35 class Fee_test : public beast::unit_test::suite
36 {
38  {
39  public:
40  org::xrpl::rpc::v1::GetFeeRequest request;
41  org::xrpl::rpc::v1::GetFeeResponse reply;
42 
43  explicit GrpcFeeClient(std::string const& grpcPort)
44  : GRPCTestClientBase(grpcPort)
45  {
46  }
47 
48  void
50  {
51  status = stub_->GetFee(&context, request, &reply);
52  }
53  };
54 
56  grpcGetFee(std::string const& grpcPort)
57  {
58  GrpcFeeClient client(grpcPort);
59  client.GetFee();
61  client.status.ok(), client.reply);
62  }
63 
64  void
66  {
67  testcase("Test Fee Grpc");
68 
69  using namespace test::jtx;
71  std::string grpcPort = *(*config)["port_grpc"].get<std::string>("port");
72  Env env(*this, std::move(config));
73  Account A1{"A1"};
74  Account A2{"A2"};
75  env.fund(XRP(10000), A1);
76  env.fund(XRP(10000), A2);
77  env.close();
78  env.trust(A2["USD"](1000), A1);
79  env.close();
80  for (int i = 0; i < 7; ++i)
81  {
82  env(pay(A2, A1, A2["USD"](100)));
83  if (i == 4)
84  env.close();
85  }
86 
87  auto view = env.current();
88 
89  auto const metrics = env.app().getTxQ().getMetrics(*env.current());
90 
91  auto const result = grpcGetFee(grpcPort);
92 
93  BEAST_EXPECT(result.first == true);
94 
95  auto reply = result.second;
96 
97  // current ledger data
98  BEAST_EXPECT(reply.current_ledger_size() == metrics.txInLedger);
99  BEAST_EXPECT(reply.current_queue_size() == metrics.txCount);
100  BEAST_EXPECT(reply.expected_ledger_size() == metrics.txPerLedger);
101  BEAST_EXPECT(reply.ledger_current_index() == view->info().seq);
102  BEAST_EXPECT(reply.max_queue_size() == *metrics.txQMaxSize);
103 
104  // fee levels data
105  org::xrpl::rpc::v1::FeeLevels& levels = *reply.mutable_levels();
106  BEAST_EXPECT(levels.median_level() == metrics.medFeeLevel);
107  BEAST_EXPECT(levels.minimum_level() == metrics.minProcessingFeeLevel);
108  BEAST_EXPECT(levels.open_ledger_level() == metrics.openLedgerFeeLevel);
109  BEAST_EXPECT(levels.reference_level() == metrics.referenceFeeLevel);
110 
111  // fee data
112  org::xrpl::rpc::v1::Fee& fee = *reply.mutable_fee();
113  auto const baseFee = view->fees().base;
114  BEAST_EXPECT(
115  fee.base_fee().drops() ==
116  toDrops(metrics.referenceFeeLevel, baseFee));
117  BEAST_EXPECT(
118  fee.minimum_fee().drops() ==
119  toDrops(metrics.minProcessingFeeLevel, baseFee));
120  BEAST_EXPECT(
121  fee.median_fee().drops() == toDrops(metrics.medFeeLevel, baseFee));
122  auto openLedgerFee =
123  toDrops(metrics.openLedgerFeeLevel - FeeLevel64{1}, baseFee) + 1;
124  BEAST_EXPECT(fee.open_ledger_fee().drops() == openLedgerFee.drops());
125  }
126 
127 public:
128  void
129  run() override
130  {
131  testFeeGrpc();
132  }
133 };
134 
135 BEAST_DEFINE_TESTSUITE(Fee, app, ripple);
136 
137 } // namespace test
138 } // namespace ripple
ripple::test::jtx::XRP
const XRP_t XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:105
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountDelete, app, ripple)
std::string
STL class.
ripple::test::Fee_test::GrpcFeeClient::reply
org::xrpl::rpc::v1::GetFeeResponse reply
Definition: Fee_test.cpp:41
std::pair
ripple::test::GRPCTestClientBase::stub_
std::unique_ptr< org::xrpl::rpc::v1::XRPLedgerAPIService::Stub > stub_
Definition: GRPCTestClientBase.h:44
ripple::test::jtx::addGrpcConfig
std::unique_ptr< Config > addGrpcConfig(std::unique_ptr< Config >)
add a grpc address and port to config
Definition: envconfig.cpp:113
ripple::test::Fee_test::GrpcFeeClient::GetFee
void GetFee()
Definition: Fee_test.cpp:49
ripple::test::Fee_test::testFeeGrpc
void testFeeGrpc()
Definition: Fee_test.cpp:65
ripple::test::jtx::envconfig
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition: envconfig.h:49
ripple::test::GRPCTestClientBase::status
grpc::Status status
Definition: GRPCTestClientBase.h:42
ripple::test::GRPCTestClientBase
Definition: GRPCTestClientBase.h:29
ripple::toDrops
XRPAmount toDrops(FeeLevel< T > const &level, XRPAmount const &baseFee)
Definition: TxQ.h:827
ripple::test::Fee_test
Definition: Fee_test.cpp:35
ripple::test::Fee_test::run
void run() override
Definition: Fee_test.cpp:129
ripple::feeunit::TaggedFee
Definition: FeeUnits.h:70
ripple::test::Fee_test::GrpcFeeClient
Definition: Fee_test.cpp:37
ripple::test::GRPCTestClientBase::context
grpc::ClientContext context
Definition: GRPCTestClientBase.h:43
ripple::test::Fee_test::grpcGetFee
std::pair< bool, org::xrpl::rpc::v1::GetFeeResponse > grpcGetFee(std::string const &grpcPort)
Definition: Fee_test.cpp:56
ripple::test::jtx::fee
Set the fee on a JTx.
Definition: fee.h:35
ripple::test::Fee_test::GrpcFeeClient::GrpcFeeClient
GrpcFeeClient(std::string const &grpcPort)
Definition: Fee_test.cpp:43
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::jtx::pay
Json::Value pay(Account const &account, Account const &to, AnyAmount amount)
Create a payment.
Definition: pay.cpp:29
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
std::unique_ptr
STL class.
ripple::test::Fee_test::GrpcFeeClient::request
org::xrpl::rpc::v1::GetFeeRequest request
Definition: Fee_test.cpp:40
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:115