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/core/DatabaseCon.h>
24 #include <ripple/protocol/ErrorCodes.h>
25 #include <ripple/protocol/jss.h>
26 #include <ripple/rpc/GRPCHandlers.h>
27 #include <ripple/rpc/impl/RPCHelpers.h>
28 #include <test/jtx.h>
29 #include <test/jtx/Env.h>
30 #include <test/jtx/envconfig.h>
31 #include <test/rpc/GRPCTestClientBase.h>
32 
33 namespace ripple {
34 namespace test {
35 
36 class Fee_test : public beast::unit_test::suite
37 {
39  {
40  public:
41  org::xrpl::rpc::v1::GetFeeRequest request;
42  org::xrpl::rpc::v1::GetFeeResponse reply;
43 
44  explicit GrpcFeeClient(std::string const& grpcPort)
45  : GRPCTestClientBase(grpcPort)
46  {
47  }
48 
49  void
51  {
52  status = stub_->GetFee(&context, request, &reply);
53  }
54  };
55 
57  grpcGetFee(std::string const& grpcPort)
58  {
59  GrpcFeeClient client(grpcPort);
60  client.GetFee();
62  client.status.ok(), client.reply);
63  }
64 
65  void
67  {
68  testcase("Test Fee Grpc");
69 
70  using namespace test::jtx;
72  std::string grpcPort = *(*config)["port_grpc"].get<std::string>("port");
73  Env env(*this, std::move(config));
74  Account A1{"A1"};
75  Account A2{"A2"};
76  env.fund(XRP(10000), A1);
77  env.fund(XRP(10000), A2);
78  env.close();
79  env.trust(A2["USD"](1000), A1);
80  env.close();
81  for (int i = 0; i < 7; ++i)
82  {
83  env(pay(A2, A1, A2["USD"](100)));
84  if (i == 4)
85  env.close();
86  }
87 
88  auto view = env.current();
89 
90  auto const metrics = env.app().getTxQ().getMetrics(*env.current());
91 
92  auto const result = grpcGetFee(grpcPort);
93 
94  BEAST_EXPECT(result.first == true);
95 
96  auto reply = result.second;
97 
98  // current ledger data
99  BEAST_EXPECT(reply.current_ledger_size() == metrics.txInLedger);
100  BEAST_EXPECT(reply.current_queue_size() == metrics.txCount);
101  BEAST_EXPECT(reply.expected_ledger_size() == metrics.txPerLedger);
102  BEAST_EXPECT(reply.ledger_current_index() == view->info().seq);
103  BEAST_EXPECT(reply.max_queue_size() == *metrics.txQMaxSize);
104 
105  // fee levels data
106  org::xrpl::rpc::v1::FeeLevels& levels = *reply.mutable_levels();
107  BEAST_EXPECT(levels.median_level() == metrics.medFeeLevel);
108  BEAST_EXPECT(levels.minimum_level() == metrics.minProcessingFeeLevel);
109  BEAST_EXPECT(levels.open_ledger_level() == metrics.openLedgerFeeLevel);
110  BEAST_EXPECT(levels.reference_level() == metrics.referenceFeeLevel);
111 
112  // fee data
113  org::xrpl::rpc::v1::Fee& fee = *reply.mutable_fee();
114  auto const baseFee = view->fees().base;
115  BEAST_EXPECT(
116  fee.base_fee().drops() ==
117  toDrops(metrics.referenceFeeLevel, baseFee));
118  BEAST_EXPECT(
119  fee.minimum_fee().drops() ==
120  toDrops(metrics.minProcessingFeeLevel, baseFee));
121  BEAST_EXPECT(
122  fee.median_fee().drops() == toDrops(metrics.medFeeLevel, baseFee));
123  auto openLedgerFee =
124  toDrops(metrics.openLedgerFeeLevel - FeeLevel64{1}, baseFee) + 1;
125  BEAST_EXPECT(fee.open_ledger_fee().drops() == openLedgerFee.drops());
126  }
127 
128 public:
129  void
130  run() override
131  {
132  testFeeGrpc();
133  }
134 };
135 
136 BEAST_DEFINE_TESTSUITE(Fee, app, ripple);
137 
138 } // namespace test
139 } // 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:42
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:50
ripple::test::Fee_test::testFeeGrpc
void testFeeGrpc()
Definition: Fee_test.cpp:66
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:826
ripple::test::Fee_test
Definition: Fee_test.cpp:36
ripple::test::Fee_test::run
void run() override
Definition: Fee_test.cpp:130
ripple::feeunit::TaggedFee
Definition: FeeUnits.h:70
ripple::test::Fee_test::GrpcFeeClient
Definition: Fee_test.cpp:38
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:57
ripple::test::jtx::fee
Set the fee on a JTx.
Definition: fee.h:34
ripple::test::Fee_test::GrpcFeeClient::GrpcFeeClient
GrpcFeeClient(std::string const &grpcPort)
Definition: Fee_test.cpp:44
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:41
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:115