Browse Source

fixed natspec evaluation

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
b76622494e
  1. 18
      libnatspec/NatspecExpressionEvaluator.cpp
  2. 9
      libnatspec/NatspecExpressionEvaluator.h
  3. 19
      test/natspec.cpp

18
libnatspec/NatspecExpressionEvaluator.cpp

@ -35,25 +35,23 @@ static QString contentsOfQResource(string const& _res)
return in.readAll(); return in.readAll();
} }
NatspecExpressionEvaluator::NatspecExpressionEvaluator(QString const& _abi, QString const& _method, QString const& _params) NatspecExpressionEvaluator::NatspecExpressionEvaluator(QString const& _abi, QString const& _transaction, QString const& _method)
: m_abi(_abi), m_transaction(_transaction), m_method(_method)
{ {
Q_INIT_RESOURCE(natspec); Q_INIT_RESOURCE(natspec);
QJSValue result = m_engine.evaluate(contentsOfQResource(":/natspec/natspec.js")); QJSValue result = m_engine.evaluate(contentsOfQResource(":/natspec/natspec.js"));
if (result.isError()) if (result.isError())
BOOST_THROW_EXCEPTION(FileError()); BOOST_THROW_EXCEPTION(FileError());
m_engine.evaluate("globals.abi = " + _abi); m_engine.evaluate("var natspec = require('natspec')");
m_engine.evaluate("globals.method = " + _method);
m_engine.evaluate("globals.params = " + _params);
} }
QString NatspecExpressionEvaluator::evalExpression(QString const& _expression) QString NatspecExpressionEvaluator::evalExpression(QString const& _expression)
{ {
QJSValue result = m_engine.evaluate("evaluateExpression(\"" + _expression + "\")"); QString call = "";
if (result.isError()) if (!m_abi.isEmpty() && !m_transaction.isEmpty() && !m_method.isEmpty())
{ call = ", {abi:" + m_abi + ", transaction:" + m_transaction + ", method: '" + m_method + "' }";
cerr << "Could not evaluate expression: \"" << _expression.toStdString() << "\"" << endl;
return _expression; QJSValue result = m_engine.evaluate("natspec.evaluateExpressionSafe(\"" + _expression + "\"" + call + ")");
}
return result.toString(); return result.toString();
} }

9
libnatspec/NatspecExpressionEvaluator.h

@ -34,12 +34,10 @@ class NatspecExpressionEvaluator
public: public:
/// Construct natspec expression evaluator /// Construct natspec expression evaluator
/// @params abi - contract's abi in json format, passed as string /// @params abi - contract's abi in json format, passed as string
/// @params transaction - json object containing transaction data
/// @params method - name of the contract's method for which we evaluate the natspec. /// @params method - name of the contract's method for which we evaluate the natspec.
/// If we want to use raw string, it should be passed with quotation marks eg. "\"helloWorld\""
/// If we pass string "helloWorld", the value of the object with name "helloWorld" will be used
/// @params params - array of method input params, passed as string, objects in array should be
/// javascript valid objects /// javascript valid objects
NatspecExpressionEvaluator(QString const& _abi = "[]", QString const& _method = "", QString const& _params = "[]"); NatspecExpressionEvaluator(QString const& _abi = "[]", QString const& _transaction = "{}", QString const& _method = "");
/// Should be called to evaluate natspec expression /// Should be called to evaluate natspec expression
/// @params expression - natspec expression /// @params expression - natspec expression
@ -48,4 +46,7 @@ public:
private: private:
QJSEngine m_engine; QJSEngine m_engine;
QString m_abi;
QString m_transaction;
QString m_method;
}; };

19
test/natspec.cpp

@ -34,7 +34,7 @@ BOOST_AUTO_TEST_CASE(natspec_eval_function_exists)
// given // given
NatspecExpressionEvaluator e; NatspecExpressionEvaluator e;
// when // when
string result = e.evalExpression("`typeof evaluateExpression`").toStdString(); string result = e.evalExpression("`typeof natspec.evaluateExpression`").toStdString();
// then // then
BOOST_CHECK_EQUAL(result, "function"); BOOST_CHECK_EQUAL(result, "function");
} }
@ -77,7 +77,7 @@ BOOST_AUTO_TEST_CASE(natspec_js_eval_input_params)
// given // given
char const* abi = R"([ char const* abi = R"([
{ {
"name": "f", "name": "multiply",
"constant": false, "constant": false,
"type": "function", "type": "function",
"inputs": [ "inputs": [
@ -94,7 +94,18 @@ BOOST_AUTO_TEST_CASE(natspec_js_eval_input_params)
] ]
} }
])"; ])";
NatspecExpressionEvaluator e(abi, "'f'", "[4]");
char const* transaction = R"({
"jsonrpc": "2.0",
"method": "eth_call",
"params": [{
"to": "0x8521742d3f456bd237e312d6e30724960f72517a",
"data": "0xc6888fa10000000000000000000000000000000000000000000000000000000000000004"
}],
"id": 6
})";
NatspecExpressionEvaluator e(abi, transaction , "multiply");
// when // when
string result = e.evalExpression("Will multiply `a` by 7 and return `a * 7`.").toStdString(); string result = e.evalExpression("Will multiply `a` by 7 and return `a * 7`.").toStdString();
// then // then
@ -108,7 +119,7 @@ BOOST_AUTO_TEST_CASE(natspec_js_eval_error)
// when // when
string result = e.evalExpression("`test(`").toStdString(); string result = e.evalExpression("`test(`").toStdString();
// then // then
BOOST_CHECK_EQUAL(result, "`test(`"); BOOST_CHECK_EQUAL(result, "Natspec evaluation failed, wrong input params");
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

Loading…
Cancel
Save