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 
34 namespace Json {
35 class Object;
36 }
37 
38 namespace ripple {
39 namespace RPC {
40 
41 struct JsonContext;
42 
43 // ledger [id|index|current|closed] [full]
44 // {
45 // ledger: 'current' | 'closed' | <uint256> | <number>, // optional
46 // full: true | false // optional, defaults to false.
47 // }
48 
50 {
51 public:
52  explicit LedgerHandler(JsonContext&);
53 
54  Status
55  check();
56 
57  template <class Object>
58  void
59  writeResult(Object&);
60 
61  static char const*
62  name()
63  {
64  return "ledger";
65  }
66 
67  static Role
68  role()
69  {
70  return Role::USER;
71  }
72 
73  static Condition
75  {
76  return NO_CONDITION;
77  }
78 
79 private:
84  int options_ = 0;
86 };
87 
90 //
91 // Implementation.
92 
93 template <class Object>
94 void
96 {
97  if (ledger_)
98  {
99  Json::copyFrom(value, result_);
101  }
102  else
103  {
104  auto& master = context_.app.getLedgerMaster();
105  {
106  auto&& closed = Json::addObject(value, jss::closed);
107  addJson(closed, {*master.getClosedLedger(), &context_, 0});
108  }
109  {
110  auto&& open = Json::addObject(value, jss::open);
111  addJson(open, {*master.getCurrentLedger(), &context_, 0});
112  }
113  }
114 }
115 
116 } // namespace RPC
117 } // namespace ripple
118 
119 #endif
ripple::RPC::LedgerHandler
Definition: LedgerHandler.h:49
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::RPC::LedgerHandler::context_
JsonContext & context_
Definition: LedgerHandler.h:80
std::shared_ptr
STL class.
std::vector
STL class.
ripple::RPC::LedgerHandler::ledger_
std::shared_ptr< ReadView const > ledger_
Definition: LedgerHandler.h:81
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
ripple::RPC::LedgerHandler::role
static Role role()
Definition: LedgerHandler.h:68
ripple::RPC::LedgerHandler::condition
static Condition condition()
Definition: LedgerHandler.h:74
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:27
ripple::Application::getLedgerMaster
virtual LedgerMaster & getLedgerMaster()=0
ripple::Role::USER
@ USER
ripple::RPC::NO_CONDITION
@ NO_CONDITION
Definition: Handler.h:40
ripple::RPC::LedgerHandler::name
static char const * name()
Definition: LedgerHandler.h:62
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
ripple::RPC::Condition
Condition
Definition: Handler.h:39
ripple::RPC::Status
Status represents the results of an operation that might fail.
Definition: Status.h:39
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:35
ripple::RPC::LedgerHandler::writeResult
void writeResult(Object &)
Definition: LedgerHandler.h:95
ripple::LedgerEntryType
LedgerEntryType
Identifiers for on-ledger objects.
Definition: LedgerFormats.h:53
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:85
ripple::RPC::LedgerHandler::check
Status check()
Definition: LedgerHandler.cpp:40
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:286
ripple::RPC::LedgerHandler::options_
int options_
Definition: LedgerHandler.h:84
ripple::RPC::LedgerHandler::queueTxs_
std::vector< TxQ::TxDetails > queueTxs_
Definition: LedgerHandler.h:82
ripple::RPC::LedgerHandler::result_
Json::Value result_
Definition: LedgerHandler.h:83
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