-
Notifications
You must be signed in to change notification settings - Fork 1.7k
finish fn result refactor #7947
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
base: ripple/smart-escrow
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -35,7 +35,6 @@ | |||||
| #include <xrpl/tx/wasm/WasmVM.h> | ||||||
|
|
||||||
| #include <cstdint> | ||||||
| #include <limits> | ||||||
| #include <memory> | ||||||
| #include <optional> | ||||||
| #include <string> | ||||||
|
|
@@ -417,42 +416,50 @@ EscrowFinish::doApply() | |||||
| return tecINTERNAL; | ||||||
| } | ||||||
| std::uint32_t const allowance = ctx_.tx[sfGas]; | ||||||
| auto re = runEscrowWasm(wasm, ledgerDataProvider, allowance, escrowFunctionName); | ||||||
| auto const re = runEscrowWasm(wasm, ledgerDataProvider, allowance, escrowFunctionName); | ||||||
| JLOG(j_.trace()) << "Escrow WASM ran"; | ||||||
|
|
||||||
| if (auto const& data = ledgerDataProvider.getData(); data.has_value()) | ||||||
| // Gas consumed, reported in the tx metadata whenever the engine has a | ||||||
| // trustworthy number: a completed run, out of gas, or a wasm fault. | ||||||
| std::optional<std::int64_t> const cost = | ||||||
| re.has_value() ? std::optional<std::int64_t>{re->cost} : re.error().cost; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, fixed |
||||||
| if (cost.has_value()) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason you switched the order of checks here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Data is only stored when the fn ran to completion. before, on out of gas or a trap, we set the data and then discarded it. There is no behavior change. |
||||||
| { | ||||||
| if (data->size() > kMaxWasmDataLength) | ||||||
| { | ||||||
| // should already be checked in the updateData host function | ||||||
| // The engine cannot spend more than it was given, and pins the cost | ||||||
| // to the allowance when it runs out. | ||||||
| if (*cost < 0 || *cost > allowance) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Can we change the cost to be unsigned to avoid this need for checking for a negative gas cost?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like unsigned too. But cost is int64 in the engine, that is the wasmi-host-functions branch. The change should come from there. |
||||||
| return tecINTERNAL; // LCOV_EXCL_LINE | ||||||
| } | ||||||
| slep->setFieldVL(sfData, makeSlice(*data)); | ||||||
| ctx_.view().update(slep); | ||||||
| ctx_.setGasUsed(static_cast<std::uint32_t>(*cost)); | ||||||
| } | ||||||
|
|
||||||
| if (re.has_value()) | ||||||
| if (!re.has_value()) | ||||||
| { | ||||||
| auto const reValue = re.value().result; | ||||||
| auto const reCost = re.value().cost; | ||||||
| JLOG(j_.debug()) << "WASM Success: " + std::to_string(reValue) << ", cost: " << reCost; | ||||||
| // No return code, and any data it wrote goes away with the view. | ||||||
| JLOG(j_.debug()) << "WASM Failure: " + transHuman(re.error().ter); | ||||||
| return re.error().ter; | ||||||
| } | ||||||
|
|
||||||
| ctx_.setVMReturnCode(reValue); | ||||||
| auto const reValue = re->result; | ||||||
| JLOG(j_.debug()) << "WASM Success: " + std::to_string(reValue) << ", cost: " << re->cost; | ||||||
|
|
||||||
| if (reCost < 0 || reCost > std::numeric_limits<uint32_t>::max()) | ||||||
| return tecINTERNAL; // LCOV_EXCL_LINE | ||||||
| ctx_.setGasUsed(static_cast<uint32_t>(reCost)); | ||||||
| ctx_.setVMReturnCode(reValue); | ||||||
|
|
||||||
| if (reValue <= 0) | ||||||
| // Only matters on a reject, where the escrow survives: | ||||||
| // Transactor::processPersistentChanges replays this after the reset. | ||||||
| if (auto const& data = ledgerDataProvider.getData(); data.has_value()) | ||||||
| { | ||||||
| if (data->size() > kMaxWasmDataLength) | ||||||
| { | ||||||
| return tecBYTECODE_REJECTED; | ||||||
| // should already be checked in the updateData host function | ||||||
| return tecINTERNAL; // LCOV_EXCL_LINE | ||||||
| } | ||||||
| slep->setFieldVL(sfData, makeSlice(*data)); | ||||||
| ctx_.view().update(slep); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| JLOG(j_.debug()) << "WASM Failure: " + transHuman(re.error().ter); | ||||||
| return re.error().ter; | ||||||
| } | ||||||
|
|
||||||
| // 0 or negative is a contract-defined reject code, reported as sfVMReturnCode. | ||||||
| if (reValue <= 0) | ||||||
| return tecBYTECODE_REJECTED; | ||||||
| } | ||||||
|
|
||||||
| AccountID const account = (*slep)[sfAccount]; | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think there might be some confusing behavior between this and this line above
view_->setVMReturnCode(*vmReturnCode_);?For instance there is a wasm return code but there is no gas used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. It can happen when the wasm runs fine but later logic returns tecINTERNAL.
fixed, now tecINTERNAL sets neither.