Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simulate RPC to dry-run a tx (XLS-69d) #5069

Open
wants to merge 75 commits into
base: develop
Choose a base branch
from

Conversation

mvadari
Copy link
Collaborator

@mvadari mvadari commented Jul 18, 2024

High Level Overview of Change

This PR adds a new API method, titled simulate, which executes a dry run of a transaction submission.

This PR also fixes #5070.

Context of Change

It is useful to take a transaction, simulate execution it in the current ledger, and return the metadata - but not persist the transaction in the ledger. This can be used for testing, analysis, and more.

XLS spec: XRPLF/XRPL-Standards#207

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking change that only restructures code)
  • Performance (increase or change in throughput and/or latency)
  • Tests (you added tests for code that already exists, or your new feature included in this PR)
  • Documentation update
  • Chore (no impact to binary, e.g. .gitignore, formatting, dropping support for older tooling)
  • Release

API Impact

  • Public API: New feature (new methods and/or new fields)
  • Public API: Breaking change (in general, breaking changes should only impact the next api_version)
  • libxrpl change (any change that may affect libxrpl or dependents of libxrpl)
  • Peer protocol change (must be backward compatible or bump the peer protocol version)

Test Plan

Testing is still in progress. Unit tests are and will be added.

Current Status

This PR is complete and ready for review. You can build this branch and sync with the network of your choice (including Mainnet). The public is welcome to test and use this code (at your own risk). Next steps are code review and QA testing.

Copy link

codecov bot commented Jul 18, 2024

Codecov Report

Attention: Patch coverage is 99.03537% with 3 lines in your changes missing coverage. Please review.

Project coverage is 78.1%. Comparing base (ccc0889) to head (705ba58).

Files with missing lines Patch % Lines
src/xrpld/app/tx/detail/apply.cpp 90.0% 1 Missing ⚠️
src/xrpld/rpc/detail/TransactionSign.cpp 98.8% 1 Missing ⚠️
src/xrpld/rpc/handlers/Simulate.cpp 99.3% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##           develop   #5069     +/-   ##
=========================================
+ Coverage     78.0%   78.1%   +0.1%     
=========================================
  Files          789     790      +1     
  Lines        66954   67162    +208     
  Branches      8108    8105      -3     
=========================================
+ Hits         52218   52434    +216     
+ Misses       14736   14728      -8     
Files with missing lines Coverage Δ
include/xrpl/protocol/ErrorCodes.h 100.0% <ø> (ø)
src/libxrpl/protocol/ErrorCodes.cpp 85.7% <ø> (ø)
src/libxrpl/protocol/InnerObjectFormats.cpp 100.0% <100.0%> (ø)
src/xrpld/app/ledger/detail/BuildLedger.cpp 91.4% <100.0%> (ø)
src/xrpld/app/ledger/detail/LedgerMaster.cpp 44.0% <100.0%> (+0.1%) ⬆️
src/xrpld/app/ledger/detail/OpenLedger.cpp 73.4% <100.0%> (+0.3%) ⬆️
src/xrpld/app/main/Main.cpp 79.8% <ø> (ø)
src/xrpld/app/misc/NetworkOPs.cpp 70.1% <100.0%> (+<0.1%) ⬆️
src/xrpld/app/misc/TxQ.h 98.2% <ø> (ø)
src/xrpld/app/misc/detail/TxQ.cpp 98.9% <100.0%> (+0.1%) ⬆️
... and 18 more

... and 8 files with indirect coverage changes

Impacted file tree graph

@mvadari mvadari requested review from godexsoft and removed request for godexsoft July 18, 2024 21:49
struct ApplyResult
{
TER ter;
bool applied;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add default values: false / tesSuccess looks good

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with this - it feels more likely that bugs may be accidentally introduced if there are default values, due to forgetting to initialize them correctly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What the difference between bugs with initialized and bugs with randomly initialized values? You can't distinguish the values. But with initialized values you can save some code space.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding of structs is that it will throw a compiler error if you don't specify them when constructing them. Is that incorrect?

@@ -630,7 +630,8 @@ class Simulate_test : public beast::unit_test::suite
auto validateOutput = [&](Json::Value const& resp,
Json::Value const& tx) {
auto result = resp[jss::result];
checkBasicReturnValidity(result, tx, 5, "20");
checkBasicReturnValidity(
result, tx, 5, env.current()->fees().base * 2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change 5 -> seq()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 0bbf589

[&](Json::Value const& resp, Json::Value const& tx) {
auto result = resp[jss::result];
checkBasicReturnValidity(
result, tx, 3, env.current()->fees().base);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change 3 to seq()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 0bbf589

tx[sfDomain] = newDomain;

// test with autofill
testTx(env, tx, testSimulation, 3);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last parameter must be bool

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 0bbf589

Json::Value const& tx) {
auto result = resp[jss::result];
checkBasicReturnValidity(
result, tx, 5, env.current()->fees().base * 2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change 5 to seq()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 0bbf589

else
{
metadata = result[jss::meta];
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add new line after bracket

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 0bbf589

Json::Value const& tx) {
auto result = resp[jss::result];
checkBasicReturnValidity(
result, tx, 4, env.current()->fees().base);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change 4 -> seq() value / const

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 0bbf589

{
TER ter;
bool applied;
std::optional<TxMeta> metadata = std::nullopt;
Copy link
Collaborator

@oleks-rip oleks-rip Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This initialization is unnecessary as default constructor will do the same.

if (!tx_json.isMember(jss::TransactionType))
{
return RPC::missing_field_error("tx.TransactionType");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add new line

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 0bbf589

return jvResult;
}

std::shared_ptr<STTx const> stpTrans;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what stp is for? "stop"? could you please rename it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 0bbf589

}

std::string reason;
auto tpTrans = std::make_shared<Transaction>(stpTrans, reason, context.app);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tpTrans is unclear name, could you please rename it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was due to copy-paste, fixed in 0bbf589

namespace ripple {

std::optional<Json::Value>
autofillTx(Json::Value& tx_json, RPC::JsonContext& context)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functions that are not exported - please make them static or put into anonymous namespace.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0bbf589

namespace ripple {

std::optional<Json::Value>
autofillTx(Json::Value& tx_json, RPC::JsonContext& context)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is too big and can be divided into smaller functions. Please consider not to create functions more than ~50 lines without good reason.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made some improvements in 0bbf589

// binary: <bool>
// }
Json::Value
doSimulate(RPC::JsonContext& context)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is too big, please divide it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried making some improvements, but it only made it more convoluted.

// Convert the TER to human-readable values
std::string sToken;
std::string sHuman;
transResultInfo(result.ter, sToken, sHuman);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some actions if transResultInfo return false. Like return error or make some default error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0bbf589

context.params.get(jss::binary, false).asBool();

// Convert the TER to human-readable values
std::string sToken;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use Hungarian notation, we are moving out from it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0bbf589 - was copy-pasted

{
static const std::string alternateSuccessMessage =
"The simulated transaction would have been applied.";
jvResult[jss::engine_result_message] = alternateSuccessMessage;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just
jvResult[jss::engine_result_message] = "The simulated transaction would have been applied."; ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0bbf589

jvResult[jss::meta] =
result.metadata->getJson(JsonOptions::none);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add new line

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0bbf589

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sign-and-submit mode doesn't autofill the correct tx fee (Version: rippled 2.1.0)
4 participants