rippled
Loading...
Searching...
No Matches
Submit.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 <xrpld/app/ledger/LedgerMaster.h>
21#include <xrpld/app/misc/Transaction.h>
22#include <xrpld/app/tx/apply.h>
23#include <xrpld/rpc/Context.h>
24#include <xrpld/rpc/detail/TransactionSign.h>
25#include <xrpl/protocol/ErrorCodes.h>
26#include <xrpl/protocol/RPCErr.h>
27#include <xrpl/resource/Fees.h>
28
29namespace ripple {
30
33{
35 context.params.isMember("fail_hard") &&
36 context.params["fail_hard"].asBool());
37}
38
39// {
40// tx_blob: <string> XOR tx_json: <object>,
41// secret: <secret>
42// }
45{
47
48 if (!context.params.isMember(jss::tx_blob))
49 {
50 auto const failType = getFailHard(context);
51
52 if (context.role != Role::ADMIN && !context.app.config().canSign())
53 return RPC::make_error(
54 rpcNOT_SUPPORTED, "Signing is not supported by this server.");
55
56 auto ret = RPC::transactionSubmit(
57 context.params,
58 context.apiVersion,
59 failType,
60 context.role,
62 context.app,
64
65 ret[jss::deprecated] =
66 "Signing support in the 'submit' command has been "
67 "deprecated and will be removed in a future version "
68 "of the server. Please migrate to a standalone "
69 "signing tool.";
70
71 return ret;
72 }
73
74 Json::Value jvResult;
75
76 auto ret = strUnHex(context.params[jss::tx_blob].asString());
77
78 if (!ret || !ret->size())
80
81 SerialIter sitTrans(makeSlice(*ret));
82
84
85 try
86 {
87 stTx = std::make_shared<STTx const>(std::ref(sitTrans));
88 }
89 catch (std::exception& e)
90 {
91 jvResult[jss::error] = "invalidTransaction";
92 jvResult[jss::error_exception] = e.what();
93
94 return jvResult;
95 }
96
97 {
98 if (!context.app.checkSigs())
100 context.app.getHashRouter(),
101 stTx->getTransactionID(),
103 auto [validity, reason] = checkValidity(
104 context.app.getHashRouter(),
105 *stTx,
106 context.ledgerMaster.getCurrentLedger()->rules(),
107 context.app.config());
108 if (validity != Validity::Valid)
109 {
110 jvResult[jss::error] = "invalidTransaction";
111 jvResult[jss::error_exception] = "fails local checks: " + reason;
112
113 return jvResult;
114 }
115 }
116
117 std::string reason;
118 auto transaction = std::make_shared<Transaction>(stTx, reason, context.app);
119 if (transaction->getStatus() != NEW)
120 {
121 jvResult[jss::error] = "invalidTransaction";
122 jvResult[jss::error_exception] = "fails local checks: " + reason;
123
124 return jvResult;
125 }
126
127 try
128 {
129 auto const failType = getFailHard(context);
130
132 transaction, isUnlimited(context.role), true, failType);
133 }
134 catch (std::exception& e)
135 {
136 jvResult[jss::error] = "internalSubmit";
137 jvResult[jss::error_exception] = e.what();
138
139 return jvResult;
140 }
141
142 try
143 {
144 jvResult[jss::tx_json] = transaction->getJson(JsonOptions::none);
145 jvResult[jss::tx_blob] =
146 strHex(transaction->getSTransaction()->getSerializer().peekData());
147
148 if (temUNCERTAIN != transaction->getResult())
149 {
150 std::string sToken;
151 std::string sHuman;
152
153 transResultInfo(transaction->getResult(), sToken, sHuman);
154
155 jvResult[jss::engine_result] = sToken;
156 jvResult[jss::engine_result_code] = transaction->getResult();
157 jvResult[jss::engine_result_message] = sHuman;
158
159 auto const submitResult = transaction->getSubmitResult();
160
161 jvResult[jss::accepted] = submitResult.any();
162 jvResult[jss::applied] = submitResult.applied;
163 jvResult[jss::broadcast] = submitResult.broadcast;
164 jvResult[jss::queued] = submitResult.queued;
165 jvResult[jss::kept] = submitResult.kept;
166
167 if (auto currentLedgerState = transaction->getCurrentLedgerState())
168 {
169 jvResult[jss::account_sequence_next] =
170 safe_cast<Json::Value::UInt>(
171 currentLedgerState->accountSeqNext);
172 jvResult[jss::account_sequence_available] =
173 safe_cast<Json::Value::UInt>(
174 currentLedgerState->accountSeqAvail);
175 jvResult[jss::open_ledger_cost] =
176 to_string(currentLedgerState->minFeeRequired);
177 jvResult[jss::validated_ledger_index] =
178 safe_cast<Json::Value::UInt>(
179 currentLedgerState->validatedLedger);
180 }
181 }
182
183 return jvResult;
184 }
185 catch (std::exception& e)
186 {
187 jvResult[jss::error] = "internalJson";
188 jvResult[jss::error_exception] = e.what();
189
190 return jvResult;
191 }
192}
193
194} // namespace ripple
Represents a JSON value.
Definition: json_value.h:148
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:475
bool asBool() const
Definition: json_value.cpp:625
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:949
virtual Config & config()=0
virtual bool checkSigs() const =0
virtual HashRouter & getHashRouter()=0
bool canSign() const
Definition: Config.h:349
std::shared_ptr< ReadView const > getCurrentLedger()
std::chrono::seconds getValidatedLedgerAge()
static FailHard doFailHard(bool noMeansDont)
Definition: NetworkOPs.h:93
virtual void processTransaction(std::shared_ptr< Transaction > &transaction, bool bUnlimited, bool bLocal, FailHard failType)=0
Process transactions as they arrive from the network or which are submitted by clients.
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
Definition: ErrorCodes.cpp:185
Json::Value transactionSubmit(Json::Value jvRequest, unsigned apiVersion, NetworkOPs::FailHard failType, Role role, std::chrono::seconds validatedLedgerAge, Application &app, ProcessTransactionFn const &processTransaction)
Returns a Json::objectValue.
ProcessTransactionFn getProcessTxnFn(NetworkOPs &netOPs)
Charge const feeMediumBurdenRPC
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
@ rpcNOT_SUPPORTED
Definition: ErrorCodes.h:132
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:84
static NetworkOPs::FailHard getFailHard(RPC::JsonContext const &context)
Definition: Submit.cpp:32
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:31
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition: Role.cpp:125
Json::Value doSubmit(RPC::JsonContext &)
Definition: Submit.cpp:44
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:30
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition: Slice.h:244
void forceValidity(HashRouter &router, uint256 const &txid, Validity validity)
Sets the validity of a given transaction in the cache.
Definition: apply.cpp:89
@ Valid
Signature and local checks are good / passed.
@ SigGoodOnly
Signature is good, but local checks fail.
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:630
std::pair< Validity, std::string > checkValidity(HashRouter &router, STTx const &tx, Rules const &rules, Config const &config)
Checks transaction signature and local checks.
Definition: apply.cpp:37
bool transResultInfo(TER code, std::string &token, std::string &text)
Definition: TER.cpp:242
@ temUNCERTAIN
Definition: TER.h:123
T ref(T... args)
unsigned int apiVersion
Definition: Context.h:48
Resource::Charge & loadType
Definition: Context.h:41
Application & app
Definition: Context.h:40
LedgerMaster & ledgerMaster
Definition: Context.h:43
NetworkOPs & netOps
Definition: Context.h:42
Json::Value params
Definition: Context.h:62
T what(T... args)