Skip to content

Commit

Permalink
*** NODE API BREAKING CHANGE *** (#1075)
Browse files Browse the repository at this point in the history
- Replace current Node API with a REST-like API
- https://meta.turtlecoin.dev/proposals/daemon-api/ for swagger view of the API
- Re-factored some code for more reuse
- Consistency between responses of the same type(s)
- Better HTTP status codes returned
- Better reporting of errors in the API use
- Added additional error codes
- Updated miner to use new API
- Updated WalletBackend to use new API
- Updated Nigel to use new API
- Removed a bunch of nlohmann JSON calls in lieu of rapidjson in WalletBackend and Miner
- Added a few JSON helpers
- Re-factored code in a few places
- Removed WalletGreen Remnants
- Destroyed JsonRpc.h/.cpp
- Removed the legacy HTTPServer and HTTPClient
- Removed --enable-blockexplorer-detailed option from daemon (no longer used)
- Added additional JSON methods to basic CryptoNote structure(s) and Crypto structure(s)
  • Loading branch information
brandonlehmann committed Jul 30, 2020
1 parent b5b4b2b commit fdb139f
Show file tree
Hide file tree
Showing 53 changed files with 2,008 additions and 9,701 deletions.
23 changes: 14 additions & 9 deletions include/CryptoNote.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,27 @@ namespace CryptoNote
void toJSON(rapidjson::Writer<rapidjson::StringBuffer> &writer) const
{
writer.StartObject();
writer.Key("block");
writer.String(Common::toHex(block));

writer.Key("transactions");
writer.StartArray();
for (const auto &transaction : transactions)
{
writer.String(Common::toHex(transaction));
writer.Key("blob");
writer.String(Common::toHex(block));

writer.Key("transactions");
writer.StartArray();
{
for (const auto &transaction : transactions)
{
writer.String(Common::toHex(transaction));
}
}
writer.EndArray();
}
writer.EndArray();
writer.EndObject();
}

void fromJSON(const JSONValue &j)
{
block = Common::fromHex(getStringFromJSON(j, "block"));
block = Common::fromHex(getStringFromJSON(j, "blob"));

for (const auto &tx : getArrayFromJSON(j, "transactions"))
{
transactions.push_back(Common::fromHex(tx.GetString()));
Expand Down
49 changes: 49 additions & 0 deletions include/CryptoTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <algorithm>
#include <common/StringTools.h>
#include <JsonHelper.h>
#include <cstdint>
#include <iterator>

Expand Down Expand Up @@ -133,6 +134,14 @@ namespace Crypto
writer.String(Common::podToHex(data));
}

void fromJSON(const JSONValue &j)
{
if (j.IsString())
{
fromString(j.GetString());
}
}

/* Initializes the class from a json string */
void fromString(const std::string &s)
{
Expand Down Expand Up @@ -178,6 +187,14 @@ namespace Crypto
writer.String(Common::podToHex(data));
}

void fromJSON(const JSONValue &j)
{
if (j.IsString())
{
fromString(j.GetString());
}
}

/* Initializes the class from a json string */
void fromString(const std::string &s)
{
Expand Down Expand Up @@ -225,6 +242,14 @@ namespace Crypto
writer.String(Common::podToHex(data));
}

void fromJSON(const JSONValue &j)
{
if (j.IsString())
{
fromString(j.GetString());
}
}

/* Initializes the class from a json string */
void fromString(const std::string &s)
{
Expand Down Expand Up @@ -272,6 +297,14 @@ namespace Crypto
writer.String(Common::podToHex(data));
}

void fromJSON(const JSONValue &j)
{
if (j.IsString())
{
fromString(j.GetString());
}
}

/* Initializes the class from a json string */
void fromString(const std::string &s)
{
Expand Down Expand Up @@ -319,6 +352,14 @@ namespace Crypto
writer.String(Common::podToHex(data));
}

void fromJSON(const JSONValue &j)
{
if (j.IsString())
{
fromString(j.GetString());
}
}

/* Initializes the class from a json string */
void fromString(const std::string &s)
{
Expand Down Expand Up @@ -366,6 +407,14 @@ namespace Crypto
writer.String(Common::podToHex(data));
}

void fromJSON(const JSONValue &j)
{
if (j.IsString())
{
fromString(j.GetString());
}
}

/* Initializes the class from a json string */
void fromString(const std::string &s)
{
Expand Down
98 changes: 69 additions & 29 deletions include/JsonHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,40 @@ template<typename T> const rapidjson::Value &getJsonValue(const T &j, const std:
return val->value;
}

template<typename T> uint64_t getUint64FromJSON(const T &j, const std::string &key)
template<typename T> uint32_t getUintFromJSON(const T &j)
{
if (!j.IsUint())
{
throw std::invalid_argument(
"JSON parameter is wrong type. Expected uint64_t, got " + kTypeNames[j.GetType()]);
}

return j.GetUint();
}

template<typename T> uint32_t getUintFromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);

if (!val.IsUint64())
return getUintFromJSON(val);
}

template<typename T> uint64_t getUint64FromJSON(const T &j)
{
if (!j.IsUint64())
{
throw std::invalid_argument(
"JSON parameter is wrong type. Expected uint64_t, got " + kTypeNames[val.GetType()]);
"JSON parameter is wrong type. Expected uint64_t, got " + kTypeNames[j.GetType()]);
}

return val.GetUint64();
return j.GetUint64();
}

template<typename T> uint64_t getUint64FromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);

return getUint64FromJSON(val);
}

template<typename T> uint64_t getInt64FromJSON(const T &j, const std::string &key)
Expand All @@ -62,66 +85,83 @@ template<typename T> uint64_t getInt64FromJSON(const T &j, const std::string &ke
}

/**
* Gets a string from the JSON, with a given keyname
* Gets a string from the JSON, with or without a given keyname
*/
template<typename T> std::string getStringFromJSON(const T &j, const std::string &key)
template<typename T> std::string getStringFromJSON(const T &j)
{
auto &val = getJsonValue(j, key);

if (!val.IsString())
if (!j.IsString())
{
throw std::invalid_argument("JSON parameter is wrong type. Expected String, got " + kTypeNames[val.GetType()]);
throw std::invalid_argument("JSON parameter is wrong type. Expected String, got " + kTypeNames[j.GetType()]);
}

return val.GetString();
return j.GetString();
}

template<typename T> std::string getStringFromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);

return getStringFromJSON(val);
}

/**
* Gets a string from the JSON, without a key. For example, we might have an
* array of strings.
* Gets an Array from JSON, with or without a given keyname
*/
template<typename T> std::string getStringFromJSONString(const T &j)
template<typename T> auto getArrayFromJSON(const T &j)
{
if (!j.IsString())
if (!j.IsArray())
{
throw std::invalid_argument("JSON parameter is wrong type. Expected String, got " + kTypeNames[j.GetType()]);
throw std::invalid_argument("JSON parameter is wrong type. Expected Array, got " + kTypeNames[j.GetType()]);
}

return j.GetString();
return j.GetArray();
}

template<typename T> auto getArrayFromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);

if (!val.IsArray())
return getArrayFromJSON(val);

return val.GetArray();
}

/**
* Gets a JSONObject from JSON, with our without a given keyname
*/
template<typename T> JSONObject getObjectFromJSON(const T &j)
{
if (!j.IsObject())
{
throw std::invalid_argument("JSON parameter is wrong type. Expected Array, got " + kTypeNames[val.GetType()]);
throw std::invalid_argument("JSON parameter is wrong type. Expected Object, got " + kTypeNames[j.GetType()]);
}

return val.GetArray();
return j.Get_Object();
}

template<typename T> JSONObject getObjectFromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);

if (!val.IsObject())
return getObjectFromJSON(val);
}

/**
* Gets a boolean from JSON, with our without a given keyname
*/
template<typename T> bool getBoolFromJSON(const T &j)
{
if (!j.IsBool())
{
throw std::invalid_argument("JSON parameter is wrong type. Expected Object, got " + kTypeNames[val.GetType()]);
throw std::invalid_argument("JSON parameter is wrong type. Expected Bool, got " + kTypeNames[j.GetType()]);
}

return val.Get_Object();
return j.GetBool();
}

template<typename T> bool getBoolFromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);

if (!val.IsBool())
{
throw std::invalid_argument("JSON parameter is wrong type. Expected Bool, got " + kTypeNames[val.GetType()]);
}

return val.GetBool();
return getBoolFromJSON(val);
}
Loading

0 comments on commit fdb139f

Please sign in to comment.