rippled
LedgerHandler.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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 #ifndef RIPPLE_RPC_HANDLERS_LEDGER_H_INCLUDED
21 #define RIPPLE_RPC_HANDLERS_LEDGER_H_INCLUDED
22 
23 #include <ripple/app/ledger/LedgerMaster.h>
24 #include <ripple/app/ledger/LedgerToJson.h>
25 #include <ripple/app/main/Application.h>
26 #include <ripple/json/Object.h>
27 #include <ripple/ledger/ReadView.h>
28 #include <ripple/protocol/jss.h>
29 #include <ripple/rpc/Context.h>
30 #include <ripple/rpc/Role.h>
31 #include <ripple/rpc/Status.h>
32 #include <ripple/rpc/impl/Handler.h>
33 #include <ripple/rpc/impl/RPCHelpers.h>
34 
35 namespace Json {
36 class Object;
37 }
38 
39 namespace ripple {
40 namespace RPC {
41 
42 struct JsonContext;
43 
44 // ledger [id|index|current|closed] [full]
45 // {
46 // ledger: 'current' | 'closed' | <uint256> | <number>, // optional
47 // full: true | false // optional, defaults to false.
48 // }
49 
51 {
52 public:
53  explicit LedgerHandler(JsonContext&);
54 
55  Status
56  check();
57 
58  template <class Object>
59  void
60  writeResult(Object&);
61 
62  static constexpr char name[] = "ledger";
63 
64  static constexpr unsigned minApiVer = RPC::apiMinimumSupportedVersion;
65 
66  static constexpr unsigned maxApiVer = RPC::apiMaximumValidVersion;
67 
68  static constexpr Role role = Role::USER;
69 
70  static constexpr Condition condition = NO_CONDITION;
71 
72 private:
77  int options_ = 0;
79 };
80 
83 //
84 // Implementation.
85 
86 template <class Object>
87 void
89 {
90  if (ledger_)
91  {
92  Json::copyFrom(value, result_);
94  }
95  else
96  {
97  auto& master = context_.app.getLedgerMaster();
98  {
99  auto&& closed = Json::addObject(value, jss::closed);
100  addJson(closed, {*master.getClosedLedger(), &context_, 0});
101  }
102  {
103  auto&& open = Json::addObject(value, jss::open);
104  addJson(open, {*master.getCurrentLedger(), &context_, 0});
105  }
106  }
107 }
108 
109 } // namespace RPC
110 } // namespace ripple
111 
112 #endif
ripple::RPC::LedgerHandler::minApiVer
static constexpr unsigned minApiVer
Definition: LedgerHandler.h:64
ripple::RPC::LedgerHandler
Definition: LedgerHandler.h:50
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::RPC::LedgerHandler::context_
JsonContext & context_
Definition: LedgerHandler.h:73
std::shared_ptr
STL class.
ripple::RPC::LedgerHandler::maxApiVer
static constexpr unsigned maxApiVer
Definition: LedgerHandler.h:66
std::vector
STL class.
ripple::RPC::LedgerHandler::ledger_
std::shared_ptr< ReadView const > ledger_
Definition: LedgerHandler.h:74
Json::copyFrom
void copyFrom(Json::Value &to, Json::Value const &from)
Copy all the keys and values from one object into another.
Definition: Object.cpp:226
Json
JSON (JavaScript Object Notation).
Definition: DeliverMax.h:28
ripple::Application::getLedgerMaster
virtual LedgerMaster & getLedgerMaster()=0
ripple::Role::USER
@ USER
ripple::RPC::NO_CONDITION
@ NO_CONDITION
Definition: Handler.h:41
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
ripple::RPC::Condition
Condition
Definition: Handler.h:40
ripple::RPC::LedgerHandler::condition
static constexpr Condition condition
Definition: LedgerHandler.h:70
ripple::RPC::Status
Status represents the results of an operation that might fail.
Definition: Status.h:39
ripple::RPC::apiMinimumSupportedVersion
constexpr unsigned int apiMinimumSupportedVersion
Definition: RPCHelpers.h:236
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RPC::LedgerHandler::LedgerHandler
LedgerHandler(JsonContext &)
Definition: LedgerHandler.cpp:34
ripple::RPC::LedgerHandler::writeResult
void writeResult(Object &)
Definition: LedgerHandler.h:88
ripple::LedgerEntryType
LedgerEntryType
Identifiers for on-ledger objects.
Definition: LedgerFormats.h:53
ripple::RPC::apiMaximumValidVersion
constexpr unsigned int apiMaximumValidVersion
Definition: RPCHelpers.h:239
Json::addObject
Json::Value & addObject(Json::Value &, Json::StaticString const &key)
Add a new subobject at a named key in a Json object.
Definition: Object.h:426
ripple::RPC::LedgerHandler::type_
LedgerEntryType type_
Definition: LedgerHandler.h:78
ripple::RPC::LedgerHandler::check
Status check()
Definition: LedgerHandler.cpp:39
ripple::addJson
void addJson(Json::Value &json, LedgerFill const &fill)
Given a Ledger and options, fill a Json::Object or Json::Value with a description of the ledger.
Definition: LedgerToJson.cpp:330
ripple::RPC::LedgerHandler::name
static constexpr char name[]
Definition: LedgerHandler.h:62
ripple::RPC::LedgerHandler::options_
int options_
Definition: LedgerHandler.h:77
ripple::RPC::LedgerHandler::queueTxs_
std::vector< TxQ::TxDetails > queueTxs_
Definition: LedgerHandler.h:75
ripple::RPC::LedgerHandler::role
static constexpr Role role
Definition: LedgerHandler.h:68
ripple::RPC::LedgerHandler::result_
Json::Value result_
Definition: LedgerHandler.h:76
ripple::Role
Role
Indicates the level of administrative permission to grant.
Definition: Role.h:43
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::open
void open(soci::session &s, BasicConfig const &config, std::string const &dbName)
Open a soci session.
Definition: SociDB.cpp:98