aboutsummaryrefslogtreecommitdiff
path: root/src/bitcoinrpc.cpp
diff options
context:
space:
mode:
authorGavin Andresen <gavinandresen@gmail.com>2012-06-22 18:36:42 -0400
committerGavin Andresen <gavinandresen@gmail.com>2012-07-05 12:42:25 -0400
commit899d373b3ccb3003f8f6e518ba4cf7ba4028e58b (patch)
tree2e35daa5f4d32a335fee22b6a8a1b28a5a0df768 /src/bitcoinrpc.cpp
parent34420d655d8c1cf15e53803ca85e0ac2a2a62fe9 (diff)
downloadbitcoin-899d373b3ccb3003f8f6e518ba4cf7ba4028e58b.tar.xz
RPCTypeCheck method to make type-checking JSON Arrays easier.
Diffstat (limited to 'src/bitcoinrpc.cpp')
-rw-r--r--src/bitcoinrpc.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp
index 19815ef0fe..c9413a5b29 100644
--- a/src/bitcoinrpc.cpp
+++ b/src/bitcoinrpc.cpp
@@ -63,6 +63,43 @@ Object JSONRPCError(int code, const string& message)
return error;
}
+void RPCTypeCheck(const Array& params,
+ const list<Value_type>& typesExpected)
+{
+ int i = 0;
+ BOOST_FOREACH(Value_type t, typesExpected)
+ {
+ if (params.size() <= i)
+ break;
+
+ const Value& v = params[i];
+ if (v.type() != t)
+ {
+ string err = strprintf("Expected type %s, got %s",
+ Value_type_name[t], Value_type_name[v.type()]);
+ throw JSONRPCError(-3, err);
+ }
+ i++;
+ }
+}
+
+void RPCTypeCheck(const Object& o,
+ const map<string, Value_type>& typesExpected)
+{
+ BOOST_FOREACH(const PAIRTYPE(string, Value_type)& t, typesExpected)
+ {
+ const Value& v = find_value(o, t.first);
+ if (v.type() == null_type)
+ throw JSONRPCError(-3, strprintf("Missing %s", t.first.c_str()));
+ if (v.type() != t.second)
+ {
+ string err = strprintf("Expected type %s for %s, got %s",
+ Value_type_name[t.second], t.first.c_str(), Value_type_name[v.type()]);
+ throw JSONRPCError(-3, err);
+ }
+ }
+}
+
double GetDifficulty(const CBlockIndex* blockindex = NULL)
{
// Floating point number that is a multiple of the minimum difficulty,