Skip to content

Commit 46347ad

Browse files
committed
rpc: Move ValueFromAmount to core_write
This is necessary because core_write has to write amounts in TxToUniv, and mistakingly uses FormatMoney for that (which is only for debugging). We don't move AmountFromValue at the same time, as this is more challenging due to the RPCError depencency there.
1 parent dac3782 commit 46347ad

File tree

7 files changed

+16
-11
lines changed

7 files changed

+16
-11
lines changed

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: 10 additions & 0 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;

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"

0 commit comments

Comments
 (0)