Skip to content

Commit

Permalink
Merge pull request #2335 from barton2526/univalue
Browse files Browse the repository at this point in the history
build: Update univalue subtree
  • Loading branch information
jamescowens authored Sep 17, 2021
2 parents 51daa38 + 357c503 commit 2a89710
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 137 deletions.
6 changes: 3 additions & 3 deletions src/rpc/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ int ReadHTTPMessage(std::basic_istream<char>& stream, std::map<std::string,
std::string JSONRPCRequest(const std::string& strMethod, const UniValue& params, const UniValue& id)
{
UniValue request(UniValue::VOBJ);
request.push_back(Pair("method", strMethod));
request.push_back(Pair("params", params));
request.push_back(Pair("id", id));
request.pushKV("method", strMethod);
request.pushKV("params", params);
request.pushKV("id", id);
return request.write() + "\n";
}

Expand Down
24 changes: 12 additions & 12 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1195,19 +1195,19 @@ UniValue consolidatemsunspent(const UniValue& params, bool fHelp)
sMultisigtype.append("_of_");
sMultisigtype.append(ToString(vOpCodes[1]));

result.push_back(std::make_pair("multi_sig_type", sMultisigtype));
result.push_back(std::make_pair("block_start", nBlockStart));
result.push_back(std::make_pair("block_end", nBlockEnd));
result.pushKV("multi_sig_type", sMultisigtype);
result.pushKV("block_start", nBlockStart);
result.pushKV("block_end", nBlockEnd);
// Let rpc caller know this was the last block we were in especially if the target amount of inputs was met before end block
result.push_back(std::make_pair("last_block_checked", nBlockCurrent));
result.push_back(std::make_pair("number_of_inputs", nInputs));
result.push_back(std::make_pair("maximum_possible_inputs", nMaxInputs));
result.push_back(std::make_pair("total_grc_in", ValueFromAmount(nTotal)));
result.push_back(std::make_pair("fee", nTxFee));
result.push_back(std::make_pair("output_amount", ValueFromAmount(nOutput)));
result.push_back(std::make_pair("estimated_signed_hex_size", (nBytes * 2)));
result.push_back(std::make_pair("estimated_signed_binary_size", nBytes));
result.push_back(std::make_pair("rawtx", sHash));
result.pushKV("last_block_checked", nBlockCurrent);
result.pushKV("number_of_inputs", nInputs);
result.pushKV("maximum_possible_inputs", nMaxInputs);
result.pushKV("total_grc_in", ValueFromAmount(nTotal));
result.pushKV("fee", nTxFee);
result.pushKV("output_amount", ValueFromAmount(nOutput));
result.pushKV("estimated_signed_hex_size", (nBytes * 2));
result.pushKV("estimated_signed_binary_size", nBytes);
result.pushKV("rawtx", sHash);

return result;
}
Expand Down
2 changes: 2 additions & 0 deletions src/univalue/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ TEST_FILES = \
$(TEST_DATA_DIR)/fail41.json \
$(TEST_DATA_DIR)/fail42.json \
$(TEST_DATA_DIR)/fail44.json \
$(TEST_DATA_DIR)/fail45.json \
$(TEST_DATA_DIR)/fail3.json \
$(TEST_DATA_DIR)/fail4.json \
$(TEST_DATA_DIR)/fail5.json \
Expand All @@ -105,6 +106,7 @@ TEST_FILES = \
$(TEST_DATA_DIR)/pass1.json \
$(TEST_DATA_DIR)/pass2.json \
$(TEST_DATA_DIR)/pass3.json \
$(TEST_DATA_DIR)/pass4.json \
$(TEST_DATA_DIR)/round1.json \
$(TEST_DATA_DIR)/round2.json \
$(TEST_DATA_DIR)/round3.json \
Expand Down
21 changes: 5 additions & 16 deletions src/univalue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,10 @@ an arbitrary depth.
This class is aligned with the JSON standard, [RFC
7159](https://tools.ietf.org/html/rfc7159.html).

## Installation
## Library usage

This project is a standard GNU
[autotools](https://www.gnu.org/software/automake/manual/html_node/Autotools-Introduction.html)
project. Build and install instructions are available in the `INSTALL`
file provided with GNU autotools.

```
$ ./autogen.sh
$ ./configure
$ make
```

## Design

UniValue provides a single dynamic RAII C++ object class,
and minimizes template use (contra json_spirit).
This is a fork of univalue used by Bitcoin Core. It is not maintained for usage
by other projects. Notably, the API may break in non-backward-compatible ways.

Other projects looking for a maintained library should use the upstream
univalue at https://github.com/jgarzik/univalue.
2 changes: 0 additions & 2 deletions src/univalue/gen/gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include <string.h>
#include "univalue.h"

using namespace std;

static bool initEscapes;
static std::string escapes[256];

Expand Down
75 changes: 5 additions & 70 deletions src/univalue/include/univalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <cassert>

#include <sstream> // .get_int64()
#include <utility> // std::pair

class UniValue {
public:
Expand Down Expand Up @@ -48,7 +47,6 @@ class UniValue {
std::string s(val_);
setStr(s);
}
~UniValue() {}

void clear();

Expand Down Expand Up @@ -102,6 +100,10 @@ class UniValue {
UniValue tmpVal(val_);
return push_back(tmpVal);
}
bool push_back(bool val_) {
UniValue tmpVal(val_);
return push_back(tmpVal);
}
bool push_back(int val_) {
UniValue tmpVal(val_);
return push_back(tmpVal);
Expand Down Expand Up @@ -131,7 +133,7 @@ class UniValue {
return pushKV(key, tmpVal);
}
bool pushKV(const std::string& key, bool val_) {
UniValue tmpVal((bool)val_);
UniValue tmpVal(val_);
return pushKV(key, tmpVal);
}
bool pushKV(const std::string& key, int val_) {
Expand Down Expand Up @@ -177,76 +179,9 @@ class UniValue {
const UniValue& get_array() const;

enum VType type() const { return getType(); }
bool push_back(std::pair<std::string,UniValue> pear) {
return pushKV(pear.first, pear.second);
}
friend const UniValue& find_value( const UniValue& obj, const std::string& name);
};

//
// The following were added for compatibility with json_spirit.
// Most duplicate other methods, and should be removed.
//
static inline std::pair<std::string,UniValue> Pair(const char *cKey, const char *cVal)
{
std::string key(cKey);
UniValue uVal(cVal);
return std::make_pair(key, uVal);
}

static inline std::pair<std::string,UniValue> Pair(const char *cKey, std::string strVal)
{
std::string key(cKey);
UniValue uVal(strVal);
return std::make_pair(key, uVal);
}

static inline std::pair<std::string,UniValue> Pair(const char *cKey, uint64_t u64Val)
{
std::string key(cKey);
UniValue uVal(u64Val);
return std::make_pair(key, uVal);
}

static inline std::pair<std::string,UniValue> Pair(const char *cKey, int64_t i64Val)
{
std::string key(cKey);
UniValue uVal(i64Val);
return std::make_pair(key, uVal);
}

static inline std::pair<std::string,UniValue> Pair(const char *cKey, bool iVal)
{
std::string key(cKey);
UniValue uVal(iVal);
return std::make_pair(key, uVal);
}

static inline std::pair<std::string,UniValue> Pair(const char *cKey, int iVal)
{
std::string key(cKey);
UniValue uVal(iVal);
return std::make_pair(key, uVal);
}

static inline std::pair<std::string,UniValue> Pair(const char *cKey, double dVal)
{
std::string key(cKey);
UniValue uVal(dVal);
return std::make_pair(key, uVal);
}

static inline std::pair<std::string,UniValue> Pair(const char *cKey, const UniValue& uVal)
{
std::string key(cKey);
return std::make_pair(key, uVal);
}

static inline std::pair<std::string,UniValue> Pair(std::string key, const UniValue& uVal)
{
return std::make_pair(key, uVal);
}

enum jtokentype {
JTOK_ERR = -1,
JTOK_NONE = 0, // eof
Expand Down
16 changes: 7 additions & 9 deletions src/univalue/lib/univalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#include "univalue.h"

using namespace std;

const UniValue NullUniValue;

void UniValue::clear()
Expand All @@ -37,15 +35,15 @@ bool UniValue::setBool(bool val_)
return true;
}

static bool validNumStr(const string& s)
static bool validNumStr(const std::string& s)
{
string tokenVal;
std::string tokenVal;
unsigned int consumed;
enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());
return (tt == JTOK_NUMBER);
}

bool UniValue::setNumStr(const string& val_)
bool UniValue::setNumStr(const std::string& val_)
{
if (!validNumStr(val_))
return false;
Expand All @@ -58,7 +56,7 @@ bool UniValue::setNumStr(const string& val_)

bool UniValue::setInt(uint64_t val_)
{
ostringstream oss;
std::ostringstream oss;

oss << val_;

Expand All @@ -67,7 +65,7 @@ bool UniValue::setInt(uint64_t val_)

bool UniValue::setInt(int64_t val_)
{
ostringstream oss;
std::ostringstream oss;

oss << val_;

Expand All @@ -76,7 +74,7 @@ bool UniValue::setInt(int64_t val_)

bool UniValue::setFloat(double val_)
{
ostringstream oss;
std::ostringstream oss;

oss << std::setprecision(16) << val_;

Expand All @@ -85,7 +83,7 @@ bool UniValue::setFloat(double val_)
return ret;
}

bool UniValue::setStr(const string& val_)
bool UniValue::setStr(const std::string& val_)
{
clear();
typ = VSTR;
Expand Down
21 changes: 15 additions & 6 deletions src/univalue/lib/univalue_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
#include "univalue.h"
#include "univalue_utffilter.h"

using namespace std;
/*
* According to stackexchange, the original json test suite wanted
* to limit depth to 22. Widely-deployed PHP bails at depth 512,
* so we will follow PHP's lead, which should be more than sufficient
* (further stackexchange comments indicate depth > 32 rarely occurs).
*/
static const size_t MAX_JSON_DEPTH = 512;

static bool json_isdigit(int ch)
{
Expand Down Expand Up @@ -42,7 +48,7 @@ static const char *hatoui(const char *first, const char *last,
return first;
}

enum jtokentype getJsonToken(string& tokenVal, unsigned int& consumed,
enum jtokentype getJsonToken(std::string& tokenVal, unsigned int& consumed,
const char *raw, const char *end)
{
tokenVal.clear();
Expand Down Expand Up @@ -114,7 +120,7 @@ enum jtokentype getJsonToken(string& tokenVal, unsigned int& consumed,
case '8':
case '9': {
// part 1: int
string numStr;
std::string numStr;

const char *first = raw;

Expand Down Expand Up @@ -174,7 +180,7 @@ enum jtokentype getJsonToken(string& tokenVal, unsigned int& consumed,
case '"': {
raw++; // skip "

string valStr;
std::string valStr;
JSONUTF8StringFilter writer(valStr);

while (true) {
Expand Down Expand Up @@ -255,9 +261,9 @@ bool UniValue::read(const char *raw, size_t size)
clear();

uint32_t expectMask = 0;
vector<UniValue*> stack;
std::vector<UniValue*> stack;

string tokenVal;
std::string tokenVal;
unsigned int consumed;
enum jtokentype tok = JTOK_NONE;
enum jtokentype last_tok = JTOK_NONE;
Expand Down Expand Up @@ -325,6 +331,9 @@ bool UniValue::read(const char *raw, size_t size)
stack.push_back(newTop);
}

if (stack.size() > MAX_JSON_DEPTH)
return false;

if (utyp == VOBJ)
setExpect(OBJ_NAME);
else
Expand Down
18 changes: 8 additions & 10 deletions src/univalue/lib/univalue_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
#include "univalue.h"
#include "univalue_escapes.h"

using namespace std;

static string json_escape(const string& inS)
static std::string json_escape(const std::string& inS)
{
string outS;
std::string outS;
outS.reserve(inS.size() * 2);

for (unsigned int i = 0; i < inS.size(); i++) {
Expand All @@ -28,10 +26,10 @@ static string json_escape(const string& inS)
return outS;
}

string UniValue::write(unsigned int prettyIndent,
unsigned int indentLevel) const
std::string UniValue::write(unsigned int prettyIndent,
unsigned int indentLevel) const
{
string s;
std::string s;
s.reserve(1024);

unsigned int modIndent = indentLevel;
Expand Down Expand Up @@ -62,12 +60,12 @@ string UniValue::write(unsigned int prettyIndent,
return s;
}

static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, string& s)
static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
{
s.append(prettyIndent * indentLevel, ' ');
}

void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, string& s) const
void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
{
s += "[";
if (prettyIndent)
Expand All @@ -89,7 +87,7 @@ void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, s
s += "]";
}

void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, string& s) const
void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
{
s += "{";
if (prettyIndent)
Expand Down
1 change: 1 addition & 0 deletions src/univalue/test/fail45.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[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