Skip to content

Commit 627c3c0

Browse files
committed
Merge #10999: Fix amounts formatting in decoderawtransaction
ce07638 doc: Add comment to use ValueFromAmount/AmountFromValue for JSON, not utilmoneystr (Wladimir J. van der Laan) ec05c50 rpc: Use ValueFromAmount instead of FormatMoney in TxToUniv (Wladimir J. van der Laan) 46347ad rpc: Move ValueFromAmount to core_write (Wladimir J. van der Laan) dac3782 doc: Correct AmountFromValue/ValueFromAmount names (Wladimir J. van der Laan) Pull request description: With this, the amounts returned in `decoderawtransaction` will be padded to 8 digits like anywhere else in the API. This is accomplished by using `ValueFromAmount` in `TxToUniv`, instead of `FormatMoney` which it currently (mistakingly) uses. The `FormatMoney` function is only for debugging/logging use! To avoid dependency issues, `ValueFromAmount` is moved to `core_write.cpp`, where it also fits better. I don't move `AmountFromValue` to `core_read.cpp` at the same time, as this would have more impact due to the RPCError dependency there. (n.b.: large number of changed files is solely due to the util_tests JSONs needing update) Tree-SHA512: 10fc2d27d33a77dbcb57aa7eccd4f53110c05d38eb7df6d40f10f14c08fad4274472e93af75aa59fe68ad0720fdf0930f0108124abef518e0dd162b3d2b2b292
2 parents fa8a063 + ce07638 commit 627c3c0

30 files changed

+46
-39
lines changed

doc/developer-notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,10 +561,10 @@ A few guidelines for introducing and reviewing new RPC interfaces:
561561
which is error prone, and it is easy to get things such as escaping wrong.
562562
JSON already supports nested data structures, no need to re-invent the wheel.
563563

564-
- *Exception*: AmountToValue can parse amounts as string. This was introduced because many JSON
564+
- *Exception*: AmountFromValue can parse amounts as string. This was introduced because many JSON
565565
parsers and formatters hard-code handling decimal numbers as floating point
566566
values, resulting in potential loss of precision. This is unacceptable for
567-
monetary values. **Always** use `AmountToValue` and `ValueToAmount` when
567+
monetary values. **Always** use `AmountFromValue` and `ValueFromAmount` when
568568
inputting or outputting monetary values. The only exceptions to this are
569569
`prioritisetransaction` and `getblocktemplate` because their interface
570570
is specified as-is in BIP22.

src/core_io.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_CORE_IO_H
66
#define BITCOIN_CORE_IO_H
77

8+
#include "amount.h"
9+
810
#include <string>
911
#include <vector>
1012

@@ -25,6 +27,7 @@ uint256 ParseHashStr(const std::string&, const std::string& strName);
2527
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);
2628

2729
// core_write.cpp
30+
UniValue ValueFromAmount(const CAmount& amount);
2831
std::string FormatScript(const CScript& script);
2932
std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0);
3033
void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex);

src/core_write.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
#include "utilmoneystr.h"
1717
#include "utilstrencodings.h"
1818

19+
UniValue ValueFromAmount(const CAmount& amount)
20+
{
21+
bool sign = amount < 0;
22+
int64_t n_abs = (sign ? -amount : amount);
23+
int64_t quotient = n_abs / COIN;
24+
int64_t remainder = n_abs % COIN;
25+
return UniValue(UniValue::VNUM,
26+
strprintf("%s%d.%08d", sign ? "-" : "", quotient, remainder));
27+
}
28+
1929
std::string FormatScript(const CScript& script)
2030
{
2131
std::string ret;
@@ -184,8 +194,7 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
184194

185195
UniValue out(UniValue::VOBJ);
186196

187-
UniValue outValue(UniValue::VNUM, FormatMoney(txout.nValue));
188-
out.pushKV("value", outValue);
197+
out.pushKV("value", ValueFromAmount(txout.nValue));
189198
out.pushKV("n", (int64_t)i);
190199

191200
UniValue o(UniValue::VOBJ);

src/rpc/misc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "base58.h"
77
#include "chain.h"
88
#include "clientversion.h"
9+
#include "core_io.h"
910
#include "init.h"
1011
#include "validation.h"
1112
#include "httpserver.h"

src/rpc/net.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "chainparams.h"
88
#include "clientversion.h"
9+
#include "core_io.h"
910
#include "validation.h"
1011
#include "net.h"
1112
#include "net_processing.h"

src/rpc/server.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,6 @@ CAmount AmountFromValue(const UniValue& value)
123123
return amount;
124124
}
125125

126-
UniValue ValueFromAmount(const CAmount& amount)
127-
{
128-
bool sign = amount < 0;
129-
int64_t n_abs = (sign ? -amount : amount);
130-
int64_t quotient = n_abs / COIN;
131-
int64_t remainder = n_abs % COIN;
132-
return UniValue(UniValue::VNUM,
133-
strprintf("%s%d.%08d", sign ? "-" : "", quotient, remainder));
134-
}
135-
136126
uint256 ParseHashV(const UniValue& v, std::string strName)
137127
{
138128
std::string strHex;

src/rpc/server.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ extern std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strNa
185185
extern std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey);
186186

187187
extern CAmount AmountFromValue(const UniValue& value);
188-
extern UniValue ValueFromAmount(const CAmount& amount);
189188
extern std::string HelpExampleCli(const std::string& methodname, const std::string& args);
190189
extern std::string HelpExampleRpc(const std::string& methodname, const std::string& args);
191190

src/test/rpc_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "rpc/client.h"
77

88
#include "base58.h"
9+
#include "core_io.h"
910
#include "netbase.h"
1011

1112
#include "test/test_bitcoin.h"

src/utilmoneystr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
#include "amount.h"
1616

17+
/* Do not use these functions to represent or parse monetary amounts to or from
18+
* JSON but use AmountFromValue and ValueFromAmount for that.
19+
*/
1720
std::string FormatMoney(const CAmount& n);
1821
bool ParseMoney(const std::string& str, CAmount& nRet);
1922
bool ParseMoney(const char* pszIn, CAmount& nRet);

test/util/data/tt-delin1-out.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@
189189
],
190190
"vout": [
191191
{
192-
"value": 1.3782,
192+
"value": 1.37820000,
193193
"n": 0,
194194
"scriptPubKey": {
195195
"asm": "OP_DUP OP_HASH160 8fd139bb39ced713f231c58a4d07bf6954d1c201 OP_EQUALVERIFY OP_CHECKSIG",

0 commit comments

Comments
 (0)