rippled
TransactionEntry.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2014 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/main/Application.h>
22 #include <ripple/app/misc/DeliverMax.h>
23 #include <ripple/ledger/ReadView.h>
24 #include <ripple/protocol/jss.h>
25 #include <ripple/rpc/Context.h>
26 #include <ripple/rpc/impl/RPCHelpers.h>
27 
28 namespace ripple {
29 
30 // {
31 // ledger_hash : <ledger>,
32 // ledger_index : <ledger_index>
33 // }
34 //
35 // XXX In this case, not specify either ledger does not mean ledger current. It
36 // means any ledger.
39 {
41  Json::Value jvResult = RPC::lookupLedger(lpLedger, context);
42 
43  if (!lpLedger)
44  return jvResult;
45 
46  if (!context.params.isMember(jss::tx_hash))
47  {
48  jvResult[jss::error] = "fieldNotFoundTransaction";
49  }
50  else if (jvResult.get(jss::ledger_hash, Json::nullValue).isNull())
51  {
52  // We don't work on ledger current.
53 
54  // XXX We don't support any transaction yet.
55  jvResult[jss::error] = "notYetImplemented";
56  }
57  else
58  {
59  uint256 uTransID;
60  // XXX Relying on trusted WSS client. Would be better to have a strict
61  // routine, returning success or failure.
62  if (!uTransID.parseHex(context.params[jss::tx_hash].asString()))
63  {
64  jvResult[jss::error] = "malformedRequest";
65  return jvResult;
66  }
67 
68  auto [sttx, stobj] = lpLedger->txRead(uTransID);
69  if (!sttx)
70  {
71  jvResult[jss::error] = "transactionNotFound";
72  }
73  else
74  {
75  if (context.apiVersion > 1)
76  {
77  jvResult[jss::tx_json] =
78  sttx->getJson(JsonOptions::disable_API_prior_V2);
79  jvResult[jss::hash] = to_string(sttx->getTransactionID());
80 
81  if (!lpLedger->open())
82  jvResult[jss::ledger_hash] = to_string(
83  context.ledgerMaster.getHashBySeq(lpLedger->seq()));
84 
85  bool const validated =
86  context.ledgerMaster.isValidated(*lpLedger);
87 
88  jvResult[jss::validated] = validated;
89  if (validated)
90  {
91  jvResult[jss::ledger_index] = lpLedger->seq();
92  if (auto closeTime = context.ledgerMaster.getCloseTimeBySeq(
93  lpLedger->seq()))
94  jvResult[jss::close_time_iso] =
95  to_string_iso(*closeTime);
96  }
97  }
98  else
99  jvResult[jss::tx_json] = sttx->getJson(JsonOptions::none);
100 
102  jvResult[jss::tx_json], sttx->getTxnType(), context.apiVersion);
103 
104  auto const json_meta =
105  (context.apiVersion > 1 ? jss::meta : jss::metadata);
106  if (stobj)
107  jvResult[json_meta] = stobj->getJson(JsonOptions::none);
108  // 'accounts'
109  // 'engine_...'
110  // 'ledger_...'
111  }
112  }
113 
114  return jvResult;
115 }
116 
117 } // namespace ripple
ripple::JsonOptions::disable_API_prior_V2
@ disable_API_prior_V2
Definition: STBase.h:44
ripple::RPC::JsonContext
Definition: Context.h:53
std::shared_ptr
STL class.
ripple::RPC::insertDeliverMax
void insertDeliverMax(Json::Value &tx_json, TxType txnType, unsigned int apiVersion)
Copy Amount field to DeliverMax field in transaction output JSON.
Definition: DeliverMax.cpp:28
Json::Value::get
Value get(UInt index, const Value &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
Definition: json_value.cpp:834
ripple::RPC::Context::ledgerMaster
LedgerMaster & ledgerMaster
Definition: Context.h:45
ripple::to_string_iso
std::string to_string_iso(date::sys_time< Duration > tp)
Definition: chrono.h:93
Json::Value::isNull
bool isNull() const
isNull() tests to see if this field is null.
Definition: json_value.cpp:967
ripple::RPC::lookupLedger
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext &context, Json::Value &result)
Look up a ledger from a request and fill a Json::Result with the data representing a ledger.
Definition: RPCHelpers.cpp:623
ripple::base_uint< 256 >
ripple::ReadView::txRead
virtual tx_type txRead(key_type const &key) const =0
Read a transaction from the tx map.
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:932
ripple::doTransactionEntry
Json::Value doTransactionEntry(RPC::JsonContext &)
Definition: TransactionEntry.cpp:38
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::JsonOptions::none
@ none
Definition: STBase.h:42
ripple::LedgerMaster::getCloseTimeBySeq
std::optional< NetClock::time_point > getCloseTimeBySeq(LedgerIndex ledgerIndex)
Definition: LedgerMaster.cpp:1766
ripple::LedgerMaster::isValidated
bool isValidated(ReadView const &ledger)
Definition: LedgerMaster.cpp:602
ripple::ReadView::seq
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition: ReadView.h:122
ripple::RPC::Context::apiVersion
unsigned int apiVersion
Definition: Context.h:50
Json::nullValue
@ nullValue
'null' value
Definition: json_value.h:36
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::base_uint::parseHex
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:496
ripple::LedgerMaster::getHashBySeq
uint256 getHashBySeq(std::uint32_t index)
Get a ledger's hash by sequence number using the cache.
Definition: LedgerMaster.cpp:1796
ripple::ReadView::open
virtual bool open() const =0
Returns true if this reflects an open ledger.
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
Json::Value
Represents a JSON value.
Definition: json_value.h:145
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469