aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2017-08-08 11:27:15 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2017-08-08 11:27:29 +0200
commit627c3c0e495b30af39a8cd8b3336770a6031097b (patch)
tree72eec7e72e54f6ca15eb1a36c43a7f5cda2469b0 /src
parentfa8a0639f7b0ce04030b72b4d5be4f0aa36fc5cb (diff)
parentce076383a8578626a7eac37533cba26dece1c877 (diff)
downloadbitcoin-627c3c0e495b30af39a8cd8b3336770a6031097b.tar.xz
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
Diffstat (limited to 'src')
-rw-r--r--src/core_io.h3
-rw-r--r--src/core_write.cpp13
-rw-r--r--src/rpc/misc.cpp1
-rw-r--r--src/rpc/net.cpp1
-rw-r--r--src/rpc/server.cpp10
-rw-r--r--src/rpc/server.h1
-rw-r--r--src/test/rpc_tests.cpp1
-rw-r--r--src/utilmoneystr.h3
8 files changed, 20 insertions, 13 deletions
diff --git a/src/core_io.h b/src/core_io.h
index 2d63be5fc4..3f25faf0ec 100644
--- a/src/core_io.h
+++ b/src/core_io.h
@@ -5,6 +5,8 @@
#ifndef BITCOIN_CORE_IO_H
#define BITCOIN_CORE_IO_H
+#include "amount.h"
+
#include <string>
#include <vector>
@@ -25,6 +27,7 @@ uint256 ParseHashStr(const std::string&, const std::string& strName);
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);
// core_write.cpp
+UniValue ValueFromAmount(const CAmount& amount);
std::string FormatScript(const CScript& script);
std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0);
void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex);
diff --git a/src/core_write.cpp b/src/core_write.cpp
index a366ef933c..217b491a0d 100644
--- a/src/core_write.cpp
+++ b/src/core_write.cpp
@@ -16,6 +16,16 @@
#include "utilmoneystr.h"
#include "utilstrencodings.h"
+UniValue ValueFromAmount(const CAmount& amount)
+{
+ bool sign = amount < 0;
+ int64_t n_abs = (sign ? -amount : amount);
+ int64_t quotient = n_abs / COIN;
+ int64_t remainder = n_abs % COIN;
+ return UniValue(UniValue::VNUM,
+ strprintf("%s%d.%08d", sign ? "-" : "", quotient, remainder));
+}
+
std::string FormatScript(const CScript& script)
{
std::string ret;
@@ -184,8 +194,7 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
UniValue out(UniValue::VOBJ);
- UniValue outValue(UniValue::VNUM, FormatMoney(txout.nValue));
- out.pushKV("value", outValue);
+ out.pushKV("value", ValueFromAmount(txout.nValue));
out.pushKV("n", (int64_t)i);
UniValue o(UniValue::VOBJ);
diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp
index f3c86038a3..cd93919eb5 100644
--- a/src/rpc/misc.cpp
+++ b/src/rpc/misc.cpp
@@ -6,6 +6,7 @@
#include "base58.h"
#include "chain.h"
#include "clientversion.h"
+#include "core_io.h"
#include "init.h"
#include "validation.h"
#include "httpserver.h"
diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp
index b4d6795e62..6271e0cc83 100644
--- a/src/rpc/net.cpp
+++ b/src/rpc/net.cpp
@@ -6,6 +6,7 @@
#include "chainparams.h"
#include "clientversion.h"
+#include "core_io.h"
#include "validation.h"
#include "net.h"
#include "net_processing.h"
diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp
index 63e4e9c630..58640d69d6 100644
--- a/src/rpc/server.cpp
+++ b/src/rpc/server.cpp
@@ -123,16 +123,6 @@ CAmount AmountFromValue(const UniValue& value)
return amount;
}
-UniValue ValueFromAmount(const CAmount& amount)
-{
- bool sign = amount < 0;
- int64_t n_abs = (sign ? -amount : amount);
- int64_t quotient = n_abs / COIN;
- int64_t remainder = n_abs % COIN;
- return UniValue(UniValue::VNUM,
- strprintf("%s%d.%08d", sign ? "-" : "", quotient, remainder));
-}
-
uint256 ParseHashV(const UniValue& v, std::string strName)
{
std::string strHex;
diff --git a/src/rpc/server.h b/src/rpc/server.h
index b20c827727..dd6f763245 100644
--- a/src/rpc/server.h
+++ b/src/rpc/server.h
@@ -185,7 +185,6 @@ extern std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strNa
extern std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey);
extern CAmount AmountFromValue(const UniValue& value);
-extern UniValue ValueFromAmount(const CAmount& amount);
extern std::string HelpExampleCli(const std::string& methodname, const std::string& args);
extern std::string HelpExampleRpc(const std::string& methodname, const std::string& args);
diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp
index 134bd7c609..c6643be7a7 100644
--- a/src/test/rpc_tests.cpp
+++ b/src/test/rpc_tests.cpp
@@ -6,6 +6,7 @@
#include "rpc/client.h"
#include "base58.h"
+#include "core_io.h"
#include "netbase.h"
#include "test/test_bitcoin.h"
diff --git a/src/utilmoneystr.h b/src/utilmoneystr.h
index 5839b07344..bc885ee167 100644
--- a/src/utilmoneystr.h
+++ b/src/utilmoneystr.h
@@ -14,6 +14,9 @@
#include "amount.h"
+/* Do not use these functions to represent or parse monetary amounts to or from
+ * JSON but use AmountFromValue and ValueFromAmount for that.
+ */
std::string FormatMoney(const CAmount& n);
bool ParseMoney(const std::string& str, CAmount& nRet);
bool ParseMoney(const char* pszIn, CAmount& nRet);