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/HashRouter.h>
22#include <xrpld/app/misc/Transaction.h>
23#include <xrpld/app/tx/apply.h>
24#include <xrpld/rpc/Context.h>
25#include <xrpld/rpc/GRPCHandlers.h>
26#include <xrpld/rpc/detail/RPCHelpers.h>
27#include <xrpld/rpc/detail/TransactionSign.h>
28#include <xrpl/protocol/ErrorCodes.h>
29#include <xrpl/protocol/RPCErr.h>
30#include <xrpl/resource/Fees.h>
31
32namespace ripple {
33
36{
38 context.params.isMember("fail_hard") &&
39 context.params["fail_hard"].asBool());
40}
41
42// {
43// tx_blob: <string> XOR tx_json: <object>,
44// secret: <secret>
45// }
48{
50
51 if (!context.params.isMember(jss::tx_blob))
52 {
53 auto const failType = getFailHard(context);
54
55 if (context.role != Role::ADMIN && !context.app.config().canSign())
56 return RPC::make_error(
57 rpcNOT_SUPPORTED, "Signing is not supported by this server.");
58
59 auto ret = RPC::transactionSubmit(
60 context.params,
61 context.apiVersion,
62 failType,
63 context.role,
65 context.app,
67
68 ret[jss::deprecated] =
69 "Signing support in the 'submit' command has been "
70 "deprecated and will be removed in a future version "
71 "of the server. Please migrate to a standalone "
72 "signing tool.";
73
74 return ret;
75 }
76
77 Json::Value jvResult;
78
79 auto ret = strUnHex(context.params[jss::tx_blob].asString());
80
81 if (!ret || !ret->size())
83
84 SerialIter sitTrans(makeSlice(*ret));
85
87
88 try
89 {
90 stTx = std::make_shared<STTx const>(std::ref(sitTrans));
91 }
92 catch (std::exception& e)
93 {
94 jvResult[jss::error] = "invalidTransaction";
95 jvResult[jss::error_exception] = e.what();
96
97 return jvResult;
98 }
99
100 {
101 if (!context.app.checkSigs())
103 context.app.getHashRouter(),
104 stTx->getTransactionID(),
106 auto [validity, reason] = checkValidity(
107 context.app.getHashRouter(),
108 *stTx,
109 context.ledgerMaster.getCurrentLedger()->rules(),
110 context.app.config());
111 if (validity != Validity::Valid)
112 {
113 jvResult[jss::error] = "invalidTransaction";
114 jvResult[jss::error_exception] = "fails local checks: " + reason;
115
116 return jvResult;
117 }
118 }
119
120 std::string reason;
121 auto transaction = std::make_shared<Transaction>(stTx, reason, context.app);
122 if (transaction->getStatus() != NEW)
123 {
124 jvResult[jss::error] = "invalidTransaction";
125 jvResult[jss::error_exception] = "fails local checks: " + reason;
126
127 return jvResult;
128 }
129
130 try
131 {
132 auto const failType = getFailHard(context);
133
135 transaction, isUnlimited(context.role), true, failType);
136 }
137 catch (std::exception& e)
138 {
139 jvResult[jss::error] = "internalSubmit";
140 jvResult[jss::error_exception] = e.what();
141
142 return jvResult;
143 }
144
145 try
146 {
147 jvResult[jss::tx_json] = transaction->getJson(JsonOptions::none);
148 jvResult[jss::tx_blob] =
149 strHex(transaction->getSTransaction()->getSerializer().peekData());
150
151 if (temUNCERTAIN != transaction->getResult())
152 {
153 std::string sToken;
154 std::string sHuman;
155
156 transResultInfo(transaction->getResult(), sToken, sHuman);
157
158 jvResult[jss::engine_result] = sToken;
159 jvResult[jss::engine_result_code] = transaction->getResult();
160 jvResult[jss::engine_result_message] = sHuman;
161
162 auto const submitResult = transaction->getSubmitResult();
163
164 jvResult[jss::accepted] = submitResult.any();
165 jvResult[jss::applied] = submitResult.applied;
166 jvResult[jss::broadcast] = submitResult.broadcast;
167 jvResult[jss::queued] = submitResult.queued;
168 jvResult[jss::kept] = submitResult.kept;
169
170 if (auto currentLedgerState = transaction->getCurrentLedgerState())
171 {
172 jvResult[jss::account_sequence_next] =
173 safe_cast<Json::Value::UInt>(
174 currentLedgerState->accountSeqNext);
175 jvResult[jss::account_sequence_available] =
176 safe_cast<Json::Value::UInt>(
177 currentLedgerState->accountSeqAvail);
178 jvResult[jss::open_ledger_cost] =
179 to_string(currentLedgerState->minFeeRequired);
180 jvResult[jss::validated_ledger_index] =
181 safe_cast<Json::Value::UInt>(
182 currentLedgerState->validatedLedger);
183 }
184 }
185
186 return jvResult;
187 }
188 catch (std::exception& e)
189 {
190 jvResult[jss::error] = "internalJson";
191 jvResult[jss::error_exception] = e.what();
192
193 return jvResult;
194 }
195}
196
197} // namespace ripple
Represents a JSON value.
Definition: json_value.h:147
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469
bool asBool() const
Definition: json_value.cpp:619
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:943
virtual Config & config()=0
virtual bool checkSigs() const =0
virtual HashRouter & getHashRouter()=0
bool canSign() const
Definition: Config.h:356
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:181
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:35
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition: Role.cpp:124
Json::Value doSubmit(RPC::JsonContext &)
Definition: Submit.cpp:47
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:243
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:629
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:236
@ temUNCERTAIN
Definition: TER.h:123
T ref(T... args)
unsigned int apiVersion
Definition: Context.h:50
Resource::Charge & loadType
Definition: Context.h:43
Application & app
Definition: Context.h:42
LedgerMaster & ledgerMaster
Definition: Context.h:45
NetworkOPs & netOps
Definition: Context.h:44
Json::Value params
Definition: Context.h:64
T what(T... args)