From 6e220cc97e1d5f2c252652cfe18bcd98b17aa60d Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Fri, 31 May 2013 16:22:38 -0700 Subject: [PATCH] More feature RPC support. --- src/cpp/ripple/FeatureTable.cpp | 87 +++++++++++++++++++++------------ src/cpp/ripple/FeatureTable.h | 3 ++ src/cpp/ripple/RPCErr.cpp | 1 + src/cpp/ripple/RPCErr.h | 1 + src/cpp/ripple/RPCHandler.cpp | 10 +++- 5 files changed, 68 insertions(+), 34 deletions(-) diff --git a/src/cpp/ripple/FeatureTable.cpp b/src/cpp/ripple/FeatureTable.cpp index 686a34df60..8570175b30 100644 --- a/src/cpp/ripple/FeatureTable.cpp +++ b/src/cpp/ripple/FeatureTable.cpp @@ -40,6 +40,19 @@ FeatureState* FeatureTable::getCreateFeature(const uint256& featureHash, bool cr return &(it->second); } +uint256 FeatureTable::getFeature(const std::string& name) +{ + if (!name.empty()) + { + BOOST_FOREACH(featureMap_t::value_type& it, mFeatureMap) + { + if (name == it.second.mFriendlyName) + return it.first; + } + } + return uint256(); +} + FeatureState* FeatureTable::addKnownFeature(const char *featureID, const char *friendlyName, bool veto) { uint256 hash; @@ -317,50 +330,60 @@ Json::Value FeatureTable::getJson(int) boost::mutex::scoped_lock sl(mMutex); BOOST_FOREACH(const featureIt_t& it, mFeatureMap) { - Json::Value& v(ret[it.first.GetHex()] = Json::objectValue); + setJson(ret[it.first.GetHex()] = Json::objectValue, it.second); + } + } + return ret; +} - if (!it.second.mFriendlyName.empty()) - v["name"] = it.second.mFriendlyName; +void FeatureTable::setJson(Json::Value& v, const FeatureState& fs) +{ + if (!fs.mFriendlyName.empty()) + v["name"] = fs.mFriendlyName; - v["supported"] = it.second.mSupported; - v["vetoed"] = it.second.mVetoed; + v["supported"] = fs.mSupported; + v["vetoed"] = fs.mVetoed; - if (it.second.mEnabled) - v["enabled"] = true; + if (fs.mEnabled) + v["enabled"] = true; + else + { + v["enabled"] = false; + if (mLastReport != 0) + { + if (fs.mLastMajority == 0) + { + v["majority"] = false; + } else { - v["enabled"] = false; - if (mLastReport != 0) + if (fs.mFirstMajority != 0) { - if (it.second.mLastMajority == 0) - { - v["majority"] = false; - } + if (fs.mFirstMajority == mFirstReport) + v["majority_start"] = "start"; else - { - if (it.second.mFirstMajority != 0) - { - if (it.second.mFirstMajority == mFirstReport) - v["majority_start"] = "start"; - else - v["majority_start"] = it.second.mFirstMajority; - } - if (it.second.mLastMajority != 0) - { - if (it.second.mLastMajority == mLastReport) - v["majority_until"] = "now"; - else - v["majority_until"] = it.second.mLastMajority; - } - } + v["majority_start"] = fs.mFirstMajority; + } + if (fs.mLastMajority != 0) + { + if (fs.mLastMajority == mLastReport) + v["majority_until"] = "now"; + else + v["majority_until"] = fs.mLastMajority; } } - - if (it.second.mVetoed) - v["veto"] = true; } } + if (fs.mVetoed) + v["veto"] = true; +} + +Json::Value FeatureTable::getJson(const uint256& feature) +{ + Json::Value ret = Json::objectValue; + boost::mutex::scoped_lock sl(mMutex); + setJson(ret[feature.GetHex()] = Json::objectValue, *getCreateFeature(feature, true)); return ret; } diff --git a/src/cpp/ripple/FeatureTable.h b/src/cpp/ripple/FeatureTable.h index 623100b7e4..8f6ef3cca7 100644 --- a/src/cpp/ripple/FeatureTable.h +++ b/src/cpp/ripple/FeatureTable.h @@ -63,6 +63,7 @@ protected: FeatureState* getCreateFeature(const uint256& feature, bool create); bool shouldEnable (uint32 closeTime, const FeatureState& fs); + void setJson(Json::Value& v, const FeatureState&); public: @@ -73,6 +74,7 @@ public: void addInitialFeatures(); FeatureState* addKnownFeature(const char *featureID, const char *friendlyName, bool veto); + uint256 getFeature(const std::string& name); bool vetoFeature(const uint256& feature); bool unVetoFeature(const uint256& feature); @@ -94,6 +96,7 @@ public: void reportValidations(const FeatureSet&); Json::Value getJson(int); + Json::Value getJson(const uint256&); void doValidation(Ledger::ref lastClosedLedger, STObject& baseValidation); void doVoting(Ledger::ref lastClosedLedger, SHAMap::ref initialPosition); diff --git a/src/cpp/ripple/RPCErr.cpp b/src/cpp/ripple/RPCErr.cpp index b445279e5c..a7240ab9fd 100644 --- a/src/cpp/ripple/RPCErr.cpp +++ b/src/cpp/ripple/RPCErr.cpp @@ -18,6 +18,7 @@ Json::Value rpcError(int iError, Json::Value jvResult) { rpcACT_MALFORMED, "actMalformed", "Account malformed." }, { rpcACT_NOT_FOUND, "actNotFound", "Account not found." }, { rpcBAD_BLOB, "badBlob", "Blob must be a non-empty hex string." }, + { rpcBAD_FEATURE, "badFeature", "Feature unknown or invalid." }, { rpcBAD_ISSUER, "badIssuer", "Issuer account malformed." }, { rpcBAD_MARKET, "badMarket", "No such market." }, { rpcBAD_SECRET, "badSecret", "Secret does not match account." }, diff --git a/src/cpp/ripple/RPCErr.h b/src/cpp/ripple/RPCErr.h index 060e010ca0..e58d6d08c9 100644 --- a/src/cpp/ripple/RPCErr.h +++ b/src/cpp/ripple/RPCErr.h @@ -47,6 +47,7 @@ enum { rpcACT_MALFORMED, rpcQUALITY_MALFORMED, rpcBAD_BLOB, + rpcBAD_FEATURE, rpcBAD_ISSUER, rpcBAD_MARKET, rpcBAD_SECRET, diff --git a/src/cpp/ripple/RPCHandler.cpp b/src/cpp/ripple/RPCHandler.cpp index 974ef89e01..82b778cb19 100644 --- a/src/cpp/ripple/RPCHandler.cpp +++ b/src/cpp/ripple/RPCHandler.cpp @@ -2263,11 +2263,17 @@ Json::Value RPCHandler::doFeature(Json::Value jvRequest, int& cost, ScopedLock& return jvReply; } - if (!jvRequest.isMember("vote")) + uint256 uFeature = theApp->getFeatureTable().getFeature(jvRequest["feature"].asString()); + if (uFeature.isZero()) { - // WRITEME + uFeature.SetHex(jvRequest["feature"].asString()); + if (uFeature.isZero()) + return rpcError(rpcBAD_FEATURE); } + if (!jvRequest.isMember("vote")) + return theApp->getFeatureTable().getJson(uFeature); + // WRITEME return rpcError(rpcNOT_SUPPORTED); }