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