From 3dab797fa2447a21d1dbbc8ffcb884a31133611b Mon Sep 17 00:00:00 2001 From: Richard Holland Date: Fri, 22 Apr 2022 10:36:53 +0000 Subject: [PATCH] add error message, address isAddable xrp edgecases --- src/ripple/protocol/STAmount.h | 18 ++++++++++++++++++ src/ripple/protocol/impl/TER.cpp | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/ripple/protocol/STAmount.h b/src/ripple/protocol/STAmount.h index c956f96ed..82dd45604 100644 --- a/src/ripple/protocol/STAmount.h +++ b/src/ripple/protocol/STAmount.h @@ -530,15 +530,33 @@ isFakeXRP(STAmount const& amount) inline bool isAddable(STAmount const& amt1, STAmount const& amt2) { + // special case: adding anything to zero is always fine if (amt1 == beast::zero || amt2 == beast::zero) return true; + // special case: adding two xrp amounts together. + // this is just an overflow check + if (isXRP(amt1) && isXRP(amt2)) + { + XRPAmount A = (amt1.signum() == -1 ? -(amt1.xrp()) : amt1.xrp()); + XRPAmount B = (amt2.signum() == -1 ? -(amt2.xrp()) : amt2.xrp()); + + XRPAmount finalAmt = A + B; + return (finalAmt >= A && finalAmt >= B); + } + static const STAmount one {IOUAmount{1, 0}, noIssue()}; static const STAmount maxLoss {IOUAmount{1, -4}, noIssue()}; STAmount A = amt1; STAmount B = amt2; + if (isXRP(A)) + A = STAmount{IOUAmount{A.xrp().drops(), -6}, noIssue()}; + + if (isXRP(B)) + B = STAmount{IOUAmount{B.xrp().drops(), -6}, noIssue()}; + A.setIssue(noIssue()); B.setIssue(noIssue()); diff --git a/src/ripple/protocol/impl/TER.cpp b/src/ripple/protocol/impl/TER.cpp index c660b1cea..7989e6b2d 100644 --- a/src/ripple/protocol/impl/TER.cpp +++ b/src/ripple/protocol/impl/TER.cpp @@ -88,7 +88,7 @@ transResults() MAKE_ERROR(tecINSUFFICIENT_FUNDS, "Not enough funds available to complete requested transaction."), MAKE_ERROR(tecOBJECT_NOT_FOUND, "A requested object could not be located."), MAKE_ERROR(tecINSUFFICIENT_PAYMENT, "The payment is not sufficient."), - + MAKE_ERROR(tecPRECISION_LOSS, "The IOU amounts used by the transaction cannot interact."), MAKE_ERROR(tefALREADY, "The exact transaction was already in this ledger."), MAKE_ERROR(tefBAD_ADD_AUTH, "Not authorized to add account."), MAKE_ERROR(tefBAD_AUTH, "Transaction's public key is not authorized."),